/* JQuery powered Javascript
 * initialisation + custom animation + JQuery Smart Columns
 * for LeComtois.com
 * by Mikaël Geljic
 * |\/|.GELJIC [à] G|\/|AIL
 */
$(document).ready(function(){
	var hoverfade = false;
	var cols = 0;
	if($("div#logo").css("position") == "relative") { // checks a single css rule to see if CSS are actually enabled*/
		$(window).bind("resize", smartColumns).trigger("resize", false);
		$('li.category ul li a').prepend("<span class='arrow'>► </span>");
		listInit();
		defilInit();
		custoInit();
	}
    // CSS & JQUERY SMART COLUMNS (by Soh Tanaka / www.sohtanaka.com) cheers man!
  	function smartColumns(event, force) {
  		//Reset column size to a 100% once view port has been adjusted
		if(($("ul.featured").length) != 0) {
			$("ul.featured").css('width',"100%");
			var colWrap = $("ul.featured").width()+10; //Get the width of row
			var colNum = Math.floor(colWrap / 185); //Find how many columns of 200px can fit per row / then round it down to a whole number
		  if(((colNum>1) && (colNum != cols)) || (force)) {
        cols = colNum;
        smartVertical(colNum);
      }
    	var colFixed = Math.floor(colWrap / colNum); //Get the width of the row and divide it by the number of columns it can fit / then round it down to a whole number. This value will be the exact width of the re-adjusted column
			var colOffset = 0;
			if(colFixed>210) {
				colFixed = 210;
				colOffset = Math.floor((colWrap-(colNum*colFixed)));
			}
			$("ul.featured").css('width',colNum*colFixed).css('margin-left',colOffset/2);//Set exact width of row in pixels instead of using % - Prevents cross-browser bugs that appear in certain view port resolutions.
			$("li.item").css('width',colFixed);//Set exact width of the re-adjusted column
		}
  	}
  	
  	function smartVertical(nbCols) {
      var placeholder = '<li class="item small placeholder"></li>';
      var k=0;
      //var ul;
      $("ul.featured").each(function() {
        $(this).children("li.placeholder").remove();
        $(this).children("li:not(.small)").css("height", "310px");
        //$(this).append($(this).children('li').length);
        var k=0;
        var l=$(this).children('li').length;
        while((k<l-(l-1)%nbCols-1) && (l>nbCols)) {
          $(this).children('li.item:eq('+(k)+')').css("height", "150px");
          if(!$(this).children('li.item:eq('+k+')').hasClass('small')) {
            $(this).children('li.item:eq('+(k+nbCols-1)+')').after(placeholder);
          }
          k++;
          l=$(this).children('li').length;
        }
      });
      $("li.placeholder").css("margin-bottom", "160px").css("height","0px");
    }
		
	// TEXTINPUTS & PASSWORD by (Mikaël Geljic)
	$('input#psdinput').val('votre pseudo').focus(function(){
		$(this).val('').unbind('focus');
	});
	$('input#pwdinput').after('<input class="textinput" id="pwdlabel" type="text" name="passe_label" size="16" value="votre mot de passe" />').css('display','none');
	$('input#pwdlabel').focus(function(){
		$('input#pwdinput').css('display','inline').trigger('focus');
		$('input#pwdlabel').css('display','none');
	});
	$('input#emailinput').val('votre e-mail').focus(function(){
		$(this).val('').unbind('focus');
	});
	
	// PARENTHESIS MENU ANIMATION (by Mikaël Geljic) using JQuery timers
	function listEnter() {
		$(this).stopTime();
		$(this).oneTime(200, function() {
			$(this).children('ul').slideDown("fast");
			$('ul#menu-categories li.category').not(this).children('ul').slideUp("fast");
		});
	}
	function listLeave() {
		$(this).stopTime();
		$(this).oneTime(700, function() {
			$(this).children('ul').slideUp("fast");
		});
	}
	function listInit() {
		$('ul#menu-categories li.category > ul').css("display","none");
		$('ul#menu-categories li.category').bind('mouseenter', listEnter).bind('mouseleave', listLeave);
	}
	
	function defilInit() {
		var times = defileur.length;
		$("div.defil p").everyTime(5000,function(i) {
			$(this).hide().empty().append(defileur[i%times]).fadeIn("fast");
		});
	}
	
	function custoInit() {
		var custo_ini = $("div.custom-div").html();
	  var custo_act_ini = '<a id="custom-action" class="tooltip" href="/compte.php" title="Diffusez vos infos">Diffusez vos infos</a>';
	  var times_c = custo.length;
		
		$("div.custom-div").everyTime(5000,function(j) {
		  if(j%times_c == 0) {
        $(this).empty().append(custo_ini);
			  $('a#custom-action').replaceWith(custo_act_ini);
      } else {
  			$(this).empty().append(custo[j%times_c]);
			  $('a#custom-action').replaceWith(custo_act[j%times_c]);
			}
		});
	}
});

// PREEXISTING
function SesameLI(page,target) {
	window.open(page,target,'width=420,height=380,top=50,left=50,location=0,scrollbars=0,status=1,menubar=0');
}

function favoris() {
	browsername=navigator.appName;
	version=navigator.appVersion;
	if (browsername.indexOf("Netscape")!=-1) {
		window.open("/../services/popupfavns.htm","Netscape","width=320,height=230");
	} else {
		if (browsername.indexOf("Microsoft")!=-1) {
			if (version.indexOf("MSIE 4") !=-1 || version.indexOf("MSIE 5") !=-1 || version.indexOf("MSIE 6") !=-1 || version.indexOf("MSIE 7") !=-1) {
				var favoriteurl="http://www.lecomtois.com";
				var favoritetitle="LeComtois.com - Le Guide Culturel en Franche-Comté";
				window.external.AddFavorite(favoriteurl, favoritetitle);
			} else {
				window.open("/../services/popupfavns.htm","Internet Explorer","width=320,height=230");
			}	
		} else {
			window.open("/../services/popupfavns.htm","Netscape","width=320,height=230");
		}
	}
}

function demarrage() {
	browsername=navigator.appName;
	version=navigator.appVersion;
	if (browsername.indexOf("Netscape")!=-1) {
		window.open("/../services/popupstartns.htm","Netscape","width=320,height=230");
	} else {
		if (browsername.indexOf("Microsoft")!=-1) {
			if (version.indexOf("MSIE 5")==-1 & version.indexOf("MSIE 6")==-1) {
				window.open("/../services/popupstartns.htm","Netscape","width=320,height=230");
			}
		} else {
			browsername="N/A";
			window.open("/../services/popupstartns.htm","Netscape","width=320,height=230");
		}
	}
}