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

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

function jQuery_domready(){
						jQuery_billboard();
						// jQuery_footer();
						jQuery_infobulle();
						// jQuery_autoComplete(); 
						// jQuery_fancybox("");
}

/* ***************************************************** */
/*             box des images produits              	  */
/* ***************************************************** */
function jQuery_fancybox(txt){

		if(txt!=''){
			$.fancybox(txt,{
				'overlayOpacity'			: 0.9,
				'overlayColor'				: '#202521',
				'centerOnScroll'			: true,
				'enableEscapeButton'	: true,
				'transitionIn'				: 'elastic',  //elastic
				'transitionOut'			: 'elastic',  //elastic
				'padding'					: 2,
				'autoScale'				: false,
				'autoDimensions'		: false,
				'width'         				: 490,
				'height'        				: 490
			});
		}
	
	$('*[rel=lyterayon]').fancybox({
		'autoScale'			: true,
		'overlayOpacity'		: 0.6,
		'overlayColor'			: '#DEE7E8',
		'showNavArrows'	: false,
		'enableEscapeButton': true,
		'padding'				: 20,
		'transitionIn'			: 'elastic',  //elastic
		'transitionOut'		: 'fade',  //elastic
		'easingIn'				: 'swing',
		'hideOnContentClick': true, //ferme lorsque l'on clique dessus
		'titleShow'				: true,
		'titlePosition' 			: 'inside'
	});
	
	$('*[rel=lytebox]').fancybox({
		//'width'					: '75%',
		//'height'				: '75%',
		'autoScale'			: true,
		//'overlayShow'		: false,
		'overlayOpacity'		: 0.6,
		'overlayColor'			: '#DEE7E8',
		'showNavArrows'	: true,
		'enableEscapeButton': true,
		'padding'				: 20,
		'transitionIn'			: 'elastic',  //elastic
		'transitionOut'		: 'fade',  //elastic
		'easingIn'				: 'swing',
		//'easingOut'			: 'swing',
		'hideOnContentClick': true, //ferme lorsque l'on clique dessus
		'titleShow'				: true,
		'titleFormat'			: formatTitle,
		'titlePosition' 			: 'inside'
	});
	function formatTitle(title, currentArray, currentIndex, currentOpts) {
		return '<p style="font:12px trebuchet MS;">'+title+'</p><H2 style="margin:0;text-align:right;">' + 'Image ' + (currentIndex + 1) + ' sur ' + currentArray.length + '</H2>';
	}

	$('*[rel=lyteinline]').fancybox({
		'autoScale'			: true,
		'overlayOpacity'		: 0.6,
		'overlayColor'			: '#DEE7E8',
		'showNavArrows'	: false,
		'enableEscapeButton': true,
		'padding'				: 20,
		'transitionIn'			: 'elastic',  //elastic
		'transitionOut'		: 'fade',  //elastic
		'easingIn'				: 'swing',
		'titleShow'				: false,
		'hideOnContentClick': true //ferme lorsque l'on clique dessus
	}); 
	
	$('*[rel=lyteframe]').fancybox({
		'type'						: 'iframe',
		'width'					: 711,
		'height'					: 900,
		'autoDimensions'	: false,
		'autoScale'			: false,
		'titlePosition'			: 'outside',
		'overlayShow'			: true,
		'overlayOpacity'		: 0.8,
		'overlayColor'			: '#000000',
		'showNavArrows'	: false,
		'enableEscapeButton': false,
		'padding'				: 20,
		'transitionIn'			: 'elastic',  //elastic
		'transitionOut'		: 'elastic',  //elastic
		'easingIn'				: 'swing',
		'easingOut'			: 'swing',
		'onComplete'		: function() {
										$('#fancybox-inner IFRAME').attr('scrolling','no');
										$('#fancybox-inner').height($('BODY').height()-20);
										$('#fancybox-wrap').height($('BODY').height());
										$('#fancybox-title').css({'top':'-37px','height':'37px'});
								  }
		// 'hideOnContentClick': true, //ferme lorsque l'on clique dessus
		// 'titleShow'				: false
		//'titlePosition' 		: 'over'
	});

	$('*[rel=lyteflash]').fancybox({
		'type'						: 'swf',
		'autoScale'			: false,
		'width'					: 500,
		'height'					: 500,
		'overlayShow'			: true,
		'overlayOpacity'		: 0.6,
		'overlayColor'			: '#DEE7E8',
		'showNavArrows'	: true,
		'enableEscapeButton': false,
		'padding'				: 20,
		'transitionIn'			: 'elastic',  //elastic
		'transitionOut'		: 'elastic',  //elastic
		'easingIn'				: 'swing',
		'easingOut'			: 'swing',
		'titleShow'				: true,
		'titlePosition' 			: 'inside',
		'swf'						: {
			   	'wmode'		: 'transparent',
				'allowfullscreen'	: 'false'
			}
	});
	
}


