var COOKIE_EXPIRATION_DAYS = 14;
var MAX_REGIONS_PER_COLUMN = 15;
var mouseOverLabel = false;
var mouseOverArea = false;


document.loaded(function() {

	if($('world-map')) {

		var areas = $('world-map').descendants();
		for(var i = 0; i < areas.length; i++) {

			Event.observe(areas[i], 'mouseover', showAreaLabel.bindAsEventListener(areas[i]));
			Event.observe(areas[i], 'click', showRegions.bindAsEventListener(areas[i]));
			Event.observe(areas[i], 'mouseout', hideAreaLabel.bindAsEventListener(areas[i]));
		}

		$('country-selector-map-data').hide();
	}

	bindLinkListener();
});



function bindLinkListener()
{
	var links = document.getElementsByTagName('a');
	for(var i = 0; i < links.length; i++) {

		if(links[i].id != 'country-selector-language-selection-close') {

			Event.observe(links[i], 'click', setMarketCookie.bindAsEventListener(links[i]));
		}
	}
}



function showAreaLabel(e)
{
	if($('map-data-' + this.id) && $('map-data-' + this.id).down('img')) {

		mouseOverArea = true;

		if($('home-overlay-area-image')) {

			$('home-overlay-area-image').remove();
		}

		/* Ensure <= IE6 PNG fix is applied to new image */
		var image = $('map-data-' + this.id).down('img');
		var filterString = new String(image.style.filter).replace(/"/g, "\'");

		new Insertion.Bottom('country-selector-container', '<img src="' + image.src + '" alt="' + image.alt + '" id="home-overlay-area-image" class="' + this.id + '" style="filter: ' + filterString + '; width: ' + image.width + 'px; height: ' + image.height + 'px;">');


		Event.observe('home-overlay-area-image', 'click', showRegions.bindAsEventListener(this));

		Event.observe('home-overlay-area-image', 'mouseover', function() {

			mouseOverLabel = true;
		});

		Event.observe('home-overlay-area-image', 'mouseout', function() {

			setTimeout(function() {

				if(!mouseOverArea) {

					hideAreaLabel();
				}
			}, 100);
			mouseOverLabel = false;
		});
	}
}



function hideAreaLabel(e)
{
	mouseOverArea = false;

	setTimeout(function() {

		if(!mouseOverArea) {

			if($('home-overlay-area-image') && !mouseOverLabel) {

				$('home-overlay-area-image').remove();
			}
		}
	}, 100);
}



function showRegions(e)
{
	var regions = $('map-data-' + this.id).next('dd');

	var strClassName = 'single';
	var leftValue = 147;

	if(regions.down('ul').childElements().length >= MAX_REGIONS_PER_COLUMN) {

		strClassName = 'double';
		leftValue = 295;
	}

	var documentDimensions = getPageSize();
	var regionSelect = Builder.node('div', {id: 'country-selector-language-selection', className: strClassName, style: 'left: ' + ((documentDimensions[0]/2) - leftValue) + 'px;'}, [
				Builder.node('h3', [
					Builder.node('img', {src: '/_images/country-selector/overlay-labels/' + this.id + '.gif'})
				]),

				Builder.node('a', {href: '#', id: 'country-selector-language-selection-close'}),
	]);
	regionSelect.innerHTML += regions.innerHTML;

	var blackOut = Builder.node('div', {id:'country-selector-map-blackout', style: 'height: ' + documentDimensions[1] + 'px;'});

	document.body.appendChild(blackOut);
	document.body.appendChild(regionSelect);

	var languageOptions = document.getElementsByClassName('language-options', 'ul');
	for(var i = 0; i < languageOptions.length; i++) {

		languageOptions[i].hide();

		/* Browser sniffing sucks, but this really /is/ taking all day */
		if(Prototype.Browser.WebKit) {

			languageOptions[i].addClassName('safari');
		}
	}

	var openers = document.getElementsByClassName('opener', 'span');
	for(var i = 0; i < openers.length; i++) {

		Event.observe(openers[i].up('li'), 'mouseover', showLanguageOptions.bindAsEventListener(openers[i]));
		Event.observe(openers[i].up('li'), 'mouseout', hideLanguageOptions.bindAsEventListener(openers[i]));
	}

	Event.observe('country-selector-language-selection-close', 'click', hideRegions);
	countrySelectorKeyPressEvent = Event.observe(window, 'keydown', hideRegionKeyPress);

	Event.stop(e);
	bindLinkListener();
}



function hideRegionKeyPress(e)
{
	if(e.keyCode == Event.KEY_ESC) {

		hideRegions(e);
	}
}



function hideRegions(e)
{
	if($('country-selector-map-blackout')) {

		$('country-selector-map-blackout').remove();
		$('country-selector-language-selection').remove();

		if(countrySelectorKeyPressEvent) {

			Event.stopObserving(countrySelectorKeyPressEvent);
		}
	}

	Event.stop(e);
}



function showLanguageOptions()
{
	this.next('.language-options').show();
}



function hideLanguageOptions()
{
	this.next('.language-options').hide();
}



function setMarketCookie(e)
{
	var date = new Date();
	date.setTime(date.getTime()+(COOKIE_EXPIRATION_DAYS*24*60*60*1000));

	if(typeof(this.rel) != 'undefined' && this.rel != '') {
	
		document.cookie = 'selected_country=' + this.rel + '; expires=' + date.toGMTString() + '; path=/';
	}

	var parts = new String(this.href).split('/');
	if((parts.length -2) > 0) {

		var marketVersion = parts[parts.length -2];
		document.cookie = 'country=' + marketVersion + '; expires=' + date.toGMTString() + '; path=/';
	}
}



function getPageSize()
{
	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY)
	{
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	}
	else if (document.body.scrollHeight > document.body.offsetHeight)
	{ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	}
	else
	{ // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight)
	{	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
	{ // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	}
	else if (document.body)
	{ // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if (yScroll < windowHeight)
	{
		pageHeight = windowHeight;
	}
	else
	{
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth)
	{
		pageWidth = windowWidth;
	}
	else
	{
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}