$(document).ready(init);

function init(open){
	//$('.tablink').remove().prependTo('body');

	// NEWSLETTER
	var clickNewsLetter = false;
	$("#newsletterinput").click(function(){
		if(!clickNewsLetter){
			clickNewsLetter = true;
			$("#newsletterinput").attr("value","");
		}
	});
	
	detectAnchorPage();
	
	// HOME PAGE INIT
	if(pageId == "home"){
		$(".tablink").click(openTab);
		if(open != false) openTab("",initTab);
		// bubble
		$(".globaltab").hover(calBubble,calBubble);
		$("#calendar").click(calBubble);
		if(bubbleOpened){
			$(bubbleDiv).remove();
			$(bubbleDiv).removeShadow();
		}
	}
	
	// Handle links in new windows
	$("#sub-club a").click(function() { window.open(this.href); return false;});
}

/* HOME SLIDER */
var wTabOpen = 760;
var wTabClose = 65;
var currentTab;
var initTab = "evenements";
var tabId;

// Stylesheet name >>  css/colors/color-[COLOR].css
var tabToColor = new Array();
tabToColor["evenements"] = "blue";
tabToColor["congres"] = "orange";
tabToColor["visiteurs"] = "green";

// Hexa code template color
var tabToColorCode = new Array();
tabToColorCode["evenements"] = "c87c24";
tabToColorCode["congres"] = "4176be";
tabToColorCode["visiteurs"] = "a7b319";

// required by google maps
var items = [];


function openTab(obj,id){
	tabId = (id == undefined ? $(this).parent().parent().attr("id") : id);
	if (tabId=='congres') return true;
	window.location.href = "#/tab/"+tabId; 
	if(tabId == currentTab && id == undefined) return;
	currentTab = tabId;
	$("#colorstyle").attr("href","/sites/all/themes/tourisme/css/color-"+tabToColor[tabId]+".css");
	$("#tab > li").each(function(){
		var nWidth = (this.id == tabId?wTabOpen:wTabClose);
		if(jQuery.browser.msie && jQuery.browser.version == "6.0"){
			if($(this).width() != nWidth) $(this).width(nWidth);
			$("#tab #"+this.id+" .globaltab .content").html("");
		}else{
			if($(this).width() != nWidth) $(this).animate({width:nWidth},500,"swing",motionFinished);
		}
	});		
	getData();
	return false;
}

function detectAnchorPage(){
	var pageURL = document.location.toString();
	if(pageURL.match('#')){
		var anchor = '#' + pageURL.split('#')[1];
		anchor = anchor.split('/')[2];
		if(anchor != currentTab) initTab = anchor;
	}
}

function getData(extra){
	var extra = extra ? extra : "";
	var where_to = basePath + pageLanguage + "/frontabs/"+currentTab;
//	alert(where_to);
	$.get(basePath + pageLanguage + "/frontabs/"+currentTab+'/'+extra,function(data){
		$("#tab #"+currentTab+" .globaltab .content").html(data);
		init(false);
		//txtToImg();
	});
}

function txtToImg(){
	$(".globaltab h2").each(function(i){
		if($(this).css("text-indent") != "-9000px"){
			var val = $(this).html();
			$(this).css("background","url(/sites/all/themes/tourisme/dtr/image.php?text="+escape(val)+"&tag=h1&color="+tabToColorCode[tabId]+") no-repeat");
			$(this).css("text-indent","-9000px");
		}
	});
}

function motionFinished(){
	// Empty div
	if(this.id != currentTab) $("#tab #"+this.id+" .globaltab .content").html("");
	if(tabId=="evenements"){
		$("ul#tab li#congres .tablink").css("background-image","url(/sites/all/themes/tourisme/images/"+pageLanguage+"/btn-home-congres.gif)");
		$("ul#tab li#evenements .tablink").css("background-image","url(/sites/all/themes/tourisme/images/"+pageLanguage+"/btn-home-evenements.gif)");				
	}
	if(tabId=="congres"){
		$("ul#tab li#congres .tablink").css("background-image","url(/sites/all/themes/tourisme/images/"+pageLanguage+"/btn-home-congres.gif)");
		$("ul#tab li#evenements .tablink").css("background-image","url(/sites/all/themes/tourisme/images/"+pageLanguage+"/btn-home-evenements-left.gif)");				
	}
	if(tabId=="visiteurs"){
		$("ul#tab li#congres .tablink").css("background-image","url(/sites/all/themes/tourisme/images/"+pageLanguage+"/btn-home-congres-left.gif)");
		$("ul#tab li#evenements .tablink").css("background-image","url(/sites/all/themes/tourisme/images/"+pageLanguage+"/btn-home-evenements-left.gif)");				
	}	
}

/* CALENDAR BUBBLE */

var bubbleDiv;
var bubbleOpened = false;
var pos;
var lastLeave;
var lastEnter;

function calBubble(event){
	var posX = event.pageX+15;
	var posY = event.pageY-220;

	var data = "<p class=\"title\">�v�nements du 18 Juillet 2008</p><ul>";
	for(var x=0;x<3;x++){
		data += "<li><a href=\"\"><p class=\"event-title\">01. Titre de l��v�nement</p><p class=\"event-desc\">Lorem ipsum dolor sit amet consectetuer adipiscing elit</p></a></li>";
	}
	data += "</ul>";
	data += "<div class=\"corner\"><img src=\"/sites/all/themes/tourisme/images/bubble-coin.gif\" /></div>";
	var id = $(event.target).attr("id");
	pos = (id == "" ? $(event.target).attr("class") :id);
	if(event.type == "click"){
		if(bubbleOpened){
			bubbleDiv.removeShadow();
			bubbleDiv.remove();
		}
		if(bubbleOpened) $(bubbleDiv).css("display","none");
		bubbleOpened = true;
		bubbleDiv = $("<div id=\"bubble\"></div>");
		$(bubbleDiv).addClass("calendarbubble");
		$(bubbleDiv).html(data);
		$(bubbleDiv).appendTo("body");
		$(bubbleDiv).css("left",posX);
		$(bubbleDiv).css("top",posY);
		$(bubbleDiv).hover(calBubble,calBubble);
		$(bubbleDiv).dropShadow();
	}else if(event.type == "mousemove"){
		//$(bubbleDiv).css("left",posX);
		//$(bubbleDiv).css("top",posY);
	}else if(event.type == "mouseenter"){
		if(pos != "calendar" && pos != "bubble" && pos.length != 0 && bubbleOpened){
			bubbleDiv.removeShadow();
			bubbleDiv.remove();
			bubbleOpened = false;
		};
	}
}

