var animate        = false;
var flashBoxHTML   = '';
var tb_pathToImage = '/templates/der-haendler/img/loadingAnimation.gif';

$(document).ready(function(){
    var ie6 = false;
    if($.browser.msie && $.browser.version=="6.0") ie6 = true;

    $(document).pngFix();

    if(typeof reseller != 'undefined') {
        $('body').css({margin:0,padding:0});
        $("select[name='package']").change( function(){
            var v = $(this).val();
            $('.disabled', top.document).removeClass('disabled');
            $('.package img', top.document).each(function(i){
                $(this).attr({'src' : $(this).attr('src').replace('_grey', '') });
                if(v == 1 && i == 1) {
                    $('#p2', top.document).addClass('disabled');
                    $(this).attr({'src' : $(this).attr('src').replace('.jpg', '_grey.jpg') });
                }
                if(v == 2 && i == 0) {
                    $('#p1', top.document).addClass('disabled');
                    $(this).attr({'src' : $(this).attr('src').replace('.jpg', '_grey.jpg') });
                }
            });
        });
    }

    $.thickBoxInit('a.thickbox, area.thickbox, input.thickbox');
    
    $('#backlink').html('<a href="index.php" onclick="history.back(1);return false;">zur&uuml;ck</a>');

    $('.co_options tr').hover(
        function () {
            $(this).addClass('moduleRowOver');
        }, 
        function () {
            $(this).removeClass('moduleRowOver');
        }
    );

    $('.co_options tr').click(
        function () {
            $('.co_options :radio').removeAttr('checked');
            $(':radio', this).attr('checked', 'checked');
            $('.co_options tr').removeClass('moduleRowSelected');
            $(this).addClass('moduleRowSelected');
            if(!animate) {
                $(this).effect('highlight', {}, 200);
            }
        }
    );

    $('.co_options :radio').click(
        function () {
            $('.co_options tr').removeClass('moduleRowSelected');
            $(this).parent().parent().addClass('moduleRowSelected');
            animate = true;
            $(this).parent().parent().effect(
                'highlight', 
                 {}, 
                 500, 
                 function(){
                    animate = false;
            });
        } 
    );

    /* Tabs */
    $('.jstabs').accessibleTabs({
        wrapperClass: 'jstabs-content',
        currentClass: 'current',
        tabhead: 'h4',
        tabbody: '.jstabs-body',
        fx: 'fadeIn',
        fxspeed: 'normal',
        currentInfoText: 'Aktueller Tab: ',
        currentInfoPosition: 'prepend',
        currentInfoClass: 'jstabs-current-info'
    });


    if($('#premium').length) {

        $('#premium #issues li').each(function(i) {
            var issue   = $(this).attr('id').replace('i', '');
            var greyImg = $('<img class="greyscale" src="/templates/der-haendler/img/premium/der_haendler_band_' + issue + '_greyscale.png" alt="" width="100" height="148" />');
            greyImg.appendTo(this).css({ opacity : '0'}).hide();
        })

        if(!ie6) {

            $('<div id="scope_p1" class="scope"></div><div id="scope_p2" class="scope"></div><div id="scope_p3" class="scope"></div>').appendTo('#issues').hide();

            $('#btn_p1 a').hover(
                function() {
                    $(this).parent().addClass('over');
                    $('.btn:not(".over")').hide();
                    $('#current_release').hide();
                    $('#price_2, #price_3').hide();
                    $('#i4 img:not(".greyscale")').show();
                    $('#scope_p1').show();
                    $('#i1 .greyscale, #i2 .greyscale, #i3 .greyscale').show().animate({ opacity: 1.0 });
                },
                function() {
                    $('#scope_p1').hide();
                    $('#i4 img:not(".greyscale")').hide();
                    $('#current_release').show();
                    $('#price_2, #price_3').show();
                    $('.btn:not(".over")').fadeIn();
                    $(this).parent().removeClass('over');
                    $('#i1 .greyscale, #i2 .greyscale, #i3 .greyscale').css({ opacity : '0'}).hide();
                }
            );
    
            $('#btn_p2 a').hover(
                function() {
                    $(this).parent().addClass('over');
                    $('.btn:not(".over")').hide();
                    $('#current_release').hide();
                    $('#price_1, #price_3').hide();
                    $('#i4 img:not(".greyscale")').show();
                    $('#scope_p2').show();
                    $('#i5 .greyscale, #i6 .greyscale').show().animate({ opacity: 1.0 });
                },
                function() {
                    $('#scope_p2').hide();
                    $('#i4 img').hide();
                    $('#current_release').show();
                    $('#price_1, #price_3').show();
                    $('.btn:not(".over")').fadeIn();
                    $(this).parent().removeClass('over');
                    $('#i5 .greyscale, #i6 .greyscale').css({ opacity : '0'}).hide();
                }
            );
    
            $('#btn_p3 a').hover(
                function() {
                    $(this).parent().addClass('over');
                    $('.btn:not(".over")').hide();
                    $('#current_release').hide();
                    $('#price_1, #price_2').hide();
                    $('#i4 img:not(".greyscale")').show();
                    $('#scope_p3').show();
                    $('#i1 .greyscale, #i2 .greyscale').show().animate({ opacity: 1.0 });
                },
                function() {
                    $('#scope_p3').hide();
                    $('#i4 img').hide();
                    $('#current_release').show();
                    $('#price_1, #price_2').show();
                    $('.btn:not(".over")').fadeIn();
                    $(this).parent().removeClass('over');
                    $('#i1 .greyscale, #i2 .greyscale').css({ opacity : '0'}).hide();
                }
            );
        }

        $('#btn_p1 a').click(function() {
            if(pageTracker)
                pageTracker._trackEvent('PremiumPackages', 'PackageView', 'Premium 1');
            window.location.href=this.href;
            return false;
        });

        $('#btn_p2 a').click(function() {
            if(pageTracker)
                pageTracker._trackEvent('PremiumPackages', 'PackageView', 'Premium 2');
            window.location.href=this.href;
            return false;
        });

        $('#btn_p3 a').click(function() {
            if(pageTracker)
                pageTracker._trackEvent('PremiumPackages', 'PackageView', 'Premium 3');
            window.location.href=this.href;
            return false;
        });
    }

});

$.thickBoxInit = function(domChunk) {

	$(domChunk).click(function(){
        var t = this.title || this.name || null;
        var a = this.href || this.alt;
        var g = this.rel || false;

        if($.browser.msie) {
            flashBoxHTML = $('#flash').html();
            $('#flash').html('');
        }
        tb_show(t,a,g);
        this.blur();
        return false;
    });
}

$.popItUp = function(url, w, h) {
    if(undefined == w) w = 620;
    if(undefined == h) h = 380;
    p = window.open(url, 'PopUp','width=' + w +',height=' + h + ',left=150,top=100,scrollbars=no');
    p.focus();
}
