﻿var IE6FLAG = ($.browser.msie && parseFloat($.browser.version) < 7) ? true : false;
/*** CUFON ***/
Cufon.replace('.helvLight', { fontFamily: 'helvetica-light' });
Cufon.replace('.helvRoman', { fontFamily: 'helvetica-roman' });

function getQueryString() {
    var result = {},
    queryString = location.search.substring(1),
    re = /([^&=]+)=([^&]*)/g, m;
    while (m = re.exec(queryString)) {
        result[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
    }
    return result;
}

//Find any images with a pinToBottom class and 'pins' them to the bottom of their conainer, whilst allowing existing content to wrap around it.
var pinToBottom = function () {
    $('.pinToBottom').load(function () {
        var theItem = $(this);
        var width = theItem.outerWidth(true);
        var height = theItem.outerHeight(true);
        var xPadding = theItem.parent().css('padding-left');
        var position = theItem.css('float');
        var replacement = $('<div style="width:' + width + 'px;height:' + height + 'px;float:' + position + '"></div>').insertBefore(theItem);      
        theItem.css({
            'position': 'absolute',
            'bottom': 0,
            'padding': 0
        });
        if (position === 'right') {
            theItem.css('right', xPadding);
        } else {
            theItem.css('left', xPadding);
        }
    });
}

var searchBox = function () { 
    //JQuery for the global search box
    $('#header .globalSearch .text').val('Search the Everest site :').focus(function () {
        $(this).parent().css('backgroundPosition', 'bottom');
        $(this).val('');
    }).blur(function () {
        $(this).parent().css('backgroundPosition', 'top');
        if ($(this).val() == '') $(this).val('Search the Everest site :');
    });
}

var slideShow = function () {
    if ($('.slideShow').length != 0) {
        var slideEffect = function () {
            $('.slideShow .slide:last').animate({ 'opacity': 0 }, function () {
                $(this).prependTo('.slideShow').css('opacity', 1);
            });
        }
        setInterval(function () { slideEffect(); }, 5000);
    };
}

var bigLink = function () {
    var container = $('.bigLink');
    container.each(function () {
        var getLink = $(this).find('a').attr('href');
        $(this).css('cursor', 'pointer')
        .click(
        function () {
        window.location = (getLink);
    });
    });
}

var gridLinks = function () { //Turn cells in the homepage grid layout into big links
    if ($('.twoUpGrid').length != 0) {
        var cell = $('.twoUpGrid > div')
    } else {
        var cell = $('.threeUpGrid > div')
    }
    cell.each(function () {
        var getLink = $(this).find('a').attr('href');
        var trackingTag = $(this).find('h3 a').html();
        $(this).css('cursor', 'pointer')
        .hover(
            function () {
                $(this).addClass('hover').css('background', 'url(/images/Q211/home-page/threeupgrid_hoverbg.jpg) repeat-x top');
            },
            function () {
                $(this).removeClass('hover').css('background', 'none');
            }
        )
        .click(
        function () {
            window.location = (getLink);
            _gaq.push(['_trackEvent', 'Internal Campaigns', 'NewHomePod', trackingTag]);
        });
    });
}

$(function () { // Start the page
    gridLinks();
    bigLink();
    slideShow();
    searchBox();
    //pinToBottom();
    if (IE6FLAG) {
        // Perform actions in IE6 only

        // Enables the hover menus for IE6
        $('.mainNavigation li').hover(
        function () { $(this).addClass('hover'); },
        function () { $(this).removeClass('hover'); }
        )
    } else {
        // Perform actions in all browsers BUT IE6 
    };
});
