function setSubproductData(data, productId, name)
{    
    data = data.split('#');
    var item = $("#" + productId);

    var price = $().number_format(data['1'], {
       numberOfDecimals: 2,
       decimalSeparator: ',',
       thousandSeparator: ' '
    });
        
    if (data['3'] == 1) {

        var priceAction = $().number_format(data['2'], {
           numberOfDecimals: 2,
           decimalSeparator: ',',
           thousandSeparator: ' '
        });

        item.find(".priceOriginal span.priceItem").text(price);
        item.find(".priceAction span.priceItem").text(priceAction);

        item.find(".priceStandard").addClass('hide');
        item.find(".priceDefault").addClass('hide');
        item.find(".priceOriginal").removeClass('hide');
        item.find(".priceAction").removeClass('hide');
        item.find(".flagAction").removeClass('hide');
    } else {
        if (data['4'] > 0) {
            var priceStandard = $().number_format(data['4'], {
               numberOfDecimals: 2,
               decimalSeparator: ',',
               thousandSeparator: ' '
            });

            item.find(".priceStandard span.priceItem").text(priceStandard);
            item.find(".priceStandard").removeClass('hide');
        } else {
            item.find(".priceStandard").addClass('hide');
        }

        
        item.find(".priceDefault span.priceItem").text(price);

        item.find(".priceOriginal").addClass('hide');
        item.find(".priceAction").addClass('hide');
        item.find(".flagAction").addClass('hide');
        item.find(".priceDefault").removeClass('hide');
    }

    // set var
    item.find('.select').text(name);
    item.find("input[name=product_id]").val(data['0']);    
    item.find(".weight span").text(data['5']);
}

function setSubproductDetailData(data, productId, name)
{
    data = data.split('#');
    var item = $("#" + productId);

    // price standard
    if (data['4'] > 0) {
            var priceStandard = $().number_format(data['4'], {
               numberOfDecimals: 2,
               decimalSeparator: ',',
               thousandSeparator: ' '
            });

            item.find(".priceStandard span.priceItem").text(priceStandard);
            item.find(".priceStandard").removeClass('hide');
    } else {
        item.find(".priceStandard").addClass('hide');
    }

    // price original

    var price = $().number_format(data['1'], {
       numberOfDecimals: 2,
       decimalSeparator: ',',
       thousandSeparator: ' '
    });

    if (data['3'] == 1) {

        var priceAction = $().number_format(data['2'], {
           numberOfDecimals: 2,
           decimalSeparator: ',',
           thousandSeparator: ' '
        });

        var priceActionVat = $().number_format(data['7'], {
           numberOfDecimals: 2,
           decimalSeparator: ',',
           thousandSeparator: ' '
        });

        item.find(".priceOriginal span.priceItem").text(price);
        item.find(".priceAction span.priceItem").text(priceAction);
        item.find(".priceAction span.priceItemVat").text(priceActionVat);
        
        item.find(".priceDefault").addClass('hide');
        item.find(".priceOriginal").removeClass('hide');
        item.find(".priceAction").removeClass('hide');        
    } else {
        var priceVat = $().number_format(data['6'], {
           numberOfDecimals: 2,
           decimalSeparator: ',',
           thousandSeparator: ' '
        });

        item.find(".priceDefault span.priceItem").text(price);
        item.find(".priceDefault span.priceItemVat").text(priceVat);

        item.find(".priceOriginal").addClass('hide');
        item.find(".priceAction").addClass('hide');
        item.find(".priceDefault").removeClass('hide');
    }

    // set var
    item.find('.select').text(name);
    item.find("input[name=product_id]").val(data['0']);
    item.find(".weight span").text(data['5']);
}

$(function() {
    $("#menu ul.sf-menu").superfish({
        animation: 		{height: 'show'},
        speed: 			200,
        delay: 			600,
        autoArrows:             false,
        dropShadows:            false
    });

    $(".treeview").treeview({
        collapsed: true,
        animated: "medium",
        prerendered: false,
        persist: "locitem",
        unique: true
    });

    $("#selSort").change(function() {
        var value = $(this).val();
        window.location = '?sort=' + value;
    });

    $("#selCount").change(function() {
        var value = $(this).val();
        window.location = '?count=' + value;
    });

    $("a[rel^='gallery']").prettyPhoto({
        animationSpeed:'fast'
       ,slideshow:3000
       ,hideflash: true
   });
   
   $("a[href^='/public/media/images/']").prettyPhoto({
        animationSpeed:'fast'
       ,slideshow:3000
       ,hideflash: true
   });
});

