﻿
var IMC2 = new Object();
IMC2.initialLoad = true;
IMC2.version = '186';
IMC2.contact = false;
IMC2.loaded = false;
IMC2.homePaths = ['/', '/default.aspx'];
IMC2.prevCid = location.hash;

jQuery('html').addClass('js');

//log message to console (instead of alert).  works in Firebug
function logItem(msg)
{
	if ( window.console )
	{
		try
		{
			console.log(msg);
		}
		catch(cErr) {}
	}
}

$(document).ready(function()
{

    //set up and embed main Flash element
    var flashvars = {};
    flashvars.version = IMC2.version;
    flashvars.debug = 'false';
    flashvars.siteRoot = '/';
    flashvars.swfPath = '/mm/Imc2.swf';
    flashvars.xmlPath = 'xml/data-v21.xml?version=' + IMC2.version;
    flashvars.bgPath = '/mm/imc2BG.swf';
    flashvars.bgStaticPath = '/mm/imc2BGContact.swf';
    flashvars.homeContent = '/mm/imc2Carousel.swf';
    flashvars.deepLinkID = '';
    flashvars.showOpening = 'false';

    var linkTestHash = window.location.hash.toLowerCase();
    var linkTestPath = window.location.pathname.toLowerCase();
    if (linkTestHash && linkTestHash.length > 1)
    {
        flashvars.deepLinkID = linkTestHash.substring(1);
        IMC2.initialLoad = false;
    }
    else if ($.inArray(linkTestPath, IMC2.homePaths) == -1)
    {
        flashvars.deepLinkID = IMC2.loaded.pageID;
    }
    else
    {
        flashvars.showOpening = 'true';
    }

    var params = {};
    params.quality = 'high';
    params.allowscriptaccess = 'always';
    params.wmode = 'opaque';
    //params.scale = 'noborder';
    //params.salign = 't';

    var attributes = {};

    function nonFlashVersion(e)
    {
        if (e.success == false)
        {
            navigationClick(IMC2.loaded.pageID);
        }
    }

    if (IMC2.contact)
    {
        flashvars.bgPath = '/mm/imc2BGContact.swf';
        swfobject.embedSWF(
						    '/mm/Imc2Contact.swf?v=' + IMC2.version,
						    'imc2FlashHolder', '1300', '1050', '9', false,
						    flashvars, params, attributes, nonFlashVersion);
    }
    else
    {
        swfobject.embedSWF(
					    '/mm/Imc2Stub.swf?t=' + (new Date()).getTime(),
					    'imc2FlashHolder', '2000', '1200', '9', false,
					    flashvars, params, attributes, nonFlashVersion);
    }
    //END - set up and embed main Flash element


    //set variables to use for the gigya share API
    var shareDescription;
    var shareTitle;
    var shareUrl = location.href;


    //start of gigya API    
    var pconf =
    {
        networksToShow: 'facebookshare,twitter,digg,delicious,livebookmarks,stumbleupon,myaol,googlebookmarks,misterwong',
        useFacebookMystuff: 'false',
        UIConfig: '<config baseTheme="v2"><display mixNetworks="true" showCodeBox="false" showCloseButton="true"></display></config>'
    };


    //Share Button for gigya Share popup
    $('li#btnShare a').live('click', function(cevt)
    {

        jqShareFeature1 = jQuery('#shareButtonWrapper');
        jqShareFeature1.toggleClass('activeShare');

        cevt.preventDefault();

        if (jqShareFeature1.hasClass('activeShare'))
        {
            Wildfire.onClose = function(evt)
            {
                jqShareFeature1.removeClass('activeShare');
            };
            Wildfire.onPostProfile = function(evt)
            {
                jqShareFeature1.removeClass('activeShare');
            };
            Wildfire.initPost('1075611', 'shareButtonWrapper', 300, 225, pconf);
        }
    });
    $('a#footerShare').click(function(cevt)
    {

        jqShareFeature1 = jQuery('#footerShareWrapper');
        jqShareFeature1.toggleClass('activeShare');

        cevt.preventDefault();

        if (jqShareFeature1.hasClass('activeShare'))
        {
            Wildfire.onClose = function(evt)
            {
                jqShareFeature1.removeClass('activeShare');
            };
            Wildfire.onPostProfile = function(evt)
            {
                jqShareFeature1.removeClass('activeShare');
            };
            Wildfire.initPost('1075611', 'footerShareWrapper', 300, 225, pconf);
        }
    });


    //Footer Hover
    $('#footer li').hover(function()
    {
        $(this).find('ul').show();
    }, function()
    {
        $(this).find('ul').hide();
    });


    //brand sustainability map
    $('#brandMap div').live('mouseover', function()
    {
        var hrefID = $(this).attr('id');
        $(this).css('textIndent', '0px');
        if (hrefID == "emotionalRelationshipMap")
        {
            $('#brandMap').css('backgroundPosition', '0 -327px');
        }
        if (hrefID == "sustainableRelationshipMap")
        {
            $('#brandMap').css('backgroundPosition', '0 -655px');
        }
        if (hrefID == "limitedRelationshipMap")
        {
            $('#brandMap').css('backgroundPosition', '0 -983px');
        }
        if (hrefID == "reluctantRelationshipMap")
        {
            $('#brandMap').css('backgroundPosition', '0 -1310px');
        }

    });
    $('#brandMap div').live('mouseout', function()
    {
        $('#brandMap').css('backgroundPosition', '0 0');
        $(this).css('textIndent', '-10000px');
    });

    //brand sustainability map more
    $('#brandMapHover div').live('mouseover', function()
    {
        var hrefID = $(this).attr('id');
        $(this).css('textIndent', '0px');
        $(this).css('background', '#713781');
        if (hrefID == "emotionalRelationshipMapMore")
        {
            $('#brandMapMore').css('backgroundPosition', '-10px -513px');
        }
        if (hrefID == "sustainableRelationshipMapMore")
        {
            $('#brandMapMore').css('backgroundPosition', '-10px -1029px');
        }
        if (hrefID == "limitedRelationshipMapMore")
        {
            $('#brandMapMore').css('backgroundPosition', '-10px -1542px');
        }
        if (hrefID == "reluctantRelationshipMapMore")
        {
            $('#brandMapMore').css('backgroundPosition', '-10px -2062px');
        }

    });
    $('#brandMapHover div').live('mouseout', function()
    {
        $('#brandMapMore').css('backgroundPosition', '-10px 0');
        $(this).css('textIndent', '-10000px');
        $(this).css('background', 'transparent');
    });

    //5i Content ********
    $('#fiveiArrows img').live('click', function()
    {
        var arrowid = $(this).attr('class');
        $('#fiveIcolumn .fiveiInfo').removeClass('activeInfo');
        $('#fiveIcolumn').removeClass().addClass(arrowid);
        $('#' + $(this).attr('class') + 'Info').addClass('activeInfo');
    });

    IMC2.trackEvent = function(trkuri, trktitle)
    {
        _tag.dcsMultiTrack('DCS.dcsuri', window.location.pathname + trkuri, 'WT.ti', trktitle);
    }

    var contentHeight;
    var col1Height;
    var col2Height;
    var totalHeight;

    IMC2.updateContentHeight = function(evt)
    {
        jQuery('#siteWrapper').addClass('showMore');
        //contentHeight = $('.content').height();
        $('.moreContent').show();

        col1Height = $('#column1').height();
        col2Height = $('#column2').height();
        totalHeight = Math.max(col1Height, col2Height) + 200;

        if ($('#naviclasshldr').hasClass('about_people') || $('#naviclasshldr').hasClass('work_profiles'))
        {
            totalHeight = col1Height + 200;
        }

        if ($('#naviclasshldr').hasClass('meandyou_sustain') || $('#naviclasshldr').hasClass('meandyou_trust'))
        {
            totalHeight = col1Height + 230;
        }

        if ($('#naviclasshldr').hasClass('work_kudos') || $('#naviclasshldr').hasClass('news'))
        {
            var col3Height = $('#column3').height();
            totalHeight = col1Height + col3Height + 240;
        }

        if ($('.moreContent').children().hasClass('.fullWidth'))
        {
            col2Height = $('#column2').height();
            var fullWidth = $('.fullWidth').height();
            totalHeight = col2Height + fullWidth + 240;
        }

        $('#siteWrapper .readmoreBackground').css('height', totalHeight);
        $('#siteWrapper').css('min-height', totalHeight + 90);
    }

    $('#htmlWrapper .readMore a').live('click', function(evt)
    {
        IMC2.updateContentHeight();
        IMC2.trackEvent('/page/more/' + IMC2.loaded.pageID, 'read more: ' + IMC2.loaded.title);
        evt.preventDefault();
    });

    jQuery('.tab a').live('click', function(evt)
    {
        $('#siteWrapper').removeClass('showMore');
        $('#siteWrapper').css('min-height', '670px');

        var cid = $(this).attr('rel');
        if (cid)
        {
            var contentURL = '/Content.aspx?cid=' + cid;
            jQuery.get(contentURL, function(data)
            {
                var columnContent = jQuery(data).find('#columnContent').html();
                /*
                var divClass = jQuery('#overlay').children();
                if(divClass.hasClass('about_people'))
                {
                divClass.attr('class', 'about about_people ' + cid);
                jQuery(divClass).children().attr('class', 'tmplTab');
                }
                */
                jQuery('#columnContent').empty().append(columnContent);
                var clssList = jQuery('#tabsclasshldr').attr('class');
                jQuery('#naviclasshldr').add('body').removeClass().addClass(clssList);
                jQuery('#tmplclasshldr').removeClass().addClass('tmplTab');
                if (IMC2.videoPath)
                {
                    embedVideoPlayer();
                }
                location.hash = '#' + cid;
            });
        }
        jQuery('.tab').removeClass('active');
        jQuery(this).parent().addClass('active');
        evt.preventDefault();
    });
    // Next/Prev controls for people tabs

    var tabWidth = 93;
    var bkgdLeft = 0;

    $('a#prev').live('click', function()
    {
        var people = $('.about_people #column2');
        var tabs = $('.tabNav');


        if ($(this).hasClass('prevDisabled'))
        {
            return false;
        }
        else
        {
            bkgdLeft -= 93;
            if (tabs.css('left') != '0px')
            {
                $('a#next').removeClass('nextDisabled');
                if (people.length)
                {
                    people.stop(true, true).animate({
                        backgroundPosition: '-' + bkgdLeft + "px 0px"
                    });
                }

                if (bkgdLeft + 'px' == '0px')
                {
                    $('a#prev').addClass('prevDisabled');
                }

                tabs.stop(true, true).animate({
                    left: '-' + bkgdLeft + 'px'
                });

            }
        }
    });

    $('a#next').live('click', function()
    {
        var people = $('.about_people #column2');
        var tabs = $('.tabNav');
        if (tabs.css('left') == '0px' || tabs.css('left') == 'auto') //Check if left position is 0. if so, reset the bkgdLeft position. Useful for tabs resetting when jumping from page to page
        {
            bkgdLeft = 0;
        }

        var tab = $('.tab').length - 5;
        var tabWidth = tab * 93;
        tabWidth = '-' + tabWidth;

        if ($(this).hasClass('nextDisabled'))
        {
            return false;
        }
        else
        {
            bkgdLeft += 93;
            if (tabs.css('left') != tabWidth + 'px')
            {
                $('a#prev').removeClass('prevDisabled');

                if (people.length)
                {
                    people.stop(true, true).animate({
                        backgroundPosition: "-" + bkgdLeft + "px 0px"
                    });
                }

                if ('-' + bkgdLeft + 'px' == tabWidth + 'px')
                {
                    $('a#next').addClass('nextDisabled');
                }

                tabs.stop(true, true).animate({
                    left: '-' + bkgdLeft + 'px'
                });
            }
        }

    });

    //    $('.tab').live('mouseover', function(){
    //        $(this).addClass('hover');
    //    });
    //    
    //    $('.tab').live('mouseout', function(){
    //        $(this).removeClass('hover');
    //    });

    $('.tabNavWrapper .tab2').click(function()
    {
        $('#tab1').hide();
        $('#cuForm').removeClass('active');
        $('#tab2').show();
        $('#cuLocations').addClass('active');
    });

    $('.tabNavWrapper .tab1').click(function()
    {
        $('#tab2').hide();
        $('#cuLocations').removeClass('active');
        $('#tab1').show();
        $('#cuForm').addClass('active');
    });

    /* ******************************************************** */
    jQuery('#htmlWrapper a,#htmlWrapper area,#footer .copyright a').live('click', function(evt)
    {
        var $t = jQuery(this);
        if ($t.parent().hasClass('readMore'))
        {
            //jQuery('#siteWrapper').addClass('showMore');
        }
        else
        {

            if (!$t.hasClass('tablink'))
            {
                var cid = $t.attr('rel');

                if (!cid)
                {
                    cid = $t.attr('href');
                    if (cid && cid.indexOf('#') == 0 && cid.length > 1)
                    {
                        cid = cid.substring(1);
                    }
                    else
                    {
                        cid = null;
                    }
                }


                if (cid)
                {
                    var $f = jQuery('#imc2FlashHolder');
                    var flMain = $f ? $f.get(0) : null;
                    if (flMain && flMain.navigateToID)
                    {
                        flMain.navigateToID(cid);
                    }
                    else
                    {
                        navigationClick(cid);
                    }
                    evt.preventDefault();
                }
            }
        }
    });

    /*
    window.onfocus = function() {
    var cid = location.hash;
    logItem('hash is now: ' + cid);
    if (cid && cid.length > 1 && cid != IMC2.prevCid)
    {
    IMC2.prevCid = cid;
    cid = cid.substring(1);
    navigationClick(cid);
    }
    }
    */


});


