$(function() {

jQuery(".accessories input:checkbox").click(function(){
        getProductPrice();
    });

    jQuery('input[name^="variantids"]:radio').live('click', function(){
        calculateStock();
        getProductPrice();
    });
    
    jQuery(".option select").live('change', function(){
        calculateStock();
        getProductPrice();
    });

    function calculateStock()
    {
        var params = 'page=modules/petstuffonline/ajax/variant_get_stock';
        var productId = jQuery('form[name="productDetailsForm"]').find('input[name="productid"]').val();
        params = params + "&product_id=" + productId;
        var options = jQuery('form[name="productDetailsForm"]').find('input[name^="variantids"]:checked,select[name^="variantids"]');
        options.each(function (i){
            params = params + "&" + jQuery(this).attr("name") +  "=" + jQuery(this).val();
        });
        jQuery.ajax({
            async:false,
            url:'index.php',
            data:params,
            success:function(response) {
                jQuery(".options").replaceWith(response);
            }
        });
    }

    function getProductPrice()
    {
        var params = 'page=modules/petstuffonline/ajax/product_get_price';
        var productId = jQuery('form[name="productDetailsForm"]').find('input[name="productid"]').val();
        params = params + "&product_id=" + productId;
        var options = jQuery('form[name="productDetailsForm"]').find('input[name^="variantids"]:checked,select[name^="variantids"]');
        options.each(function (i){
            params = params + "&" + jQuery(this).attr("name") +  "=" + jQuery(this).val();
        });
        var accessories = jQuery('form[name="productDetailsForm"]').find('input[name^="accessories"]:checked');
        accessories.each(function (i){
            params = params + "&accessories[]=" + jQuery(this).val();
        });
        jQuery.post(
            'index.php',
            params,
            function(response) {
                jQuery("#price").html(response);
            }
        );
    }

    $('#product_details .image-block .image a').lightBox();
    $('.enlarge-img a').lightBox();

    $('#thumbnails a').click(function(){
        $("#mainImage").attr('src', $(this).attr('href'));
        $('#product_details .image-block .image a').attr('href', $(this).attr('rel'));
        $('.enlarge-img a').attr('href', $(this).attr('rel'));
        return false;
    });

    $("#form-container").abform({
        attributes :'id="review-form" action="index.php" method="POST"',
        convert : '{page|hidden}{command|hidden}{name|text}{details|textarea}{save|button|class="absubmit"}',
        sequential_disable : false
    });

    $(".add-review, .edit-review").click(function(){
        $("#dialog-add-review").dialog('destroy');
        $("#dialog-add-review").dialog({
            draggable: false,
		    resizable: false,
			width:500,
			height:400,
			modal: true
		});
		return false;
    });

});
