var cont;
var site_url = "http://www.edwardthompson.com/";
var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;

function jsddm_open()
{
	jsddm_canceltimer();
   	jsddm_close();
   	ddmenuitem = $('.isLangNav > #more').css('display', 'block');
}

function jsddm_close()
{
	if(ddmenuitem) ddmenuitem.css('display', 'none');
}

function jsddm_timer()
{
	closetimer = window.setTimeout(jsddm_close, timeout);
}

function jsddm_canceltimer()
{
	if(closetimer)
	{
		window.clearTimeout(closetimer);
      	closetimer = null;
	}
}

// Commented out the below, as the design as of SEPT2011 works better
// w/o the flash.
// Hide the text on initial load, but only if they have JS enabled
//$('#footerAzureText SPAN').css('display', 'none');

function showFooterAzureText()
{
   	//$('#footerAzureText SPAN').css('display', 'block');
}

function hideFooterAzureText()
{
   	//$('#footerAzureText SPAN').css('display', 'none');
}

function doFlashNav(targetCont)
{
	var width = screen.width;
	if (document.location.hash == "")
	{
		var currentX = 0;
	}
	else
	{
		var currentX = $('#contentContainer').css('left').split('px')[0];
	}
	var theElement = '#contSection' + targetCont;
	document.location.hash = targetCont;
	var title = "New Title";
	var nextLinkX = 0 - $(theElement).css('left').split('px')[0];
	var distance = currentX - nextLinkX;
	var duration = Math.sqrt(Math.pow(((distance/width) * 400),2));
	$('#contentContainer').animate({'left': nextLinkX  + 'px'}, duration, 'swing');
}

var animating = false;
 
function showContainer(product) {
	animating = true;
	$(".prod_enabled").eq(0).fadeOut(100, function() 
	{
		$(this).removeClass("prod_enabled");
		$("#product_"+product).fadeIn(300, function()
		{
			$(this).addClass("prod_enabled");
			animating = false;
		});
	});
}

function removeHoverHome() {
	$("#home").css('background-position', '0 0');
}

$(document).ready(function(event)
{
	$("a.gallery-image").fancybox({'overlayOpacity': 0.6});
	
	$(".isLangNav").hover(function(){
		$("#home").css('background-position', '0 -30px');
	}, function(){
		window.setTimeout(removeHoverHome, timeout);
	});
	
    // --- START: jQuery Carousel Block ---
	//var promo = document.URL.split("/");
	//if(promo[3] == "promotional-games")
	//{
	//	$('#link_167').addClass('active');
	//}
	//
	//$("#product_134").fadeIn(200);
	//$("#product_134").addClass("prod_enabled");
	//$("#menu_134").addClass("active");
	//
	//$(".v_menuitem a").bind('click', function(e) {
	// e.preventDefault();
	// 	if (animating === true)
	//	return;
	//	showContainer($(this).attr("rel"));
	//	$(".active").removeClass("active");
	//	$(this).parent().addClass("active");
	//  return false;
	//});
 	
	//$('#slidingCategoriesCarousel').jcarousel({
        // Configuration goes here
	//			vertical: true,
	//			buttonNextHTML: null,
    //    buttonPrevHTML: null,
	//			scroll: 3,
	//			visible: 6,
	//			initCallback: mycarousel_initCallback,
	//			wrap: "both"
    //});
	
	//function mycarousel_initCallback(carousel) {
    //$('#carousel-next').bind('click', function() {
    //    carousel.next();
    //    return false;
    //});

    //$('#carousel-previous').bind('click', function() {
    //    carousel.prev();
    //    return false;
    //});
	//};
    // --- END: jQuery Carousel Block ---

	$(function()
	{
		$('.contentBody').jScrollPane();
        $('.contentBodyGames').jScrollPane();
		$('.contentBodyHome').jScrollPane();
		$('.contentBodyCase').jScrollPane();
		$('.contentBodyTerms').jScrollPane();
	});

    /*
	if (navigator.appVersion.match("4.0"))
	{
		$("#primNavLi > li:first-child").prepend("<img id=\"navFlourish\" src=\""+site_url+"skin/img/tmp_navflourish-ie6.png\" border=\"0\">");
		$("#navFlourish").addClass("navFlourish");
	}
	else
	{
		$("#primNavLi > li:first-child").prepend("<img id=\"navFlourish\" src=\""+site_url+"skin/img/tmp_navflourish.png\" border=\"0\">");
		$("#navFlourish").addClass("navFlourish");
	}
    */
	
	$('.isLangNav > #home').bind('mouseenter', '',  jsddm_open);
   	$('#listLangNav').bind('mouseleave', '', jsddm_timer);
	
	/* Temporarily disabled click on language */
	$('a#home').click(function(event)
	{
		event.preventDefault();
	});
	
	$('#footerAzureImage').bind('mouseenter', '',  showFooterAzureText);
   	$('#footerAzureImage').bind('mouseleave', '', hideFooterAzureText);
	
	var str = document.location.toString();
	
	if ((document.location.hash != "") || ($(".contSection").length > 1))
	{
		if (document.location.hash.match("-"))
		{
			doPosConts();
			
			hashSplit = document.location.hash.split("-");
			
			var currentCont = hashSplit[0].substring(1);
			var targetCont = hashSplit[1];
			var currentPos = getPos(currentCont);
			
			document.getElementById("contentContainer").style.left = currentPos+"px";
			document.getElementById("contentContainer").style.top = "0px";
			document.location.hash = targetCont;
			
			doFlashNav(targetCont);
		}
		else
		{
			doPosConts();
			
			if (document.location.hash != "")
				var currentCont = document.location.hash.substring(1);
			else
				var currentCont = getCont();
				
			var currentPos = getPos(currentCont);
			
			document.getElementById("contentContainer").style.left = currentPos+"px";
			document.getElementById("contentContainer").style.top = "0px";
		}
	}
	else if(str.substring(30,34) == "home" || str.substring(30,34) == "" || str.substring(30,35) == "home?")
	{
		doPosConts();
		document.getElementById("contentContainer").style.top = "0px";
	}
	else
	{
		document.getElementById("contentContainer").style.top = "0px";
	}
	
	
	$('a.isPrimNav').click(function(event)
	{
		/** Add hover state**/
		$('a.isPrimNav').removeClass('active');
		$(this).addClass('active');
		
		if($(this).hasClass('static'))
		{
			/* We want to be able to skip the JS, if the page is to be served statically (not in the slider) | S.Dawson */
			return;
		}
		
		event.preventDefault();
		
		var targetCont = new Array();
		targetCont = this.id.split('_').reverse();
		
		var currentCont = getCont();
		var currentState = getState();
		
		if (currentState == "1")
		{
			document.location = site_url+"home#"+targetCont[0];
		}
		else
		{
			doFlashNav(targetCont[0]);
		}
	});
	
	$('a#homeBtn').click(function(event)
	{
		event.preventDefault();
		
		var targetCont = '1';
		
		var currentCont = getCont();
		var currentState = getState();
		
		if (currentState == "1")
		{
			document.location = site_url+"home#"+currentCont+"-"+targetCont;
		}
		else
		{
			doFlashNav(targetCont);
		}
	});
});

