/*  * © Albin Carrier for LILHOOT, www.lilhoot.eu - 2 févr. 2010 */

/*******************************************************************************CONFIG*/

/*LILHOOT.EU SLIDESWITCH*/
var SLIDESWITCH_INTERVAL = 10000;
var SLIDESWITCH_ANIMATION_LENGTH = 1000;
var slideTimer;


/*******************************************************************************ACTION*/

//gaLoadFile(); /*GOOGLE ANALYTICS Must be outside $ready to load the external ga file first fm google server */


$(function() //jQuery call
{
	//gaLaunch(); /*EXTERNAL GOOGLE ANALYTICS*/
	
	highlightAnchor(); /*highlights anchor targets if any*/
	tooltip(); /*provides custom tooltips if any*/

	if(document.location.toString().match('/references'))
		slideSwitch();	/*LILHOOT.EU SLIDESWITCH*/

	if(browserVersion() == 'IE7')
	{
		var zIndexNumber = 1000;
		$('div').each(function() {
			$(this).css('zIndex', zIndexNumber);
			zIndexNumber -= 10;
		});
	}

});




/*******************************************************************************FUNCTIONS*/

/*--------------------------------------------------------LILHOOT.EU EXTERNAL GOOGLE ANALYTICS
function gaLoadFile()
{
	var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
	var gaScript = document.createElement("script");
	gaScript.setAttribute("type", "text/javascript");
	gaScript.setAttribute("src", gaJsHost +"google-analytics.com/ga.js");
	document.getElementsByTagName("head")[0].appendChild(gaScript); //$('head');
}


function gaLaunch()
{
	if (typeof(_gat) == 'object')
	{
		var pageTracker = _gat._getTracker("UA-12800388-1");
		pageTracker._setDomainName(".lilhoot.eu");
		pageTracker._trackPageview();
	}
}
*/



/*--------------------------------------------------------LILHOOT.EU HIGHLIGHT_ANCHOR*/
function highlightAnchor()
{
	if(document.location.toString().match('#') != null)
		$("#"+ document.location.toString().split('#')[1]).addClass("highlight_anchor");

	$("a").click(function () {
		$(".highlight_anchor").removeClass("highlight_anchor");
		
		if(this.href.split('#')[1])
			$("#"+ this.href.split('#')[1]).addClass("highlight_anchor");
	});
}




/*--------------------------------------------------------LILHOOT.EU TOOLTIPS*/
function tooltip()
{
	$(".tipped").mouseover(function()
	{
		$(this).addClass("tip_active");
		var tipWidth = Math.floor($(".tip").css("width").substr(0, $(".tip").css("width").length -2));
		var containerWidth = Math.floor($("#content").css("width").substr(0, $("#content").css("width").length -2));
		var containerPos = (document.documentElement.clientWidth - containerWidth) /2;
		var tipPosX = $('.tip_active').findPos().x - containerPos;

		if((tipPosX + tipWidth) > (containerPos + containerWidth)) 
			tipPosX = containerPos + (containerWidth - tipWidth -15);	/* Evite que tooltip ne sorte du cadre*/

		$(".tip_active .tip").css({
			"left": tipPosX,
			"display": "block",
			"padding" : "15px",
			"border" : "2px solid #cba",
			"opacity": "0.95",
			"filter": "alpha(opacity=95)"
		});
	});
	$(".tipped").mouseout(function() { $(this).removeClass("tip_active"); $(".tip").css("display", "none"); });
}


jQuery.fn.extend({
   findPos : function() {
       obj = jQuery(this).get(0);
       var curleft = obj.offsetLeft || 0;
       var curtop = obj.offsetTop || 0;
       while (obj = obj.offsetParent) {
                curleft += obj.offsetLeft
                curtop += obj.offsetTop
       }
       return {x:curleft,y:curtop};
   }
}); //pos = $('body').findPos();  alert(pos.x); alert(pos.y);





/*--------------------------------------------------------LILHOOT.EU SLIDESWITCH*/
function slideSwitch()
{
	$('#slideshow .slide').css("opacity", "0.0").css("filter", "alpha(opacity=0)");
	$('#slideshow .active').css("opacity", "1.0").css("filter", "alpha(opacity=100)");

	$('#slideshow .slide .done_list').append('<span class="slide_control_pause" title="pause"> </span>');
	$('#slideshow .slide .done_list').append('<span class="slide_control_prev" title="previous"> </span>');
	$('#slideshow .slide .done_list').append('<span class="slide_control_next" title="next"> </span>');

	$('#slideshow .slide:first .done_list .slide_control_prev').remove();
	$('#slideshow .slide:last .done_list .slide_control_next').remove();

	$('.slide_control_pause').toggle(function() { slideSwitchPause(); }, function() { slideSwitchPlay(); });
	$('.slide_control_prev').click(function() { slideSwitchPause(); slideSwitchMain('-1'); });
	$('.slide_control_next').click(function() { slideSwitchPause(); slideSwitchMain('1'); });

	slideTimer = setInterval( "slideSwitchMain('1')", SLIDESWITCH_INTERVAL );
}


