var toggle_form_connexion = false;

$(document).ready(function(){
	$(".m_dialog").dialog({modal:true});
	
	$('.input_form.recherche').blur(function(){
		if(this.value=='')
			this.value='rechercher sur le site ...';						   
	});
	$('.input_form.recherche').focus(function(){
		if(this.value=='rechercher sur le site ...')
			this.value='';						   
	});
	
	$(".input_form").before("<span class='sprite input_form_gauche'></span>")
						.after("<span class='sprite input_form_droite'></span>");
	
	$(".widget p a").before("<span class='sprite pictoLirePlus'></span>");
	
	// bouton espace pro
	$('.connexionEspacePro').hover(function(){
		$('.connexionEspacePro').css("background","url(../images/acceder_off-02.png)");				   
	});
	$('.connexionEspacePro').mouseout(function(){
		$('.connexionEspacePro').css("background","url(../images/acceder_off.png)");				   
	});
	$('.accesEspacePro').hover(function(){
		$('.accesEspacePro').css("background","url(../images/espacepro_on.png)");				   
	});
	$('.accesEspacePro').mouseout(function(){
		$('.accesEspacePro').css("background","url(../images/espacepro_off.png)");				   
	});
	
	// bouton connexion
	$('.connecEsp').hover(function(){
		$('.connecEsp').css("backgroundPosition","-105px -35px");				   
	});
	$('.connecEsp').mouseout(function(){
		$('.connecEsp').css("backgroundPosition","-105px 0px");				   
	});
	
	// bouton newsletter
	$('.inscriptionNewsletter').hover(function(){
		$('.inscriptionNewsletter').css("backgroundPosition","-310px -35px");				   
	});
	$('.inscriptionNewsletter').mouseout(function(){
		$('.inscriptionNewsletter').css("backgroundPosition","-310px 0px");				   
	});
	
	// loupe recherche
	$('.loupeRecherche').hover(function(){
		$('.loupeRecherche').css("backgroundPosition","-195px -35px");				   
	});
	$('.loupeRecherche').mouseout(function(){
		$('.loupeRecherche').css("backgroundPosition","-195px 0px");				   
	});
	
	
	
	$('.affichageConnexion').click(function(){
											
		if(toggle_form_connexion == false)
		{
			$('#btnEspacePro').css({"display":"block"});
			$('#btnEspacePro').animate({top:'0px'});
			$('.connexionEspacePro').css({"background":"url(../images/fermer_off.png)"});
			$('.connexionEspacePro').hover(function(){
				$('.connexionEspacePro').css("background","url(../images/fermer_on.png)");				   
			});
			$('.connexionEspacePro').mouseout(function(){
				$('.connexionEspacePro').css("background","url(../images/fermer_off.png)");				   
			});
		}
		else
		{
			$('#btnEspacePro').animate({top:'-161px'},function(){
				$('#btnEspacePro').css({"display":"none"});												  
			});
			$('.connexionEspacePro').css({"background":"url(../images/acceder_off.png)"});
			$('.connexionEspacePro').hover(function(){
				$('.connexionEspacePro').css("background","url(../images/acceder_off-02.png)");				   
			});
			$('.connexionEspacePro').mouseout(function(){
				$('.connexionEspacePro').css("background","url(../images/acceder_off.png)");				   
			});
		}
		toggle_form_connexion = !toggle_form_connexion;
	});
});


// --------------------- Fonction gestion de comtpe ----------------
function get_recover_password() {
	$("#frm_recover_pass").dialog({modal:true, width:400});
}
function recover_password() {
	var email = $("#recovery_email").val();
	$.post("?", {recovery_email:email}, function(ret){
		var status = ret.substring(0, 1);
		var msg = ret.substring(1);
		
		var dialog_otpions = {title:"R&eacute;cup&eacute;ration de mot de passe", modal:true};
		if(status == "1") {
			dialog_otpions.close = function(){document.location.href="?";};
		}
		$("<div>"+msg+"</div>").dialog(dialog_otpions);
	}, "text");
}

function submit_form_crea(frm) {
	$.post("?", $(frm).serialize(), function(ret){
		var status = ret.substring(0, 1);
		var msg = ret.substring(1);
		
		var dialog_otpions = {title:"Cr&eacute;ation de compte", modal:true};
		if(status == "1") {
			dialog_otpions.close = function(){document.location.href="?";};
		}
		$("<div>"+msg+"</div>").dialog(dialog_otpions);
	}, "text");
}

