function StatusBar(sel, options) {
  var _I=this;
  var _sb=null;
  // options
  this.elementId="_showstatus";
  this.prependMultiline=true;
  this.showCloseButton=false;
  this.afterTimeoutText=null;
  this.cssClass="statusbar";
  this.highlightClass="statusbarhighlight";
  this.errorClass="statuserror";
  this.closeButtonClass="statusbarclose";
  this.additive=false;
  $.extend(this, options);
  if(sel) _sb=$(sel);
  // create statusbar object manually
  if(!_sb)
  {
    _sb=$("<div id='_statusbar' class='"+_I.cssClass+"'>"+"<div class='"+_I.closeButtonClass+"'>"+(_I.showCloseButton ? " X </div></div>" : "") ).appendTo(document.body).show();
  }
  //if (_I.showCloseButton) $("."+_I.cssClass).click(function(e) { $(_sb).hide(); });
  if (_I.showCloseButton) $("."+_I.closeButtonClass).click(function(e) { $(_sb).hide(); $.cookie('barre_bas', '0', { expires:30 }); });
  this.show=function(message, timeout, isError)
  {
    if(_I.additive)
    {
      var html="<div style='margin-bottom: 2px;'>"+message+"</div>";
      if(_I.prependMultiline) _sb.prepend(html);
      else _sb.append(html);
    }
    else
    {
      if(!_I.showCloseButton) _sb.text(message);
      else
      {
        var t=_sb.find("div.statusbarclose");
        _sb.text(message).prepend(t);
      }
    }
    _sb.show();
    if(timeout)
    {
      if(isError) _sb.addClass(_I.errorClass);
      else _sb.addClass(_I.highlightClass);
      setTimeout(function() { _sb.removeClass(_I.highlightClass); if(_I.afterTimeoutText) _I.show(_I.afterTimeoutText); }, timeout);
    }
  }
  this.release=function() {
    if(_statusbar) $(_statusbar).remove();
  }
}

var _statusbar=null;
function showStatus(message, timeout, additive, isError) {
  if(!_statusbar) _statusbar=new StatusBar();
  _statusbar.show(message, timeout, additive, isError);
}

function show_barre_bas() {
  _statusbar=new StatusBar(null, { timeout:2000, showCloseButton:true, additive:true, afterTimeoutText:status });
  _statusbar.show('<a href="'+url_website+'nouveautes-v2.php">Voir les nouvelles fonctionnalités du site</a> - <a href="'+url_website+'newsletter.php">Inscription à la newsletter</a>');
}







//var url_website="http://localhost/histoires-hard.com/www/";
var url_website="http://www.histoires-hard.com/";
var popup_width=96*screen.width/100;
var popup_height=96*screen.width/100;

function AddFav() {
	if(navigator.appName!='Microsoft Internet Explorer') { window.sidebar.addPanel(document.title, location.href, ""); }
	else { window.external.AddFavorite(document.title, location.href); }
}

function patienter(div) {
	$("#"+div).html('<center><img src="'+url_website+'images/ajax-loader.gif" class="loader" /><br />Chargement en cours</center>');
}

var pub = function ()
{
	var div_pub = $("#pub"),
		iframe = $('#cd_frame'),
		wd = $("#main").width() * 0.99,
		css;
	if (typeof(la_pub) === 'undefined') {
		var la_pub = 0;
	}
	if (wd <= 640) {
		var defs = { t:0, n:4, w:512, h:90 };
	} else if (wd <= 736) {
		var defs = { t:0, n:5, w:640, h:90 };
	} else if (wd < 912) {
		var defs = { t:1, n:4, w:736, h:136 };
	} else {
		var defs = { t:2, n:4, w:912, h:170 };
	}
	if (defs.w != div_pub.css('width')) {
		css = { 'width': defs.w + 'px', 'height': defs.h + 'px' };
		div_pub.css(css);
		iframe.css(css);
		iframe.attr('src', url_website + 'cd.php?t=' + defs.t + '&n=' + defs.n + '&pub=' + la_pub);
	}
};

