var cartdialog = {};

cartdialog.default_modal_posandwidth = {width: 800,top:150};

cartdialog.modes = {
		menu:{id:'menu',loadurl:'/overlay/cartmenu/',modalmode:cartdialog.default_modal_posandwidth},
		init:{id:'init',loadurl:'/overlay/cartinit/',modalmode:cartdialog.default_modal_posandwidth},
};

cartdialog.currentmode = cartdialog.modes.init;

cartdialog = $.extend({
	productid: 0,
	attributeid: 0,
	amount: 0,
	price: 0.00,
	comment:'',
	unique:0,
	refresh_on_exit:false,
	
	
	Loader: {
		start: function(){}, 
		done: function(){}
	},
	
	OnExitModal: function(){
		//this.Post();
		if(cartdialog.refresh_on_exit)
			location.reload();
	},
	
	ShowModal: function(){
		//close old one
		//$('#closeimg').trigger('click');
		
		//open new one
		var options = {productid:this.productid,attributeid:this.attributeid,mode:this.currentmode.id,number:this.amount};
		var overlay_params = {options:options,scroll:false,boxtop:this.currentmode.modalmode.top,boxwidth: this.currentmode.modalmode.width,contenturl:  baseurl + this.currentmode.loadurl};
		
		//if(checkoutcart)
		overlay_params.onclosehandler = this.OnExitModal;
			
		$("body").overlay(overlay_params);
	},
	
	ChangeMode: function(mode){
			this.currentmode = this.modes[mode];
			this.ShowModal();
	},
	
	
	Init: function(productid,attributeid,price,amount,refresh_on_exit){
		this.Empty();
		this.productid = productid;
		this.attributeid = attributeid;
		this.amount = amount;
		this.price = price;
		this.currentmode = this.modes.init;
		this.unique = 'dummy' + uniqid();
		this.refresh_on_exit = refresh_on_exit;
		this.comment = '';
		//alert(this.currentmode.id);
		this.ShowModal();
	},
	
	//bring back empty state
	Empty: function(){
		this.productid = 0;
		this.attributeid = 0;
		this.amount = 0;
		this.price = 0.00;
		this.currentmode = this.modes.init;
		this.comment = '';
		this.unique = 0;
	},
	
	
	IsValid: function(){
		return (this.amount > 0 && this.productid != 0);
	},
	
	SaveToCart: function(){
		shoppingcart.saveCookie();
		LoadCartSummary();
	},
	
	
	
	PostProduct: function(){
		if(this.amount > 0 && this.productid != 0)
		{
			  var options = {};
			  
			  if(!empty(this.comment))
			  {
			  	options['comment'] = this.comment;
					shoppingcart.addProduct(this.productid,this.unique,this.amount,this.price,'','',options);
				}
				else
					shoppingcart.addProduct(this.productid,this.attributeid,this.amount,this.price,'','',options);
				this.SaveToCart();
		}
	},
	
	
	
	//post results to server
	Post: function(){
		
		//options = {label:{productid:this.label.productid,attributeid:unique},wrapper:{productid:this.wrapper.productid,attributeid:unique},card:{productid:this.card.productid,attributeid:unique}};
		var options = {};
		
		if(this.IsValid())
		{
				this.PostProduct();
		}
		//else 
		//	console.log('dialog posted but no valid params');
	}	
},cartdialog || {});

	

