function addLoadEvent(func){
	var oldonload = window.onload;
	if(typeof window.onload != 'function'){
		window.onload = func;
	} else {
		window.onload = function(){
			oldonload();
			func();
		}
	}	
}


addLoadEvent(showHidden);
addLoadEvent(contactBox);
addLoadEvent(optionsBox);
addLoadEvent(cart);
addLoadEvent(addToCart);

function showHidden(){
		
}

function optionsBox(){
	//new Effect.Opacity('refine', { from: 0, to: 1 }); 
	//$('products').style.visibility = 'visible';
	//Effect.BlindDown('products');
	//Effect.Grow('products');return false;
}

function contactBox(){
	$('contact_you').value = 'Phone No. or Email';
	$('contact_you').onfocus= function(){
		if(this.value=='Phone No. or Email')this.value='';
	}
	
	$('details_more').value = "Regarding? Best time?";
	$('details_more').onfocus = function(){
		if(this.value=='Regarding? Best time?')this.value='';
	}
	
	$('details').value = "Regarding? Best time?";
	$('details').onfocus = function(){
		if(this.value=='Regarding? Best time?')this.value='';
		if($('details_more_text').value=='Regarding? Best time?') $('details_more_text').value='';
	}	
	
	$('more_space').onclick= function(){Effect.BlindUp('details_less'); Effect.BlindDown('details_more'); $('details').value='';return false;};
	$('less_space').onclick= function(){Effect.BlindUp('details_more'); Effect.BlindDown('details_less'); return false;};
	$('details_more').hide();  	
} 

function cart(){
	var products = document.getElementsByClassName('product');	
	for (var i = 0; i < products.length; i++) {
		new Draggable(products[i].id, {ghosting:true, revert:true})	
	}
	//detail page
	var prod = document.getElementsByClassName('drag');
	for (var x = 0; x < prod.length; x++) {
		new Draggable(prod[x].id, {ghosting:true, revert:true})		
	}
	Droppables.add('cart', {onDrop:addProduct})
	return;
}
function addProduct(element, dropon, event) {
	sendData(element.id);
}

function sendData (prod, option) {
	if(option > 0){
		var product = option;	
		var no_option = '/false'
	} else {
		var product = prod;	
		var no_option = '/true'
	}
	var url    = 'http://www.rosieandwellington.com/index.php/app/index/cart/' + product + no_option;
	var myAjax = new Ajax.Request( url, {onLoading: showLoad, onComplete: showResponse} );
}	
function lessProduct (id) {
	var url    = 'http://www.rosieandwellington.com/index.php/app/index/cart/clear/' + id;
	var myAjax = new Ajax.Request( url, {onLoading: showLoad, onComplete: showResponse} );
}

function clearCart () {
	var url    = 'http://www.rosieandwellington.com/index.php/app/index/cart/clear';
	var myAjax = new Ajax.Request( url, {onLoading: showLoad, onComplete: showResponse} );
}
function showResponse (originalRequest) {
	//$('loading').style.display = "none";
	$('cart_heading').update('Shopping Basket');
	//$('clearCart').style.display = "block";
	$('cart_items').update(originalRequest.responseText);
}
function showLoad () {
	//$('clearCart').style.display = "none";
	$('cart_heading').update('Loading');
	//$('loading').style.display = "block";
}

function addToCart(){
	$('click_to_add').onclick = function(){
		$('cart').style.display = 'block';
		var option = $F('product_option');
		sendData($F('product_id'), option);	
		//$('added').style.display = 'inline';
		//$('click_to_add').style.display = 'none';
		return false;
	}
}