var KontorX = {
	ajaxIsSuccess: function(json) {
		var success = null;
		if (json.success != undefined) {
			success = json.success;
		} else
		if (json.response != undefined && json.response.success != undefined) {
			success = json.response.success;
		}
		return success == true ? true : false;
	}
};

$(document).ready(function(){
	$('a[rel*=facebox]').facebox();

	$('#zend_form_contact').parents('.zend_form').css('height','350px');

	  $("#fast-login input[@name=username], #fast-login input[@name=password]").defaultvalue("e-mail",'haslo');


	$('.round').corner();
	$('.zend_form').corner();
	//$('#kategorie ul li').corner();
	$('#kategorie ul li ul li a').map(function(){
		if($(this).parents('li:eq(0)').find('ul').size() > 0){
			$(this).css({"font-weight": "bold"});
		}
	});
	$('#kategorie ul li ul li a').toggle(
		function(){
			var ul = $(this).parents('li:eq(0)').find('ul');
			if(ul.size() > 0){
				ul.show();
				return false;
			}
			document.location = this.href;
		},
		function(){
			var ul = $(this).parents('li:eq(0)').find('ul');
			if(ul.size() > 0){
				ul.hide();
				return false;
			}
			document.location = this.href;
		}
	);
	//$('#kategorie ul li ul li ul a').unbind();
	$('.product-show').corner();

	$('.product').corner();
	$('.product-show').corner();
	$('.manufacturer').corner();
	$('.manufacturer-show').corner();
	
	$('.branch-list').corner();
	
	

	$('.product').hover(
		function(){
			$(this).find('.product-actions').show();
		},
		function(){
			$(this).find('.product-actions').hide();
		}
	);
	$('#top').flash({ src: 'swf/top_1a.swf', width: 950, height: 120});

	//$('#menu-category').accordion();
	//$('#tabs-frontend').tabs().tabsRotate(15000);
	//$('#tabs-manufacturers > ul').tabs({event: 'mouseover'});
	
	
	$('.cart-add').livequery('click',function(){
		var self = this;
		$.getJSON(this.href ,{}, function(data){
			if (KontorX.ajaxIsSuccess(data)) {
				var href = self.href.replace('add','remove');
				$(self).removeClass('cart-add').addClass('cart-remove').attr('href', href).text('Usun z koszyka');
				renderCart(data.cart);
			}
			return;
		});
		return false;
	});
	$('.cart-remove').livequery('click',function(){
		var self = this;
		$.getJSON(this.href ,{}, function(data){
			if (KontorX.ajaxIsSuccess(data)) {
				var href = self.href.replace('remove','add');
				$(self).removeClass('cart-remove').addClass('cart-add').attr('href', href).text('Do koszyka');
				renderCart(data.cart);
				$(self).parents('.cart-action-box').fadeOut('slow');
			}
			return;
		});
		return false;
	})
	
	var cartQuantity = $('.cart-quantity');
	var cartSumcost = $('.cart-sumcost');

	function renderCart(cart) {
		cartQuantity.text(0);
		cartSumcost.text(0);

		$.each(cart, function(i,e){
			cartQuantity.text(parseInt(cartQuantity.text()) + e.quantity);
			cartSumcost.text(parseInt(cartSumcost.text()) + parseInt(e.price));
		});
	}

	$('a.cart-product-more').facebox();

	//$('.cart-add').click(cartAdd);
	//$('.cart-remove').click(cartRemove);

	function cartAdd(){
		$(this)
			.removeClass('cart-add')
			//.removeClass('cart-addToCart')
			.addClass('cart-remove')
			//.addClass('cart-removeFromCart')
			.unbind('click',cartAdd)
			.bind('click',cartRemove);
		
		var oProduct = $(this).parents('.cart-productItem');
		var iProductId = oProduct.attr('id');
		var iProductPrice = $('.cart-product-price',oProduct).text();

		// powieksz wartosci w koszyku
		oCartQuantity.text(parseInt(oCartQuantity.text())+1);
		oCartSumcost.text(parseInt(oCartSumcost.text())+parseInt(iProductPrice));
		
		$.get("<?php print $baseUrl ?>/koszyk/dodaj/" + iProductId);
		return false;
	}
		
	function cartRemove(){
		$(this)
			.removeClass('cart-remove')
			//.removeClass('cart-removeFromCart')
			.addClass('cart-add')
			//.addClass('cart-addToCart')
			.unbind('click',cartRemove)
			.bind('click',cartAdd);

		var oProduct = $(this).parents('.cart-productItem');
		var iProductId = oProduct.attr('id');
		var iProductPrice = $('.cart-product-price',oProduct).text();

		// powieksz wartosci w koszyku
		oCartQuantity.text(parseInt(oCartQuantity.text())-1);
		oCartSumcost.text(parseInt(oCartSumcost.text())-parseInt(iProductPrice));
		
		$.get("<?php print $baseUrl ?>/koszyk/usun/" + iProductId);
		return false;
	}
});