//URL-Get-Parameter extrahieren
$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});
//Navigation oben ein-/ausblenden
function header_navigation(object) {
    var item = $(object).parent().parent();
    if (item.hasClass('active'))
        item.removeClass('active');
    else {
        $('#container_header_navigation_items li').removeClass('active');
        item.addClass('active');
    }
    $(object).blur();
}
//Navigation oben laden
function load_header_navigation(){
    var all_items_list = new Array('herren_nav', 'damen_nav', 'kinder_nav', 'service_nav');
    var container_list = new Array('container_header_navigation_item_men', 'container_header_navigation_item_ladies', 'container_header_navigation_item_kids', 'container_header_navigation_item_more');

    $.each(all_items_list, function(i,item){
        jQuery.getJSON('/misc/top_navi_helper/get_layer_content.php?navname='+item+'&language='+language,
            function(data){
                var result = '';
                var counter = 0;
                result += '<li>';
                result += '<h4>'+data.content.header_left+'</h4>';
                result += '<ul>';
                counter = 0;
                $.each(data.content.content_left, function(content_i,content_item){
                    result += '<li>'+content_item+'</li>';
                    counter++;
                });
                while (counter < data.content.content_left.length || counter < data.content.content_center.length || counter < data.content.content_right.length) {
                    result += '<li>&nbsp;</li>';
                    counter++;
                }
                result += '</ul>';
                result += '</li>';
                result += '<li>';
                result += '<h4>'+data.content.header_center+'</h4>';
                result += '<ul>';
                counter = 0;
                $.each(data.content.content_center, function(content_i,content_item){
                    result += '<li>'+content_item+'</li>';
                    counter++;
                });
                while (counter < data.content.content_left.length || counter < data.content.content_center.length || counter < data.content.content_right.length) {
                    result += '<li>&nbsp;</li>';
                    counter++;
                }
                result += '</ul>';
                result += '</li>';
                result += '<li>';
                result += '<h4>'+data.content.header_right+'</h4>';
                result += '<ul>';
                counter = 0;
                $.each(data.content.content_right, function(content_i,content_item){
                    result += '<li>'+content_item+'</li>';
                    counter++;
                });
                while (counter < data.content.content_left.length || counter < data.content.content_center.length || counter < data.content.content_right.length) {
                    result += '<li>&nbsp;</li>';
                    counter++;
                }
                result += '</ul>';
                result += '</li>';
                $('#'+container_list[i]+' > ul').html(result);
            }
        );
    });
    
    $('#container_header_navigation_items').mouseleave(function() {
        $('#container_header_navigation_items li').removeClass('active');
    });
}
//Vollständigen Produkttext ein-/ausblenden
function product_complete_description_toggle() {
    if ($('.product_description_text .inner').outerHeight() > $('.product_description_text').outerHeight()) {
        $('.product_description_text').animate({height: $('.product_description_text .inner').outerHeight()});
    } else {
        $('.product_description_text').animate({height: descriptionHeight});
    }
    $('#product_complete_description_show').toggle();
    $('#product_complete_description_hide').toggle();
}
//Vollständigen Seitentext ein-/ausblenden
function page_complete_description_toggle() {
    if ($('.page_description_text .inner').outerHeight() > $('.page_description_text').outerHeight()) {
        var difference = $('.page_description_text .inner').outerHeight() - $('.page_description_text').outerHeight();
        $('.page_description_text').animate({height: $('.page_description_text .inner').outerHeight()});
        $('#info_text').animate({height: $('#info_text').height() + difference});
    } else {
        var difference = pageDescriptionHeight - $('.page_description_text').outerHeight();
        $('.page_description_text').animate({height: pageDescriptionHeight});
        $('#info_text').animate({height: $('#info_text').height() + difference});
    }
    $('#page_complete_description_show').toggle();
    $('#page_complete_description_hide').toggle();
}
function close_pp() {
    $('a.pp_close').click();
}
function resize_pp(width) {
    $('#pp_full_res iframe').width(width);
    $('.pp_pic_holder').width(width+40);
    $(window).resize();
}
function printMe() {
    self.focus();
    self.print();
}
function showimage(number) {
    auto_change_enabled = false;
    if (number != current_number)
        loadimage(number);
}
function nextimage() {
    if (auto_change_enabled) {
        current_number = (current_number + 1) % images.length;
        loadimage(current_number);
        window.setTimeout('nextimage()', autoswitch_time);
    }
}
function loadimage(number) {
    if (!change_enabled)
        return;
    change_enabled = false;
    $('.product_image').fadeIn(0);
    $('.product_image_new').fadeOut(0);
    $('.product_image_new .inner').css('background-image', 'url('+images[number]+')');
    $('.product_image_new').fadeIn(500);
    $('.product_image').fadeOut(500,function(){
        $('.product_image .inner').css('background-image', 'url('+images[number]+')');
        $('.product_image').fadeIn(0);
        change_enabled = true;
    });
    current_number = number;
    $('.thumbnail').removeClass('active');
    $('.thumbnail:eq('+number+')').addClass('active');
    $('#zoom_link').attr('href','/?action=misc&misc_url=gallery.php&lang=' + (typeof(gallery_lang) == 'undefined' ? 'de' : gallery_lang) + '&item='+gallery_item+'&area='+gallery_area+'&active='+number+'&layerview=1&iframe=true&height=620&width=710');
    
}