/*function pub()
{
	var pub = $("#pub", $("#main")),
		wd = $("#main").width() * 0.99;
	if (wd <= 620) {
		var defs = { thumbsize: '120x90', cols: 4, width: 493, height: 95 };
	} else if (wd <= 740) {
		var defs = { thumbsize: '120x90', cols: 5, width: 616, height: 95 };
	} else if (wd < 920) {
		var defs = { thumbsize: '180x135', cols: 4, width: 733, height: 140 };
	} else {
		var defs = { thumbsize: '180x135', cols: 5, width: 916, height: 140 };
	}
	if (defs.width != pub.css("width")) {
		pub.css({ "width": defs.width + "px", "height": defs.height + "px" });
		$("#mozaix", pub).css({ "width": defs.width + "px", "height": defs.height + "px" });
		$.get(
			url_website + "iv.php", { thumbsize: defs.thumbsize, cols: defs.cols },
			function(data)
			{
				eval(data);
			}
		);
	}
}

function pub() {
	var wd=$("#main").width()*0.99;
	if(wd<=648) { var defs=[{ taille:100, nb:5, width:560, height:87 }, { w:160, h:363, width:156, height:117 } ]; }
	if(wd<=848) { var defs=[{ taille:150, nb:4, width:648, height:117 }, { w:160, h:363, width:156, height:117 } ]; }
	else if(wd<948) { var defs=[{ taille:200, nb:4, width:848, height:154 }, { w:184, h:417, width:180, height:135 } ]; }
	else { var defs=[{ taille:250, nb:4, width:1048, height:191 }, { w:224, h:522, width:220, height:170 } ]; }
	if(defs.width!=$("#pub").css("width"))
	{
		$("#pub").css({ "width":defs[0].width+"px", "height":defs[0].height+"px", });
		$("#pub2").css({ "width":defs[1].w+"px", "height":defs[1].h+"px", });
		$.get(url_website+"pub.php", { taille:defs[0].taille, nb:defs[0].nb, width:defs[1].width, height:defs[1].height },
		function(data) {
			eval(data);
		});
	}
}

function pub() {
	var wd=$("#main").width()*0.99;
  if(wd<=700) { var tablo=new Array(5, 500, 80, 80, 160, 363, 161, 359, 122, 408, "120x90"); }
  else if(wd<900) { var tablo=new Array(5, wd, 120, 132, 184, 417, 161, 359, 154, 504, "152x114"); }
  else { var tablo=new Array(5, wd, 140, 160, 224, 522, 211, 473, 222, 708, "220x165"); }
  $("#pub").css({ "width":tablo[1], "height":tablo[2] });
  $("#beerich").css({ "width":tablo[1], "height":tablo[2] });
  var src="http://xml.bee-rich.com/vod_fr/tools/thumb_creator_v2.php?limit="+tablo[0]+"&ida=266&thumb_sizes="+tablo[3]+"&text_size=10&text_police=Verdana&color_bg=%23ffffff&color_link=%23ff1f28&color_txt=%23ff1f28&thumb_border=0&target=_blank&display_link=1&cols="+tablo[0]+"&desc_pos=b&bold_link=1&cat=22&random=1&md_tracker=histoiresh-top&integration=iframe";
  if(src!=$("#beerich").attr("src")) { $("#beerich").attr("src", src); }
  
  $("#wendise").css({ "width":tablo[4], "height":tablo[5] });
  var src=url_website+"wendise.php?w="+tablo[4]+"&h="+tablo[5];
  if(src!=$("#wendise").attr("src")) { $("#wendise").attr("src", src); }

  $("#smartmovies").css({ "width":tablo[6], "height":tablo[7] });
  var src=url_website+"smartmovies.php?w="+tablo[6]+"&h="+tablo[7];
  if(src!=$("#smartmovies").attr("src")) { $("#smartmovies").attr("src", src); }
}*/

function ajax(div, fichier, args, effet) {
	if(!effet) { effet=100; }
  $.get(url_website+fichier+".php?"+args, function(data) {
  	if(effet!=0) {
  		$("#"+div).fadeOut(effet);
      patienter(div);
      setTimeout(function() { $("#"+div).html(data); }, effet);
      //$("#"+div).html(data);
      $("#"+div).fadeIn(effet);
  	}
  	else { $("#"+div).html(data); }
  });
}

