// Let's start off by adding a CSS hook
(function() {if(document.getElementsByTagName){var h = document.getElementsByTagName('html')[0]; h.className=h.className + (h.className.length>0? " ": "")+'scripted';}})();

/*@cc_on // IE version detection
var ie_rv = (function(){
	var undef, v = 3, div = document.createElement('div'), all = div.getElementsByTagName('i');
	while (div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->', all[0]);
	return v > 4 ? v : undef;
}());
@*/

/*
 * Over menus for keyboard-only users
 */
function navigation(triggers) {  
	var liens = triggers.find('a.overlaycall');
	
	if (liens.length) {
		liens.each(function(i, item){
		    	$(item).bind('focus', 
				function() { 
					liens.next().css('left', '-999em');
					$(this).next().css('left', '0px');
				});
				/*	$(item).bind('focusout', function() { 	$(this).next().css('left', '-999em');	});*/					 
		});
		$('#nav2 a#direct-link').bind('focus', 	function() { 	liens.next().css('left', '-999em');	});
		$('#header #customer-access a').bind('focus', 	function() { 	liens.next().css('left', '-999em');	});

	}
	
	return false;
};  



function slider(container,  player_class, visible, linktitle, infinite, autoscroll) {  
//function slider(container, ul, items, player_class, visible, linktitle, infinite, autoscroll) {  
//container = "#news"  ul = "ul#news_list"   items = 'li.breve'
// si le modele n'est pas en div ul li, rétablir les deux parametres ci-dessus, et les trois lignes numerotées plus bas
/*
 *	WRAPPING ET PLACEMENT 
 */
   	var speed = 600;
	var interval = 3000;
	if(/speed/.test($(container).attr("class") )){
		var reg = new RegExp("speed-([0-9]*)", "gi");
		var s = $(container).attr("class").match(reg)[0];
		s = s.split('speed-')[1];
		speed = parseInt(s,10);
	}
	//console.log('vitesse d\'animation : '+ speed+ ' millisecondes' );
	if(/interval/.test($(container).attr("class") )){
		reg = new RegExp("interval-([0-9]*)", "gi");
		s = $(container).attr("class").match(reg)[0];
		s = s.split('interval-')[1];
		interval = parseInt(s,10);
	}
	//console.log('affichage toutes les '+ interval + ' millisecondes' );

	//  $(container +' > ul'), ou $(ul) si le modele n'est pas en ul/li
	var  $slider = $(container +' > ul'); 
//	var  $slider = $(ul); 			// 1/3
	$slider.wrap('<div class="news_wrapper" />');
	var $wrapper = $(container+ " .news_wrapper");

	var $items = $slider.find(' > li')
//	var $items = $slider.find(items);  	// 2/3
	var count_depart = $items.length ; 
	var $h = 0; 
	$items.each(function(){  	if ($(this).height() > $h ) $h = $(this).height() ;	}); // hauteur max
	var larg = $items.outerWidth(true) ;
	$slider.css("height", $h).css("width", $items.length * larg).css('position', 'relative');
	$items.css("height", $h).removeClass('hidden')
	
	//$items.css('position', 'relative').css('float', 'left'); // ou ce qui suit :
	$items.css('float', 'none').css('position', 'absolute').css('top', 0) ; 
	
	if (infinite || autoscroll) {
		
		var $premiers = $items.filter(':lt('+( visible) +')') ;
		var $derniers = $items.filter(':gt('+( count_depart-1-visible) +')') ;
		$premiers.clone().appendTo($slider);
		$derniers.clone().prependTo($slider);
		
		$items = $slider.find(' > li')
		$items.each(function(i) {            $(this).css('left', ((i- visible)* larg)+'px');     });
	} else {
		$items.each(function(i) {            $(this).css('left', (i* larg)+'px');     });
	}

 	//var $single = $items.filter(':first'); 
	/*
	 * CREATION DU PLAYER 
	 */
	  var $news = $(container).css('overflow', 'hidden').append
		(		
			$('<div />')
			 .attr('class', player_class)
			 .css('float', 'right').css('overflow', 'hidden')
			.append
			(
				$('<a />')
				 .attr('href', '#').attr('title', linktitle)				 
				 .attr('class', 'left').text('<<')
			,
				$('<a />')
				 .attr('href', '#').attr('title', linktitle)
				 .attr('class', 'right').text('>>')				 
			)
		);

	 var $leplayer = $news.find('.player')
	$leplayer.find('a').css('display', 'block').css('float', 'left')

	var cpt =0, LIMIT = count_depart - visible ; // includes margin

var playing =false;
	$leplayer.find('a.left').bind('click', function() {
       		if (!playing) { playing =true;
       			if (cpt == -visible) {
        				$items.each(function(i) {    $(this).css('left', ((i-count_depart )* larg)+'px');     });
				cpt += count_depart
       			}		
			cpt--; 
			$items.animate({left: "+=" + larg }, speed, function() { playing=false;});
			
			
		}
		return false;
	});
	$leplayer.find('a.right').bind('click',    function() { 
       		if (!playing) { playing =true;     			
			if (cpt == count_depart) {				
 				$items.each(function(i) {   $(this).css('left', ((i- visible)* larg)+'px');     });
				cpt -= count_depart
				
			}					
			cpt++;
			$items.animate({left: "-=" + larg  }, speed, function() { playing=false;});
		 }
			
		return false;
	});
	
	if(autoscroll) {
	    var autoscrolling = true;
	    $news.mouseover(function () {        autoscrolling = false;    })
	    	 .mouseout(function () {       autoscrolling = true;    });
	    
	    setInterval(function () {
	        if (autoscrolling && !playing) { playing =true;     
	
	  			if (cpt == count_depart) {				
					$items.each(function(i) {            $(this).css('left', ((i- visible)* larg)+'px');     });
					cpt -= count_depart
				}					
				cpt++;
				$items.animate({left: "-=" + larg  }, speed, function() { playing=false;});
	        }
	    }, interval);
	}
}  

