$(document).ready(function() {
    // topNav fix menu items width
    var topNavWidth = 900;
    var topNavAntal = $('#topNav li').not('.last, .search').size();           // Get number of menu items, exept search and last
    var topNavLiSearch = $('#topNav li.search').outerWidth();                 // Get width of search
    var topNavLiLast = $('#topNav li.last').outerWidth();                     // Get width of last
    var topNavLiWidth = Math.floor((topNavWidth - ((topNavLiLast + topNavLiSearch))) / topNavAntal);  // Calculate width of menu items
    var totalWidth = (topNavLiWidth * topNavAntal) + (topNavLiLast + topNavLiSearch);
    $('#topNav li').not('.last, .search').each(function() {
        $(this).width(topNavLiWidth - 1);
    });
    if (totalWidth < topNavWidth) {
        var lastWidth = ($('#topNav li.last').width() + topNavWidth - totalWidth);
        $('#topNav li.last').css('width', lastWidth);
    }

    // Safari fix
    if ($.browser.safari)
        $('#topNav li').not('.search').css('padding', '9px 0 1px');

    // TopNav Share Hover
    $('#topNav li.last').hover(function() {
        $('.searchArrow').attr('src', '/Templates/Images/Layout/share_arrow_right_mo.gif');
    }, function() {
        $('.searchArrow').attr('src', '/Templates/Images/Layout/share_arrow_right.gif');
    });

    // LeftNav .guide Hover
    $('#leftNav .guide a').hover(function() {
        $(this).children('img').attr('src', '/Templates/Images/Layout/arrow_right_gray.gif');
    }, function() {
        $(this).children('img').attr('src', '/Templates/Images/Layout/arrow_right_blue.gif');
    });
    // Callout Hover
    $('.callout a').hover(function() {
        $(this).children('.calloutCopy').children('img').attr('src', '/Templates/Images/Layout/arrow_right_yellow2.gif');
    }, function() {
        $(this).children('.calloutCopy').children('img').attr('src', '/Templates/Images/Layout/arrow_right_blue.gif');
    });

    $('.search input').searcher();

    $('.swapper').hover(function() {
        //alert(this.id);
        swapMeter(this.id);
    }, function() {
        swapMeter('Meter');
    });
    $('#rolloverMeter').hover(function() { }, function() {
        swapMeter('Meter');
    });
    $('.hoverInfo').hover(function() {
        swapMeter('Meter');
    });
    $('.spacer').hover(function() {
        var cid = $(this).attr('rel');
        swapMeter(cid);
    });


    // 360 / Hotspot toggle
    $('.intHintsBlock a').click(function() {
        var w = $(this).attr('class');
        if (w == 'goto360') {
            $('#gotoHotSpot').hide();
            $('#goto360').show();
        } else {
            $('#goto360').hide();
            $('#gotoHotSpot').show();
        }
        return false;
    });
    $('.swapDiv').click(function() {
        $('#gotoHotSpot, #goto360').toggle();
        return false;
    });


    // Rating stars
    $('.ratingBlock .star a').click(function() {
        var rating = $(this).text();
        alert(rating);
        return false;
    });

    // Fancybox
    $('.knowledge').fancybox({
        'hideOnContentClick': false,
        'frameWidth': 343,
        'frameHeight': 258,
        'overlayOpacity': .75
    });

    $('#footer ul').each(function() {
        $('li:last span', this).remove();
    });

});



$(window).load(function() {
    // Fix for blueBox more-links padding
    var $blueBoxHeight = $('.blueMore').innerHeight() + 10;
    $('.blueCopy').css('padding-bottom', $blueBoxHeight);
    if ($.browser.msie && Math.floor($.browser.version) == 7) {
        $('.bluePanelBottom').css('bottom', '-21px');
    }
    // Tip Block center
    $('.tipBlock').setPlacement();

    // LeftNav height fix
    $('#leftNav, #rightContent').equalHeight();
});






$.fn.setPlacement = function() {
    return this.each(function() {
        var relTop = parseInt($(this).attr('rel'));
        relTop = (!relTop) ? 100 : relTop;
        $(this).css('top', relTop);
        $(this).show();
    });
}

function swapMeter(m) {
    $('.swapper').hide();
    $('#rolloverMeter').removeClass().addClass('rollover' + m);
    $('#' + m).css('display', 'block');
}


// Search box on focus fix
$.fn.searcher = function() {
    return this.focus(function() {
        if (this.value == this.defaultValue) {
            this.value = '';
        }
    }).blur(function() {
        if (!this.value.length) {
            this.value = this.defaultValue;
        }
    });
}

// Fix for leftNav height
$.fn.equalHeight = function() {
    var height = 0;
    var maxHeight = 0;

    // Store the tallest element's height
    this.each(function() {
        height = $(this).outerHeight();
        maxHeight = (height > maxHeight) ? height : maxHeight;
    });

    // Set element's min-height to tallest element's height
    return this.each(function() {
        var t = $(this);
        var minHeight = maxHeight - (t.outerHeight() - t.height());
        var property = $.browser.msie && $.browser.version < 7 ? 'height' : 'min-height';
        var minHeight = $.browser.msie && $.browser.version < 7 ? (minHeight - 2) : minHeight;
        t.css(property, minHeight + 'px');
    });
}


function share() {
    $('.trigger a').trigger('click');
}

function onAfter() {
    sIFR.replace(tradeGothicBold, {
        selector: '.featBlock h3.title',
        css: '.sIFR-root { color: #009ddc; }',
        wmode: 'transparent'
    });
}












