// Chargement de la librairie jQuery
var isJquery = true;
if (typeof jQuery == 'undefined') {
	alert('jQuery n\'est pas instancié');
	isJquery = false;
}
if (isJquery == true)
{
	$(document).ready(function() {
		init();
	});
}

var minWidthToolbar = 910;
var minHeightToolbar = 800;
var objetId = 0;
var isIe7 = checkIe7();
var isIe = checkIe();
var currentAp = 1;
var cellCount = 0;

function checkIe()
{
	var isIE = (navigator.appName=="Microsoft Internet Explorer");
	var IEversion = navigator.appVersion;
	if(isIE) {
		return true;
	} else {
		return false;
	}
}
function checkIe7()
{
	var isIE = (navigator.appName=="Microsoft Internet Explorer");
	var IEversion = navigator.appVersion;
	if(isIE) {
		IEversion = parseInt(IEversion.substr(IEversion.indexOf("MSIE")+4));
	} else {
		IEversion = 0;
	}
	if (IEversion == 7)
	{
		return true;
	}
	else
	{
		return false;
	}
}

// Initialisation de la toolbar
function init()
{
	var monUrl = window.location;
	monUrl = monUrl.toString();
	monUrl = monUrl.split("/");
	// Ajustement des background
	if (monUrl[3] == "adolescences")
	{
		// $("body").attr("style","background-position: center top;");
	}
	else if (monUrl[3] == "revisions")
	{
		$("body").attr("style","margin-top:52px;background-position:center 52px;");
	}
	if (monUrl[2] != 'webtveduclocal.intra.france5.fr' && monUrl[2] != 'www.curiosphere.tv' && monUrl[2] != 'pp.curiosphere.tv' && monUrl[2] != 'curiosphere.tv' && monUrl[2] != 'prem.curiosphere.tv' && monUrl[2] != 'sec.curiosphere.tv' &&  monUrl[2] != 'ter.curiosphere.tv' &&  monUrl[2] != 'frontbackcuriosphere.tv')
	{
		monUrl = "www.curiosphere.tv"
	}
	else
	{
		monUrl = monUrl[2];
	}
	urlJsIncludeToolbar = $("#includeToolbarCuriosphere").attr("src");
	parseUrlInclude(urlJsIncludeToolbar);
	
	$.ajax({
		url: 'http://' + monUrl + '/_include/mini-site-toolbar.cfm?objetid=' + objetId,
		success: function(data) {
			$('body').prepend(data);
			// monTimerLive = setTimeout("showBackground();",1000);
			showBackground();
			if ($("body").css("margin-top") == "0px")
			{
				$("#tbc_container").attr("style","top: 0;");
			}
			showRandomAutopromo();
			initFunctionsToolbar();
			/*
			setTimeout(function(){
				$('body').prepend(data);
				// monTimerLive = setTimeout("showBackground();",1000);
				
				if ($("body").css("margin-top") == "0px")
				{
					$("#tbc_container").attr("style","top: 0;");
				}
				showRandomAutopromo();
				initFunctionsToolbar();
			},2000);
			*/
		}
	});
}

function showBackground()
{
	$("#tbc_container").css("height","52px");
	$("#tbc_container").css("z-index","9999");
	$("#tbc_container").fadeIn("slow");
}

var activeRandomAutoPromo = 0;
function detectActivePage()
{
	if (jQuery(".displayAutopromo"))
	{
		if (activeRandomAutoPromo == 0)
		{
			activeRandomAutoPromo = 1;
			showRandomAutopromo();
		}
	}
}


function parseUrlInclude(urlToParse)
{
	var lUrl = $.url(urlToParse);
	if (lUrl.param('objetid'))
	{
		objetId = lUrl.param('objetid');
	}
}

function initFunctionsToolbar()
{
	//$("#tbc_container").css("width",$("body").width());
	//$("#tbc_ombreToolbar").css("width",$("body").width());
	$('#tbc_partenaire').tipsy();
	$('#tbc_contact').tipsy();
	initSocialPlugins();
	menuCuriosphere();
	openLiveNotifications(); // Ouverture de l'autopromo au chargement de la page
	actionLinkNotification(); // Changement de l'aspect du bouton de notification
	clicNotification();
	screenSize();
}

