$(document).ready(function() {
	replacePNGonIE6();
	
	var countriesLinks = $(".countries a, .europe a, .america a, .international a");
	var link = "";
	for(var a = 0; a < countriesLinks.length; a++){
		// Is external link ?
		if(countriesLinks[a].href.substr(0,4) == "http") {
			var start = countriesLinks[a].href.indexOf("://");
			start = (start > 0) ? start + 3 : 0;
			var end = countriesLinks[a].href.indexOf("/", start);
			end = (end > 0 && end > start) ? end : 0;
			var domain = (end > start) ? countriesLinks[a].href.substring(start, end) : "";
			var domainParts = domain.split(".");
			var mainDomain = (domainParts.length >= 2) ? domainParts.pop() + "." + domainParts.pop() : "";
			if(mainDomain.length > 0 && mainDomain != "com.playgivenchy") continue;
		}
		
		// else
		link = "";
		var links = countriesLinks[a].href.split("/");
		while(link.length <= 0 && links.length > 0){
			link = links.pop();
			if(link.indexOf("?") >= 0) link = link.substr(0, link.indexOf("?"));
		}
		
		if(link == "us") continue;
		
		countriesLinks[a].href = "javascript:loadSpot('" + link + "');"
	}
	
	$("#content").show();
	$("#contentVideo").hide();
	$("#contentDispatch").hide();
	
	setTimeout("_st_initialized()", 100);
});

function _st_initialized(){
	if($("#content .main")) {
		$("#content .main").jScrollPane({ scrollbarWidth:21, showArrows:true, arrowSize:27, observeHash:false });
		$("#content .main").bind({
			emchange : function(){
				$("#content .main").jScrollPaneRemove();
				$("#content .main").jScrollPane({ scrollbarWidth:21, showArrows:true, arrowSize:27, observeHash:false });
			}
		});
	} else {
		setTimeout("_st_initialized()", 100);
	}
}

var country = "";
function loadSpot(_country){
	country = _country;
	
	$("#content").hide();
	$("#contentVideo").show();
	$("#contentDispatch").hide();
	
	$("#enter").css("display", "block");
	$("#enter a").attr("href", country + "/");
	$.get('common/request/get_country_enter_label.php?label=global_2&lang='+country, function(data) {
		$("#enter a").html("[ " + data + " ]");
	});
	$.get('common/request/get_country_enter_label.php?label=ra_0a&lang='+country, loadVideoPlayer);
}

function loadVideoPlayer(data) {
	//var flashvars = { file:"../" + country + "/medias/tv_spot.flv", url:country + "/", image:"images/preview.jpg" };
	var flashvars = {};
	flashvars.file = "../" + country + "/medias/tv_spot.flv";
	flashvars.url = (data && data.length > 0) ? "javascript:loadDispatch();" : country + "/";
	// Video size : 656 x 369px
	swfobject.embedSWF("images/rootPlayer.swf", "videoPlayerSpot", "100%", "100%", "9.0.124", "",
		flashvars, { quality:"high", wmode:"transparent", allowscriptaccess:"sameDomain", allowfullscreen:"true" }, { id:"videoPlayerSpot", name:"videoPlayerSpot" });
	
	setTimeout("loadContent()", 100);
}

function loadDispatch(){
	swfobject.removeSWF("#videoPlayerSpot");
	$("#videoPlayerSpot").empty();
	
	$("#content").hide();
	$("#contentVideo").hide();
	$("#contentDispatch").show();
	
	$("#enter2").css("display", "block");
	$("#enter2 a").attr("href", country + "/");
	$("#dispatch .dispatchContainer a").attr("href", country + "/ra/");
	$.get('common/request/get_country_enter_label.php?label=global_2&lang='+country, function(data) {
		$("#enter2 a").html("[ " + data + " ]");
	});
	$.get('common/request/get_country_enter_label.php?label=ra_prehome_1&lang='+country, function(data) {
		$("#dispatch .dispatchContainer .dispatchContentTitle").html("" + data + "");
	});
	$.get('common/request/get_country_enter_label.php?label=ra_prehome_2&lang='+country, function(data) {
		if(data.indexOf("<br") > 0) $("#dispatch .dispatchContainer a").attr("class", "pinkButton2 mediumLabelMulti");
		else $("#dispatch .dispatchContainer a").attr("class", "pinkButton2 mediumLabel");
		$("#dispatch .dispatchContainer a span.label").html("" + data + "");
	});
}

function loadContent(){
	
}

function replacePNGonIE6(){
	if($.browser.msie && parseFloat($.browser.version) < 7){
		var pngImages = $("img[src$='.png']");
		for(var a=0; a<pngImages.length; a++){
			var imgSrc = pngImages[a].src;
			var gifImg = imgSrc.substr(0, imgSrc.length - 4) + ".gif";
			pngImages[a].src = gifImg;
		}
	}
}

$(window).resize(function(){
	if($("#content .main")) {
		$("#content .main").jScrollPaneRemove();
		$("#content .main").jScrollPane({ scrollbarWidth:21, showArrows:true, arrowSize:27, observeHash:false });
	}
});
