/*shop 13.07.2010*/
var param="shop";
var str=''; var count=0;var total=0;var items=new Array();
var emptytext='<h2>Ваша корзина пуста</h2>';//'Заказ и доставка лекарств<h2>502-08-19</h2>';

$(document).ready(function(){ 			
	str=$.cookie(param); 
	if(str!=null){
	var s=str.split("###");
	for(var i=0; i<s.length-1; i++){	
		items.push(s[i].split("!"));
		items[i][3]=parseInt(items[i][3]);
		count+=items[i][3];
		items[i].push(roundNumber(parseInt(items[i][3])*parseFloat(items[i][2]),2));
		total+=items[i][5];
	}
	}
	SetBasket();
});
function SetBasket(){	

		if(count==0) total=0;
	$('.basket').html(count==0?emptytext:"<a href='#' onclick='showorder()'>В вашей корзине</a><h4></h4><h5 class='total'></h5> <a href='/order' class='do'>Заказать</a>");	
	$('.basket h4').html(count+(count==1?" товар":count<5?" товара":" товаров")+" на сумму");
	$('.total').html(roundNumber(total,2)+" грн.");
}
function showorder(){
	$("#showorder").remove();
	var h=(document.height-500)/2>200?(document.height-500)/2:200;
	var w=(document.width-500)/2>200?(document.width-500)/2:200;
	$(document.body).append("<div style='position:absolute;z-index:111122;top:"+h+"px;left:"+w+"px;padding:3px;border:1px solid navy;background:#777;' id='showorder'></div>");
	$("#showorder").html("<h2 onclick='$(\"#showorder\").remove();' style='text-align:right;cursor:pointer;color:red'>X</h2><div style='padding:10px;background:#eee;'>"+GetOrder()+"</div>");
}

function clear(){
	str=$.cookie(param,null);
	count=0;
	SetBasket();
	$('.order').html("Ваш заказ принят. Вам перезвонят");
}
function GetOrder(){
	var str="<table class='order'><tr><th>Удалить</th><th>Товар</th><th class='hidden'>Произв-ль</th><th>Кол-во</th><th>Цена</th><th>Стоимость</th></tr>";
	for(var i=0; i<items.length; i++)
			if (items[i] != undefined)
		str+='<tr><td><a href="#" class="remove" onclick="remove('+i+')" id="remove'+i+'">x</a></td><td>'+items[i][1]+'</td><td class="hidden">'+items[i][4]+'</td><td>'+items[i][3]+'</td><td>'+items[i][2]+'</td><td>'+items[i][5]+' грн.</td></tr>';
	str+="<tr><td style='border:none' class='hidden'></td><td colspan='4' style='text-align:right;font-weight:bold;border:none'>Итого: </td><td class='total'>"+total+" грн.</td></tr></table>";
	return str;	
}

function add(id,title,price,firm){
	var col=1;var flag=true;
	if($('#col'+id).val()>0)
		col=parseInt($('#col'+id).val());
	for(i=0; i<items.length; i++)
		if(items[i] != undefined&&items[i][0]==id)	{
			items[i][3]+=col;items[i][5]=roundNumber(items[i][3]*items[i][2],2);
			flag=false;
			break;
		}
	if(flag)
		items.push(new Array(id,title,price,col,firm,roundNumber(parseFloat(price)*col,2)));	

	save();
	count+=col;	total+=roundNumber(col*price,2);
	SetBasket();
}
function save(){
	str='';
	for(var i=0; i<items.length; i++)
		if (items[i] != undefined)
			str+=items[i][0]+"!"+items[i][1]+"!"+items[i][2]+"!"+items[i][3]+"!"+items[i][4]+"###";
	$.cookie(param,str);
}
function remove(id){
	total-=items[id][5];
	count-=parseInt(items[id][3]);
	$("#remove"+id).parent().parent().remove();	
	delete items[id];
	save();
	SetBasket();
}
/*round*/
function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}
/*cookie*/
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
       
        document.cookie = name+"="+value+"; Path=/";
		
		} else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
				
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
