
iPay = {

	$products:null,
	Overlay:null,
	iPay_simpleCart_Engine:function(){
	
		var products_node_list = {};
		var products_list = {} ; 
		var productCheckouts_list = {} ; 
		$A($$('a[rel="ipay"]')).each(function(product,index) {
		
				if (
						product.hasAttribute('id') &&
						product.hasAttribute('href') &&
						product.hasAttribute('title') &&
						product.hasAttribute('price')
				    ){
						
						this[index] = $(product.id) ;
						
						var add_to_cart_button_id = 'add_item_button_'+ product.id;						
						var remove_from_cart_button_id = 'remove_item_button_'+ product.id;
						
						add_to_cart_button=new Element('a', { 'href': 'javascript:return this','id':add_to_cart_button_id,"style":"cursor:hand;cursor:pointer;font-weight:bold","class":"cart_icon"});
						add_to_cart_button.insert("add to cart");
						
						spacer=new Element('div',{ 'id': 'spacer','style':'padding:5px;text-align:center'});
						spacer.insert(add_to_cart_button);
						$(product.id+'_location').insert(spacer);

						
						$(product.id).onclick=function(){
							return true;
						}
						
						$(add_to_cart_button_id).observe('click', function(event){		
							          simpleCart.add( 'name='+product.readAttribute('title') , 'price='+product.readAttribute('price') , 'quantity=1');
									  alert("1 x " + product.readAttribute('title') + " @ "+ product.readAttribute('price') + " " + simpleCart.currency + " added to your order basket");
								}
								);	

							
															
					 }
						
					 },products_node_list);				
		
		this.$products=products_list;
					 
		$A($$('a[rel="ipayCheckout"]')).each(function(productCheckout,index) {
		
				if (
						productCheckout.hasAttribute('id') &&
						productCheckout.hasAttribute('currency')
				    ){
						

						$(productCheckout.id).observe('click', function(event){		
										var Dialog = new Element('div');
										iPay.Overlay= new DialogOverlay(Dialog);
										var iPayMerchant=(typeof iPayProfile!=undefined)?iPayProfile:"false"; 
										var iPayHome="/iorder/icartmanager.phtml?iPayProfile="+iPayMerchant;
											iPayHome+="&cart="+iPay.getCart();
										Dialog.insert("<div style='text-align:right;font-weight:bolder;font-size:20px;' class='close_canvas' id='canvas_close'><a href='#' onclick='iPay.Overlay.hide();'>[X]</a></div><OBJECT width='100%' height='100%'  style='height:500px' type='text/html' data='"+ iPayHome +"'></OBJECT>");
										iPay.Overlay.show(); 
								}
								);											
					 }
						
					 },productCheckouts_list);
		},
		
			
		runCart:function(){	
						simpleCart.initialize();						
						this.iPay_simpleCart_Engine();
		},
		
		getCart:function()
		{
		var c_name="simpleCart";
		
		if (document.cookie.length>0)
		  {
		  c_start=document.cookie.indexOf(c_name + "=");
		  if (c_start!=-1)
		    {
		    c_start=c_start + c_name.length+1;
		    c_end=document.cookie.indexOf(";",c_start);
		    if (c_end==-1) c_end=document.cookie.length;
		    cookie=unescape(document.cookie.substring(c_start,c_end));
			return(cookie);
		    }
		  }
		return "";
		}		

}

