var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-7223764-2']);
_gaq.push(['_trackPageview']);


$(function() { 
    var ua = navigator.userAgent.toLowerCase(); 
    var myBrowsers = { 
        // Déjà détectés en natif par jQuery 
        mozilla: $.browser.mozilla, 
        safari:  $.browser.safari, 
        opera:   $.browser.opera, 
        ie:      $.browser.msie, 
        // Quelques améliorations et ajouts 
        ie6:     $.browser.msie && ($.browser.version < 7), 
        ie7:     $.browser.msie && ($.browser.version == 7), 
        ie8:     $.browser.msie && ($.browser.version > 7), 
        iphone:  /iphone/.test(ua), 
        chrome:  /chrome/.test(ua), 
        firefox: /firefox/.test(ua), 
        webkit:  /webkit/.test(ua), 
        // Détection de plateformes 
        osx:     /mac os x/.test(ua), 
        win:     /win/.test(ua), 
        linux:   /linux/.test(ua) 
    }; 
    $.each(myBrowsers, function(a, b) { 
        if (b)
		{
			$('html').addClass(a);
		}
    }); 
}); 



$(document).ready(function()
{
	//logo gif pour ie6
	$('.ie6 #bandeau h1 a img').attr('src', 'images/ie6/logo.gif');
	
	//menu principal
	$('#menu ul li.niveau_1').mouseenter(function ()
	{
		$(this).children('.sous-menu').show();
	});
	$('#menu ul li.niveau_1').mouseleave(function ()
	{
		$(this).children('.sous-menu').hide();
	});
	
	//menu surdimensionné
	$('#menu ul li.niveau_1.rub-CCI-ROA-FR-ACC').addClass('surdimensionne');
	
	//on cache 2 menu principaux
	//$('#menu ul li#menu-principal-3').hide();
	$('#menu ul li.rub-CCI-ROA-FR-PROFIL').hide();
	
	//accès direct
	$('#menu .acces_direct .champs select#profil').selectmenu({style:'dropdown'});
	$('#menu .acces_direct .champs select#besoins').selectmenu({style:'dropdown'});
	
	$('#menu .acces_direct .champs select#profil').change(function(){ location.href=$(this).val();});
	$('#menu .acces_direct .champs select#besoins').change(function(){ location.href=$(this).val();});
	
	$('#menu .acces_direct .recherche #MOTS_CLEFS').click(function ()
	{
		if ($(this).val() == "Rechercher...")
		{
			$(this).val("");
		}
	});
	$('#menu .acces_direct .recherche #MOTS_CLEFS').blur(function ()
	{
		if ($(this).val() == "")
		{
			$(this).val("Rechercher...");
		}
	});	
	
	
	
	//diaporama
	diaporama();
	
	
	
	//a la une : image
	$('.accueil img').each(function(i)
	{
		var border = $(this).css('border');
		if (border != undefined && border != "")
		{
			border = border.split(" ");
		
			if (border[0] == "1px")
			{
				var position = $(this).attr("align");
				
				$(this).css('border','0px');
				$(this).css('margin','0px');
				
				$(this).before("<div class='image "+position+"'></div>");		
				$(this).parent().children('.image').html($(this));
			}
		}
	});
	
	$('div.image').each(function(i)
	{
		imgEffet($(this).children('img'), $(this));
		
		//pour chrome et safari
		$(this).children('img').load(function()
		{
			imgEffet($(this), $(this).parent());
		});
	});
	
	
	
	//encadrés
	$("#encadre h3.no_bg").parent().addClass("no_bg");
	
	
	
	//h3
	$(".colonne_deco h3").each(function(i)
	{
		if ($(this).height() <= 40) $(this).addClass("ligne_1");
		else if ($(this).height() <= 60) $(this).addClass("ligne_2");
		else if ($(this).height() <= 80) $(this).addClass("ligne_3");
		else if ($(this).height() <= 100) $(this).addClass("ligne_4");
		else if ($(this).height() <= 120) $(this).addClass("ligne_5");
	});	
	
	
	
	//statistiques
	statistiquesPDF();
	statistiquesLiensExternes();
	
});

var slideTimer = null;
var slideTime = 20;

function imgEffet(img, parent)
{
	var imgWidth = img.width() - 4;
	var imgHeight = img.height() - 4;
	
	parent.append('<div class=\'cadre\'>&nbsp;</div>');
	parent.children('.cadre').css({'width':imgWidth+'px', 'height':imgHeight+'px'});
	
	parent.append('<div class=\'ombre\'>&nbsp;</div>');
	parent.children('.ombre').css({'width':imgWidth+'px', 'height':imgHeight+'px', 'top':'12px', 'left':'12px'});
}

