/* ***************************************************** */
/* COPYRIGHT: Creavalley, Caen 14000                     */
/* Auteurs: Cyril Lemartinet, Ludvig Le Tullier          */
/* ***************************************************** */

/* ***************************************************** */
/*             A CHARGER AU DEMARAGE           */
/* ***************************************************** */

function jQuery_domready(){
						jQuery_billboard();
						jQuery_footer();
						// jQuery_gamme();
						jQuery_infobulle();
						// jQuery_panier();
						// jQuery_stop();
						jQuery_autoComplete();
}

/* ***************************************************** */
/*           STOP ANIMATION EN COURS              */
/* ***************************************************** */

function jQuery_stop(){
	$(document).keypress(function (event) {
		if (event.keyCode == '27'){  // appuie sur la touche ECHAP
			jQuery_chargement(false);
			jQuery_alert(false);
		}
	});
}

/* ***************************************************** */
/*                     CHARGEMENT                        */
/*        parametres : (string) texte à afficher        */
/* ***************************************************** */

function jQuery_chargement(txt){
	if(txt==false)$('.black_charg, .chargement').fadeTo(500).remove();
	else {
		$('body').append('<div id="lbOverlay" class="black_charg" style="height: 100%;width:100%; opacity: 0;filter:alpha(opacity=0);"></div><div class="chargement"></div>');
		$('.chargement').html(txt+'<br><img src="img/billboard/loader.gif">');
		$('.black_charg').fadeTo(300,0.5);
		$('.chargement').fadeTo(300,1);
		$('.chargement').draggable();
		}
}

/* ***************************************************** */
/*                            ALERT                             */
/*        parametres : (string) texte à afficher        */
/*        parametres : (string) fondu ou non           */
/* ***************************************************** */

function jQuery_alert(txt,fade){
	if(txt==false)$('.black_charg, .chargement').fadeTo(500).remove();
	else {
		$('body').append('<div id="lbOverlay" class="black_charg" style="height: 100%;width:100%; opacity: 0;filter:alpha(opacity=0);"></div><div class="chargement"></div>');
		$('.chargement').html('<div class="warning"></div><div class="warn_txt">'+txt+'<br><input type="button" value="OK" width="40" height="60" onclick="$(\'.black_charg, .chargement\').fadeTo(500).remove();"></div>');
		if(!fade){$('.black_charg').fadeTo(300,0.5);}
		else {$('.black_charg').css('opacity','0.7');}
		$('.chargement').css('opacity','1');
		$('.warn_txt input:button').focus();
		$('.chargement').draggable();
	}
}

/* ***************************************************** */
/*                         COMFIRM                           */
/*        parametres : (string) texte à afficher        */
/* ***************************************************** */

function jQuery_confirm(txt){
	$('body').append('<div id="lbOverlay" class="black_charg" style="height: 100%;width:100%; opacity: 0;filter:alpha(opacity=0);"></div><div class="chargement"></div>');
	$('.chargement').html('<div class="warning"></div><div class="warn_txt">'+txt+'<br><input type="button" value="Oui" width="40" height="60" onclick="$(\'.black_charg, .chargement\').fadeTo(500).remove();return true;"><input type="button" value="Annuler" width="40" height="60" onclick="$(\'.black_charg, .chargement\').fadeTo(500).remove(); return false;"></div>');
	$('.black_charg').fadeTo(6000,0.4);
	$('.chargement').css('opacity','1');
	$('.chargement').draggable();
}

/* ***************************************************** */
/*                  HOME BILLBOARD                     */
/* ***************************************************** */

function roll() {
	if(!$('.Billboard').hasClass('over'))
	{
		next = $('#Billboard_menu div.current').next('div');
		if($(next).has('.fleche').size() == 0)
		next = $('#Billboard_menu div:first'); // :first = webmag :eq(2) = ecriture ...
		$(next).triggerHandler('click');
	}
	window.setTimeout('roll()', 5000);
}

function jQuery_billboard(){
	var started = false;
	var slide_height = 125;
	var slide_title_height = 41;
	
	$('#Billboard_Pictures div').css({opacity: 0,display:'none'});
	$('.box_boutique div').css({opacity: 1,display:'block'});
	$('#Billboard_menu div').bind('click', function(){
		if($(this).hasClass('current')) return false;
		$('.fleche').animate({width: 0}, 200);
		$('#Billboard_menu div').removeClass('current');
		$('.current .fleche,#Billboard_Pictures div,#Billboard_Rayons').stop();
		var idx = $('#Billboard_menu div:not(.fleche)').index(this);
		$('#Billboard_Pictures div:not(' + idx + ')').css({'z-index': 0,display:'none'});
		$('#Billboard_Pictures div:eq(' + idx + ')').css({'z-index': 1,display:'block'}).fadeTo(1000, 1, function(){
			$('#Billboard_Pictures div:not(div:eq(' + idx + '))').css({opacity: 0,display:'none'});
		});
		$('#Billboard_Rayons').animate({top : -slide_title_height}, 300, function(){
			$('#Billboard_Rayons div,#box_hide').css({display:'none'});
			//if(idx!=0) $('#Billboard_Rayons div:eq(' + idx + '),#box_hide').css({display:'block'}); //condition pour le slide du premier
			$('#Billboard_Rayons div:eq(' + idx + '),#box_hide').css({display:'block'});
			$('#Billboard_Rayons').animate({top : -slide_height}, 300);
		});
		$(this).addClass('current');
		$('.current .fleche').animate({width: 240 }, 300);
		//if(started == null) started = false;   //condition pour le slide du premier
		//else if(!started) { window.setTimeout('roll()', 5000); started = true;}    //condition pour le slide du premier
		if(!started) { window.setTimeout('roll()', 5000); started = true;} 
		return false;
	});
	$('.Billboard').bind('mouseleave', function(){
		$('#Billboard_Rayons').clearQueue();
		$('#Billboard_Rayons').animate({top : -slide_title_height}, 700);
		$(this).removeClass("over");
	});	
	$('.Billboard').bind('mouseenter', function(){
		$('#Billboard_Rayons').clearQueue();
		$('#Billboard_Rayons').animate({top : -slide_height}, 300);
		$(this).addClass("over"); 
	});	
	$('#Billboard_menu div:first').click();

}

