// Cart Object
var _cart = new Cart();
var _items = new Items();

function Cart(){
	this.productList = null; // Unused
	this.count = 0;
	this.checkout = "/Wcc";
	this.isEmpty = true;
	this.tipTimer = null;
	this.intTimer = null;
	this.interval = 1000;
}

Cart.prototype.setData=function(checkout){
	this.checkout = checkout;
}

Cart.prototype.showCartList=function(flag){
	if(flag){
		if(this.isEmpty){
			this.getCartList();
			this.isEmpty = false;
		}
		$("#checkout").attr("className","cartover");
		$(".cart-lists").show();
	}else{	
		$("#checkout").attr("className","cart");
		$(".cart-lists").hide();
	}
}

Cart.prototype.getCartList=function(){
	var url = this.checkout+"/shopping.do";
	var d = "act=listitems"+"&callback=?";
    $.getJSON(url, d, function(data){
    	_cart.handleCartMsg(data);
    });		
}

Cart.prototype.handleCartMsg=function(data){
	var c = eval(data);
	if(c.error>=0){
		_cart.isEmpty = false;
	}
	$("#cartloading").hide();
	if(!c || c.error>0){
		$("#withprod").hide();
		$("#noprod").show();
		return;
	}
	$("#noprod").hide();
	$("#withprod").show();
	$("#hidden_prod_num").val(c.ptotalcount);
	$("#hidden_prod_price").val(c.ptotalprice);
	$("#prod_num").html(c.ptotalcount);
	$("#prod_price").html("$" + c.ptotalprice);	
	var thtml = "<table class='oltable'>";
	for(var i=0;i<c.plist.length;i++){
		thtml += "<tr id='tr_"+c.plist[i].pid+"'><td class='img' valign='top'><a href='"+c.plist[i].purl+"'><img src='";		
		thtml += c.plist[i].piconurl;
		thtml += "'/></a></td>";
		thtml += "<td class='info' valign='top'><a href='"+c.plist[i].purl+"'>";
		thtml += unescape(c.plist[i].pname).replace(/\+/g," ");
		thtml += "</a></td>";
		thtml += "<td class='price' valign='top'>";
		thtml += "$"+c.plist[i].pprice;
		thtml += "<br/>"+c.plist[i].pcount;
		thtml += "<br/><a href=\"javascript:_cart.delProduct('"+c.plist[i].pid+"','"+c.plist[i].id+"')\"><b>Remove</b></a>";
		thtml += "<input type='hidden' name='p_count_"+c.plist[i].pid+"' id='p_count_"+c.plist[i].pid+"' value='"+c.plist[i].pcount+"'/>";
		thtml += "<input type='hidden' name='p_price_"+c.plist[i].pid+"' id='p_price_"+c.plist[i].pid+"' value='"+c.plist[i].pprice+"'/>";
		thtml += "</td></tr>";
	}
	thtml += "</table>";
	$("#cartinfo").html(thtml);
	_cart.count = c.plist.length;
}

Cart.prototype.delProduct=function(pid,opid){
	var totalCount = $("#hidden_prod_num").val();
	var totalPrice = $("#hidden_prod_price").val();
	var pcount = $("#p_count_"+pid).val();
	var pprice = $("#p_price_"+pid).val();
	totalCount -= pcount;
	totalPrice -= pprice;
	$("#hidden_prod_num").val(totalCount);
	$("#hidden_prod_price").val(totalPrice);
	$("#prod_num").html(totalCount);
	$("#prod_price").html("$" + totalPrice.toFixed(2));
	$("#tr_" + pid).remove();
	if(totalCount <= 0){
		$("#withprod").hide();
		$("#noprod").show();
	}
	this.removeCartItem(opid);
}

Cart.prototype.removeCartItem=function(opid){
	var url = this.checkout+"/shopping.do";
	var data = "act=remove&opid="+opid+"&callback=?";
    $.getJSON(url, data, function(data){
    	_cart.handleChangeItem(data);
    });			
}

Cart.prototype.handleChangeItem=function(data){
	var c = eval(data);
	if(c.error > 0){
		return;
	}
	_cart.isEmpty = true;
	_cart.count = c.itemcount;
	$("#cart_itemcount").html(c.itemcount);
}

Cart.prototype.addCartItem=function(btn,sellerid,pid,ptype,pqty){
	if(!pid || !ptype || !pqty) return;
	this.initTipPosition('shopping_tip',btn);
	
	var url = this.checkout+"/shopping.do";
	var d = "act=shopping&sellerid="+sellerid+"&productid="+pid+"&producttype="+unescape(ptype)+"&quantity="+pqty+"&callback=?";
    $.getJSON(url, d, function(data){
    	_cart.handleChangeItem(data);
    	_cart.showTips(data,"shopping_tip");
    });	
    return true;	
}

Cart.prototype.showTips=function(data,id){
	var c = eval(data);
	var tip_img = "/images/tip_ok.gif";
	var tip_msg = "<font color='green'>Successfully added</font> <a href='"+this.checkout+"/checkout.do' style='color:green'>Checkout</a>";
	if(c.error > 0){
		tip_img = "/images/tip_fail.gif";
		tip_msg = "<font color='red'>Unsuccessfully added to cart</font>"		
	}
	$("#tip_img").attr("src",tip_img); 
	$("#tip_msg").html(tip_msg);
	$("#"+id).show();
	_cart.tipTimer = window.setTimeout("_cart.hideTip('"+id+"')", 2000);
}

