$(document).ready(function() {
	if (window.attachEvent) { sfHover(); }
	loginAnimInit();
	ShowDiv();
	deroulerAuto();

	//$("form[class*='form1']").jForms({listSize:20, imagePath:BasePath + '/medias/pictures/css/'});
	$("form[class*='form2']").jForms({listSize:10, imagePath:BasePath + '/medias/pictures/css/'});
	$("form[class*='form3']").jForms({listSize:10, imagePath:BasePath + '/medias/pictures/css/'});
	$("form[class*='form4']").jForms({listSize:10, imagePath:BasePath + '/medias/pictures/css/'});
	$("form[class*='form5']").jForms({listSize:10, imagePath:BasePath + '/medias/pictures/css/'});
	$("form[class*='form9']").jForms({listSize:10, imagePath:BasePath + '/medias/pictures/css/'});

	$("form[class*='form_date']").jForms({listSize:10, imagePath:BasePath + '/medias/pictures/css/'});
	$("div[class*='form4']").jForms({listSize:10, imagePath:BasePath + '/medias/pictures/css/'});

	$("div[class*='inpt_pass']").jForms({listSize:20, imagePath:BasePath + '/medias/pictures/css/'});
	$("div[class*='login_input']").jForms({listSize:20, imagePath:BasePath + '/medias/pictures/login/'});
	$("div[class*='inpt_search']").jForms({listSize:20, imagePath:BasePath + '/medias/pictures/search/'});


});



function sfHover() {
	$('#menu_haut li').hover(
		function(){$(this).addClass('sfhover');},
		function(){$(this).removeClass('sfhover');}
	);
}

function ShowDiv() {
	$('#recherche_val').hover(
		function(){$('#sub_menu').fadeIn('fast');},
		function(){$('#sub_menu').fadeOut('fast');}
	);
}


function switchTab(objClick) {
	var objParentLi = $(objClick).parents('li:eq(0)');
	var objParentTab = $(objClick).parents('div[class*="tabbedParent"]:eq(0)');
	var classe = $(objParentLi).attr('class');

	$(objParentTab).find('a').removeClass('actif');
	$(objClick).addClass('actif');

	$(objParentTab).find('div[class*="tabbedMain"]:visible').slideUp(400, function () {
		$(objParentTab).find('div[class*="tabbedMain '+classe+'"]').slideDown(300);
	});

	return false;
}


 /*
  *  Load the numbered container ajax data
  *  Update active number
  */
 function numberedLoad(clickedObj,urlStr) {

   var containerObj = $(clickedObj).parents('div[class*="numberedParent"]').get(0);
   urlStr = urlStr.replace(new RegExp('&amp;','g'),'&');

   var height = $(containerObj).height();
   $(containerObj).html('');
   $(containerObj).addClass('loading');
   $(containerObj).height(height);

   $(containerObj).load(urlStr,function() {
		$(containerObj).hide();
		$(containerObj).removeClass('loading');
   		$(containerObj).height('');
		$(containerObj).fadeIn('fast');
   });

   return false;
 }


 /*
  *  Load the numbered container ajax data
  *  Update active number
  */
 function tabbedLoad(clickedObj,urlStr) {

   var containerObj = $(clickedObj).parents('div[class*="tabbedParent"]').get(0);
   containerObj = $(containerObj).find('div[class*="tabbedMain"]').get(0);
   urlStr = urlStr.replace(new RegExp('&amp;','g'),'&');

	// Empty content
   var height = $(containerObj).height();
   $(containerObj).html('');
   $(containerObj).addClass('loading');
   $(containerObj).height(height);

	// Switch tab style
   var parentObj = $(clickedObj).parents('ul').get(0);
   $(parentObj).find('a').removeClass('actif');
   $(clickedObj).addClass('actif');
   $(clickedObj).blur();

	// Load content
   $(containerObj).load(urlStr,function() {
		$(containerObj).hide();
		$(containerObj).removeClass('loading');
   		$(containerObj).height('');
		$(containerObj).fadeIn('fast');
   });


   return false;
 }



