/*
    $(".cart-content").droppable({
    	accept: ".item-in-list",
    	activeClass: 'cart-highlight',
		drop: function(ev, ui) {
    		item_id=$(ui.draggable).attr("id").replace("item_", "");
    		$.ajax({
				url: "/cart/add/id/"+item_id,
				success: function(data) {
					updateCartWidget();
				}
			});
			
		},
		tolerance: 'pointer'
    });
});


*/
var tips = $("#validateTips");

	function updateTips(t) {
	    tips.text(t).effect("highlight",{},1500);
	}
	function checkRegexp(o, regexp, n) {
	    if (!( regexp.test(o.val()) )) {
	        o.addClass('ui-state-error');
	        updateTips(n);
	        return false;
	    } else {
	        return true;
	    }
	}

	function cartDialog(item_id) {
		$("#cart-dialog").fadeIn("fast");
	    tips.hide();
	    // reset quantity field
	    $("input[name=quantity]").removeClass('ui-state-error').val("");
	    //adding event to form
	    $("#cart-dialog form").unbind();
	    $("#cart-dialog form").submit(function(){
	        $("#cart-dialog input").removeClass('ui-state-error');
	        var valid = true;
	        valid = valid && checkRegexp( $("#quantity"), /\d+$/, "Здесь должны быть цифры");
	        if (valid) {
	            $.ajax({
	                url: "/cart/add/",
	                data: {
	                    // make here correct params
	                    id: item_id,
	                    quantity: $("input[name=quantity]").val()
	                },
	                success: function(data) {
	                    //$('.cart-content').html(data);
	                	updateCartWidget();                	
	                    initCart();
	                    setTimeout('Shadowbox.setup()', 200);
	                }
	            });
	            $("#cart-dialog").fadeOut("fast");
	        }
	        return false;
	    })
	}
	
	
    function initCart() {
        $(".cart-content").droppable({
            accept: ".item-in-list",
            activeClass: 'cart-highlight',
            drop: function(ev, ui) {
                //deleteImage(ui.draggable);
        		cartDialog($(ui.draggable).attr("id").replace("item_", ""));
            },
            tolerance: 'pointer'
        });
    }
    
    
$(document).ready(function(){

    jQuery.ajaxSetup({
        complete: initCart
    });
    
    initCart();

    $("#top-nav a").hover(
        function(){
            $(this).closest(".top-nav-item").fadeTo(10, 0.5).addClass("hover").fadeTo(200, 1);
            return false;

        },
        function(){
            $(this).closest(".top-nav-item").removeClass("hover", 500);
            return false;
        }
    );

    $("#left-nav > ul > li > ul").hide();
    $("#left-nav > ul > li.expanded > ul").show();
    $("#left-nav > ul > li > a").click(function(){
    	if ( $(this).closest("li").hasClass("expanded")){
        	$(this).find("span").html("+").closest("li").find("ul").slideUp().end().closest("li").removeClass("expanded");    		
    	}
    	else {
        	$(this).find("span").html("&mdash;").closest("li").find("ul").slideDown().end().closest("li").addClass("expanded");    		
    	}
        return false;
    });

    $("#price-value-flyout").hide();
    $("#icon-calc").hover(
            function() {
                $("#price-value-flyout").stop(true, true).slideDown("fast");
                return false;
            },
            function() {
                $("#price-value-flyout").stop(true, true).slideUp("fast");
                return false;
    });
    
    $(".item-in-list").draggable({
    	cursor: 'move',
		cursorAt: { top: -6, left: -8 },
    	/*helper: function(event) {
				return $('<div class="ui-widget-header">' + $(this).find(".item-description").text() + '</div>');
			}*/
		helper: 'clone',
		revert: 'invalid'
    });

//    $(".dialog-container").unbind();
    $("body").keydown(function(e){
        if (e.keyCode == '27') {
//             event.preventDefault();
            $(".dialog-container").fadeOut("fast", function(){
                $("input[type=text], input[type=password]", ".dialog-container").val("");
            });
            return false;
        }
    });
    // add event to 'close' button
    $(".dialog-close").unbind();
    $(".dialog-close").click(function(){
        $(".dialog-container").fadeOut("fast", function(){
            $("input[type=text], input[type=password]", ".dialog-container").val("");
        });
    });

    $("#login-link").click(function(){
        $("#login-dialog").fadeIn("fast");
        return false;
    });

	var prevImg;
	$('.item-in-list .previews a').hover(
		function() {
			prevImg = $(this).parent().parent().find('.item-image a img').attr('src')
			$(this).parent().parent().find('.item-image a img').attr('src', $(this).attr('href'))
		},
		function() {
			$(this).parent().parent().find('.item-image a img').attr('src',prevImg)
		}
	);
});