/* ***************************************************** */
/*                  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();

}


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

function jQuery_infobulle(){

//	$('*[info]').InfoBulle({attrName:'info'});
	$('*[info]').InfoBulle({attrName:'info',position:'right top'});
	$('*[infoM]').InfoBulle({attrName:'infoM'});
	
	/* $('*[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();
	}); */
}
(function($) {
	$.fn.InfoBulle = function(options) {
		var params = jQuery.extend({
			objName:		'infobulle',
			folderImg:		'http://www.ma-papeterie.com/img/',
			attrName:		'title',
			theme :			'black',
			position: 		'center bottom',
			effect: 			'fixed',
			cursor:			'fixed',
			opacity: 		1,
			delay: 			200,
			fadeIn: 			500,
			fadeOut: 		100
		}, options);
		
		this.live('mouseenter',function(){
			obj=$(this);
			$('body').append('<div class="'+params.objName+' '+params.theme+'"><span></span></div>');
			infobulle=$('.'+params.objName);
			infobulle_span=$('.'+params.objName+' span');
			infobulle_span.html(obj.attr(params.attrName));
			infobulle.clearQueue().delay(params.delay).css({opacity:0}).animate({opacity:params.opacity}, params.fadeIn);
			if(!obj.hasClass('right')&&!obj.hasClass('left')&&!obj.hasClass('center')&&!obj.hasClass('top')&&!obj.hasClass('bottom')){obj.addClass(params.position);}
			pos=obj.offset();
			info_width=infobulle.width();
			info_height=infobulle.height();
			this_width=obj.width();
			this_height=obj.height();
		});
		this.live('mousemove',function(e){
			obj=$(this);
			infobulle=$('.'+params.objName);
			var pos2=infobulle.offset();
			var cur_posX=e.pageX;
			var cur_posY=e.pageY;
			if(obj.hasClass('center')){var center=true;}else{var center=false;}
			if(obj.hasClass('left')){var left=true;}else{var left=false;}
			if(obj.hasClass('right')){var right=true;}else{var right=false;}
			if(obj.hasClass('top')){var top=true;}else{var top=false;}
			if(obj.hasClass('bottom')){var bottom=true;}else{var bottom=false;}
			if(((pos2.left+info_width)>$(window).width())&&((right)||(center))){ obj.removeClass('center right').addClass('left');}
			if((pos2.left<0)&&((left)||(center))){ obj.removeClass('center left').addClass('right');}
			if(((pos2.top+info_height)>$(window).height())&&(bottom)){ obj.removeClass('bottom').addClass('top');}
			if((pos2.top<0)&&(top)){ obj.removeClass('top').addClass('bottom');}						
			if(right){fl_pos='left';pos_left=(pos.left)+(this_width/2)-4;}
			if(left){ fl_pos='right';pos_left=(pos.left-(info_width))+(this_width/2)+4;}
			if(center){fl_pos='center';pos_left=(pos.left-(info_width)/2)+(this_width/2)+4;}
			if(bottom){fl_pos+=' top';pos_top=(pos.top+this_height)+4;}
			if(top){fl_pos+=' bottom';pos_top=((pos.top)-info_height)-4;}
			if(params.cursor=='move'){var pos_fleche=((pos.left-9-pos2.left)+(cur_posX-pos.left));if(pos_fleche<0){pos_fleche=0;}if(pos_fleche>info_width-19){pos_fleche=info_width-19;}if(top){fl_pos=pos_fleche+'px bottom';}if(bottom){fl_pos=pos_fleche+'px top';}}
			if(params.effect=='move'){if(center){pos_left=(cur_posX-info_width/2);}if(left){pos_left=(cur_posX-info_width)+4.5;}if(right){pos_left=cur_posX-4.5;}}
			if(params.effect=='follow'){if(top){pos_top=cur_posY-35;pos_left=cur_posX-info_width/2;}if(bottom){pos_top=cur_posY+this_height+4.5;pos_left=cur_posX-4.5;fl_pos='top left';}}
			if(bottom){fl_padding='4px 0 0';img=params.folderImg+'fleche_haut_'+params.theme+'.png';}
			if(top){fl_padding='0 0 4px';img=params.folderImg+'fleche_bas_'+params.theme+'.gif';}
			infobulle.css({ 'padding':fl_padding, 'background-position': fl_pos, 'background-image': 'url('+img+')', left:pos_left+'px', top:pos_top+'px'});
		});
		this.live('mouseleave',function(){
			obj=$(this);
			infobulle=$('.'+params.objName);
			infobulle.clearQueue().animate({opacity:0}, params.fadeOut, function(){
				$(this).remove();
			});
		});
	};
})(jQuery);


/* ***************************************************** */
/*                    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();  
		// }  
	// );

// }

