/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \
|		
|		Copyright (c) 2009
|		Design + HTML/CSS/DOM JavaScript : Smart Agence
|		http://www.smartagence.com/
|		
\ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

/* ______________________[ 03 | equalizeCols ]________________________ */
(function($) {
	/**
	 * equalizes the heights of all elements in a jQuery collection
	 * thanks to John Resig for optimizing this!
	 * usage: $("#col1, #col2, #col3").equalizeCols();
	 */
	 
	$.fn.equalizeCols = function(){
		var height = 0,
			reset = $.browser.msie ? "1%" : "auto";
  
		return this
			.css("height", reset)
			.each(function() {
				height = Math.max(height, this.offsetHeight);
			})
			.css("height", height)
			.each(function() {
				var h = this.offsetHeight;
				if (h > height) {
					$(this).css("height", height - (h - height));
				};
			});
			
	};
	
})(jQuery);

var mycarousel_itemList = [];
function mycarousel_itemVisibleInCallback(carousel, item, i, state, evt)
{
    // The index() method calculates the index from a
    // given index who is out of the actual item range.
    var idx = carousel.index(i, mycarousel_itemList.length);
    carousel.add(i, mycarousel_getItemHTML(mycarousel_itemList[idx - 1]));
};

function mycarousel_itemVisibleOutCallback(carousel, item, i, state, evt)
{
    carousel.remove(i);
};

/**
 * Item html creation helper.
 */
function mycarousel_getItemHTML(item)
{
    return '<a href="' + item.href + '" class="' + item.style + '">' + item.title + '</a><img src="' + item.url + '" width="113" height="57" alt="' + item.title + '" />';
};