/*
 * remet les colonnes à la meme hauteur (pour marches, notamment)
 */
jQuery.equalHeight = jQuery.fn.equalHeight  = function(repeat) {  
	
	var h =0; 
	if (repeat != undefined) { // meme hauteur tous les (repeat) elements
		var i=0; var everybody = $(this); var j = 0; var ligne=0;
		for(i=0;i<=$(this).length;i++){
			if (i>0 && (i%repeat == 0)) { 
				j=0; ligne++;
				for (j=(ligne-1) * repeat ; j < ligne *repeat ; j++) {
					$(everybody[j]).css("height", h); 
				}
				h=0;
			}
			haut = Math.abs($(everybody[i]).height()); 
			if (haut > h ) 	h = haut ; 
			
		}
	} else { // tout le monde a la meme hauteur
		$(this).each(function(){
			haut = Math.abs($(this).height()); 
			if (haut > h ) 	h = haut ; 
		});
		$(this).css("height", h);	
	}
	return false;
};  

function share(triggers) {
	var $lienshare = triggers.find('a.share-trigger');
	var $sharetarget = triggers.find('div.share-on'); 
	$lienshare.bind('click', function() { $(this).toggleClass('share-on'); $sharetarget.toggleClass('hidden'); return false });

};




/* Plugins
----- */
(function($) {

	/* Specialized links
	----- */
	$.fn.setLink = function(options, callback) {
		
		if ($.isFunction(options)) {
			callback = options;
			options = null;
		}
		options = $.extend($.fn.setLink.defaults,options);
		
		return this.each(function() {
			if($(this).attr('rel') == 'bookmark' || $(this).attr('rel') == 'external')
				$(this).attr('target','_blank');
				
			$.isFunction( options.setup ) && options.setup.call(this);
		});
	}
	
	/* Expands
	----- */
	$.fn.setExpand = function(options, callback) {

		if ($.isFunction(options)) {
			callback = options;
			options = null;
		}
		options = $.extend($.fn.setExpand.defaults,options);

		return this.each(function() {

			var prt = $(this), caller = prt.find('h2'), tgt = prt.find('.expando');

			if(caller.length==1 && tgt.length==1) {
				caller.wrapInner('<span></span>');
				caller.bind('click',function(){
					if(tgt.not(':animated')) {
						if(prt.hasClass('expanded')) {
							tgt.slideUp(500,function(){
								prt.removeClass('expanded');
							});
						}
						else {
							tgt.slideDown(500,function(){
								prt.addClass('expanded');
							});
						}
					}
				});
			}

			$.isFunction( options.setup ) && options.setup.call(this);
		});

	}

	/* 
	----- */
	$.fn.setSub = function(options, callback) {

		if ($.isFunction(options)) {
			callback = options;
			options = null;
		}
		options = $.extend($.fn.setSub.defaults,options);

		return this.each(function() {
			var prt = $(this);

			prt.hover( function() {
				var tm=window.setTimeout(function() {
    					prt.addClass("wearehere")
				}, 400);
				prt.data('timehover',tm);
			} , function() {
				if ( prt.data('timehover') ) {
					window.clearTimeout(prt.data('timehover'));
      					prt.data('timehover',"")
				}

				$(this).removeClass("wearehere")
			});



			$.isFunction( options.setup ) && options.setup.call(this);
		});
	}
	
	
	
	/* Collapsable
	----- */
	$.fn.collapsable = function(options, callback) {

		if ($.isFunction(options)) {
			callback = options;
			options = null;
		}
		options = $.extend($.fn.collapsable.defaults,options);
		
		return this.each(function() {
			
			var prt = $(this), target = prt.find(options.tgt).eq(0), trigger = prt.find(options.trigger).eq(0), type = options.type;
			if(target.length>0) {
				prt.addClass('collapsable');
				trigger.append('<span class="state"></span>');
				trigger.bind('click',function(){
					target.slideToggle(300,function(){
						if(type=='reversed')
							prt.toggleClass('expanded');
						else
							prt.toggleClass('collapsed');
					});
				});
			}
			


			$.isFunction( options.setup ) && options.setup.call(this);
		});
	}
	$.fn.collapsable.defaults = {
		trigger: '.trigger',
		tgt: '.target',
		type: 'standard'
	};
	
	
	
	
	/* Set news
	----- */
	$.fn.setCarroussel = function(options, callback) {

		if ($.isFunction(options)) {
			callback = options;
			options = null;
		}
		options = $.extend($.fn.setCarroussel.defaults,options);
		
		return this.each(function() {
			
			var prt = $(this), itms = prt.find('> div'), max = itms.length;
			
			var w = itms.eq(0).outerWidth(true);
			
			$('<div class="dataBlock" />').insertBefore(prt);
			var d = prt.closest('.carroussel').find('.dataBlock'), p = prt.closest('.carroussel').find('.placeholder');
			d.html(p.html());
			itms.hover(function(){
				d.html($(this).find('.data').html());
			},function(){
				d.html(p.html());
			});
			
			if(max<5) return;

			$('<button type="button" class="prev">&laquo;</button><button type="button" class="next">&raquo;</button>').insertAfter(prt);
			
			prt.closest('.carroussel').find('> button').bind('click',function(){
				shiftItems($(this).attr('class'));
			});
			
			function shiftItems(dir) {
				if(prt.data('status')=='shifting') return;
				prt.data('status','shifting');
				if(dir=='prev') {
					prt.find('>div').eq(0).animate({
						'margin-left': -w
					},400,function(){
						$(this).appendTo(prt).css({'margin-left':0});
						prt.data('status','iddle');
					});
				}
				else {
					prt.find('>div').eq(max-1).css({'margin-left':-w}).prependTo(prt).animate({
						'margin-left': 0
					},400,function(){
						prt.data('status','iddle');
					});
				}
			}
			
			$.isFunction( options.setup ) && options.setup.call(this);
		});
	}
	$.fn.setCarroussel.defaults = {

	};
	
	
	
	

})(jQuery);

