function addOnloadEvent(fnc){
  if ( typeof window.addEventListener != "undefined" )
    window.addEventListener( "load", fnc, false );
  else if ( typeof window.attachEvent != "undefined" ) {
    window.attachEvent( "onload", fnc );
  }
  else {
    if ( window.onload != null ) {
      var oldOnload = window.onload;
      window.onload = function ( e ) {
        oldOnload( e );
        window[fnc]();
      };
    }
    else 
      window.onload = fnc;
  }
}

	// replace function
function ReplaceText(text)
{
	var word;
	for (word in replacer)
	    text = text.replace(replacer_re[word], replacer[word]);
	return text;
	}

	// DOM recursing function
	function ReplaceTextRecursive(element)
	{
	  if (element.childNodes)
	  {
	    var children = element.childNodes;
	    for (var i = children.length - 1; i >= 0; i--)
	      ReplaceTextRecursive(children[i]);
	  }    

	  if (element.nodeType == 3) // 3 == TEXT_NODE
	    element.nodeValue = ReplaceText(element.nodeValue);
	}
addOnloadEvent(function(){ initSite(); firstRun(); });

function firstRun()
{
	if (document.getElementById("searchQuery"))
	{
		addAction("searchQuery", "focus", function(){clValue(this);});
		addAction("searchQuery", "blur", function(){inValue(this);});
	}
	if (document.getElementById("featured"))
	{
		startscroll();
	}
}			

function startscroll()
{
	var y = document.getElementById('selector').getElementsByTagName('li');
	var ylen = y.length, index = 0;
	y[0].className = 'current';
	revbox = window.setInterval(function() { y[index].className = ''; index = (index + 1) % ylen; y[index].className = 'current';}, 5000);
}

function selectCurrency()
{
	createCookie("price", document.getElementById("selectCurrency").value, 14);
	window.location.reload();	
}

function initSite()
{
	var curUSD = new Array("99¢", "$1.99", "$2.99", "$3.99", "$4.99", "$5.99", "$6.99", "$7.99", "$8.99", "$9.99", "$10.99", "$11.99", "$12.99", "$13.99", "$14.99", "$15.99", "$16.99", "$17.99", "$18.99", "$19.99", "$20.99", "$21.99", "$22.99", "$23.99", "$24.99", "$25.99", "$26.99", "$27.99", "$28.99", "$29.99");
	var curAUD = new Array("$1.19", "$2.49", "$3.99", "$4.99", "$5.99", "$7.99", "$8.99", "$9.99", "$11.99", "$12.99", "$13.99", "$14.99", "$15.99", "$16.99", "$17.99", "$18.99", "$19.99", "$21.99", "$22.99", "$23.99", "$24.99", "$26.99", "$27.99", "$28.99", "$29.99", "$31.99", "$32.99", "$33.99", "$34.99", "$36.99");
	var curEUR = new Array("79c", "€1.59", "€2.39", "€2.99", "€3.99", "€4.99", "€5.49", "€5.99", "€6.99", "€7.99", "€8.99", "€9.99", "€10.49", "€10.99", "€11.99", "€12.99", "€13.99", "€14.49", "€14.99", "€15.99", "€16.99", "€17.99", "€18.49", "€18.99", "€19.99", "€20.99", "€21.49", "€21.99", "€22.99", "€23.99");
	var p = readCookie("price");
	if (p)
	{
		switch (p)
		{
				case "usd":
				cur = curUSD;
				break;
				case "aud":
				cur = curAUD;
				break;
				case "eur":
				cur = curEUR;
				break;
				case "gbp":
				return;
				break;
		}
	}
	else
	{
		createCookie("price", "usd", 14);
		cur = curUSD;
	}
	replacer = {
				"59p": cur[0],
				"£1.19": cur[1],
				"£1.79": cur[2],
				"£2.39": cur[3],
				"£2.99": cur[4],
				"£3.49": cur[5],
				"£3.99": cur[6],
				"£4.99": cur[7],
				"£5.49": cur[8],
				"£5.99": cur[9],
				"£6.49": cur[10],
				"£6.99": cur[11],
				"£7.49": cur[12],
				"£7.99": cur[13],
				"£8.99": cur[14],
				"£9.49": cur[15],
				"£9.99": cur[16],
				"£10.99": cur[17],
				"£11.49": cur[18],
				"£11.99": cur[19],
				"£12.49": cur[20],
				"£12.99": cur[21],
				"£13.99": cur[22],
				"£14.49": cur[23],
				"£14.99": cur[24],
				"£15.49": cur[25],
				"£15.99": cur[26],
				"£16.99": cur[27],
				"£17.49": cur[28],
				"£17.99": cur[29]
			};
			// prepare regex cache
			replacer_re = (function ()
			  {
			    var replacer_re = {};
			    var re_specials = '/[][/.*+?|(){}\\\\]/g';
			    var word;
			    for (word in replacer)
			    {
			      var escaped_word = word.replace(re_specials, "\\\1");
			      replacer_re[escaped_word] = new RegExp(escaped_word, "g");
			    }
			    return replacer_re;
			  }
			)();
	if (p != "gbp") { ReplaceTextRecursive(document.body); }
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function addAction (vId,sType,fnHandler)
{
	var oId = ((typeof vId)=="string") ? document.getElementById(vId) : vId;
	if (oId.addEventListener)
	{
		oId.addEventListener(sType,fnHandler,false);
	}
	else // IE
	{
		oId.attachEvent("on"+sType,fnHandler);
	}
}

/* Clear & insert values into forms
------------------------------------ */
function clValue(input)
{
	if (input.value==input.defaultValue) input.value="";
}

function inValue(input)
{
	if (input.value=="") input.value = input.defaultValue;
}