$(function() {
    if ($.browser.msie && ($.browser.version >= 6 && $.browser.version < 8)) {
        $('#contentpane').css('height', $('#mainpane').height() - $('#contentpane').position().top);
    }
    
    if (!($.browser.msie && ($.browser.version == 6))) {
        $('.form_quicksearch').css('opacity', 0.8);
        $('.functions').css('opacity', 0.8);
        $('#contentpane_bg').css('opacity', 1);
        $('#teasers').css('opacity', 0.8);
        
        $('#logo img').mouseover(function() {
            $(this).fadeTo(300, 0.5);
        });
        $('#logo img').mouseout(function() {
            $(this).fadeTo(300, 1);
        });
        
        $('a').mouseover(function() {
            $(this).fadeTo(300, 0.5);
        });
        $('a').mouseout(function() {
            $(this).fadeTo(300, 1);
        });
        
        $('button').mouseenter(function() {
            $(this).fadeTo(300, 0.5);
        });
        $('button').mouseleave(function() {
            $(this).fadeTo(300, 1);
        });
        
        $('.shop_overview. .shop_articles .titleimage[rel]').each(function() {
            $(this).qtip({
                content: {
                    text: '<div class="img" style="background-image:url(' + $(this).attr('rel') + ');"><img src="' + $(this).attr('rel') + '" alt="" width="400" height="400" /></div>'
                },
                position: {
                    target: 'mouse',
                    adjust: {
                        x: 20, y: 20,
                        screen: true
                    }
                },
                show: {
                    solo: true,
                    delay: 0
                },
                style: {
                    classes: 'overview_article'
                }
            });
        });
        
        $('body').delegate('div.ui-tooltip.greeting_cards .ui-tooltip-content label.card', 'mouseenter', function() {
            $(this).fadeTo(300, 0.5);
        });
        $('body').delegate('div.ui-tooltip.greeting_cards .ui-tooltip-content label.card', 'mouseleave', function() {
            $(this).fadeTo(300, 1);
        });
    }
    
    $('.shop_basket .step_basket input.greeting_card').each(function() {
        var greeting_card_row = $('#' + $(this).attr('name'));
        greeting_card_row.removeClass('active');
        if ($(this).is(':checked')) {
            greeting_card_row.addClass('active');
        }
    });
    $('body').delegate('.shop_basket .step_basket input.greeting_card', 'change', function() {
        var greeting_card_row = $('#' + $(this).attr('name'));
        greeting_card_row.removeClass('active');
        if ($(this).is(':checked')) {
            greeting_card_row.addClass('active');
        }
    });
    $('body').delegate('.shop_basket .step_basket a.btn_select_greeting_card', 'click', function(event) {
        event.preventDefault();
        $(this).qtip({
            content: {
                title: {
                    text: 'Karte wählen',
                    button: 'Schliessen'
                },
                text: function() {
                    var qtip_content = $(this).next().clone();
                    qtip_content.undelegate('label.card', 'click');
                    $('input.fld_greeting_card_card', qtip_content).remove();
                    qtip_content.removeClass('cards').addClass('innercontent');
                    return qtip_content;
                }
            },
            position: {
                my: 'top left',
                at: 'top left',
                target: $('#greeting_card_' + $(this).metadata().id),
                adjust: {
                    x: -90, y: -20
                }
            },
            hide: {
                fixed: true,
                event: false
            },
            show: {
                ready: true,
                delay: 0,
                event: false
            },
            style: {
                classes: 'greeting_cards'
            }
        });
    });
    
    $('body').delegate('div.ui-tooltip.greeting_cards .ui-tooltip-content label.card', 'click', function() {
        var $this = $(this);
        var $target = $('#' + $this.attr('for'));
        var $target_label = $target.parent();
        var $target_cards = $target_label.parent();
        $target_cards.children().removeClass('active');
        $target_label.addClass('active');
        $target_cards.prev().data('qtip').hide();
    });
    
    $('.shop_basket .step_basket textarea.fld_greeting_card_text').each(function() {
        var countfield = $('#' + $(this).attr('name') + '_signs_left_count');
        textLimiter($(this), countfield, 240);
        $('#' + $(this).attr('name') + '_signs_left').show();
        
        $(this).keydown(function() {
            textLimiter($(this), countfield, 240);
        });
        $(this).keyup(function() {
            textLimiter($(this), countfield, 240);
        });
    });
    
    $('.shop_basket .step_basket input.ribbon').each(function() {
        var ribbon_row = $('#' + $(this).attr('name'));
        ribbon_row.removeClass('active');
        if ($(this).is(':checked')) {
            ribbon_row.addClass('active');
        }
    });
    
    $('body').delegate('.shop_basket .step_basket input.ribbon', 'change', function() {
        var ribbon_row = $('#' + $(this).attr('name'));
        ribbon_row.removeClass('active');
        if ($(this).is(':checked')) {
            ribbon_row.addClass('active');
        }
    });
    
    $('.shop_basket .step_basket input.fld_ribbon_text').each(function() {
        var countfield = $('#' + $(this).attr('name') + '_signs_left_count');
        textLimiter($(this), countfield, 35);
        $('#' + $(this).attr('name') + '_signs_left').show();
        
        $(this).keydown(function() {
            textLimiter($(this), countfield, 35);
        });
        $(this).keyup(function() {
            textLimiter($(this), countfield, 35);
        });
    });
    
    if ($('input.fld_shipping_selection[value=delivery]').is(':checked')) {
        $('fieldset.section_delivery_address').show();
        $('fieldset.section_delivery_time').show();
    } else {
        $('fieldset.section_delivery_address').hide();
        $('fieldset.section_delivery_time').hide();
    }
    $('input.fld_shipping_selection').click(function() {
        if ($(this).val() == 'delivery') {
            $('fieldset.section_delivery_address').show();
            $('fieldset.section_delivery_time').show();
        } else {
            $('fieldset.section_delivery_address').hide();
            $('fieldset.section_delivery_time').hide();
        }
    });
    
    if ($('input.fld_shipping_selection[value=collection]').is(':checked')) {
        $('fieldset.section_collection_location').show();
        $('fieldset.section_collection_time').show();
    } else {
        $('fieldset.section_collection_location').hide();
        $('fieldset.section_collection_time').hide();
    }
    $('input.fld_shipping_selection').click(function() {
        if ($(this).val() == 'collection') {
            $('fieldset.section_collection_location').show();
            $('fieldset.section_collection_time').show();
        } else {
            $('fieldset.section_collection_location').hide();
            $('fieldset.section_collection_time').hide();
        }
    });
    
    $('textarea.fld_comments').each(function() {
        var countfield = $('#' + $(this).attr('name') + '_signs_left_count');
        textLimiter($(this), countfield, 300);
        $('#' + $(this).attr('name') + '_signs_left').show();
        
        $(this).keydown(function() {
            textLimiter($(this), countfield, 300);
        });
        $(this).keyup(function() {
            textLimiter($(this), countfield, 300);
        });
    });
});