/* Determines whether are inside subnav or not. 1=inside, 0=not */
function getState()
{
	var cont = new Array();
	cont = document.URL.split("/");
	
	if (cont[4] != undefined || cont[3] == "promotional-games")
	{
		return currentState = "1";
	}
	else
	{
		return currentState = "0";
	}
}

function getCont()
{
	var currentCont = "";
	
	if (document.location.hash == "")
	{
		var cont = new Array();
		cont = document.URL.split("/");
		if (cont[3] == "bingo-and-games")
		{
			return currentCont = "8";
		}
		else if (cont[3] == "electronic-gaming")
		{
			return currentCont = "13";
		}
		else if (cont[3] == "marketing-and-print")
		{
			return currentCont = "14";
		}
		else if (cont[3] == "build-specialists")
		{
			return currentCont = "15";
		}
		else if (cont[3] == "mailing")
		{
			return currentCont = "16";
		}
		else if (cont[3] == "promotional-games")
		{
			return currentCont = "17";
		}
		else if (cont[3] == "gifts")
		{
			return currentCont = "18";
		}
		else
		{
			return currentCont = "1";
		}
	}
	else
	{
		return currentCont = document.location.hash.substring(1);
	}
}

function getPos(currentCont)
{
	var width = screen.width;
	var currentPos = "";
	
	if (currentCont == "1")
	{
		return currentPos = "0";
	}
	else if (currentCont == "8")
	{
		return currentPos = "-"+width;
	}
	else if (currentCont == "13")
	{
		return currentPos = "-"+(2*width);
	}
	else if (currentCont == "14")
	{
		return currentPos = "-"+(3*width);
	}
	else if (currentCont == "15")
	{
		return currentPos = "-"+(4*width);
	}
	else if (currentCont == "16")
	{
		return currentPos = "-"+(5*width);
	}
	else if (currentCont == "17")
	{
		return currentPos = "-"+(6*width);
	}
	else if (currentCont == "18")
	{
		return currentPos = "-"+(7*width);
	}
}

function doPosConts()
{
	var conts = new Array(1,8,13,14,15,16,17,18);
	var count = 0;
	var width = screen.width;
	while (count < 8)
	{
		$("#contSection"+conts[count]).css('left', (count * width)+"px");
		count++;
	}
}


document.onclick = jsddm_close;

$(function() {
	$('a.gallery-image').fancybox({'hideOnContentClick': false, 'frameWidth': 720, 'frameHeight': 404});
});

/* --------------------------------------------------------------------------------------------------------------------- */
