//::::::DEPENDENCIES:::::::::::::::::::::::::::::::::::::::::::::::::::::
//This site uses the jQuery 1.4.1 build as a library
	//Plugins are .hoverIntent and jQuery easing
//::::::GLOBAL VARIABLES:::::::::::::::::::::::::::::::::::::::::::::::::

//::::::FUNCTIONS::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//Make columns the same height
function equalHeight(group)
{
    tallest = 0;
    group.each(function() {
        thisHeight = $(this).height();
        if (thisHeight > tallest)
        {
            tallest = thisHeight;
        }
    });
    group.height(tallest);
}
//Make big clickable divs with semantic markup
    //Note can only have one a tag in the container for this to work.
function bigClicks (box)
{
    box.css('cursor', 'pointer')
    box.click(function(){		
        var link = $(this).find('a').attr('href');
        var target = $(this).find('a').attr('target');		
		var elementType = $(this).attr('class');		        
		// call tracking functions based on element clicked
		if (elementType == 'channelsLink')
		{
			var retName = $(this).find("h3").text();
			var retTitle = $(this).find("h4").text();
			var retLink = $(this).find("h5").text();
			retentionTrack(retName, retTitle, retLink);
		}
		else if (elementType == 'latestDeal')		
		{
			var ldc = $(this).find("p").text();
			latestDealTrack(ldc);
		}
        if (target == "_blank")
        {
            window.open(link);
        }
        else
        {
            document.location = link
        }
        return false;
    });
}
//::::::OBJECTS::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//Main Navigation dropdown menus
	//the .hoverIntent plugin does not allow a JSON structure
	//Instead all functions and elements will be maintained here.
var mainNavConfig = {
	'sensitivity': 1,    
    'interval': 5,
    'over': showMainNavDropDownMenu,
    'timeout': 5,
    'out': hideMainNavDropDownMenu
};
function showMainNavDropDownMenu()
{
	$(this).children('a').addClass('active');
	$(this).children('.dropDownMenu').show();
}
function hideMainNavDropDownMenu()
{
	$(this).children('a').removeClass('active');
	$(this).children('.dropDownMenu').hide();
}
var mainNavMenu = {
	'init': function() {
		$('#mainNav > li').hoverIntent(mainNavConfig);
	}
};
//Accordian
var accordian = {
    'eventHook':'.accordian li a.accordianTrigger',
    'slide': function(element){
        var checkElement = $(element).parent().next();
        if (checkElement.is(':animated'))
        {
            return false;
        }
        if((checkElement.is('div')) && (checkElement.is(':visible')))
        {
            $(element).removeClass('open');
            $(checkElement).slideUp(300);
            return false;
        }
        if((checkElement.is('div')) && (!checkElement.is(':visible')))
        {
            checkElement.slideDown(300, function (){}).prev().children('a').addClass('open');
            return false;
        }
    },
    'setup': function(){
        $('.accordian li div').hide();
    },
    'open': function () {
    
        var open_param = gup( 'open' );
        if(open_param != "")
        {
        var panelOpen =  $('.accordian > li').eq(open_param - 1)
        
        panelOpen.children().show().children('a.accordianTrigger').addClass('open');
        $.scrollTo( '.accordian > li > h3 > a.open', 1 );
        
        }
        
    },
    'init': function(){
        accordian.setup();
        accordian.open();
        $(accordian.eventHook).toggle(function() {
            accordian.slide(this);
            return false;
        },function(){
            accordian.slide(this)
            return false;
        });
    }
};
//Frontpage image rotator
var heroBurstRotator = {
    //Private Variables
    'eventHook':'#mainBurstRotatorControl li a',
    //Routines
    'cycle': setInterval("heroBurstRotator.cycleNext()", 7000),
    'cycleThrough': function(){
        return this.cycle;
    },
    'stopCycle': function() {
        clearInterval(this.cycle);
    },
    'swapImage': function(element) {
        var heroSpot = $('#heroImage');
        $('.currentImage').removeClass('currentImage');
        element.addClass('currentImage');
        heroSpot.fadeOut(600,function() {
            var imageLocation = element.next().find('img').attr('src');
            var imageDescription = element.next().find('img').attr('alt');
            var linkLocation = element.attr('href');
            heroSpot.attr('src', imageLocation).fadeIn(600);
            heroSpot.attr('alt', imageDescription)
            heroSpot.parent('a').attr('href', linkLocation);
        });
    },
    'cycleNext': function () {
        var nextPic = $('.currentImage').parent().next().children('a');
        if (nextPic.parent().is('.nextButton'))
        {
            nextPic = $('li', 'ol#mainBurstRotatorControl').first().children('a');
        }
        this.swapImage(nextPic);
    },
    //Event handlers
    'hoverEvent': {
        'over': function(element) {
            var shareBubble = element.next();
            $('.featureBubble:animated').stop(true, true).hide();
            shareBubble.fadeIn(600);
        },
        'out': function(element) {
            var shareBubble = element.next();
            $('.featureBubble:animated').stop(true, true)
            shareBubble.fadeOut(600);
        }
    },
    //event listeners and initialization
    'init': function(){
        $(this.eventHook).bind({
            'click': function() {
                if($(this).parent().is('.nextButton'))
                {
                    heroBurstRotator.cycleNext();
                    heroBurstRotator.stopCycle();
                    return false;
                }
                else
                {
                    heroBurstRotator.swapImage($(this));
                    heroBurstRotator.stopCycle();
                    return false;
                }
            },
            'mouseenter': function() {
                heroBurstRotator.hoverEvent.over($(this));
            },
            'mouseleave': function() {
                heroBurstRotator.hoverEvent.out($(this));
            }
        });
        heroBurstRotator.cycleThrough;
    }
};
//Flash swfObject Wrapper
    //Dependant on SWFObject