function screenSize()
{
	var larg = (document.body.clientWidth);
	var haut = (document.body.clientHeight);
	largeurManquante = 0;
	if (window.innerWidth < minWidthToolbar)
	{
		largeurManquante = minWidthToolbar - window.innerWidth;
	}
	if (window.innerHeight < minHeightToolbar)
	{
		window.resizeBy(largeurManquante,52); //augmente la hauteur de 52 si on est dans une popup
	}
}

function clicNotification()
{
	$('#tbc_linkNotification').live('click', function() {
		var currentClass = $(this).attr("class");
		if (currentClass == "tbc_notifActive")
		{
			closeNotification();
		}
		else if (currentClass == "tbc_notifInactive")
		{
			openNotification();
		}
		return false;
	});
}
function actionLinkNotification()
{

	$('#tbc_linkNotification').mouseenter(function() {
		if ($(this).attr("src") == "/_assets/images/toolbar/fleche-notifications-on.png")
		{
			$(this).attr("src","/_assets/images/toolbar/fleche-notifications-on-hover.png")
		}
		else if ($(this).attr("src") == "/_assets/images/toolbar/fleche-notifications-off.png")
		{
			$(this).attr("src","/_assets/images/toolbar/fleche-notifications-off-hover.png")
		}
		currentImage = $(this).attr("src");

	});
	$('#tbc_linkNotification').mouseleave(function() {
		if ($(this).attr("src") == "/_assets/images/toolbar/fleche-notifications-on-hover.png")
		{
			$(this).attr("src","/_assets/images/toolbar/fleche-notifications-on.png")
		}
		else if ($(this).attr("src") == "/_assets/images/toolbar/fleche-notifications-off-hover.png")
		{
			$(this).attr("src","/_assets/images/toolbar/fleche-notifications-off.png")
		}
	});
}

function initSocialPlugins()
{
	var monUrl = window.location;
	$("#btnFacebook").html('<div id="fb-root"></div><fb:like href="' + monUrl + '" send="true" layout="button_count" width="166" show_faces="false" font="arial"></fb:like>')
	$(".fb_ltr").css("style","width: 136px;");
	$("#gPlusContainer > div").attr("style","width: 60px");
	$("#gPlusContainer > iframe").attr("style","width: 60px");
}

function menuCuriosphere()
{
	$('#tbc_logoCuriosphere > img').mouseenter(function() {
		$(this).attr("src","/_assets/images/toolbar/logo-curiosphere-hover.png")
	});
	$('#tbc_logoCuriosphere > img').mouseleave(function() {
		$(this).attr("src","/_assets/images/toolbar/logo-curiosphere.png")
	});
	$('#tbc_logoCuriosphere').live('click', function() {
		openMenuCuriosphere();
		return false;
	});
}

function openMenuCuriosphere()
{
	if ($("#tbc_menuCuriosphere").css("margin-top") == "-240px")
	{
		$("#tbc_menuCuriosphere").animate({
			marginTop: '-181px'
			}, 0, function() {
		});
		$("#tbc_menuCuriosphere").animate({
			marginTop: '-6px'
			}, 1000, function() {
		});
	}
	else
	{
		$("#tbc_menuCuriosphere").animate({
			marginTop: '-240px'
			}, 200, function() {
		});
	}
	// 151px;
}

var monTimerLive;
function openLiveNotifications()
{
	var arrayTime = new Array();
	$(".displayAutopromo").hide();
	openNotification();
	jQuery(".displayAutopromo").each(function(i){
		var idAndDuration = this.id.split("_");
		arrayTime[i] = idAndDuration[2];
	});
	var firstTiming = (arrayTime[0] * 1000);
	monTimerLive = setTimeout("closeNotification();",firstTiming);
}

