
if (window.location.toString().indexOf('empirestaging') < 0) {
    try {
        var pageTracker = _gat._getTracker("UA-0000000-0");
        pageTracker._trackPageview();
    } 
    catch (err) {
    }
}

//Does a given selector exists  
$.exists = function(selector){
    return ($(selector).length > 0);
}


function createCookie(name, value, days){
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else 
        var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name){
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') 
            c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) 
            return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name){
    createCookie(name, "", -1);
}

$(document).ready(function(){



    // Activate SuperFish
    $("ul.sf-menu").supersubs({
        minWidth: 12,
        maxWidth: 27,
        extraWidth: 1
    }).superfish({
        animation: {
            height: 'show'
        },
        speed: 'fast',
        delay: 500
    });
    
    
    
    
    /**
     * Splash fadeIn
     */
    
    //Test to see if cookie exists and we are on the home page
    if ((readCookie('isSplashLoaded') === null) && (homePage === true )) {
            //fades the splash page in
            $('#splash_wrapper').show();
            $('#splash').hide(0);
            $('#splash').fadeIn(2000);         
            
            //fades the splash page out after 4000ms if no click is made 
            function fader(){
                $('#splash').fadeOut(1000, function(){
                    $('#splash_wrapper').fadeOut(2000);
                })
            };
            
            //initialize the timer  
            var timer = window.setTimeout(fader, 4000);
            
            //creates isSplashLoaded
            createCookie('isSplashLoaded', 'true');
        }
        else {
            $('#splash').fadeOut(0);
            $('#splash_wrapper').fadeOut(0);
        }

    
    //fades the splash page out with a click
    $('#splashLink').click(function(){
        window.clearTimeout(timer)
        $('#splash').animate().css('color:', '#FFF').fadeOut(0);
        $('#splash_wrapper').fadeOut(4000);
        return false
    });


    /**
     * Create up/down scroll navigation
     */
    // get the navigation elements  
    var homepagePanelElement = $('#scroller_arrows a');
    var homepagePanelActive = $('#scroller_arrows a.selected');
    //Grays out down arrow on load
    $('#homepageScrollDown').show(0);
    $('#homepageScrollDown').fadeTo(200, 0.2);
    
    
    // perform the scrolling to the different elements
    homepageSlider = function(prevElm, selectedElm){
        $(prevElm).removeClass('selected');
        $(selectedElm).addClass('selected');
        $('#banner').scrollTo($(selectedElm).attr('href'), 400);
    }
    
    // scroll Up
    $('#homepageScrollUp').click(function(){
    
    
        var currentSib = homepagePanelActive.next();
        
        
        if (homepagePanelActive.text() < 3) {
            $('#homepageScrollDown').fadeTo(200, 1);
        }
        else 
            if (homepagePanelActive.text() == 3) {
                $('#homepageScrollUp').fadeTo(200, 0.2);
            }
        if (currentSib.length == 0) {
            currentSib = $(homepagePanelElement.get(0));
            return false;
        }
        
        homepageSlider(homepagePanelActive, currentSib);
        homepagePanelActive = currentSib;
        
        return false;
    });
    
    // scroll Down
    $('#homepageScrollDown').click(function(){
    
        var currentSib = homepagePanelActive.prev();
        
        
        if (homepagePanelActive.text() > 3) {
            $('#homepageScrollUp').fadeTo(200, 1);
        }
        else 
            if (homepagePanelActive.text() <= 2) {
                $('#homepageScrollDown').fadeTo(200, 0.2);
            }
        
        if (currentSib.length == 0) {
            currentSib = $(homepagePanelElement.get(homepagePanelElement.length - 1));
            return false;
        }
        
        homepageSlider(homepagePanelActive, currentSib);
        homepagePanelActive = currentSib;
        
        return false;
    });
    
    
    
    /**
     * This script is used to show/hide the home page popup
     */
    $('img.scroller_arrow').click(function(){
        $('.scroller_popup_wrapper').fadeIn();
        $('.scroller_popup_wrapper').show();
        $('.scroller_popup').fadeIn(200);
        $('.scroller_popup').show(200);
    });
    
    $('.close_popup').click(function(){
        $('.scroller_popup_wrapper').fadeOut();
        $('.scroller_popup').fadeOut(200);
    });
    
    
    // Window resize for home page
    $(window).resize(function(){
        resizePanel();
    });
    
    
    
    /**
     * Get relative position of the mouse click to an element
     * @param {Object} elementID - pass in the selector for the object
     * @param {Object} mouseX - mouse clicked x
     * @param {Object} mouseY - mouse clicked y
     */
    rPosition = function(elementID, mouseX, mouseY){
        var offset = $(elementID).offset();
        var x = mouseX - offset.left;
        var y = mouseY - offset.top;
        
        return {
            'x': x,
            'y': y
        };
    }
    
    
    
    
    
    /**
     * script to align the heights of the home three column layout below the banner. Calculates the total heights
     * of both div boxes to ensure that they always line-up
     */
    if ($.exists('#title') && $.exists('.section_info')) {
    
        var colTitle = $('#title').height();
        var colInfo = $('.section_info').height();
        
        /*apply the height fix to either the main content or the sidebar appropriately*/
        if (colTitle > colInfo) {
            $('.section_info').css('height', colTitle);
        }
        else {
            $('#title').css('height', colInfo);
        }
        
    }
    
    /**
     * Expertise Methodology Animated Widget
     */
    //hide blurb element on load
    
    $('#discover_on').hide();
    $('#plan_on').hide();
    $('#execute_on').hide();
    $('#measure_on').hide();
    
    
    $('#discover').mouseenter(function(){
        $('#discover_on').fadeIn(400);
        $('.blurb.discover').fadeIn(0);
    });
    $('#discover').mouseleave(function(){
        $('#discover_on').fadeOut(400);
        $('.blurb.discover').hide();
    });
    
    $('#plan').mouseenter(function(){
        $('#plan_on').fadeIn(400);
        $('.blurb.plan').fadeIn(0);
    });
    $('#plan').mouseleave(function(){
        $('#plan_on').fadeOut(400);
        $('.blurb.plan').hide();
    });
    
    $('#execute').mouseenter(function(){
        $('#execute_on').fadeIn(400);
        $('.blurb.execute').fadeIn(0);
    });
    $('#execute').mouseleave(function(){
        $('#execute_on').fadeOut(400);
        $('.blurb.execute').hide();
    });
    
    $('#measure').mouseenter(function(){
        $('#measure_on').fadeIn(400);
        $('.blurb.measure').fadeIn(0);
    });
    $('#measure').mouseleave(function(){
        $('#measure_on').fadeOut(400);
        $('.blurb.measure').hide();
    });
    
    /**
     * Expertise Capabilities Animated Widget
     */
    //hide blurb element on load
    if ($.exists('.blurb')) {
        $('.blurb').hide();
    }
    
    var currentCapabiltiesClass = "baseBackgroundImage";
    
    
    
    //Our homegrown image map
    $('.wrapper.capabilities').mousemove(function(e, i){
        //check for the x and y position of the click and call appropriate location 
        cords = rPosition('.wrapper.capabilities', $(e).get(0).pageX, $(e).get(0).pageY);
        
        
        
        //Media Rollover
        if ((cords.x > 50) && (cords.x <= 150) && (cords.y > 130) && (cords.y < 230) && (currentCapabiltiesClass != 'mediaBackgroundImage')) {
            $(e.target).fadeOut(0, function(){
                $(e.target).fadeIn(400);
                $(e.target).removeClass(currentCapabiltiesClass);
                $(e.target).addClass('mediaBackgroundImage');
                currentCapabiltiesClass = 'mediaBackgroundImage';
                $('.blurb').hide();
                $('.blurb.media').show();
            });
        }
        
        //Consumer Rollover
        else 
            if ((cords.x > 150) && (cords.x <= 265) && (cords.y > 70) && (cords.y < 130) && (currentCapabiltiesClass != 'consumerBackgroundImage')) {
                $(e.target).fadeOut(0, function(){
                    $(e.target).fadeIn(400);
                    $(e.target).removeClass(currentCapabiltiesClass);
                    $(e.target).addClass('consumerBackgroundImage');
                    currentCapabiltiesClass = 'consumerBackgroundImage';
                    $('.blurb').hide();
                    $('.blurb.consumer').show();
                });
            }
            
            //Creative Rollover
            else 
                if ((cords.x > 250) && (cords.x <= 400) && (cords.y > 130) && (cords.y < 230) && (currentCapabiltiesClass != 'creativeBackgroundImage')) {
                    $(e.target).fadeOut(0, function(){
                        $(e.target).fadeIn(400);
                        $(e.target).removeClass(currentCapabiltiesClass);
                        $(e.target).addClass('creativeBackgroundImage');
                        currentCapabiltiesClass = 'creativeBackgroundImage';
                        $('.blurb').hide();
                        $('.blurb.creative').show();
                    });
                }
                //PR Rollover
                else 
                    if ((cords.x > 230) && (cords.x <= 419) && (cords.y > 280) && (cords.y < 400) && (currentCapabiltiesClass != 'prBackgroundImage')) {
                        $(e.target).fadeOut(0, function(){
                            $(e.target).fadeIn(400);
                            $(e.target).removeClass(currentCapabiltiesClass);
                            $(e.target).addClass('prBackgroundImage');
                            currentCapabiltiesClass = 'prBackgroundImage';
                            $('.blurb').hide();
                            $('.blurb.pr').show();
                        });
                    }
                    //Social Rollover
                    else 
                        if ((cords.x > 120) && (cords.x <= 210) && (cords.y > 280) && (cords.y < 400) && (currentCapabiltiesClass != 'socialBackgroundImage')) {
                            $(e.target).fadeOut(0, function(){
                                $(e.target).fadeIn(400);
                                $(e.target).removeClass(currentCapabiltiesClass);
                                $(e.target).addClass('socialBackgroundImage');
                                currentCapabiltiesClass = 'socialBackgroundImage';
                                $('.blurb').hide();
                                $('.blurb.social').show();
                            });
                        }
        
    })
    
    
    /**
     * Most jQuery.serialScroll's settings, actually belong to jQuery.ScrollTo, check it's demo for an example of each option.
     * @see http://flesler.demos.com/jquery/scrollTo/
     * You can use EVERY single setting of jQuery.ScrollTo, in the settings hash you send to jQuery.serialScroll.
     */
    /**
     * The plugin binds 6 events to the container to allow external manipulation.
     * prev, next, goto, start, stop and notify
     * You use them like this: $(your_container).trigger('next'), $(your_container).trigger('goto', [5]) (0-based index).
     * If for some odd reason, the element already has any of these events bound, trigger it with the namespace.
     */
    /**
     * IMPORTANT: this call to the plugin specifies ALL the settings (plus some of jQuery.ScrollTo)
     * This is done so you can see them. You DON'T need to specify the commented ones.
     * A 'target' is specified, that means that #screen is the context for target, prev, next and navigation.
     */
    var scrollStart = 0;
    
    if (window.location.href.indexOf("#") != -1) {
        // Your code in here accessing the string like this
        scrollStart = parseInt(window.location.href.substr(window.location.href.indexOf("#") + 1))
        
    }
    
    $('#screen').serialScroll({
        target: '#sections',
        items: 'div.section_wrapper', // Selector to the items ( relative to the matched elements, '#sections' in this case )
        prev: 'img.prev',// Selector to the 'prev' button (absolute!, meaning it's relative to the document)
        next: 'img.next',// Selector to the 'next' button (absolute too)
        axis: 'xy',// The default is 'y' scroll on both ways
        navigation: '#navigation li a',
        duration: 300,// Length of the animation (if you scroll 2 axes and use queue, then each axis take half this time)
        force: true, // Force a scroll to the element specified by 'start' (some browsers don't reset on refreshes)
        //easing:'easeOutQuart', //use this easing equation for a funny effect
        //queue:false,// We scroll on both axes, scroll both at the same time.
        //event:'click',// On which event to react (click is the default, you probably won't need to specify it)
        //stop:false,// Each click will stop any previous animations of the target. (false by default)
        lock: true, // Ignore events if already animating (true by default)    
        start: scrollStart, // On which element (index) to begin ( 0 is the default, redundant in this case )   
        cycle: false,// Cycle endlessly ( constant velocity, true is the default )
        step: 1, // How many items to scroll each time ( 1 is the default, no need to specify )
        //jump:false, // If true, items become clickable (or w/e 'event' is, and when activated, the pane scrolls to them)
        lazy: false,// (default) if true, the plugin looks for the items on each event(allows AJAX or JS content, or reordering)
        //interval:1000, // It's the number of milliseconds to automatically go to the next
        //constant:true, // constant speed
        
        onBefore: function(e, elem, $pane, $items, pos){
            /**
             * 'this' is the triggered element
             * e is the event object
             * elem is the element we'll be scrolling to
             * $pane is the element being scrolled
             * $items is the items collection at this moment
             * pos is the position of elem in the collection
             * if it returns false, the event will be ignored
             */
            //those arguments with a $ are jqueryfied, elem isn't.
            e.preventDefault();
            if (this.blur) 
                this.blur();
            
            window.location.href = (window.location.href.substr(0, window.location.href.indexOf("#"))) + '#' + pos
            
        },
        onAfter: function(elem){
            //'this' is the element being scrolled ($pane) not jqueryfied
        }
    });
    
    
    /**
     * This script hides the previous or last button when a page loads that uses serialScroll
     */
    //Initialize vars used to determine serialScolls position vs. its length
    var urlLength = ((window.location.href.length) - 2);
    var urlPosition = window.location.href.substr(urlLength, 2);
    var portfolioItems = ($('.section_wrapper').length - 1);
    
    //Test if we are at the last of the portfolio    
    if ((urlPosition == portfolioItems) == true) {
        $('.next').fadeTo(200, 0.3);
    }
    //Test if we are at the first item in the portfolio
    else 
        if (urlPosition == "#0") {
            $('.prev').fadeTo(200, 0.3);
        }
    
    
    /**
     * Colorbox lightbox used for advertising assets
     */
    $(".asset").colorbox();
    
    
    
});//End $(document).ready
function resizePanel(){

    width = $(window).width();
    height = $(window).height();
    
    mask_height = height * $('.item').length;
    
    $('#debug').html(width + ' ' + height + ' ' + mask_height);
    
    $('#banner, .item').css({
        width: width,
        height: height
    });
    $('#mask').css({
        width: width,
        height: mask_height
    });
    $('#banner').scrollTo($('a.selected').attr('href'), 0);
    
}