//Such-Autovervollstaendigung laden
function load_autocomplete() {
    if (
        typeof(ac_data) != "undefined" && 
        typeof(ac_data['keywords']) != "undefined" && 
        typeof(ac_data['categories']) != "undefined" && 
        typeof(ac_data['products']) != "undefined"
    ) {
        $('#field_search').suggest({
            width: 334,
            offsetTop: 3,
            groups: [
                {
                    source: ac_data['keywords'],
                    searchMode: 'begins',
                    maxEntries: 6,
                    theme: {
                        containerClass: 'keywords'
                    },
                    formatFunction: function(entry, searchterm){ 
                        return '<span class="keyword" keyword="' + (entry.special ? 'catonly' : '1') + '">'+entry.value+'</span>';
                    },
                    submitFunction: function(entry){ entry.closest('form').find('input[name=all_sections]').val(entry.find('.keyword').attr('keyword')); entry.closest('form').submit(); }
                },
                {
                    source: ac_data['categories'],
                    searchMode: 'begins',
                    title: 'Kategorien',
                    maxEntries: 6,
                    theme: {
                        containerClass: 'categories'
                    },
                    formatFunction: function(entry, searchterm){ 
                        return '<span class="keyword" keyword="'+entry.search+'">'+entry.value+'</span>';
                    },
                    applyFunction: function(entry){ 
                        $('#field_search').val(entry.find('.keyword').attr('keyword')); 
                    }
                },
                {
                    source: ac_data['products'],
                    title: 'Produkte',
                    maxEntries: 6,
                    theme: {
                        containerClass: 'products'
                    },
                    formatFunction: function(entry, searchterm){
                        var out = '<span class="title">' + entry.value + '</span>';
                        out += '<span class="price' + (entry.old_price != '0' ? ' new' : '') + '">' + (entry.old_price != '0' ? '<span class="old">' + (parseInt(entry.old_price)/100).toFixed(2).replace('.', ',') + ' &euro;</span>' : '') + (entry.from_price == 'y' ? 'ab ' : '') + (parseInt(entry.price)/100).toFixed(2).replace('.', ',') + ' &euro;</span>';
                        out += '<img src="/cat_images/40/40_' + entry.image + '.png" alt="' + entry.keyword + '" class="image" />'; 
                        out += '<a class="link" href="/' + language + '/' + entry.area + '/' + entry.item + '/"><br/></a>'; 
                        return out;
                    },
                    applyFunction: function(entry){ },
                    submitFunction: function(entry){ window.location.href = entry.find('.link').attr("href"); },
                    rankFunction: function(entry, rank){ return entry.rank <= 1 ? rank : (rank <= 1 ? rank : entry.rank); }
                }
            ]
        });
    }
}

/**
 * Erstellt eine Info-Box mit dynamischem Inhalt
 */
(function($) {
  $.fn.infobox = function(options) {
    return this.each(function() {
        //Standardeinstellungen
        var o = $.extend({
            effect_duration: typeof(effectDuration) != 'undefined' ? effectDuration : 500
        }, options);
        
        var box = $(this);
        var nav_items = $(this).find('> li').length;
        var content_items = $(this).find('> li').length;
        
        //Navigation-Funktionalitaet setzen
        box.find('.navigation ul li a').click(function() {
            if (!$(this).hasClass("active")) {
                box.find('.content').stop(true, true).slideUp(o.effectDuration);
                box.find('.content-' + $(this).attr("number")).stop(true, true).slideDown(o.effectDuration);
                box.find('.navigation ul li a').removeClass("active");
                $(this).addClass("active");
            }
        });
    });
  }
})(jQuery); 

