$(function() {
    function updateShopBox() {
        var $sb = $('#shop-box');
        $.get(location.pathname+'?ucms_show_page_module_id='+$sb.attr('data-module-id'), function(data) {
            $sb.parents('.sidebar-container').replaceWith(data);
        });
    }
    function updateCompareBox() {
        var $cb = $('#compare-box');
        $.get(location.pathname+'?ucms_show_page_module_id='+$cb.attr('data-module-id'), function(data) {
            $cb.parents('.sidebar-container').replaceWith(data);
        });
    }

    function bindShopForm($elems) {
        $elems.submit(function(e) {
            e.preventDefault();
            var $form = $(this);
            var artNr = /value="([^"]+)"/.exec($form.html())[1];
            var quantity = $form.children('[name="quantity"]').val();
            $.post($form.attr('action'), {shop_add: artNr, "quantity": quantity}, function() {
                updateShopBox();
            });
        });
    }
    bindShopForm($('.shop-form'));

    function bindCompareForm($elems) {
        $elems.submit(function(e) {
            e.preventDefault();
            var $form = $(this);
            var artNr = /value="([^"]+)"/.exec($form.html())[1];
            var quantity = $form.children('[name="quantity"]').val();
            $.post($form.attr('action'), {compare_add: artNr, "quantity": quantity}, function() {
                updateCompareBox();
            });
        });
    }
    bindCompareForm($('.compare-form'));
    
    $('.no-js').remove();

    if(typeof IE7_GLOBAL === undefined) {
        Shadowbox.init({players: ["img","html"], slideshowDelay: 2.5});
    }
    else {
        // delay shadowbox a little (50ms) since it doesn't like ie9.js
        setTimeout(function () {
            Shadowbox.init({
                players: ["img","html"],
                slideshowDelay: 2.5
            });
        }, 50);
    }
    $('#manufacturer_id').change(function() {
        window.location.search = $.query.set(this.name, this.value).set('model_id', 0)+"";
    })
    $('.query-change').change(function() {
        var $form = $(this).closest('form');
        var q = $.query;
        $form.find('.query-change').each(function() {
            q.SET(this.name, this.value);
        });
        window.location.search = ""+q;
    });
    $('#main-links li.fold.active-sub').addClass('show');
    $('#main-links li.fold').removeClass('hover');
    $('#main-links li.fold > a').click(function () {
        var $li = $(this).closest('li');
        $li.addClass('show');
        $li.siblings('li.fold').removeClass('show');
        return false;
    });
    $('#bottom-pane > div').addClass('hidden');
    // needed for smooth firefox animation
    $('#extra-images').css('width', $('#bottom-pane').width());
    $('li.fold > a').click(function () {
            var $li = $(this).closest('li');
            $li.siblings('.fold').removeClass('active');
            var target = $(this).attr('data-tabid');
            var $div = $('#'+target);
            $div.siblings('div:visible').hide('fast');
            if($div.is(':visible')) {
                $li.removeClass('active');
                $div.hide('fast');
            } else {
                if($div.is('.not-loaded')) {
                    $div.load($(this).attr('href'), function() {
                        $div.show('fast');
                        $li.addClass('active');
                        $div.removeClass('not-loaded');
                        bindShopForm($div.find(".shop-form"));
                        Shadowbox.setup('#'+$div.attr('id')+' a[rel*="shadowbox"]');
                    });
                } else {
                    $div.show('fast');
                    $li.addClass('active');
                }
            }
            return false;
    });
    $('#search-box').submit(function() {
        searchElement = $(this).find('input[type=text]');
        window.location.href = this.action + $.query.set('query', searchElement.attr('value'));
        return false;
    });
    $('#order-form button.link-like').each(function(index) {
        var $el = $(this);
        var $form = $('#order-form');
        var artNr = /value="([^"]+)"/.exec($el.parent().html())[1];
        $el.after('<a id="remove-'+artNr+'" class="link-like" href="#" >'+$el.html()+'</a>');
        $el.parent().find('a.link-like').click(function(e) {
            e.preventDefault();
            $.post(window.location.href,
                {shop_remove: artNr});
            $(this).closest('tr').remove();
            if($('#order-form tr').length == 0) {
                window.location = window.location.href;
            }
        });
        $el.remove();
    });
})