Cart.prototype.hideTip=function(id){
	$("#"+id).hide();
	window.clearTimeout(_cart.tipTimer);
}

Cart.prototype.initTipPosition=function(id,btn){
	var pos = this.getPosition(btn);
	$("#"+id).css({left:pos.left+"px",top:(pos.top+btn.offsetHeight+4)+"px"});
}

Cart.prototype.getPosition=function(btn){
	var t=btn.offsetTop;   
	var l=btn.offsetLeft;   
	var height=btn.offsetHeight;   
	while(btn=btn.offsetParent){ 
		t+=btn.offsetTop;   
		l+=btn.offsetLeft;   
	}
	return {top:t,left:l};
}

Cart.prototype.showCartItemCount=function(domain){
	this.count = this.getCartItemCount(domain);
	this.setCartItemCount(this.count);
}

Cart.prototype.setCartItemCount=function(count){
	$("#cart_itemcount").html(this.count+"");
}

Cart.prototype.getCartItemCount=function(domain){
	var co = new wck("shscn",domain);
	var c = co.getck();
	var acvl;
	var itemcount = 0;
	if(c){
		acvl = c.split("|");	
		if(acvl.length>=7) {
			itemcount = acvl[6];
		}		
	}
	return itemcount;
}

Cart.prototype.detectCartChange=function(domain){
	var c = this.getCartItemCount(domain);
	if(c != this.count){
		this.isEmpty = true;
		this.count = c;
		this.setCartItemCount(this.count);
	}
}

Cart.prototype.createDetectTimer=function(domain){
	this.intTimer = window.setInterval("_cart.detectCartChange('"+domain+"')", this.interval);
}

Cart.prototype.buyNow=function(a){
	var url = this.checkout+"/ecobnSetup.do";
	url = url + "?sellerid=" + $("#sellerid").val();
	url = url + "&productid=" + $("#productid").val();
	url = url + "&producttype=" + $("#producttype").val();
	url = url + "&quantity=" + $("#quantity").val();
	url = url + "&paymentAmount=" + $("#paymentAmount").val();
	window.location = url;
}



// Items
function Items(){
	this.tipTimer = null;
	this.interval = 3000;
}

Items.prototype.showItem=function(btn,sd,pd,url){
	//if(!sd || !pd || !url) return;
	this.initTipPosition('shopping_tip',btn);
	
	var d = "act=show&id="+pd+"&sellerid="+sd+"&callback=?";
    $.getJSON(url, d, function(data){
    	_items.showTips(data,"shopping_tip");
    });		
}

Items.prototype.hideItem=function(btn,sd,pd,url){
	//if(!sd || !pd || !url) return;
	this.initTipPosition('shopping_tip',btn);
	
	var d = "act=hide&id="+pd+"&sellerid="+sd+"&callback=?";
    $.getJSON(url, d, function(data){
    	_items.showTips(data,"shopping_tip");
    });		
}

Items.prototype.downItem=function(btn,sd,pd,url){
	//if(!sd || !pd || !url) return;
	this.initTipPosition('shopping_tip',btn);
	
	var d = "act=down&id="+pd+"&sellerid="+sd+"&callback=?";
    $.getJSON(url, d, function(data){
    	_items.showTips(data,"shopping_tip");
    });		
}

Items.prototype.dtItem=function(btn,sd,pd,url){
	//if(!sd || !pd || !url) return;
	this.initTipPosition('shopping_tip',btn);
	
	var d = "act=dt&id="+pd+"&sellerid="+sd+"&callback=?";
    $.getJSON(url, d, function(data){
    	_items.showTips(data,"shopping_tip");
    });		
}

Items.prototype.showTips=function(data,id){
	var c = eval(data);
	var tip_img = null;
	var tip_msg = null;
	if( c.error == 0 ){
		tip_img = "/images/tip_ok.gif";
		tip_msg = "<font color='green'>"+c.msg+"</font>";
	}else{
		tip_img = "/images/tip_fail.gif";
		tip_msg = "<font color='red'>"+c.msg+"</font>";
	}
	$("#tip_img").attr("src",tip_img);
	$("#tip_msg").html(unescape(tip_msg));
	$("#"+id).show();
	_items.tipTimer = window.setTimeout("_items.hideTip('"+id+"')", _items.interval);
}

Items.prototype.hideTip=function(id){
	$("#"+id).hide();
	window.clearTimeout(_items.tipTimer);
}

Items.prototype.initTipPosition=function(id,btn){
	var pos = this.getPosition(btn);
	$("#"+id).css({left:pos.left+"px",top:(pos.top+btn.offsetHeight+4)+"px"});
}

Items.prototype.getPosition=function(btn){
	var t=btn.offsetTop;   
	var l=btn.offsetLeft;   
	var height=btn.offsetHeight;   
	while(btn=btn.offsetParent){ 
		t+=btn.offsetTop;   
		l+=btn.offsetLeft;   
	}
	return {top:t,left:l};
}