var apDuration = 7000;
var currentAp = 1;
var timerAp = "";
var monTimer;
function openNotification()
{
	$("#tbc_linkNotification").attr("src","/_assets/images/toolbar/fleche-notifications-on.png")
	$("#tbc_linkNotification").removeClass("tbc_notifInactive");
	$("#tbc_linkNotification").addClass("tbc_notifActive");
	if ($("#tbc_autopromoContainer").css("margin-top") == "-346px")
	{
		showRandomAutopromo();
		$("#tbc_autopromoContainer").animate({
			marginTop: '-346px'
			}, 0, function() {
		});
		$("#tbc_autopromoContainer").animate({
			marginTop: '0'
			}, 1000, function() {
		});

	}
	else
	{

	}

	// Compte le nommbre d'AP
	var k = 0;
	$('#tbc_autopromoContainer > .displayAutopromo').each(function(index) {
		k = k + 1;
	});
	k = k + 1;
	var numAp = Math.floor(Math.random() * k);
	if (numAp == 0)
	{
		numAp = 1;
	}

	// Boucle sur le AP dans tbc_autopromoContainer et on afficha la premiere
	var j = 0;
	$('#tbc_autopromoContainer > .displayAutopromo').each(function(index) {
		j = j + 1;
		if (j == numAp)
		{
			$(this).show();
		}
	});

	$("#autopromo_1").show();
}

function closeNotification()
{
	window.clearTimeout(monTimer);
	window.clearTimeout(monTimerLive);
	//openAutopromo = false;
	$("#tbc_linkNotification").attr("src","/_assets/images/toolbar/fleche-notifications-off-hover.png")
	$("#tbc_linkNotification").removeClass("tbc_notifActive");
	$("#tbc_linkNotification").addClass("tbc_notifInactive");
	$(".displayAutopromo").hide();
	$("#tbc_autopromoContainer").animate({
		marginTop: '-346px'
		}, 200, function() {
	});
}

function showRandomAutopromo()
{
	var arrayDuration = new Array();
	jQuery(".displayAutopromo").each(function(i){
		var idAndDuration = this.id.split("_");
		arrayDuration[i] = idAndDuration[2];
	});

	var firstDuree = arrayDuration[0] * 1000;
	monTimer = setTimeout(function(){
    	changeAp(arrayDuration);
	},firstDuree);
}

function changeAp(duree) {
	window.clearTimeout(monTimer);
	dureeForFunction = duree;
	j = duree.length;
	apToHide = currentAp;
	cellTab = cellCount;
	if (currentAp < j)
	{
		currentAp = currentAp + 1;
		cellCount = cellCount + 1;
	}
	else
	{
		currentAp = 1;
		cellCount = 0;
	}
	jQuery(".displayAutopromo").hide();
	var cheminAp = jQuery("#autopromo_" + currentAp + "_" + dureeForFunction[cellCount]).children("a").children("img").attr("src");
	var cheminAp = jQuery("#autopromo_" + currentAp + "_" + dureeForFunction[cellCount]).children("a").children("img").attr("src",cheminAp);
	jQuery("#autopromo_" + currentAp + "_" + dureeForFunction[cellCount]).fadeIn("slow");
	laDuree = dureeForFunction[cellCount] * 1000;
	monTimer = setTimeout(function(){
    	changeAp(dureeForFunction);
	},laDuree);
}

/*
function showRandomAutopromo()
{
	j = 0;
	jQuery(".displayAutopromo").each(function(i){
		j = j + 1;
	});
	timerAp = setTimeout("changeAp(j)",apDuration);
}

function changeAp(nbAp) {
	j = nbAp;
	apToHide = currentAp;
	if (currentAp < j)
	{
		currentAp = currentAp + 1;
	}
	else
	{
		currentAp = 1;
	}
	jQuery("#autopromo_" + apToHide).hide();
	jQuery("#autopromo_" + currentAp).fadeIn("slow");
	timerAp = setTimeout("changeAp(j)",apDuration);
}
*/


//$("body").append('<script type="text/javascript" src='+url+'></script>');