// the following functions are called by Flash (and must therefore live outsite of document.ready)
function maximizeVideo()
{
    logItem('max video js function called');
	jQuery('#siteWrapper').addClass('maxVideo');
}
function minimizeVideo()
{
	logItem('min video js function called');
	jQuery('#siteWrapper').removeClass('maxVideo');
}

function carouselNavigationClick(navid)
{
    var $f = jQuery('#imc2FlashHolder');
    var flMain = $f ? $f.get(0) : null;
    if (flMain && flMain.navigateToID)
    {
        flMain.navigateToID(navid);
    }
}

function navigationClick(navid)
{
	logItem('click ' + navid);
	
	var $overlay = jQuery('#overlay');
	
	if (navid == 'contact')
	{
		location.href='/ContactUs.aspx';
		return;
	}
	else if (IMC2.initialLoad)
	{
	    IMC2.initialLoad = false;
        if ($overlay.is(':hidden') )
        {
            $overlay.fadeIn(200,function(){
	            if (jQuery.browser.msie)
	            {
		            this.style.removeAttribute("filter"); //fixes IE disappearing ClearType!
	            }
            });
        }
        if ($('body').hasClass('fullheight'))
	    {
	        IMC2.updateContentHeight();
	    }
	}
	else if (IMC2.contact)
	{
	    location.href = '/Default.aspx#' + navid;
	}
	else 
	{
	    IMC2.contentUrl = '/Content.aspx?cid=' + navid;
	    //$overlay.fadeOut(100);
	    jQuery.get(IMC2.contentUrl, function(data)
	    {
	        $overlay.empty().append(data);
	        $('#siteWrapper').removeClass('showMore maxVideo').css('min-height', '670px'); // Resets min-height on site wrapper back to default

	        jQuery('body').removeClass()
					.addClass(jQuery('#naviclasshldr').attr('class'))
					.addClass(jQuery('#tmplclasshldr').attr('class'));
	        if ($overlay.is(':hidden'))
	        {
	            $overlay.fadeIn(200, function()
	            {
	                if (jQuery.browser.msie)
	                {
	                    this.style.removeAttribute("filter"); //fixes IE disappearing ClearType!
	                }
	            });
	        }
	        if (IMC2.videoPath)
	        {
	            embedVideoPlayer();
	        }
	        if ($('body').hasClass('fullheight'))
	        {
	            IMC2.updateContentHeight();
	        }

	        if (IMC2 && IMC2.loaded)
	        {
	            document.title = IMC2.loaded.title;
	            IMC2.trackEvent('/page/load/' + navid, 'loaded: ' + IMC2.loaded.title);
	        }
	        //check what page your on and set active tab
	        /* var currentPage = location.hash;
	        currentPage = currentPage.substring(1);
            
	        jQuery('.tab a').each(function(){
	        var tabID = jQuery(this).attr('rel');
	        if(currentPage == tabID)
	        {
	        jQuery('.tab').removeClass('active');
	        jQuery(this).parent().addClass('active');
	        }
	        });	        */
	        IMC2.checkActiveTab();

	    });

	    location.hash = '#' + navid;
	}
}
IMC2.checkActiveTab = function(){
//check what page your on and set active tab
    var currentPage = location.hash;
    currentPage = currentPage.substring(1);
    
    jQuery('.tab a').each(function(){
        var tabID = jQuery(this).attr('rel');
        if(currentPage == tabID)
        {
            jQuery('.tab').removeClass('active');
            jQuery(this).parent().addClass('active');
        }
    });	        
}

function embedVideoPlayer()
{
    swfobject.embedSWF(
        '/mm/Imc2VideoPlayer.swf?version=' + IMC2.version, 
        'imc2VideoHolder', '100%', '100%', '9', false, 
        //flashvars
        {
	        siteRoot: '/',
	        videoPath: IMC2.videoPath
        }, 
        //params
        {
	        wmode: 'transparent',
	        scale: 'noborder',
	        salign: 'b'
        }, 
        //attributes
        {}
    );
}