function loginAnimInit() {
	// Animation
	$('#Login').focus(function () {
			$(this).attr('value','');
			$(this).unbind();
			loginAnim();
		});


	$('#loginpassfield input#password').focus(function () {
			$(this).unbind();

			var id = $(this).attr('id');
			var name = $(this).attr('name');
			var css = $(this).attr('class');
			var size = $(this).attr('size');

			$(this).replaceWith('<input type="password" value="" id="'+id+'" size="'+size+'" name="'+name+'" class="'+css+'" />' );
			$('#'+id).focus();

		});

		return true;
}


function loginAnim() {
	$('label[for="Login"]').hide();
	$('label[for="Login"]').after('<label id="LoginJquery"><img src="' + BasePath + '/medias/pictures/css/login_close.gif" /></label>');
	$('#recherche_form').hide();
	$('#loginpassfield:hidden').fadeIn('slow');
	$('.form form.login').width('450px');


	$('label#LoginJquery').click(function() {
			$('#loginpassfield').hide();
			$('.form form.login').width('286px');
			$('#recherche_form').fadeIn('slow');
			$(this).remove();
			$('label[for="Login"]').show();
			$('#Login').focus(function () { $(this).unbind(); loginAnim(); });
		})
}


function deroulerAuto () {
	$('div[class*="lien_droulant"]').hover(
			function () { $(this).next('ul').show(); },
			function () { $(this).next('ul').hide(); }
		);
	$('ul[class*="menu_deroulant"]').hover(
			function () { $(this).show(); },
			function () { $(this).hide(); }
		);

	return true;
}


function loginStatus() {
	var div = $('div[class="loginStatus"]');
	$(div).find('script').remove();
	var content = $(div).html()
	$(div).remove();

	popunder(content,'','500');

	return true;
}



function paginationActu(objParent, nbr, page) {
  // Cleaning
  page = parseInt(page,0);
  nbr = parseInt(nbr,0);

  // Filtering
  $(objParent).find('ul li').show();
  $(objParent).find('ul li:lt('+((page * nbr) - 1)+')').hide();
  $(objParent).find('ul li:gt('+(((page + 1) * nbr) - 1)+')').hide();

  // Counting
  var total = $(objParent).find('ul li').length;
  var pages = Math.ceil(total / nbr);

  // Interractive
  $(objParent).find('div[class*="pagination_biz"]').each(function() {
  	if (pages == 1) {
  		$(this).remove();
  	}

    // clean events
    $(this).find('img').unbind().css('cursor','auto').show().each(function(){$(this).attr('src', $(this).attr('src').replace(new RegExp('-disabled.gif','g'),'.gif') );});

    // Start/end pages
    $(this).find('img:eq(0)').click(function(){ paginationActu(objParent,nbr,0) }).css('cursor','pointer');
    $(this).find('img:eq(3)').click(function(){ paginationActu(objParent,nbr,(pages - 1)) }).css('cursor','pointer');

    // Next/prev pages
    if (page == 0) {
      $(this).find('img:lt(2)').each(function(){$(this).attr('src', $(this).attr('src').replace(new RegExp('.gif','g'),'-disabled.gif') );}).css('cursor','auto');
    } else {
      $(this).find('img:eq(1)').click(function(){ paginationActu(objParent,nbr,(page - 1)) }).css('cursor','pointer');
    }

    if ((page + 1) == pages) {
      $(this).find('img:gt(1)').each(function(){$(this).attr('src', $(this).attr('src').replace(new RegExp('.gif','g'),'-disabled.gif') );}).css('cursor','auto');
    } else {
      $(this).find('img:eq(2)').click(function(){ paginationActu(objParent,nbr,(page + 1)) }).css('cursor','pointer');
    }

    // Selector
    $(this).find('select').unbind().html('');
    for (i = 0; i < pages; i++) {
      $(this).find('select').append('<option value="'+i+'">'+ (i + 1) +'</option>');
    }
    $(this).find('select option:eq('+page+')').attr('selected','selected');
    $(this).find('select').change(function(){
      paginationActu(objParent,nbr,this.value);
    });

  });
}


