TGP.Cart = { 
	Cart:null,
	Shop:null,
	VPHeight:null,
	Items:{},
	LoadingDialog:null,
	init:function(){ 
		this.Cart = $('CartOuter'); 
		this.Shop = $('Shop'); 
		TGP.Cart.Place();
		this.Items = TGP.Init.initData.CartItems; 
		E.addListener(window,'resize',function(){TGP.Cart.Place()});
		E.addListener('AddToCart','click',this.Listeners.DoAddToCart); 
		E.addListener('GoToCart','click',function(){ window.location.href='/cart'; }); 
		E.addListener(Dom.getElementsByClassName('cartitemimage'),'mouseover',this.Listeners.ShowCartItemHover); 
		E.addListener(Dom.getElementsByClassName('cartitemimage'),'mouseout',this.Listeners.HideCartItemHover); 
		E.addListener(Dom.getElementsByClassName('removelink'),'click',this.Listeners.RemoveItem); 
		E.addListener(Dom.getElementsByClassName('addtocart_multi'),'click',this.Listeners.DoAddToCart); 
	},
	Place:function(){ 
		var difference = parseInt(Dom.getViewportHeight()) - parseInt(Dom.getStyle(this.Cart,'height'))-10; 
		Dom.setStyle(this.Shop,'height',difference+'px');
	},
	Add:function(productid,qty){ 
		if(qty==undefined){ qty = 1; }
				var oOptions = { width: "180px", 
						effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.25}, 
						fixedcenter:true,
						modal:true,
						close:false,
						visible:false,
						draggable:false
					   };

		this.LoadingDialog = new YAHOO.widget.SimpleDialog("AddToCartLoading", oOptions);							
		this.LoadingDialog.setHeader("");
		this.LoadingDialog.setBody("<center><img src=\"/js/yui28/assets/skins/sam/wait.gif\" width=\"18\" height=\"18\" style=\"vertical-align:middle; margin-top:-5px;\" /> Adding to your cart</center>");
		this.LoadingDialog.render(document.body);
		this.LoadingDialog.show(); 
		
				var Txn = YAHOO.util.Connect.asyncRequest(	'POST', 
																								'/shop/addtocart/', 
																								{success:this.Listeners.PostAddToCart,
																								failure:this.Listeners.AddToCartProblem }, 
																								'conduit=json&qty='+qty+'&product_id='+productid); 

	},
	Listeners:{ 
		ShowCartItemHover:function(){ 
			var id = this.id.split("_")[1];
			$('cartpopupcopy').innerHTML = '<h2>'+TGP.Cart.Items[id][0]+'</h2>'+
																		'<p>Price: '+TGP.Cart.Items[id][1]+'</p>';
//																	'<br /><i>Click the thumbnail to view the product page</i></p>'; 
			var aXY = Dom.getXY(this); 
			var width = parseInt(Dom.getStyle(this,'width')); 
			var height = parseInt(Dom.getStyle(this,'height')); 
				var targetlocationX = aXY[0]-45;
				var targetlocationY = aXY[1]-90;
			Dom.setStyle($('CartHover'),'display',''); 
			Dom.setXY($('CartHover'),[targetlocationX,targetlocationY]); 
		},
		HideCartItemHover:function(){ 
			Dom.setStyle($('CartHover'),'display','none'); 
		},
		RemoveItem:function(){ 
			var rowid = this.id.split("_")[1]; 
				var Txn = YAHOO.util.Connect.asyncRequest(	'POST', 
																								'/shop/removeitem/', 
																								{success:TGP.Cart.Listeners.PostRemoveFromCart,
																								failure:TGP.Cart.Listeners.RemoveFromCartProblem }, 
																								'conduit=json&rowid='+rowid); 

		}, 
		DoAddToCart:function(){
			if(this.id == 'AddToCart'){ 
				var i = $('AddProductId').value; 
				var q = $('AddProductQty').value; 
				TGP.Cart.Add(i,q); 
			} else { 
				var id = this.id.split('_')[1]; 
				var i = $('AddProductId_'+id).value; 
				var q = $('AddProductQty_'+id).value; 
				TGP.Cart.Add(i,q); 
			} 
		},
		PostRemoveFromCart:function(o){ 
		//	try{ 
				var R = YAHOO.lang.JSON.parse(o.responseText);
				if(R.ok){ 
						var A = R.response; 
						
						var countExisting = Dom.getElementsByClassName('item'); 
						var startMovinAt; 
						for(var a=0;a<countExisting.length;a++){ 
							if(countExisting[a].id == 'Item_'+A.itemremoved){
								startMovinAt = a; 
								break; 
							} 
						} 
							
						$('CartStuff').removeChild($('Item_'+A.itemremoved)); 
						var newobj = new Object(); 
						for(var i in TGP.Cart.Items){ 
							if(i!=A.itemremoved){ 
								newobj[i] = TGP.Cart.Items[i]; 
							} 
						}
						TGP.Cart.Items = newobj;
						
						var thisnewleft; 
						var animations = new Array(); 
						
						for(var b=(startMovinAt);b<countExisting.length;b++){ 
							thisnewleft = parseInt(Dom.getStyle(countExisting[b],'left'))-80; 
							animations[animations.length] = new YAHOO.util.Anim(countExisting[b],{left:{to:thisnewleft}},0.4); 
						 }
						

							if(animations.length>0){ 
								for(var c=0;c<animations.length;c++){ 
									animations[c].animate(); 
								} 
							}



						
						if(A.cart_item_count>0){ 
							$('CartLanguage').innerHTML = 'Your Cart Has <span>'+A.cart_item_count+'</span> Item'+((A.cart_item_count>1)?'s':'');
							$('CartIcon').src = '/images/carticon_happy.png'; 
							Dom.setStyle($('GoToCart'),'display',''); 
						} else { 
							$('CartLanguage').innerHTML = 'Aww, Your Cart Is Empty';
							$('CartIcon').src = '/images/carticon_sad.png'; 
							Dom.setStyle($('GoToCart'),'display','none'); 
						} 
				} else { 
					TGP.Alert('warn','Egad!','The server could not remove this item from your cart. Try reloading this page or trying again later.'); 
				} 
			// } catch(err){ 
			// 	TGP.Alert('warn','Egad! There was a server problem','Our server is speaking nonsense, and we\'re not exactly sure what it just did. Try reloading this page and your cart will be up to date.'); 
			// }
		},
		
		PostAddToCart:function(o){ 
		//	try{ 
			TGP.Cart.LoadingDialog.hide(); 
				var R = YAHOO.lang.JSON.parse(o.responseText);
				if(R.ok){ 
						var A = R.response; 

						if(A.already_in_cart){ 
							$('Qty_'+A.rowid).innerHTML = A.product_qty+((A.product_qty>1)?' copies':' copy');
						} else { 

							var countExisting = Dom.getElementsByClassName('item'); 
							var thisnewleft; 
							var animations = new Array(); 
							for(var a=0;a<countExisting.length;a++){ 
								thisnewleft = parseInt(Dom.getStyle(countExisting[a],'left'))+80; 
								animations[a] = new YAHOO.util.Anim(countExisting[a],{left:{to:thisnewleft}},0.4); 
							 }
				
							var cont = document.createElement('div'); 
									cont.id = 'Item_'+A.rowid;
									cont.className = 'item'; 
								
							//		Dom.setStyle(cont,'left',((countExisting.length*80)+10)+'px'); 
									Dom.setStyle(cont,'left','7px'); 
									Dom.setStyle(cont,'top','-100px'); 
									Dom.setStyle(cont,'opacity','0'); 
						
						
							var a = document.createElement('a'); 
									a.href="/game/"+A.link;

							var img = new Image(); 
									img.src = '/images/products/'+A.product_thumb
									img.id = 'ItemImg_'+A.rowid;
									img.width=52;
									img.height=40; 
						
							a.appendChild(img); 
						
							cont.appendChild(a); 
						
							var span = document.createElement('span'); 
									span.id = 'Qty_'+A.rowid
									span.innerHTML = A.product_qty+((A.product_qty>1)?' copies':' copy');
						
							var a2 = document.createElement('a'); 
									a2.href="javascript:void(0);";
									a2.className="removelink";
									a2.id="Remove_"+A.rowid;
									a2.innerHTML = 'X';
						
							cont.appendChild(a2); 

							cont.appendChild(span); 
							if(animations.length>0){ 
								for(var b=0;b<animations.length;b++){ 
									animations[b].animate(); 
								} 
							}
							var down = new YAHOO.util.Anim(cont,{top:{to:7 },opacity:{to:1}},0.4); 
							down.animate(); 

							TGP.Cart.Items[A.rowid] = [A.product_name,A.product_price,A.product_qty,A.item_total]
						
							E.addListener(img,'mouseover',TGP.Cart.Listeners.ShowCartItemHover); 
							E.addListener(img,'mouseout',TGP.Cart.Listeners.HideCartItemHover); 
							E.addListener(a2,'click',TGP.Cart.Listeners.RemoveItem); 
						
							Dom.insertBefore(cont,$('CartStuff').childNodes[0])
						
				//			$('CartStuff').appendChild(cont); 
						}
						$('CartLanguage').innerHTML = 'Your Cart Has <span>'+A.cart_item_count+'</span> Item'+((A.cart_item_count>1)?'s':'');
						$('CartIcon').src = '/images/carticon_happy.png'; 
						Dom.setStyle($('GoToCart'),'display',''); 
					
				} else { 
					TGP.Alert('warn','Egad!','The server could not put this item in your cart. Try reloading this page or trying again later.'); 
				} 
			// } catch(err){ 
			// 	TGP.Alert('warn','Egad! There was a server problem','Our server is speaking nonsense, and we\'re not exactly sure what it just did. Try reloading this page and your cart will be up to date.'); 
			// }
		},
		AddToCartProblem:function(){ 
			TGP.Alert('warn','Egad! There was a server problem','For some reason the cart can\'t communicate with the server. Try reloading this page, checking your internet connection, or spinning around in circles. That last one won\'t fix the server, but it\'s a fun game to play until we get you the one you just tried to buy.'); 
		},
		RemoveFromCartProblem:function(){ 
			TGP.Alert('warn','Egad! There was a server problem','For some reason the cart can\'t communicate with the server. Try reloading this page, checking your internet connection, or spinning around in circles. That last one won\'t fix the server, but it\'s a fun game to play until we get you the one you just tried to buy.'); 
		} 
	} 
} 
TGP.Init.initThese.push("Cart"); 
