// global variables to hold the timer on the search box (otherwise we cant reset the search)
var t = 0;
var buttonelement;
var f = 0;



var shoprules = {

	'#cartdisplaylink' : function(el)
	{
		el.onclick = function() {
			if ($('cart').style.display == 'none') {
				Element.show('cart');
				
				//new Effect.Appear('cart', {duration: 0.5, delay: 0}); 
				el.innerHTML = 'Hide Cart';
			}
			else {
			
				Element.toggle($('cart'));
				el.innerHTML = 'View Cart / Proceed to checkout';
			}	
			return false;
		}
		
	}
	,
	'#useotheradd' : function(el)
	{
		el.onclick = function(){
				Element.toggle($('delfields'));
		}
	}
	,
	'.newwindow' : function(el)
	{
		el.onclick = function()
		{
		window.open(el.href);
		return false;
		}
	}
	,
	'#shop #cart #deleteall' : function(el){
		el.onclick = function(){
				clearCart();
				return false;
				
		}
	},
	
	
	'.addform' : function(el){
		
		if (el.lastChild)
		{ 
			el.lastChild.onclick = function(){
		
			var select = el.firstChild.nextSibling.nextSibling.nextSibling;
			
			if (select.name == 'variation')
				select = select.value
			else
				select = ''; 
	
			addToCart(el.firstChild.nextSibling.nextSibling.value, el.firstChild.nextSibling.value, select)
			
			if ($('cart').style.display == 'none') {
				
				Element.show('cart');
				
				//new Effect.Appear('cart', {duration: 0.5, delay: 0}); 
				$('cartdisplaylink').innerHTML = 'Hide Cart';
			}
			
			return false;
		}	
		}
	},
	
	'.removeform .button' : function(el){
	
		if (!$('cartwide')) {
			el.onclick = function(){
					removeFromCart(el.parentNode.firstChild.nextSibling.value, el.parentNode.firstChild.nextSibling.nextSibling.value)
					return false;
			
			}
		}
	},
	'#processbutton' : function(el){
		el.onclick = function(){
				el.value = 'Processing...';
			
		}
	},
	'#searchtext' : function(el){
		el.onkeyup = function(){
			
			if (el.value.length > 2)
			{
				$('searchresults').innerHTML = '<div id="spinner"><p>Searching..</p></div>'; 		
				clearTimeout(t);
				t=setTimeout("search('" + el.value +"')", 500)
			}
			return false;
				
		}
	},
	
	'#searchbutton' : function(el){
		el.style.visibility = 'hidden';
		
	}
	,
	
	'#l2navigator' : function(el){
		el.onmouseover = function(){	
			$('l2navigator-menu').style.display= 'block';
		}
		
		
	},
	
	'#l2navigator-menu' : function(el){
		el.onmouseout = function(){	
			el.style.display= 'none';
		}
		
	}
	,
	
	'.prodimagethumb' : function(el){
		el.onclick = function(){	
		var currentimage = $('productimage').src;
		
		$('productimage').src = el.href;
		el.href = currentimage;
		el.firstChild.src = currentimage.substring(0, currentimage.lastIndexOf('/')) + '/thumbs/' + currentimage.substring(currentimage.lastIndexOf('/'), currentimage.length);
		
		return false;
		}
		
	}
	,
	'#cart' : function(el){
		//el.style.display = 'none';
		
	}
		
};


function reRegister()
{
	faderesponse();
	Behaviour.apply();
}


function faderesponse()
{
	
	if (document.getElementById('response')) {
		clearTimeout(f);
		f = setTimeout("hideReponse()", 4000);
	
	}
		
	//new Effect.Fade('response', {duration: 0.5, delay: 4.0}); 
}

function hideReponse()
{
	Element.hide('response');
}

function returnToDefault(productform, valuein)
{
	buttonelement = $(productform).lastChild;
	buttonelement.value = valuein;
}


function addToCart(item, qty, variation) {
	var url = 'index.php';
	var pars = 'action=additemcomm&product_ID='+item+', &quantity='+qty+'&variation='+variation;
	
	var myAjax = new Ajax.Updater( 'cart', url, { method: 'post', parameters: pars, onComplete: reRegister }); 	
	
	var productform = "productform" + item;
	buttonelement = document.getElementById(productform).lastChild;
	var defaultValue = 'Add';
	buttonelement.value = 'Added to cart';
	
	setTimeout('returnToDefault( \'' + productform + '\', \'' + defaultValue + '\')',3000);
	
}


function removeFromCart(item, variation) {
	var url = 'index.php';
	var pars = 'action=removeitemcomm&product_ID='+item+'&variation='+variation;
	var myAjax = new Ajax.Updater( 'cart', url, { method: 'post', parameters: pars, onComplete: reRegister }); 
}

function clearCart() {
	var url = 'index.php';
	var pars = 'action=clearcartcomm';
	var myAjax = new Ajax.Updater( 'cart', url, { method: 'post', parameters: pars, onComplete: reRegister }); 
}


function search(querystring) {
	var url = 'index.php';
	var pars = 'action=searchcomm&searchtext=' + querystring;
	var myAjax = new Ajax.Updater( 'searchresults', url, { method: 'post', parameters: pars, onComplete: reRegister }); 

}


function updateCustomer(form) {
	var args = '&ID=' + form.ID.value +
	'&firstname=' + form.firstname.value +
	'&surname=' + form.surname.value +
	'&address1=' + form.address1.value +
	'&address2=' + form.address2.value +
	'&suburb=' + form.suburb.value +
	'&state=' + form.state.value +
	'&postcode=' + form.postcode.value +
	'&country=' + form.country.value +
	'&telephone=' + form.telephone.value +
	'&mobile=' + form.mobile.value +
	'&facsimile=' + form.facsimile.value +
	'&email=' + form.email.value;
	
	var url = 'index.php';
	var pars = 'action=editcustomercomm' + args;
	var myAjax = new Ajax.Updater( 'custdetails', url, { method: 'post', parameters: pars, onComplete: reRegister }); 
}

function hideCart()
{
	if (document.getElementById('cartdisplaylink') != null)
	Element.hide($('cart'));
}


Behaviour.addLoadEvent(faderesponse);
Behaviour.addLoadEvent(hideCart);

Behaviour.register(shoprules);

function CurrencyPopup(QueryString) {
    CurrencyWindow = window.open ('', 'CurrencyWindow', 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,height=130,width=500');
    CurrencyWindow.focus();
    CurrencyWindow.location.href = 'http://www.xe.net/ecc/input.cgi?Template=sw&'+QueryString;
} 	
	
