﻿// JScript File


//Workaround for jQuery bug reporting IE7 (and 8?) as IE6
$.browser.msie6 = $.browser.msie && /MSIE 6\.0/i.test(window.navigator.userAgent) && !/MSIE 7\.0/i.test(window.navigator.userAgent);

var $animatedbanner;

$(document).ready(function(){
    loadAnimatedBanners();
});

$.fn.safeFadeIn = function (speed, callback) {
    this.find("li").animate({ opacity: 'show' }, speed);
    return this.animate({ opacity: 'show' }, speed, function () {
        if (jQuery.browser.msie)
            this.style.removeAttribute('filter');
        if (jQuery.isFunction(callback))
            callback();
    });
};

$.fn.safeFadeOut = function (speed, callback) {
    this.find("li").animate({ opacity: 'hide' }, speed);
    return this.animate({ opacity: 'hide' }, speed, function () {
        if (jQuery.browser.msie)
            this.style.removeAttribute('filter');
        if (jQuery.isFunction(callback))
            callback();
    });
};

function loadAnimatedBanners() {
    $.get('banners.html', {cachebuster: parseInt(Math.random()*99999999)}, onBannersLoaded);
};

function onBannersLoaded(data) {
    var $bannerHTML = data;
    $('#bannercontainer').append($bannerHTML);
    $animatedbanner = $('#animatedbanner');
    $animatedbanner.hide();
    var $bannerImages = $animatedbanner.find('img');
    var loadedCount = 0;
    
    $bannerImages.each(function() {
        $(this).load(function() {
            loadedCount++;
            if(loadedCount == $bannerImages.length) startAnimation();
        });
    });
};

function startAnimation() {
    $('.staticbanner').hide();
    $('#animatedbanner').innerfade({
        animationtype: 'fade',
        speed: 3000,
        timeout: 6000,
        type: 'sequence',
        containerheight: '191px',
        runningclass: 'marketingbanner'
    });
    $animatedbanner.show();
};

// Selector Extender: QueryString Contains [parameter] = [value].
// Usage $(":queryStringParam(param,value)")
// Requires jQuery.url plugin (http://projects.allmarkedup.com/jquery_url_parser/)
$.extend($.expr[':'], {
    queryStringParam: function(a, i, m) {
        if(!a.href) return false; // If the anchor doesn't have a URL, return false
        if(!m[3]) {return false}; // If no parameters passed in, return false
        arguments = m[3].split(','); //split and assign to into an array (paramName,value)
        return ($.url.setUrl(a.href).param(arguments[0]) == arguments[1]);
    }
});


var $hashParams = new Object;

function getLocationHashParam(paramName) {
    return $.url.setUrl('?' + location.hash.substr(1,location.hash.length-1)).param(paramName); //retrieve the hash, remove the # and prefix with ? to create a querystring
};

function setHashParam(paramName,value) {
    if (!paramName) return false;
    $hashParams[paramName] = value;
    var hashString = new String;
    for (var key in $hashParams) { hashString += (key + '=' + $hashParams[key] + '&'); };
    location.hash = hashString;
    hashString += 'ajaxcall'; //allows us to differentiate Ajax pages when viewed in Google Analytics.
    gaTrackAjaxPage($.url.setUrl(location).attr('path') + '?' + hashString);
    return false;
};

function isScrolledIntoView(elem)
{
    var docViewTop = $(window).scrollTop();
    var docViewBottom = docViewTop + $(window).height();

    var elemTop = $(elem).offset().top;
    var elemBottom = elemTop + $(elem).height();

    return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom));
}
