function Cart(){};

Cart._buld_params_array = function (id, qty, attrs){
	var p = {};
	if ('undefined' != typeof(id))
		p['id'] = id;
	if ('undefined' != typeof(qty))
		p['qty'] = qty;
	if ('undefined' != typeof(attrs)) {
		for (key in attrs) {
			p['attrs['+key+']'] = attrs[key];
		}
	}
	return p;
}

Cart.find_attrs = function (el){	
	var block_id = $(el).parents().filter(".cart_selector:first").attr("id");
	var attrs = [];
	
	$("#"+block_id+" .cart_attr").each(
			function (){
				var attr_id = $(this).attr('attr_id');
				var val = $(this).attr('value');
				if ('undefined' != typeof(val))
					if (eval(val)) {
						attrs[attr_id] = val;
					}
			})
	
	return attrs;
}


Cart.find_qty = function (el){
	var block_id = $(el).parents().filter(".cart_selector:first").attr("id");	
	var qty = eval($("#"+block_id+" input.cart_qty").attr('value'));
	if ('undefined' == typeof(qty))
		qty = 1;
	return qty;
}

Cart.set_qty = function (el, qty){
	var block_id = $(el).parents().filter(".cart_selector:first").attr("id");
	$("#"+block_id+" input.cart_qty").attr('value', qty);
}


Cart.find_goods_id = function (el){
	return $(el).parents().filter('.cart_selector:first').attr('goods_id');
}

Cart.find_goods_key = function (el){
	return $(el).parents().filter('.cart_selector:first').attr('goods_key');
}

Cart.change_qty = function(el, modifier, instant_update){	
	var qty = Cart.find_qty(el);
	var attrs = Cart.find_attrs(el);
	var new_qty;
	new_qty = qty + modifier;
	if (new_qty > 0) {
		Cart.set_qty(el, new_qty);
		if ('undefined' != typeof(instant_update)) {
			var goods_key = Cart.find_goods_key(el);
			if ('undefined' != typeof(goods_key)) {				
				Cart.update_qty(goods_key, new_qty);
			} 			
		}
	}
}



Cart.clear_n_push = function (el){
	var caller_el = el;
	$('div#cart').load(Cart.url()+'a.clear', {}, function(){Cart.add(caller_el)});
}

Cart.packing_add = function(el){
	Cart.add(el,packing_size);
}

Cart.add = function(el,packing_size){
	var goods_id = Cart.find_goods_id(el);
	if('undefined' == typeof(packing_size)) packing_size=1;
	var qty = Cart.find_qty(el)*packing_size;
	var attrs = Cart.find_attrs(el);
	var caller_el = el;
	if (qty > 0) {
		$.post(Cart.url()+'a.add', Cart._buld_params_array(goods_id, qty, attrs),
				function(d, st){
					if (d.status == 'ok') {
						Cart.update_widget();
						Console.notice('Товар добавлен в корзину');
					} else {
						if (d.status == 'incompatible') {
							if (confirm(d.msg)) {
								Cart.clear_n_push(caller_el);
							};
						} else {
							alert(d.msg);
						}
					}
				},
				"json"
		);
	}
}

Cart.update_qty = function(key, qty){
	if (qty > 0) {
		$.post(Cart.url()+'a.update_qty', {'key':key, 'qty': qty},
				function(d, st){
					if (d == 'ok') {
						Cart.update_widget();
					} else {
						alert(d);
					}
				}
		);
	}
}



Cart.del = function(key){
	$.post(Cart.url()+'a.del', {'key':key},
				function(d, st){
					if (d == 'ok') {
						Console.notice('Товар удален из корзины');
						Cart.update_widget();
					} else {
						alert(d);
					}
				}
		);
}

Cart.set_discount = function(discount){
	$.post(Cart.url()+'a.set_discount', {'discount':discount},
				function(d, st){
					if (d == 'ok') {
						//Console.notice('Скидка установлена');
						Cart.update_widget();
					} else {
						alert(d);
					}
				}
		);
}

Cart.get_discount = function(){
	$.post(Cart.url()+'a.get_discount', {},
				function(d, st){
					if (!isNaN(d)) {
						return d;
					} else {
						alert(d);
					}
				}
		);
}


Cart.update_widget = function(){
	try {
	if (opener && 'undefined' != typeof(opener.Cart)) {
		opener.Cart.update_widget(); 
		return;
	}
	if( $('div#big_cart').html() == null ){		
		var cart = $('div#cart');		
		if (cart.html() != null)
			cart.load(Cart.url()+'a.get_cart', {});
	}else{
		$('div#big_cart').load(Cart.url()+'a.get_big_cart', {});
	}
	} catch(err) {
	}
}

Cart.clear = function(){		
	$.post(Cart.url()+'a.clear', {}, function(){Cart.update_widget()});
}

Cart.reset_defaults  = function () {
	$(".cart_inc").unbind('click').bind('click', function(){Cart.change_qty(this, 1)});
	$(".cart_dec").unbind('click').bind('click', function(){Cart.change_qty(this, -1)});
	$(".cart_add").unbind('click').bind('click', function(){Cart.add(this)});
	
	//$(".cart_packing_add").unbind('click').bind('click', function(){Cart.packing_add(this)});
}

Cart.reset_defaults_big  = function () {
	$(".cart_inc").unbind('click').bind('click', function(){Cart.change_qty(this, 1, true)});
	$(".cart_dec").unbind('click').bind('click', function(){Cart.change_qty(this, -1, true)});
	$(".cart_add").unbind('click').bind('click', function(){Cart.add(this)});
}

Cart.url = function(){
    if ('undefined' == typeof(shop_cart_url))
        return '/cart/';
    else
        return shop_cart_url;
}

$(window).load( function(){Cart.reset_defaults(); Cart.update_widget()});

function popupWin (url, width, height) {
	if ('undefined' == typeof(width))
		width = 500;
	if ('undefined' == typeof(height))
		height = 560;
		
    newwin = window.open(url,'','toolbar=no, location=no, directories=no, status=no, menubar=no, copyhistory=no, scrollbars=yes,width='+width+', height='+height);
    newwin.moveTo(10,10);
    newwin.focus();
    return false;
}

function popup_show(id, url,w){
    var d_id =  'd_dlg_' + id;  
    current_dialog = new dialog(d_id, w );
    current_dialog.load(url, {id:id});
    current_dialog.show();
}

function toCompare(id) {
	VipCompare = window.open("/products/compare?id="+id, "VipCompare", "width=800,height=600,resizable=yes,directories=no,status=no,toolbar=no,menubar=0,location=no,scrollbars=yes");
	VipCompare.focus()
	return false;
}
