/**
 * @compagny : e-magineurs
 * @website :www.e-magineurs.com
 * @author : Ian SEBBAGH
 * @contact : isebbagh@e-magineurs.com
 * @copyright : Tous droits réservés 
 */


jQuery(document).ready(function() {

	//fermeture du plan du site par defaut
	plan_site_on_off();
	
	jQuery('#nav_principal').hover(show_transp,hide_transp);
	
	flashinfoInit();
	
	if(jQuery('#flashinfoActive').hasClass("active")){
		flashinfoActive();
	}
	
	enableColorBox();

});


function plan_site_on_off(){

	var vitesse = 500;
	var hauteur = 490;
	var titleOn = 'Cacher le plan du site';
	var titleOff = 'Afficher le plan du site';


	if(jQuery('#plan_site_on_off').hasClass('on')){
		jQuery('#plan_site_on_off').removeClass('on');
		
		jQuery('#plan_site_couleur').animate(
			{height: 0},
			vitesse
			);
			
		jQuery('#plan_site_on_off').addClass('off');
		jQuery('#plan_site_on_off').attr('title', titleOff);
	}
	else{
		jQuery('#plan_site_on_off').removeClass('off');
		
		jQuery('#plan_site_couleur').animate(
			{height: hauteur},
			vitesse
			);
			
		jQuery('#plan_site_on_off').addClass('on');
		jQuery('#plan_site_on_off').attr('title', titleOn);
	}
};


function show_transp(){
	width = jQuery("#global").width();
	height = jQuery("#global").height();
			
	div = jQuery("<div>")
			.attr({'id':'transparent_bg',
				   'style':'width:'+width+'px;height:'+height+'px;'});
	jQuery("#top").prepend(div);
};

function hide_transp(){
	jQuery('#transparent_bg').remove();
};


function startScrolling(scroller_obj, velocity, start_from){
	//bind animation to the children inside the scroller element
	scroller_obj.children().bind('marquee', function(event,c) {
		//text to scroll
		var ob = jQuery(this);
		//scroller width
		var sw = parseInt(ob.parent().width());
		//text width
		var tw = parseInt(ob.width());
		//text left position relative to the offset parent
		var tl = parseInt(ob.position().left); 
		//velocity converted to calculate duration
		var v  = velocity>0 && velocity<=100 ? (100-velocity)*100 : 15000;
		//same velocity for different text's length in relation with duration
		var dr = (v*tw/sw)+v;
		//is it scrolling from right or left?
		switch(start_from){
		case 'right':
			//is it the first time?
			if(typeof c == 'undefined'){
				//if yes, start from the absolute right
				ob.css({ left: sw });
				sw = -tw;
			}else{
				//else calculate destination position
				sw = tl - (tw + sw);
			};
			break;
		default:
			if(typeof c == 'undefined'){
				//start from the absolute left
				ob.css({ left: -tw });
			}else{
				//else calculate destination position
				sw += tl + tw;
			};
		}
		//attach animation to scroller element and start it by a trigger
		ob.animate( {left:sw}, 
					{   duration:dr,
						easing:'linear',
						complete:function(){ob.trigger('marquee');},
						step:function(){   
							//check if scroller limits are reached
							if(start_from == 'right'){
								if(parseInt(ob.position().left) < -parseInt(ob.width())){
									//we need to stop and restart animation
									ob.stop();
									ob.trigger('marquee');
								};
							}else{
								if(parseInt(ob.position().left) > parseInt(ob.parent().width())){
									ob.stop();
									ob.trigger('marquee');
								};
							};
						}
					});
	}).trigger('marquee');
	//pause scrolling animation on mouse over
	scroller_obj.mouseover(function(){
		jQuery(this).children().stop();
	});
	//resume scrolling animation on mouse out
	scroller_obj.mouseout(function(){
		jQuery(this).children().trigger('marquee',['resume']);
	});
};

function flashinfoInit(){
	jQuery('#flashinfo_contenu .close').click(function() {
	  jQuery('#flashinfo').slideUp();
	});
	
	
	var scroller = jQuery('#flashinfo_contenu .texte');
	var scrolling_velocity  = 0; 
	var scrolling_from      = 'right';
	startScrolling( scroller, scrolling_velocity, scrolling_from );
};

function flashinfoActive(){
	jQuery('#flashinfo').animate({height: 60},500);
};

function enableColorBox(){

	jQuery.each(jQuery(".news-agenda-linkpoi"),function(i,item){
		var url = jQuery(this).attr('href');
		if(url.indexOf('?') == -1)
			jQuery(this).attr('href',url+'?type=1');
		else
			jQuery(this).attr('href',url+'&type=1');
	});
	
	
	jQuery(".news-agenda-linkpoi").colorbox({
		width:"800px", 
		height:"540px",
		opacity:"0.7", 
		iframe:true,
		scrolling:"false",
		close:""
	});

	jQuery.each(jQuery(".lienGoogleMap"),function(i,item){
		var url = jQuery(this).attr('href');
		if(url.indexOf('?') == -1)
			jQuery(this).attr('href',url+'?type=1');
		else
			jQuery(this).attr('href',url+'&type=1');
	});
	
	
	jQuery(".lienGoogleMap").colorbox({
		width:"800px", 
		height:"540px",
		opacity:"0.7", 
		iframe:true,
		scrolling:"false",
		close:""
	});
	
	jQuery(".popupLightBox").colorbox({
		width:"800px", 
		height:"540px",
		opacity:"0.7", 
		iframe:true,
		scrolling:"false",
		close:""
	});
};


