jQuery.fn.compare = function(t) {
    if (this.length != t.length) {return false;}
    var a = this.sort(),
        b = t.sort();
    for (var i = 0; t[i]; i++) {
        if (a[i] != b[i]) {
                return false;
        }
    }
    return true;
};

var eshopProduct = new Object();
eshopProduct['images'] = [];
eshopProduct['combinations'] = [];

var eshopProductSettings = new Object();

//default eshop settings
eshopProductSettings['hasCombinations'] = 0;

eshopProductAddImage = function(hash, thumb, picture, big)
{
    eshopProduct['images'].push({hash: hash, thumb: thumb, picture: picture, big: big});
}

eshopProductAddCombination = function(id, attributes, price, stock, weight, sizeWidth, sizeHeight, sizeDepth, imageHash)
{
    eshopProduct['combinations'].push ({id: id, attributes: attributes, price: price, stock: stock, weight: weight, sizeWidth: sizeWidth, sizeHeight: sizeHeight, sizeDepth: sizeDepth, imageHash: imageHash});
}

eshopSetCombination = function(attr)
{
    var i;
    
    for (i = 0; i<eshopProduct.combinations.length; i++){
        if ($(attr).compare(eshopProduct.combinations[i].attributes)) {
            eshopProduct.currentCombination = i;
            eshopUpdateProductView();
            return;
        }
    }
    
    alert($.i18n('Combination not found'));
    eshopProduct.currentCombination = eshopProductSettings['defaultCombination'];
    eshopUpdateProductView();
}

eshopUpdateProductView = function()
{
    var i;
    var product;

    //default values
    $('#eshop-product-stock').html('');
    $('#eshop-product-weight').html('');
    $('#eshop-product-sizeWidth').html('');
    $('#eshop-product-sizeHeight').html('');
    $('#eshop-product-sizeDepth').html('');
    $('#eshop-product-priceWithoutVat').html('');
    $('#eshop-product-priceWithVat').html('');
    $('.eshop-product-priceNormal').hide();
    $('.eshop-product-priceDiscount').hide();
    $('.eshop-product-addToCart').hide();

    if (eshopProductSettings['hasCombinations'] == 1) {

        product = eshopProduct.combinations[eshopProduct.currentCombination];
        for (i = 0; i < product.attributes.length; i++) {
            $("select[name=eshop-product-attr[]] option[value="+product.attributes[i]+"]:not(:selected)").parent().val(product.attributes[i]);
        }

        if (product.imageHash) {
            for (i = 0; i < eshopProduct.images.length; i++) {
                if (eshopProduct.images[i].hash == product.imageHash) {
                    product['imageId'] = i;
                    break;
                }
            }
        }

        if (product.imageId == undefined)
            product['imageId'] = eshopProduct.product.imageId;
    } else {
        product = eshopProduct.product;
    }

    //price
    $('#eshop-product-priceWithoutVat').html(eshopRound(product.price.actual.withoutVat, eshopProductSettings.currency.decimals));
    $('#eshop-product-priceWithVat').html(eshopRound(product.price.actual.withVat, eshopProductSettings.currency.decimals));

    if (product.price.old != undefined) {
        $('#eshop-product-priceNormal').html(eshopRound(product.price.old.withVat, eshopProductSettings.currency.decimals));

        $('#eshop-product-priceDiscount').html( eshopRound((product.price.old.withVat - product.price.actual.withVat)/product.price.old.withVat*100) + '% / ' + eshopRound(product.price.old.withVat - product.price.actual.withVat, eshopProductSettings.currency.decimals));

        $('.eshop-product-priceNormal').show();
        $('.eshop-product-priceDiscount').show();
    }

    if ( (eshopProductSettings.stockType != 1 && product.stock > 0) || eshopProductSettings.stockType == 1)
        $('.eshop-product-addToCart').show();

    $('#eshop-product-stock').html(eshopRound(product.stock, 4));
    $('#eshop-product-weight').html(eshopRound(product.weight));
    $('#eshop-product-sizeWidth').html(eshopRound(product.sizeWidth));
    $('#eshop-product-sizeHeight').html(eshopRound(product.sizeHeight));
    $('#eshop-product-sizeDepth').html(eshopRound(product.sizeDepth));

    $('.eshop-product-priceSymbol').html(eshopProductSettings.currency.symbol);
    eshopDisplayImage(product.imageId);
    $('#eshop-product-images-slideshow').trigger('goto', [product.imageId]);

}

eshopDisplayImage = function (id)
{
    $("img.eshop-product-mainImage").attr('src', '/var/files/' + eshopProduct.images[id].big);
}

loadProduct = function()
{
    $(document).ready(function()
    {
	$('#eshop-product-images-slideshow').serialScroll({
		items:'li',
		prev:'a#eshop-product-prev-image',
		next:'a#eshop-product-next-image',
		offset: -71, //when scrolling to photo, stop 230 before reaching it (from the left)
		start:1, //as we are centering it, start at the 2nd
		duration: 400,
		force:true,
		stop:true,
		lock:false,
		cycle:false //don't pull back once you reach the end
		/*easing:'easeOutQuart', //use this easing equation for a funny effect*/
		/*jump: true //click on the images to scroll to them*/
	});

        
        eshopUpdateProductView();
        $("div#eshop-product-images-slideshow ul li").hover(function(){eshopDisplayImage($(this).attr('id').split('-')[1]);});

        $("select[name=eshop-product-attr[]]").bind('change', function(){eshopSetCombination($("select[name=eshop-product-attr[]] option:selected").map(function(i, e){return (e.value);}).get());});

    });
}

eshopProductAddIntoCart = function(id, comb, unit, quantity)
{

    if (id == undefined) {
        id = eshopProduct.id;
        if (eshopProductSettings['hasCombinations'] == 1) {
            comb = eshopProduct.combinations[eshopProduct.currentCombination].id;
        }

        unit = $('select[name=eshop-product-unit]').val();
        quantity = $('input[name=eshop-product-quantity]').val();
    }
    alert('id: ' + id + ', comb: ' + comb + ', unit: ' + unit + ', quantity: ' + quantity);
}

eshopRound = function(value, decimals)
{
    value = parseFloat(value);
    
    if (decimals == undefined)
        decimals = 2;

    return value.toFixed(decimals);
}