/* ***************************************************** */
/*               AFFICHAGE GAMMES                   */
/* ***************************************************** */

function jQuery_gamme(){
	$('#img_gamme').html('<img src="img/gammes/gamme_'+$('.link_img:first').attr('thumb')+'.jpg" border="0">').fadeIn(1200);
	$('#link_gamme').html('Gamme '+$('.link_img:first').text()).fadeIn(1200);
	$('#img_gamme').css({'float':'right'});
	$('.link_img').mouseenter(function(){
		var img = $(this).attr('thumb');
		var texte = $(this).text();
		$('#link_gamme').hide();
		$('#link_gamme').html('');
		$('#img_gamme, #link_gamme').clearQueue();
		$('#img_gamme').css({'width':'0px'});
		$('#img_gamme').html('<img src="img/gammes/gamme_'+img+'.jpg" border="0">');
		$('#img_gamme').animate({width:'631px'},300,function(){
			$('#link_gamme').html('Gamme '+texte);
			$('#link_gamme').fadeTo(500,1);
		});
	});
}

/* ***************************************************** */
/*                      SLIDE PANIER                        */
/* ***************************************************** */

function jQuery_panier(){
	var the_height = $('#tab_panier').height();
	$('#tab_panier').css('margin-top', -the_height-9 + 'px');

	$('#panier').bind('mouseenter', function(){
		$('#tab_panier').stop();
		$('#panier').stop();							
		$('#tab_panier').animate({'margin-top': -9 + 'px'},600,'easeOutBounce');
		$('#panier').animate({'height': the_height+29},600,'easeOutBounce');
		
	}).bind('mouseleave', function(){
		$('#tab_panier').stop();
		$('#panier').stop();							
		$('#tab_panier').animate({'margin-top': -the_height + 'px'},600,'easeOutBounce');
		$('#panier').animate({'height': 29},600,'easeOutBounce');
	});
}

/* ***************************************************** */
/*                       INFOBULLE                            */
/* ***************************************************** */

function jQuery_infobulle2(){
	$('*[info]').mouseover(function(){ $('body').append('<div class="infobulle"></div>'); $('.infobulle').html('<img id="bulle" src="ico/info_bulle.gif">'+$(this).attr('info')); });
	$('*[info]').mousemove(function(e){ $('.infobulle').css({left:e.pageX,  top:e.pageY+25}); });
	$('*[info]').mouseleave(function(){$('.infobulle').remove();});
}

function jQuery_infobulle(){

	$('*[info]').mouseenter(function(){
	  $('body').append('<div class="infobulle"></div>')
	  $('.infobulle').css({opacity:0});
	  $('.infobulle').animate({opacity:0.99}, 700 );
	  $('.infobulle').html($(this).attr('info'));
	  var pos=$(this).offset();
	  var pos_left=((pos.left)-($('.infobulle').width()/2))+($(this).width()/2)-10
	  $('.infobulle').css({left:pos_left,  top:pos.top+$(this).height()});
	});
	$('*[info]').mouseleave(function(){
	  $('.infobulle').remove();
	});

	$('*[infoM]').mouseenter(function(){
	  $('body').append('<div class="infobulle_maximiles"></div>')
	  $('.infobulle_maximiles').css({opacity:0});
	  $('.infobulle_maximiles').animate({opacity:0.99}, 700 );
	  $('.infobulle_maximiles').html($(this).attr('infoM'));
	  var pos=$(this).offset();
	  var pos_left=((pos.left)-($('.infobulle_maximiles').width()/2))+($(this).width()/2)-10
	  $('.infobulle_maximiles').css({left:pos_left,  top:pos.top+$(this).height()});
	});
	$('*[infoM]').mouseleave(function(){
	  $('.infobulle_maximiles').remove();
	});
}

/* ***************************************************** */
/*                          FOOTER                             */
/* ***************************************************** */

function jQuery_footer(){
		$('#bg_footer').css({
			'height':$('#footer').height() + 'px',
			'margin-top':-$('#footer').height() + 'px'
		});
}

/* ***************************************************** */
/*                    RECHERCHE                          */
/* ***************************************************** */

function jQuery_autoComplete(){
	$('#ac_field').autocomplete(  
		'script_recherche.php',  
		{  
				'minChars': 1,
				'max': 10,
				'cacheLength':10,
				'matchSubset': false,
				'selectFirst': false
		}
		).result(
		function(e, i){  
			if(i != undefined){  
				$('#ac_value').val(i[1]);  
			}else{  
				$('#ac_value').val('');  
			}  
		}
		).blur(  
		function(){  
			$('#ac_value').val();  
			$(this).search();  
		}  
	);

}