function slideSwitchMain(nb)
{
    var active = ($('#slideshow .active').length == 0) ? $('#slideshow .slide:last') : $('#slideshow .active');
	var slide =  active.next().length ? active.next() : $('#slideshow .slide:first');	// divs in the order

	if(nb == '-1')
		slide =  active.prev().length ? active.prev() : $('#slideshow .slide:last');	// divs in the order

    active.addClass('last-active');
	slide.css({opacity: 0.0}).addClass('active');
	slide.animate({opacity: 1.0}, SLIDESWITCH_ANIMATION_LENGTH, function() { active.removeClass('active last-active'); });
}


function slideSwitchPlay()
{
	slideTimer = setInterval( "slideSwitchMain('1')", SLIDESWITCH_INTERVAL );
	$('.slide_control_play').addClass('slide_control_pause').removeClass('slide_control_play');
}
function slideSwitchPause()
{
	clearInterval(slideTimer);
	$('.slide_control_pause').addClass('slide_control_play').removeClass('slide_control_pause');
}



/*--------------------------------------------------------LILHOOT.EU BROWSERDETECT partial*/
function browserVersion()
{
	if (/OPERA[\/\s](\d+)\.(\d+)/.test(navigator.userAgent.toUpperCase())) //test for Opera/x.x or Opera x.x must be done before IE
	{
		if (RegExp.$1 <= 6 && window.opera) return 'OP6';
		if (RegExp.$1 == 7 && document.getElementById && !window.getComputedStyle && window.opera) return 'OP7';
		if (RegExp.$1 == 8 && window.getComputedStyle && window.opera) return 'OP8';
		if (RegExp.$1 == 9 && window.opera) return 'OP9';
		if (RegExp.$1 == 9 && RegExp.$2 == 5 && document.getElementsByClassName && window.opera) return 'OP9.5';
		if (RegExp.$1 >= 10 && window.opera) return 'OP10';
	}
	else if(/MSIE (\d+).(\d+);/.test(navigator.userAgent.toUpperCase())) //test IE
	{
		if (RegExp.$1 < 5 && !document.attachEvent) return 'IE4';
		if (RegExp.$1 == 5 && window.attachEvent && !window.createPopup) return 'IE5';
		if (RegExp.$1 == 5 && window.createPopup && !document.compatMode) return 'IE5.5';
		if (RegExp.$1 == 6 && !window.XMLHttpRequest) return 'IE6';
		if (RegExp.$1 == 7 && window.XMLHttpRequest && document.all && !document.documentMode) return 'IE7'; //test IE7
		if (RegExp.$1 == 8 && document.documentMode) return 'IE8'; //test IE8
		if (RegExp.$1 >= 9 && document.documentMode) return 'IE9'; //test IE8
	}
	else if (/FIREFOX[\/\s](\d+)\.(\d+)/.test(navigator.userAgent.toUpperCase())) //test Firefox/x.x or Firefox x.x
	{
		if (RegExp.$1 < 1 && !window.XMLHttpRequest) return 'FF0';
		if (RegExp.$1 == 1 && window.XMLHttpRequest) return 'FF1';
		if (RegExp.$1 == 2 && window.globalStorage) return 'FF2';
		if (RegExp.$1 >= 3 && window.globalStorage && window.postMessage) return 'FF3';
	}
	else if (/CHROME[\/\s](\d+)\.(\d+)/.test(navigator.userAgent.toUpperCase())) //test for Chrome/x.x or Chrome x.x must be done before Safari
	{
		if (RegExp.$1) return 'CH'+ RegExp.$1;
	}
	else if (/SAFARI[\/\s]\d+\.\d+/.test(navigator.userAgent.toUpperCase()) && /VERSION[\/\s](\d+)\.(\d+)/.test(navigator.userAgent.toUpperCase())) //test for Chrome/x.x or Chrome x.x must be done before Safari
	{
		if (RegExp.$1 <= 1 && !window.getComputedStyle) return 'SA1';
		if (RegExp.$1 == 2 && !document.getElementsByClassName && window.getComputedStyle) return 'SA2';
		if (RegExp.$1 >= 3 && document.getElementsByClassName) return 'SA3';
	}
	return false;
}
