// handle hiding the country select onmouseout
var countryTimer;
var ajaxUrl = "/sitecore/Settings/Icc/Configuration/Ajax.aspx";

function timeoutcountries1()
{
	countryTimer = setTimeout("document.getElementById('countries1').style.display = 'none'", 1500);
}

function stopTimeoutcountries1()
{
	clearTimeout(countryTimer);
}


// toggle visibility of an object
function toggle(obj)
{
	var el = document.getElementById(obj);
	el.style.display = (el.style.display != 'block' ? 'block' : 'none' );
}

// caption boxes below curtain effects
var defaultTxt;
var defaultColor;
function captionBoxes(elem,txt,color,status)
{
	el = document.getElementById(elem);
	if(status == true)
	{
		el.innerHTML = txt;
		el.style.backgroundColor = '#'+color;
		clearTimeout(defaultTxt);
		clearTimeout(defaultColor);
	}
	else
	{
		defaultTxt = setTimeout("el.innerHTML = '"+txt+"'", 200);
		defaultColor = setTimeout("el.style.backgroundColor = '#"+color+"'", 200);
	}
}

// get elements by class name
function getElementsByClass(searchClass, domNode, tagNames)
{
	if (domNode == null) domNode = document;
	if (tagNames == null) tagNames = '*';
	var el = new Array();
	var tags = domNode.getElementsByTagName(tagNames);
	var tcl = " "+searchClass+" ";
	for(i=0,j=0; i<tags.length; i++)
	{
		var test = " " + tags[i].className + " ";
		if (test.indexOf(tcl) != -1)
		{
			el[j++] = tags[i];
		}
	}
	return el;
}

// show one item in a group of objects and hide the rest (grouped by classname)
function switchid(id,cls)
{
	// create array of items
	var ids = getElementsByClass(cls);

	// loop and hide all items in array
	for(i=0; i<ids.length; i++)
	{
		ids[i].style.display = 'none';
	}

	// show an element with a specified id - safe for older browsers
	if (document.getElementById) // DOM3 = IE5, NS6
	{
		document.getElementById(id).style.display = 'block';
	}
	else
	{
		if (document.layers) // Netscape 4
		{
			document.id.display = 'block';
		}
		else // IE 4
		{
			document.all.id.style.display = 'block';
		}
	}
}

function getUrlVars(url) {
	var vars = [], hash;
	var hashes = url.slice(url.indexOf('?') + 1).split('&');
	 
	for(var i = 0; i < hashes.length; i++)
	{
		hash = hashes[i].split('=');
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}
	 
	return vars;
}

function displayRelatedProducts(index)
{
	var url = $$('#main div.thumbnail')[index].style.backgroundImage;
	var from = 4;
	var to = url.length - 1;
	var hash = getUrlVars(url.substring(from,to));
	var update = $('products');
	var headline = $('productImage');
	update.setStyle('height', update.offsetHeight)
	update.empty();
	if(headline) {
		if(headline.getStyle('display') != 'none')
			update.addClass('ajaxLoading');
	}

	// Find releated products
	(function(){
		var output = new Ajax(ajaxUrl + "?task=gallery&item=" + hash['item'], {
			method: 'get',
			update: update,
			onSuccess: function() {
				update.setStyle('height', 'auto');
				update.removeClass('ajaxLoading');
			},
			onFailure: function() {
				update.empty();
				update.removeClass('ajaxLoading');
			},
			onComplete: function() {
				if(headline) {
					if(update.innerHTML.length < 2)
						headline.setStyle('display', 'none');
					else
						headline.setStyle('display', 'inline');
				}
			}
		}).request();
	}.delay(700));
}