function compteur(id) {
	$.get(url_website+"ajax.php?type=out&id="+id);
 //alert(id);
}

function hide_search() {
	$("#recherche").fadeOut(1000);
}

function click() {
	if(!$.cookie("dejapopup")) {
		popunder(url_website+"top.php");
		$.cookie('dejapopup', '1', { expires:1 });
	}
}

function disclaimer() {
	if(!$.cookie("disclaimerok")) {
		$("#disclaimer-fond").css('height', $(document).height());
		ajax("disclaimer-cadre", "disclaimer", "");
		$("#disclaimer").show();
	}
}

function disclaimerok() {
	var la_duree=24*3600*30000;
	$.cookie('disclaimerok', '1', { expires:30 });
	$("#disclaimer").hide();
}

function bookmarks() {
	$("#b").html('<a href="javascript:;" onclick="hide_bookmarks();" class="bf" title="Masquer la barre d\'outils, attention, cela masquera aussi les outils des histoires !" style="top:95px;"><img src="'+url_website+'images/fermer.png" /></a><a href="http://www.histoires-hard.com/histoires.xml" target="_blank" class="bf" title="Abonnez-vous au flux RSS des histoires hard" style="top:113px;"><div class="rss">RSS</div></a><a href="javascript:;" onclick="AddFav();" class="bf" title="Ajoutez cette page &agrave; vos favoris" style="top:161px;"><div class="favoris">Favoris</div></a>');
	if((!$.cookie('bookmarks'))||($.cookie('bookmarks')!='0')) { show_bookmarks(); }
	else { hide_bookmarks(); }
}

function hide_bookmarks() {
	$("#b").fadeOut(2000);
  $("#c").html('<a href="javascript:;" onclick="show_bookmarks();" class="bf" title="Afficher les outils" style="top:95px;"><img src="'+url_website+'images/ouvrir.png" /></a>');
  $("#c").fadeIn(2000);
  $.cookie('bookmarks', '0', { expires:30 });
}

function show_bookmarks() {
	$("#b").fadeIn(2000);
  $("#c").fadeOut(2000);
  $.cookie('bookmarks', '1', { expires:30 });
}

function masquer_aide_note() {
	$("#aide_note").fadeOut(1000);
	$.cookie('aide_note', '0', { expires:30 });
}

function outils_histoire(id) {
	var contenu=$("#b").html();
	$("#b").html(contenu+'<a href="mailto:?subject=Histoire hard &agrave; lire&body=Voici une histoire hard que j\'ai bien aim&eacute; : http://www.histoires-hard.com/histoire-'+id+'.html" class="bf" title="Envoyer cette histoire hard par email" style="top:340px;"><div class="email">Email</div></a><a href="histoire-hard-'+id+'.txt" class="bf" title="T&eacute;l&eacute;charger cette histoire au format TXT" style="top:388px;"><div class="txt">TXT</div></a><a href="histoire-hard-'+id+'.pdf" class="bf" title="T&eacute;l&eacute;charger cette histoire au format PDF" style="top:436px;"><div class="pdf">PDF</div></a><a href="javascript:;" onclick="window.print();" title="Imprimer ce r&eacute;cit" class="bf" style="top:484px;"><div class="imprimer">Imprimer</div></a>');
}

function populate_search(type) {
	$.get(url_website+"ajax.php?type=filtre&filtre="+type, function(data) { $("#r").html(data); });
}

function popunder(url) {
	if((navigator.userAgent.indexOf("Firefox")!=-1)||(navigator.userAgent.indexOf("MSIE 8")!=-1))
	{
		exit_win=window.open(url, '_blank', 'width='+screen.width+', height='+screen.height+' , toolbar=1, location=1, directories=1, status=1, scrollbars=1, resizable=1, copyhistory=1, menuBar=1');
		if(exit_win!=null) exit_win.blur();
	}
	else
	{
		window.open(url, '_blank', 'width='+screen.width+', height='+screen.height+' , toolbar=1, location=1, directories=1, status=1, scrollbars=1, resizable=1, copyhistory=1, menuBar=1');
		self.focus();
	}
}