function mycarousel_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });
    carousel.buttonNext.bind('mouseover', function() {
        carousel.stopAuto();
    });
    carousel.buttonNext.bind('mouseout', function() {
        carousel.startAuto(2);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });
    carousel.buttonPrev.bind('mouseover', function() {
        carousel.stopAuto();
    });
    carousel.buttonPrev.bind('mouseout', function() {
        carousel.startAuto(2);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};


jQuery(document).ready(function($jQ) { 
	if($jQ("#carousel").length != 0){
		var lg = $jQ('ul#carousel li img').length ;
		for(var i = 0; i < lg; i += 1) {
			var url = $jQ('ul#carousel li img').eq(i).attr('src');
			var title = $jQ('ul#carousel li a').eq(i).text();
			var style = $jQ('ul#carousel li a').eq(i).attr('class');
			var href = $jQ('ul#carousel li a').eq(i).attr('href');
			if(/'/.test(title)) {
				title =title.replace("'","&rsquo;");
			}
			var str = "{url : '" + url + "', title : '" + title+ "',style : '" +style+ "',href : '" +href+ "'}";
			mycarousel_itemList[i] = eval('(' + str + ')');
		}
		$jQ('ul#carousel').empty();		
		$jQ('#carousel').jcarousel({
			scroll:3,
			auto: 5,
			wrap: 'circular',
			initCallback: mycarousel_initCallback,
			itemVisibleInCallback: {onBeforeAnimation: mycarousel_itemVisibleInCallback},
			itemVisibleOutCallback: {onAfterAnimation: mycarousel_itemVisibleOutCallback}
		});
	}
	
	/*_______________  references  ________________ */
	if( $jQ("div.more").length > 0 ) {
		$jQ("div.more .liens a").click(function(){
			var a = $jQ(this);
			var parent = a.parents("div.more:first");
			parent.toggleClass("on");
			if( parent.hasClass("on") ) {
				parent.find(".innMore").slideDown("slow");			
			} else {
				parent.find(".innMore").slideUp("slow");	
			}
		});	
	}
	
	/*______________page type______________*/
	
	if( $jQ("#system").length > 0 ) {
        $jQ("#system .carteSchema area").each( function () {   
            //var name = $jQ(this).attr('alt');
			var name =  $jQ(this).attr('rel'); 
            var area = $jQ(this);
            var id = "#"+name;
			if($jQ(id).length > 0) {
            	area.bind("mouseover",areaOver);
            	$jQ("img.imgSchema").mouseout(function(){$jQ("div.ftr").css("display","none");}); 
			}
			function areaOver(event){
                $jQ("div.ftr").css("display","none"); 
				if( $jQ(id).length > 0) {
					$jQ(id).fadeIn()
					.mouseover(function(){
						$jQ(this).css("display","block");   
					}).mouseout(function(){$jQ(this).css("display","none");});  
				}
            }
        });
    }
	
	
	/*______________Careers_____________*/
	
	if( $jQ(".menuJobs").length > 0 ) {			
		$jQ(".blocJobs").eq(0).css("display","block");
		$jQ(".menuJobs li").each(function(i){
			var li = $jQ(this);
			var a = li.find("a");
			a.click(function(){
				if( li.hasClass("on") ) {
					return;
				}else {
					$jQ(".menuJobs li.on").removeClass("on");
					li.addClass("on");	
					$jQ(".blocJobs").css("display","none");
					$jQ(".blocJobs").eq(i).fadeIn(1000);
				}
			});			
		});	
	}
	
	
	/*_______________FR en bref _____________*/
	
	if( $jQ(".clients").length > 0 ) {
		$jQ(".clients:first").find(".clientsDesc").css("display","block");
		$jQ(".clientsImg a").click(function(){
			var clients = $jQ(this).parents(".clients:first");		
			if( clients.find(".clientsDesc").is(":visible") ) {
				return;
			}
			$jQ(".clientsDesc").slideUp("slow");
			clients.find(".clientsDesc").slideDown("slow");				
		});
	}
	
	/*______________entreprise par marche___________________*/
	
	function updateHeightBloc(pays,d,f) {
			var h_max = 103;
			var pays = pays
			var bloc = pays.find(".bloc");
			bloc.slice(d,f).each(function(){
				if( $jQ(this).length > 0) {
					var h = parseInt( $jQ(this).height() );
					if( h > h_max ) {
						h_max = h ;
					}
				}
			});	
			bloc.slice(d,f).css("height",h_max);		
	}
	
	if( $jQ(".pays").length > 0 ) {		
		$jQ(".pays").each(function(){
			var pays = $jQ(this);
			var bloc = pays.find(".bloc");
			var ul = pays.find("ul");
			var nb = bloc.length;
			var mod = 3;
			if( !$jQ('.boxA').length) {
				mod = 4 ;
			}
			for(var i = 1; i <= nb;i += 1) {
				if( i%mod == 0) {
					bloc.eq(i-1).css("marginRight","0");
					//updateHeightBloc(pays,i-mod,i-1);
				}
			}		
			if( nb < mod ) {
				bloc.equalizeCols();
			}else {
				var tt = 1;
				do{				
					bloc.slice( (tt-1) * mod,tt * mod ).equalizeCols();
					nb = nb - mod;
					tt += 1;					
				}while( nb > mod ) ;
				//bloc.equalizeCols();
				bloc.slice( tt * mod ).equalizeCols();
			}
			
		});		
	}

	/*______________carte la France &amp; Europe_______________*/	
	if( $jQ("#carteRegion,#carteEurope").length > 0 ) {
		$jQ("#carteRegion,#carteEurope").find("area").each( function () {
			var map = $jQ(this).parents("map:first");
			var alt = $jQ(this).attr('alt');
			var name = $jQ(this).attr('id');
			var carte = $jQ(this);
			if( $jQ("img#imgCarte").length > 0) {
				var img = $jQ("img#imgCarte");
				var src = img.attr("src");
				var path = src.substring(0,src.lastIndexOf('/')+1);
				var ftype = src.substring(src.lastIndexOf('.'), src.length);			
				$jQ(this).mouseover( function(event){
					event.stopPropagation();
					event.preventDefault(); 
					var newsrc = path + name + ftype;				
					var dx,dy;
					img.attr("src",newsrc);
					 if( !($jQ.browser.msie) ){	
						startMV(event);
					 }				
				}).mouseout(function(event){ 
						endMV(event);
						img.attr("src",src);
				});	
			}
			var d1,d2;
			function startMV(event) {
				$jQ(this).bind("mousemove",move);
			}
			function endMV(event) {
				$jQ(this).unbind("mousemove",move);
				$jQ("div.popup").css("display","none");
			}
			function move(event) {
				event.stopPropagation();
				event.preventDefault(); 
				var pos = getCoords(event);
				$jQ("div.popup").css({top : pos.y, left : pos.x});
				$jQ("div.popup").html(alt).fadeIn(); 				
			}
			
			function getCoords(e) {
				var pos = {'x' : 0, 'y' : 0};
				if (!e) var e = window.event;	

				if ($jQ.browser.safari) {
					pos.x =e.clientX - 450;
					pos.y =e.clientY - 200;	
					if( map.attr("id") == "carteEurope") {
						pos.y =e.clientY - 100;
					}
				}else {
					pos.x = e.pageX - carte.offset().left;
					pos.y = e.pageY - carte.offset().top + 40;
				}

				return pos;
			}
		});
	}
	
	
	/*______________carte par region_______________*/	
	if( $jQ("#cartDept").length > 0 ) {
        $jQ("#cartDept").find("area").each( function () {   
            var name = $jQ(this).attr('alt');
            var area = $jQ(this);
            var id = "#"+name,dx,dy;
            area.bind("mouseover",areaOver);           
            function areaOver(event){
                $jQ("div.popup").css("display","none"); 
				if( $jQ(id).length > 0) {
					$jQ(id).fadeIn()
					.mouseover(function(){
						$jQ(this).css("display","block");   
					}).mouseout(function(){$jQ(this).css("display","none");});  
				}
            }
            $jQ("img#imgDept").mouseout(function(){$jQ("div.popup").css("display","none");});           
        });
    }
	/*_________cas sans image pour left,mettre le margin-left = 0 afin que la fleche apparait dans h3_______*/
	if( $jQ(".encart02 .inner2 .left").length > 0 ) {
		$jQ(".encart02 .inner2 .left .blocVisu").each(function(){
			var bv = $jQ(this);
			var bt = bv.parents(".left").find(".blocTxt");
			var lg = bv.find("img").length;
			if( lg == 0 ) {
				bt.css("marginLeft","0");
			}
		});
	}
	
	/*_______________traitement la hauteur pour Onglets______________*/
	if( $jQ(".subListe").length > 0 ) {
		$jQ(".subListe").each(function(){
			$jQ(this).find("ul li:last").addClass("lastChild");
			var a = $jQ(this).find("ul li a");
			var nb = a.length;
			var h = 34;
			var i = nb;
			a.each( function(n){
				if( $jQ(this).height() > h ) {
					h = $jQ(this).height();
				}
			});
			a.css("height",h+'px');			
		});
	}
	
	if( $jQ("form#contacts").length > 0 ) {
		$jQ("form#contacts fieldset").each(function(i){
			var fs = $jQ(this);
			var i = i+1;
			var nomClass = "ext_fieldset_" + i;
			$jQ(this).addClass(nomClass);
		});
	}
	
});