// products
$(function() {
    var productDetailPrice = null;
    $("#input_voucher").click(function() {
        var price;
        if(productDetailPrice == null) {
            productDetailPrice = $("#product-detail #totalPrice").text() + ':' + $("#product-detail #totalPriceWhVat").text();
        }
        if($(this).attr('checked')) {
            price = $(this).val().split(':');
        } else {
            price = productDetailPrice.split(':');            
        }
        $("#product-detail #totalPrice").text(price[0]);
        $("#product-detail #totalPriceWhVat").text(price[1]);

    });

    var productAmountChanged = false;
    $(".product-box input[name=submit], #product-detail input[type=image]").hover(function() {
        var amount = $(this).parent().find("input[name=amount]");
        if (amount.val() == 0) {
            amount.val('1');
            productAmountChanged = true;
        }        
    }, function() {
        if (productAmountChanged) {
            var amount = $(this).parent().find("input[name=amount]");
            amount.val('0');
            productAmountChanged = false;
        }
    });

    // subproduct select
    $(".productItem .select").click(function(e) {
        
        e.stopPropagation();

        var option = $(this).parent().find('.selectOption');
        var test = option.css('display');

        if (test == 'none') {
            $(".productItem .selectOption:visible").hide();
            option.slideDown('fast');
        } else {
            option.hide();
        }        
    });

    $('body').click(function () {                
        $(".selectOption").hide();
    });

    $(".productItem .selectOption a").click(function() {
        var data = $(this).attr('rel');
        var id = $(this).parents('.productItem').attr('id');
        var name = $(this).find('.name').text();

        if ($(this).parents('.productItem').hasClass('productDetail')) {
            setSubproductDetailData(data, id, name);
        } else {
            setSubproductData(data, id, name);
        }
    });
});

// order
$(function() {
    $('form#order_method input[name=delivery]').change(function() {
        var delivery = $(this).val();      
        
        if (delivery == 2) {
            $('#input_payment_3').attr('checked', false);
            $('#input_payment_3').attr('disabled', true);
            $('#input_payment_2').attr('disabled', false);
        } else if (delivery == 3) {
            $('#input_payment_2').attr('checked', false);
            $('#input_payment_2').attr('disabled', true);
            $('#input_payment_3').attr('disabled', false);
        }
    });
});

// registracia zakaznika
$(function() {
    $('input[name="user_form"]').change(function() {
        if($(this).val() == 1) {
            $("#type_individual").css('display', 'block');
            $("#type_company").css('display', 'none');
        } else {
            $("#type_individual").css('display', 'none');
            $("#type_company").css('display', 'block');
        }
    });
});

// jcaroseul init callback
function mycarousel_initCallback(carousel)
{
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

function openWindow(url, name, param)
{
    if (!name) {
        name = 'popup';
    }
    
    if (!param) {
        param = 'width=500, height=500, scrollbars=1, resizable=1. location=0, menubar=0, toolbar=0, status=1';
    }
    
    window.open(url, name, param);
}

// quatro popup
function openQuatro(url, input)
{    
    if (input) {
        if (!$("#" + input).is(':checked')) {
            return false;
        }
    }       
    
    openWindow(url, 'quatro');
    
}

$(document).ready(function() {
    // search
    var search_q = $("#search_q").val();

    $($("#search_q")).focus(function() {
        if ($(this).val() == search_q) {
            $(this).val('');
        }
    });

    $($("#search_q")).blur(function() {
        if ($(this).val() == '') {
            $(this).val(search_q);
        }
    });

    // subproduct select
    $('.productItem .selectOption a.selected').each(function() {
        var data = $(this).attr('rel');
        var id = $(this).parents('.productItem').attr('id');
        var name = $(this).find('.name').text();

        if ($(this).parents('.productItem').hasClass('productDetail')) {
            setSubproductDetailData(data, id, name);
        } else {
            setSubproductData(data, id, name);
        }
    });
    
    // main action
    $('#main-action-holder').jcarousel({
        auto: 5,
        wrap: 'circular', //circular - last
        initCallback: mycarousel_initCallback,
        scroll: 1,
        visible: 1,
        animation: 'slow'
    });
    
    // newsletter    
    var newsletter_name = $("#newsletter_name").val();
    var newsletter_email = $("#newsletter_email").val();

    $($("#newsletter_name")).focus(function() {
        if ($(this).val() == newsletter_name) {
            $(this).val('');
        }
    });

    $($("#newsletter_name")).blur(function() {
        if ($(this).val() == '') {
            $(this).val(newsletter_name);
        }
    });
    
    $($("#newsletter_email")).focus(function() {
        if ($(this).val() == newsletter_email) {
            $(this).val('');
        }
    });

    $($("#newsletter_email")).blur(function() {
        if ($(this).val() == '') {
            $(this).val(newsletter_email);
        }
    });
});
