﻿var splashVisible = false;

cookieIndentifier = 'distCenter';

$(document).ready(function() {
var dist = getDistCenterCookieValue();

	$(".SiteStatus a").click(function() {
		if(!splashVisible)
			showSplash();
	});

	if (currentBrand == 'TM') {
		$(".Splash .MoreInfo").hide();
		$(".Splash .Button[name$='Canada']").hide();
		$(".Splash .Button[name$='China']").hide();
	}
	if (currentBrand == 'TZ') {
		$(".Splash .Button[name$='Scotland']").hide();
	}

	$(".Splash .Button").click(function() {
		setDistCenterCookie($(this).attr('name'));

		site = $(this).attr('name');

		$(".Splash .Close").hide();
		$(".Splash .Dist").fadeOut("slow");
		$(".Splash").animate({ "height": "-=50px" }, { duration: "slow", complete: function() {
			if (site != currentSite)
				navigateTo(currentBrand, site);
		}
		});
	});

	$(".Splash .Close").click(function() {
		setDistCenterCookie('none');
		hideDistSelect();
	});

	$(".Splash .Msg a").click(function() {
		$(".Splash .Msg").fadeOut("slow", function() {
			$(".Splash .Buttons").fadeIn("slow");
		});
	});

	if (dist == undefined) {
		showSplash();
	}
	else if (dist == 'none')
		;
	else if (dist != currentSite) {
		$(".Splash .Buttons").hide();
		showSplash(function() {
			$(".Splash .Msg").fadeIn("slow");
		});
	}
});

function showSplash(callback) {
	splashVisible = true;

	$(".Splash").animate({ "height": "+=50px" }, { duration: "slow", complete: callback});
	$(".Splash .Close").show();

	$(".Splash .Dist").show();
}

function getDistCenterCookieValue() {
	return $.cookie(currentBrand + cookieIndentifier);
}

function setDistCenterCookie(val) {
	d = undefined;
	if (!(window.location.href.toString().indexOf('localhost') > 0)) {
		if (currentBrand == 'TM')
			d = 'teddymountain.com';
		else if (currentBrand == 'TZ')
			d = 'teddyzoo.com';
	}

	if (d != undefined) {
		$.cookie(currentBrand + cookieIndentifier, val, { domain: d, expires: 365, path: '/' });
		return;
	}
	else
		$.cookie(currentBrand + cookieIndentifier, val);
}

function navigateTo(brand, dist, page) {
	var link = "";
	if (brand == 'TZ') {
		if (dist == 'United States')
			link = 'http://www.teddyzoo.com/{0}';
		else if (dist == 'Canada')
			link = 'http://ca.teddyzoo.com/{0}';
		else if (dist == 'Mexico')
			link = 'http://mx.teddyzoo.com/{0}';
		else if (dist == 'Europe')
			link = 'http://eu.teddyzoo.com/{0}';
		else if (dist == 'Scotland')
			link = 'http://sct.teddyzoo.com/{0}';
		else if (dist == 'China')
			link = 'http://cn.teddyzoo.com/{0}';			
	}
	else if (brand == 'TM') {
		if (dist == 'United States')
			link = 'http://www.teddymountain.com/{0}';
		else if (dist == 'Mexico')
			link = 'http://mx.teddymountain.com/{0}';
		else if (dist == 'Europe')
			link = 'http://eu.teddymountain.com/{0}';
		else if (dist == 'Scotland')
			link = 'http://sct.teddymountain.com/{0}';
	}
	if (link != "") {
		if (page != null)
			link = link.replace("{0}", page);
		else
			link = link.replace("{0}", "");
		window.top.location.href = link;
	}
}

function hideDistSelect() {
	splashVisible = false;
	$(".Splash .Close").hide();
	$(".Splash").animate({ "height": "-=50px" }, "slow");
	$(".Splash .Dist").fadeOut("slow");
}