function commentaires(id, nb, page, type) {
	ajax("commentaires", "ajax", "type=comment&page="+page+"&nb="+nb+"&id="+id+"&type2="+type, 1000);
}

function comment_form(id, type) {
	$.ajax({
		url: "commentaire.php?type="+type+"&id="+id,
		success: function(data) {
			$("#commentaire").html(data);
			if(type==1)
			{
        $caption=$("<span/>");
        $("#note").children().not(":input").hide();
        $("#note").stars({ cancelShow:false, captionEl:$caption });
        $caption.appendTo("#note");
        $("#form_comment").validate({
        	rules: { email: { email:true }, site: { url:true } },
        	messages: { email: "Renseignez votre adresse email valide.", site: "Renseignez un site internet valide." }
        });
			}
			else
			{
				$("#form_comment").validate({
					rules: { commentaire: { required:true }, email: { email:true }, site: { url:true } },
					messages: { email: "Renseignez votre adresse email valide.", site: "Renseignez un site internet valide.", commentaire: "Entrez un commentaire." }
				});
			}
			$("#form_comment").ajaxForm({
				target: "#commentaire",
				success: function() { $("#commentaire").fadeIn("slow"); },
				beforeSubmit: function() { return $("#form_comment").validate().form(); }
			});
		},
		error: function(data) { $("#commentaire").html("Une erreur s'est produite et le formulaire n'a pas pu &ecirc;tre charg&eacute;, merci de <a href=\"contact.php\">contacter le webmaster</a> si le disfonctionnement persiste."); }
	});
}

function signaler(id, type) {
	ajax("comment"+id, "ajax", "type=signaler&id="+id+"&type2="+type, 500);
	$("#comment"+id).addClass("erreur");
	$("#comment"+id).css({ "width":"470px", "padding-left":"50px" });
}

function go_top() {
	var delay=Math.round($(document).height()/2);
	$(document).scrollTo(0, 0, {duration:delay});
}

function backtotop() {
	if($(document).height()>($(window).height()*2.7)) {
		$("#top").html("Retour en haut");
		$("#top").addClass("right clear");
		$("#top").click(function() { go_top(); });
	}
}

function valid_filtres() {
	$(location).attr("href", url_website+"1-"+$("#filtres .lieu").val()+"-"+$("#filtres .theme").val()+"-"+$("#filtres .persos").val()+"-"+$("#filtres .type").val()+"-1.html");
	return false;
}

function menu_auteur() {
	$.get(url_website+"ajax.php?type=menu", function(data) {
		$("#menu_auteur").html(data);
		$("#menu_auteur a.ajax").fancybox({"type":"ajax" });
	});
}

function compteur(id) { $.get(url_website+"ajax.php?type=out&id="+id); }

//$(window).resize(function() { pub(); });

$(document).ready(function() {
	disclaimer();
	menu_auteur(); bookmarks(); backtotop();
	//pub();
	$("a.acount").click(function() { compteur($(this).attr("rel")); });
	$("a.contact").attr("href", "contact.php?mode=ajax"); $("a.contact").fancybox();
	$("a.details_note").attr("title", "Détail des notes");
	$("a.details_note").attr("href", function() { return url_website+"ajax.php?type=note&id="+$(this).attr("rel"); });
	$("a.details_note img").css({ width:'16px', height:'16px', border:'0', display:'inline', clear:'none' });
	$("a.details_note").fancybox({});
	$("#bas_droite").append($("<a />", {
		"html": "vid&eacute;o gratuite",
		"click": function () {
			$.fancybox({
				"type": "iframe",
				"width": 640,
				"height": 496,
				"href": "http://promo.vador.com/index.php/generateflv?w=318&e=0&v=0&p=15&ct=2&t1=histoiresh&t2=video&s=&c=17&f=640x480&d=90&bc=FFFFFF&fc=000000&lc=000000&lo=&h=&pc=19&lpp=true&ts=12&tc=ff0000&pf=0"
			});
			return false;
		}
	}));
});
