// JavaScript Document

$(document).ready(function(){

	//Country dropdown functionlity
	$("#country-selector").click(function() {
  	    var position = $("#country-selector").position();
   	    $("ul.countries").css("left", position.left - 158);
   	    $("ul.countries").css("top", position.top + 1);
		$("ul.countries").toggle();
	});
	
	var countriesLeft;
	
	$("ul.countries").bind("mouseleave", function() {countriesLeft = setTimeout(function(){
		$("ul.subcountries").hide();
		$("ul.countries").hide();}, 500);	
	});
	
	$("ul.countries").bind("mouseenter", function() {
		clearTimeout(countriesLeft);
	});
	
	$("ul.countries>li").hover(
		function(){$("ul", this).show()},
		function(){$("ul", this).hide()});
	
	$("ul.subcountries a").click(function() {
		$("#country-selector").text($(this).text());		
		$("ul.subcountries").hide();
		$("ul.countries").hide();
	});

	//WTB dropdown functionality
	var xml_path = "xml/where-to-buy.xml";
	var wtb_url = "where-to-buy.html?wtb=";
		
	// If we're in the products subdirectory, change the path
	if (document.location.pathname.indexOf('/products') >= 0)
	{
		var xml_path = "../" + xml_path;
		var wtb_url = "../" + wtb_url;
	}
	
	$.ajax({
		type: "GET",
		url: xml_path,
		dataType: "xml",
		success: function(xml) {
			// Look for relevant region in the XML 
			var i =0;
			$(xml).find('region').each(function(){
				var location_text = $(this).attr('location');
				var location = location_text.toLowerCase().replace(" ","_");
				i == 0?
					$('<li class="first"></li>')
					.html('<a href="' + wtb_url + escape(encodeURI(location)) + '">' + location_text + '</a>')
					.appendTo('ul.wtb'):
					$('<li></li>')
					.html('<a href="' + wtb_url + escape(encodeURI(location)) + '">' + location_text + '</a>')
					.appendTo('ul.wtb');
				i++;
			});
		}
	});

	// Position the dropdown
	$("#wtb-selector").click(function() {
  	    var position = $("#wtb-selector").position();
   	    $("ul.wtb").css("left", position.left);
   	    $("ul.wtb").css("top", position.top + 20);
		$("ul.wtb").toggle();
	});
	
	var wtbLeft;
	
	$("ul.wtb").bind("mouseleave", function() {wtbLeft = setTimeout(function(){
		$("ul.subwtb").hide();
		$("ul.wtb").hide();}, 500);	
	});
	
	$("ul.wtb").bind("mouseenter", function() {
		clearTimeout(wtbLeft);
	});
	
	$("ul.wtb>li").hover(
		function(){$("ul", this).show()},
		function(){$("ul", this).hide()});

	//clear Searchbox on click
	$("#search-text").bind("focus", function() {
		if(this.value == "Search...")
			this.value = "";
	});
	
	$("#search-text").bind("blur", function() {
		if(this.value == "")
			this.value = "Search...";
	});
	
	//products functionality
	$(".product-selector").click(function() {
		if($(".product-selector-dropdown", this).css("display") == "block") {
			$(".product-selector-dropdown").hide();
		}
		else {
			$(".product-selector-dropdown").hide();
			$(".product-selector-dropdown", this).show().css("z-index", 200);
		}

	});
	
	
	// Light Box (Thick Box) functionality
	// Check if ThickBox classes are on the page, if they are, load script and CSS
	var thickbox_class = '.thickbox';
	if ($(thickbox_class)) {
		
		// Check if were're in a sub-directory
		// Hack to avoid hard-coded links in EVERY page
		var path_prefix = '';
		if ($(thickbox_class).attr('href').slice(0,3) == '../') {
			path_prefix = '../';
		}

		// Load CSS
		$('head').append(
			'<link type="text/css" rel="stylesheet" href="'+ path_prefix + 'css/thickbox.css" />'
		);
		// GET and load JavaScript
		$.getScript(path_prefix + 'scripts/thickbox-compressed.js', function(){});
		$.getScript(path_prefix + 'scripts/swfobject.js', function(){});

	}
    
    /*External links*/
    $('a[href*=http]').each(	
        function()
        {	
            if(this.href.indexOf(location.hostname) == -1) 
            {					
                $(this).attr('target', '_blank');		
            }
        }
    );	
    
});