function popunder(content, url, width, height) {

	var style = '';

  	if (typeof(width) != "undefined") {
  		style = 'width:' + width + 'px;';
  	}


  	if (typeof(height) != "undefined") {
  		style = style + 'height:' + height + 'px;';
  	}

	//Hhide for IE
	$('embed, object, select').css({ 'visibility' : 'hidden' });

	// Add container
	if (typeof(url) != "undefined" && url != "") {
		url = url.replace(new RegExp('&amp;','g'),'&');
		$('body').prepend('<div id="popunder-overlay"></div><div id="popunder-lightbox"><iframe src="'+url+'" frameborder="0" scrolling="no" id="popunder-container" name="popunder-container" style="' + style + '"></iframe><a href="#" id="popunder-close" style="'+style+'"><span class="right_sub">&nbsp;</span><span class="center_sub">Fermer</span><span class="left_sub">&nbsp;</span></a></div>');
		$('#popunder-lightbox iframe').attr('src', url);
	} else {
		$('body').prepend('<div id="popunder-overlay"></div><div id="popunder-lightbox"><div id="contentbox" style="' + style + '">'+content+'</div><a href="#" id="popunder-close" style="'+style+'"><span class="right_sub">&nbsp;</span><span class="center_sub">Fermer</span><span class="left_sub">&nbsp;</span></a></div>');
	}
	// Get page size
	var arrPageSizes = ___getPageSize();


	// Styling overlay
	$('#popunder-overlay').css({
		backgroundColor:	'#050000',
		opacity:			'0.7',
		width:				arrPageSizes[0],
		height:				arrPageSizes[1]
	}).fadeIn('slow');


	// Calculate top and left offset for the iframe and show it
	var arrPageScroll = ___getPageScroll();
	$('#popunder-lightbox').css({
		top:	arrPageScroll[1] + (arrPageSizes[3] / 10),
		left:	arrPageScroll[0]
	}).show();

	// Assigning click events in elements to close overlay
	$('#popunder-overlay,#popunder-close').click(function() {
		popunder_kill();
		return false;
	});

	// If window was resized, calculate the new overlay dimensions
	$(window).resize(function() {
		popunder_overlayresize();
	});


	$('#popunder-lightbox iframe').load(function() {
					var hauteur = parseInt(this.contentWindow.document.body.offsetHeight,10) + 'px';
					$(this).animate({
						height: hauteur
					}, 400 );
	});
	$(document).ready(function () {
		popunder_overlayresize();
	});


	return false; // block link
}

function popunder_kill() {
	$('#popunder-lightbox').slideUp('fast', function() {
		$('#popunder-overlay').fadeOut('fast',function() {
			// Show some elements to avoid conflict with overlay in IE. These elements appear above the overlay.
			$('embed, object, select').css({ 'visibility' : 'visible' });

			$('#popunder-lightbox').remove();
			$('#popunder-overlay').remove();
		});
	});
}


function popunder_overlayresize() {
	// Get page sizes
	var arrPageSizes = ___getPageSize();
	// Style overlay and show it
	$('#popunder-overlay').css({
		width:		arrPageSizes[0],
		height:		arrPageSizes[1]
	});
	// Get page scroll
	var arrPageScroll = ___getPageScroll();

	// Calculate top and left offset for the iframe and show it
	$('#popunder-lightbox').css({
		top:	arrPageScroll[1] + (arrPageSizes[3] / 8),
		left:	arrPageScroll[0]
	});



}







/**
 / THIRD FUNCTION
 * getPageSize() by quirksmode.com
 *
 * @return Array Return an array with page width, height and window width, height
 */
function ___getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth;
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = xScroll;
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
};
/**
 / THIRD FUNCTION
 * getPageScroll() by quirksmode.com
 *
 * @return Array Return an array with x,y page scroll values.
 */
function ___getPageScroll() {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;
	}
	arrayPageScroll = new Array(xScroll,yScroll)
	return arrayPageScroll;
};