function diaporama()
{	
	var nbItem = $('.diaporama .center ul li').length;
	var itemWidth = $('.diaporama .center ul li').width() + parseInt($('.diaporama .center ul li').css('padding-left')) + parseInt($('.diaporama .center ul li').css('padding-right'));
	
	//positions min et max du conteneur des items
	var posMin = 0;
	if (nbItem%2 == 0) var posMax = ((nbItem*itemWidth) - (2*itemWidth)) * (-1);
	else var posMax = (((nbItem+1)*itemWidth) - (2*itemWidth)) * (-1);
	
	//mise à jour de la largeur du conteneur
	$('.diaporama .center ul').css({'width':(nbItem*itemWidth)+'px'});
	
	//mise à jour des pages
	var pages = nbItem;
	var page = 1;
	diaporamaPages(page, pages);
	
	$('.diaporama .right a').click(function ()
	{
		//var pos = parseInt($('.diaporama .center ul').css('left'));
		var pos = (page - 1) * (2*itemWidth) * (-1);
		pos -= (2*itemWidth);
		
		if (pos >= posMax)
		{
			//mise à jour des pages
			page++;
			diaporamaPages(page, pages);
			
			//déplacements des items
			if (slideTimer) clearInterval(slideTimer);
			slideTimer = setInterval('diaporamaSlide('+(-1)+', '+pos+', '+posMin+', '+posMax+')', slideTime);
			
			//mise à jour de la navigation
			diaporamaNav(pos, posMin, posMax);			
		}
		
		return false;
	});
	
	$('.diaporama .left a').click(function ()
	{
		//var pos = parseInt($('.diaporama .center ul').css('left'));
		var pos = (page - 1) * (2*itemWidth) * (-1);
		pos += (2*itemWidth);
		
		if (pos <= posMin)
		{
			//mise à jour des pages
			page--;
			diaporamaPages(page, pages);
			
			//déplacements des items
			if (slideTimer) clearInterval(slideTimer);
			slideTimer = setInterval('diaporamaSlide('+1+', '+pos+', '+posMin+', '+posMax+')', slideTime);
			
			//mise à jour de la navigation
			diaporamaNav(pos, posMin, posMax);			
		}
		
		return false;
	});
}

function diaporamaNav(pos, posMin, posMax)
{
	if (pos == posMin)
	{
		$('.diaporama .left a').addClass('disabled');
		$('.diaporama .right a').removeClass('disabled');
	}
	else if (pos == posMax)
	{
		$('.diaporama .left a').removeClass('disabled');
		$('.diaporama .right a').addClass('disabled');
	}
	else
	{
		$('.diaporama .left a').removeClass('disabled');
		$('.diaporama .right a').removeClass('disabled');
	}
};

function diaporamaPages(page, pages)
{
	var items = page * 2;
	if (items > pages) items = pages;
	
	$('.diaporama .pages').html(items + "/" + pages);
};
	
function diaporamaSlide(direction, pos, posMin, posMax)
{
	var posCurrent = parseInt($('.diaporama .center ul').css('left')) + (22 * direction);
		
	if ((direction == -1 && posCurrent >= pos) || (direction == 1 && posCurrent <= pos))
	{
		$('.diaporama .center ul').css({'left':posCurrent+'px'});
	}
	else
	{
		clearInterval(slideTimer);
	}
}

function statistiquesPDF()
{
	$("#contenuhome a, #contenupage a").each(function(i)
	{
		var href = $(this).attr('href');
		
		if (href.search(/LectureFichiergw/) != -1)
		{
			var fichier = href.substr(href.search(/CODE_FICHIER/));			
			
			$(this).click(function ()
			{
				_gaq.push(['_trackPageview',fichier]);
			});
		}
	});	
}
function statistiquesLiensExternes()
{
	$("#contenuhome a, #contenupage a").each(function(i)
	{
		var href = $(this).attr('href');
		if (href.search(/http/) != -1 && href.search(/cciroanne.local/) == -1 && href.search(/cciroanne.projects.clever-age.net/) == -1 && href.search(/roanne.cci.fr/) == -1)
		{
			$(this).click(function ()
			{
				_gaq.push(['_trackEvent', 'Liens sortants', href]);
			    return false;
			});
		}
	});	
}
function recordOutboundLink(link, category, action) {
  try {
	  var pageTracker=_gat._getTracker("UA-7223764-3");
	  pageTracker._trackEvent(category, action);
	  //setTimeout('document.location = "' + link.href + '"', 100)
	  // window.open( document.getElementById("tgt_"+id).action, "_blank");
	  setTimeout(window.open(link.href, "_blank"), 100)
  } catch(err){}
}