/*Fonction Visionneuse*/
function appelFlash(movieName) {

    if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
    }
    else {
	    return document[movieName];
    }
} 

function afficherPartage(cle){
	appelFlash("laVisionneuse_"+cle).afficherPartage();	
}

function afficherExport(cle){
	appelFlash("laVisionneuse_"+cle).afficherExport();	
}

function affItemBlog(sens){
	if(sens=='prev'){offset = offset - 1;}
	if(sens=='next'){offset = offset +1;}
	if(offset < 0){offset = nb-1;}
	if(offset == nb){offset = 0;}
	//alert(offset);
	$("#divActuBlog").fadeOut(500).load('/ajax/getItemBlog.php?id='+offset).fadeIn(500);
}


/** SLIDER **/
/*
 * jQuery infinitecarousel plugin
 * @author admin@catchmyfame.com - http://www.catchmyfame.com
 * @version 1.2.2
 * @date August 31, 2009
 * @category jQuery plugin
 * @copyright (c) 2009 admin@catchmyfame.com (www.catchmyfame.com)
 * @license CC Attribution-Share Alike 3.0 - http://creativecommons.org/licenses/by-sa/3.0/
 */

(function($){
	$.fn.extend({ 
		infiniteCarousel: function(options)
		{
			var defaults = 
			{
				transitionSpeed : 1500,
				displayTime : 6000,
				textholderHeight : '13px',
				displayProgressBar : 1,
				displayThumbnails: 1,
				displayThumbnailNumbers: 1,
				displayThumbnailBackground: 1,
				thumbnailWidth: '20px',
				thumbnailHeight: '28px',
				thumbnailFontSize: '14px'
			};
		var options = $.extend(defaults, options);
	
    		return this.each(function() {
    			var randID = Math.round(Math.random()*100000000);
				var o=options;
				var obj = $(this);
				var curr = 1;

				var numImages = $('img', obj).length; // Number of images
				var imgHeight = $('img:first', obj).height();
				var imgWidth = $('img:first', obj).width();
				var autopilot = 1;
		
				$('p', obj).hide(); // Hide any text paragraphs in the carousel
				$(obj).width(imgWidth).height(imgHeight);
			
				// Build progress bar
				if(o.displayProgressBar)
				{
					$(obj).append('<div id="progress'+randID+'" style="position:absolute;bottom:0;background:#bbb;left:'+$(obj).css('paddingLeft')+'"></div>');
					$('#progress'+randID).width(imgWidth).height(5).css('opacity','.5');
				}
			
				// Move last image and stick it on the front
				$(obj).css({'overflow':'hidden','position':'relative'});
				$('li:last', obj).prependTo($('ul', obj));
				$('ul', obj).css('left',-imgWidth+'px');
				$('ul',obj).width(9999);

				$('ul',obj).css({'list-style':'none','margin':'0','padding':'0','position':'relative'});
				$('li',obj).css({'display':'inline','float':'left'});
			
				// Build textholder div thats as wide as the carousel and 20%-25% of the height
				$(obj).append('<div id="textholder'+randID+'" class="textholder" style="position:absolute;bottom:0px;margin-bottom:'+-imgHeight*o.textholderHeight+'px;left:'+$(obj).css('paddingLeft')+'"></div>');
				var correctTHWidth = parseInt($('#textholder'+randID).css('paddingTop'));
				var correctTHHeight = parseInt($('#textholder'+randID).css('paddingRight'));
				$('#textholder'+randID).width("250px").height("430px").css({'background':'url(../images/bg_txt_slideshow.png)'});
				showtext($('li:eq(1) p', obj).html());
				$('#textholder'+randID).wrapInner('<div class="contentTextSlideshow"></div>');
				if(o.displayThumbnails)
				{
					// Build thumbnail viewer and thumbnail divs
					$(obj).after('<div id="thumbs'+randID+'" class="thumbDiv"></div>');
					
						$('#thumbs'+randID).append('</span><div class="thumb" id="btn_lt'+randID+'" style="cursor:pointer;;display:inline-block;float:left;width:25px;height:'+o.thumbnailHeight+';line-height:'+o.thumbnailHeight+';padding:0;overflow:hidden;text-align:center;margin-right:0px;font-size:'+o.thumbnailFontSize+';vertical-align:bottom"><img src="images/left-arrow.png" style="height:13px;width:11px;margin-top:8px;" /></div>');
						
					for(i=0;i<=numImages-1;i++)
					{
						thumb = $('img:eq('+(i+1)+')', obj).attr('src');
						$('#thumbs'+randID).append('<div class="thumb" id="thumb'+randID+'_'+(i+1)+'" style="cursor:pointer;background-image:url('+thumb+');display:inline;float:left;width:30px;height:'+o.thumbnailHeight+';line-height:'+o.thumbnailHeight+';padding:0;overflow:hidden;text-align:center;margin-right:0px;font-size:'+o.thumbnailFontSize+';">'+(i+1)+'</div>');
						if(i==0) $('#thumb'+randID+'_1').css({'color':'#0C6922','fontWeight':'bold'});
					}
					
						$('#thumbs'+randID).append('<div class="thumb" id="btn_rt'+randID+'" style="cursor:pointer;;display:inline;float:left;width:20px;height:'+o.thumbnailHeight+';line-height:'+o.thumbnailHeight+';padding:0;overflow:hidden;text-align:center;margin-right:0px;font-size:'+o.thumbnailFontSize+';"><img src="images/right-arrow.png" style="height:13px;width:11px;margin-top:8px;" /></div>');
				
					// Left and right arrow image button actions
					$('#btn_rt'+randID).click(function(){
						autopilot = 0;
						anim('next');
						clearTimeout(clearInt);
					});
					$('#btn_lt'+randID).click(function(){
						autopilot = 0;
						anim('prev');
						clearTimeout(clearInt);
					});
				
				
					// Next two lines are a special case to handle the first list element which was originally the last
					thumb = $('img:first', obj).attr('src');
					$('#thumb'+randID+'_'+numImages).css({'background-image':'url('+thumb+')'});
					// $('#thumbs'+randID+' div.thumb:not(:first)').css({'opacity':'1'}); // makes all thumbs 65% opaque except the first one
					// $('#thumbs'+randID+' div.thumb').hover(function(){$(this).animate({'opacity':1},150)},function(){if(curr!=this.id.split('_')[1]) $(this).animate({'opacity':1},250)}); // add hover to thumbs

					// Assign click handler for the thumbnails. Normally the format $('.thumb') would work but since it's outside of our object (obj) it would get called multiple times
					$('#thumbs'+randID+' div').bind('click', thumbclick); // We use bind instead of just plain click so that we can repeatedly remove and reattach the handler
				
					if(!o.displayThumbnailNumbers) $('#thumbs'+randID+' div').text('');
					if(!o.displayThumbnailBackground) $('#thumbs'+randID+' div').css({'background-image':''});
				}
				
				
				function thumbclick(event)
				{
					target_num = this.id.split('_'); // we want target_num[1]
					if(curr != target_num[1])
					{
						$('#thumb'+randID+'_'+curr).css({});
						$('#progress'+randID).stop().fadeOut();
						clearTimeout(clearInt);
						
						$('#thumbs'+randID+' div').css({'cursor':'default'}).unbind('click'); // Unbind the thumbnail click event until the transition has ended
						autopilot = 0;
					}
					if(target_num[1] > curr)
					{
						diff = target_num[1] - curr;
						anim('next',diff);
					}
					if(target_num[1] < curr)
					{
						diff = curr - target_num[1];
						anim('prev', diff);
					}
					
					$('#btn_rt'+randID).click(function(){
						autopilot = 0;
						anim('next');
						clearTimeout(clearInt);
					});
					$('#btn_lt'+randID).click(function(){
						autopilot = 0;
						anim('prev');
						clearTimeout(clearInt);
					});
				}

				function showtext(t)
				{
					// the text will always be the text of the second list item (if it exists)
					if(t != null)
					{
						$('#textholder'+randID).html(t).animate({marginLeft:'0px'},500); // Raise textholder
						$('#textholder'+randID).wrapInner('<div class="contentTextSlideshow"></div>');
						showminmax();
					}
				}
				function showminmax()
				{
						if(!autopilot)
						{
							$('#min').fadeIn(250).click(function(){$('#textholder'+randID).animate({marginBottom:(-imgHeight*o.textholderHeight)-(correctTHHeight * 2)+24+'px'},500,function(){$("#min,#max").toggle();});});
							$('#max').click(function(){$('#textholder'+randID).animate({marginBottom:'0px'},500,function(){$("#min,#max").toggle();});});
							$('#close').fadeIn(250).click(function(){$('#textholder'+randID).animate({marginBottom:(-imgHeight*o.textholderHeight)-(correctTHHeight * 2)+'px'},500);});
							$('#textholder'+randID).wrapInner('<div class="contentTextSlideshow"></div>');
						}
				}
				function borderpatrol(elem)
				{
					//alert(curr)
					$('#thumbs'+randID+' div').css({'color':'#31442B','fontWeight':'normal'});
					setTimeout(function(){elem.css({'color':'#0C6922','fontWeight':'bold'})},o.transitionSpeed);
				}
				function anim(direction,dist)
				{
					// Fade left/right arrows out when transitioning
					// animate textholder out of frame
					$('#textholder'+randID).animate({marginLeft:'-402px'},500);		
					$('#textholder'+randID).wrapInner('<div class="contentTextSlideshow"></div>');
					//?? Fade out play/pause?
					
					if(direction == "next")
					{
						if(curr==numImages) curr=0;
						if(dist>1)
						{
							borderpatrol($('#thumb'+randID+'_'+(curr+dist)));
							$('li:lt(2)', obj).clone().insertAfter($('li:last', obj));
							$('ul', obj).animate({left:-imgWidth*(dist+1)},o.transitionSpeed,function(){
								$('li:lt(2)', obj).remove();
								for(j=1;j<=dist-2;j++)
								{
									$('li:first', obj).clone().insertAfter($('li:last', obj));
									$('li:first', obj).remove();
								}
								showtext($('li:eq(1) p', obj).html());
								$(this).css({'left':-imgWidth});
								curr = curr+dist;
								$('#thumbs'+randID+' div').bind('click', thumbclick).css({'cursor':'pointer'});
							});
						}
						else
						{
							
							borderpatrol($('#thumb'+randID+'_'+(curr+1)));
							$('#thumbs'+randID+' div').css({'cursor':'default'}).unbind('click'); // Unbind the thumbnail click event until the transition has ended
							// Copy leftmost (first) li and insert it after the last li
							$('li:first', obj).clone().insertAfter($('li:last', obj));	
							// Update width and left position of ul and animate ul to the left
							$('ul', obj)
								.animate({left:-imgWidth*2},o.transitionSpeed,function(){
									$('li:first', obj).remove();
									$('ul', obj).css('left',-imgWidth+'px');
									showtext($('li:eq(1) p', obj).html());
									if(autopilot)
									{
										$('#progress'+randID).width(imgWidth).height(5);
									}
									curr=curr+1;
									$('#thumbs'+randID+' div').bind('click', thumbclick).css({'cursor':'pointer'});
								});
						}
					}
					if(direction == "prev")
					{ 
						if(dist>1)
						{
							borderpatrol($('#thumb'+randID+'_'+(curr-dist)));
							$('li:gt('+(numImages-(dist+1))+')', obj).clone().insertBefore($('li:first', obj));
							$('ul', obj).css({'left':(-imgWidth*(dist+1))}).animate({left:-imgWidth},o.transitionSpeed,function(){
								$('li:gt('+(numImages-1)+')', obj).remove();
								showtext($('li:eq(1) p', obj).html());
								curr = curr - dist;
								$('#thumbs'+randID+' div').bind('click', thumbclick).css({'cursor':'pointer'});
							});
						}
						else
						{
							borderpatrol($('#thumb'+randID+'_'+(curr-1)));
							$('#thumbs'+randID+' div').css({'cursor':'default'}).unbind('click'); // Unbind the thumbnail click event until the transition has ended
							// Copy rightmost (last) li and insert it after the first li
							$('li:last', obj).clone().insertBefore($('li:first', obj));
							// Update width and left position of ul and animate ul to the right
							$('ul', obj)
								.css('left',-imgWidth*2+'px')
								.animate({left:-imgWidth},o.transitionSpeed,function(){
									$('li:last', obj).remove();
									showtext($('li:eq(1) p', obj).html());
									curr=curr-1;
									if(curr==0) curr=numImages;
									$('#thumbs'+randID+' div').bind('click', thumbclick).css({'cursor':'pointer'});
								});
						}
					}
					$('#btn_rt'+randID).click(function(){
					autopilot = 0;
					anim('next');
					clearTimeout(clearInt);
				});
				$('#btn_lt'+randID).click(function(){
					autopilot = 0;
					anim('prev');
					clearTimeout(clearInt);
				});
				}

				var clearInt = setInterval(function(){anim('next');},o.displayTime+o.transitionSpeed);
				$('#progress'+randID).animate({'width':0},o.displayTime+o.transitionSpeed,function(){

				});
  		});
    	}
	});
})(jQuery);
/** FIN SLIDER **/
