$(document).ready(function() {
    $("a.photo").fancybox();

    $("a.summary-link").fancybox({
    	'frameWidth'  : 750,
    	'frameHeight'  : 500,
    	'hideOnContentClick' : false
    });

    $("a.help-link").fancybox({
    	'frameWidth'  : 750,
    	'frameHeight'  : 500,
    	'hideOnContentClick' : false
    });

    $('.quantity').change(function () {

    	var id = $(this).attr('name');
    	var qty = $(this).val();

    	// show loading graphic
    	$(this).after('<div class="loading"></div>');

    	// send to server
    	$.post("/orders/order/update-qty/", { 'id': id, 'qty' : qty },
            function(data){

                // hide loading graphic
                $(".loading").remove();

                // update order total
                $("#order-total").text("$" + data.total);
                
            }, "json"
        );
    });

    $('#c').change(function () {
    	var id = $("#c").val();
    	var q = $("#search_order_query").val();
    	var link = "/orders/order/index/o/" + CUR_ORDER + "/s/" + CUR_SORT + "/";

    	if (id.length > 0) {
    		link = link + "id/" + id + "/";
    	}
    	
    	if (q.length > 0) {
    		link = link + "q/" + q + "/";
    	}

    	parent.location = (link);
    	return false;
    });

    $("#search_order").submit(function () {
    	var id = $("#c").val();
    	var q = $("#search_order_query").val();
    	var link = "/orders/order/index/o/" + CUR_ORDER + "/s/" + CUR_SORT + "/";
    	
    	if (id.length > 0) {
    		link = link + "id/" + id + "/";
    	}
    	
    	if (q.length > 0) {
    		link = link + "q/" + q + "/";
    	}
    	
    	parent.location = (link);
    	return false;
    });

    $("#search_order_clear").click(function() {
    	var link = "/orders/order/";
    	parent.location = (link);
    	return false;
    });

    $('#payment_delivery_date').datepicker({ dateFormat: 'yy-mm-dd'});
    $('#payment_cancel_date').datepicker({ dateFormat: 'yy-mm-dd'});

    $('#date_from').datepicker({ dateFormat: 'yy-mm-dd'});
    $('#date_to').datepicker({ dateFormat: 'yy-mm-dd'});
});