var flashEmbed = {
    'flashVersion': '10',
    'flashVars':{},
    'flashParams':{
                allowScriptAccess: 'always',
                scaleMode: 'noscale',
                wmode: 'opaque'
            },
    'flashOptions':{},
    'init': function(file, whereToEmbed, vars, params, options){
        //this check may be unnecessary for mobile.
        if (swfobject.hasFlashPlayerVersion(this.flashVersion))
        {
            if ( vars )
            {
                this.flashVars = vars;
            }
            if ( params )
            {
                this.flashParams = params;
            }
            if ( options )
            {
               this.flashOptions = options;
            }
            swfobject.embedSWF (
                file,
                whereToEmbed,
                '100%',
                '100%',
                this.flashVersion,
                '/swf/swfobject/expressInstall.swf',
                //Flash Vars
                this.flashVars, 
                //Params
                this.flashParams,
                //Options
                this.flashOptions
            );
        }
    }
};
//Tabbed Boxes content rotator
function tabBox(y, z)
{
    //Global Variables
    var wrapper = y;
    var controller = z;
    var activeClassName = 'active';
    //hide everything ::Private
    this.hideEverything = function(context) {
        context.children().hide();
    }
    //Swapping out the Tab ::Privileged
    function swapTab(context) {
        tabBox.prototype.constructor.apply(this);
        var module = $(context).attr('href');
        if ($(module).is(':visible'))
        {
            return false;
        }
        else
        {
            this.hideEverything(wrapper);
            $(module).stop().show();
            $(context).addClass(activeClassName)
                .parent().siblings().find('.'+activeClassName).removeClass(activeClassName);
        }
    }
    //setup and click event
    this.init = function(){
        tabBox.prototype.constructor.apply(this);
        this.hideEverything(wrapper)
        wrapper.children(':first').show();
        controller.first().addClass(activeClassName)
        controller.click(function(){
            swapTab(this);
            return false;
        });
    }
}

//parse for url
function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}
//::::::INITIALIZATION:::::::::::::::::::::::::::::::::::::::::::::::::::
//If you need to turn anything off temporarily do so here!!!
$(document).ready(function() {
	mainNavMenu.init();
});