function fixIE(){
	$('#nav2 .contact').hover(function(){
		$('#nav2 .share').css({'z-index':-1});
	},function(){
		$('#nav2 .share').css({'z-index':1});
	});
	
	$('#header .contact').hover(function(){
		$(this).css({'z-index':1});
	},function(){
		$(this).css({'z-index':0});
	});
}

$(document).ready(function(){
	// common
	debug = false;	

	if ($('#nav2').length) {
		share($('#nav2 li'));
		$('#nav2 .marches .cols li.col').equalHeight();
		$('#nav2 .collaborative .carres .l1 li.col').equalHeight();
		$('#nav2 .collaborative .carres .l2 li.col').equalHeight();
//		$('#nav2 .challenges ul.flashy li').columns(); // pb titre
		
		$('#nav2 > ul > li').setSub();
		
		// call this AFTER all the resizings
		navigation( $('#nav2 li')); // for keyboard-only users

	}
	
		if ($('#nav33').length) {
			share($('#nav33 li'));
			$('#nav33 .marches .cols li.col').equalHeight();
			$('#nav33 .collaborative .carres .l1 li.col').equalHeight();
			$('#nav33 .collaborative .carres .l2 li.col').equalHeight();
	//		$('#nav2 .challenges ul.flashy li').columns(); // pb titre

			$('#nav33 > ul > li').setSub();

			// call this AFTER all the resizings
			navigation( $('#nav33 li')); // for keyboard-only users

		}
	
	$('a[rel=external], area[rel=external], a[rel=bookmark]').setLink();
	
	// home
//	if ($(".home #news").length)     slider("#news", "ul#news_list", "li.breve", 'player', 4, 'navigate news', true, true);
	if ($(".home #news").length)     slider("#news",  'player', 4, 'navigate news', true, false);
	// challenges
//	if ($(".page-challenges #main").length)     slider("#main", "ul#slides_list", "li.slide", 'player', 1,  'navigate challenges', true, true);
	if ($(".page-challenges #main").length)     slider("#main",  'player', 1,  'navigate challenges', true, true);


	$('.expandable').setExpand();
	
	/* News
	------- */
	$('.webtv .carroussel .slider').setCarroussel();
	
	/* Collapse
	------- */
	$('.testimony').collapsable();
	$('#ourSolutions').collapsable({'trigger':'strong','tgt':'ul'});
	$('.references li').collapsable({'trigger':'h4','tgt':'.description','type':'reversed'});
	
	/*@ if(ie_rv<8) fixIE(); @*/
	
	
	/*stats */
	$("a").each( function() {
		var host=this.hostname, path=this.pathname;

		if ( (host)&&(host!=location.host) ) {
			$(this).bind('click', function() {
				if ( "undefined"!==typeof(_gaq) ) {
					_gaq.push(['_trackEvent', 'External sites', host]);
				}
			});
		} else {
			if (path.match(/\.(doc|pdf|xls|ppt|zip)$/)) {
				var file_details = path.split('/');
				var file_path =  file_details[(file_details.length-1)];
				file_path =  "/virtual/download/" + file_path;
			
				$(this).bind('click', function() {
					if ( "undefined"!==typeof(_gaq) ) {
						_gaq.push(['_trackPageview', file_path]);
					}
				});
			}
		}
	});

});

// Generix Group
var generix_group = generix_group || {};
(function(o){
	o.video={
		state : function(obj) {
			var s=(obj.newstate) ? obj.newstate.toLowerCase():null;
			s && s!="state" && generix_group.video[s] && generix_group.video[s](obj);
		},
		playing : function (obj) {
			if ( "undefined"!==typeof(_gaq) ) {
				var p=jwplayer(obj.id), f=p.config.file;
				f=f.replace(/^.*\//,'');
				_gaq.push(['_trackEvent', 'Videos', 'Play', f]);
			}
		},
		paused : function (obj) {
			
		}
	};
}(generix_group));

function playerReady(obj) {
	var p=document.getElementById(obj.id);
	p.addModelListener("STATE", "generix_group.video.state");
} 

function fnOpenWindow(url,parameters)
{
	parameters=parameters.replace(/\s+/g,'');

	var aParameters=parameters.split(',');
	var data=new Array();
	for (var i=0;i<aParameters.length;i++) {
		var pr=aParameters[i];
		var key = pr.substr(0,pr.indexOf('='));
		var value=pr.substr(pr.indexOf('=')+1);
		data[key]=value;
	}

	if ( (data['hposition'])&&(data['width'])&&(data['hposition']!='system') )
	{
		var posX=null;
		if (data['hposition']=='left')
		{
			posX=0;
		}
		if ( (window.screen)&&(window.screen.availWidth) )
		{
			if (data['hposition']=='center')
				posX=(window.screen.availWidth-data['width'])/2;
			if (data['hposition']=='right')
				posX=window.screen.availWidth-data['width'];
		}

		if (posX!=null)
			parameters+=",left="+parseInt(posX);
	}

	if ( (data['vposition'])&&(data['height'])&&(data['vposition']!='system') )
	{
		var posY=null;
		if (data['vposition']=='top')
		{
			posY=0;
		}
		if ( (window.screen)&&(window.screen.availHeight) )
		{
			if (data['vposition']=='center')
				posY=(window.screen.availHeight-data['height'])/2;
			if (data['vposition']=='bottom')
				posY=window.screen.availHeight-data['height'];
		}

		if (posY!=null)
			parameters+=",top="+parseInt(posY);
	}


	var oWnd=window.open(url,'',parameters);
	oWnd.focus();
}
