(function($){
	$(document).ready(function(){
		
		
		/* 
		 * External Links
		 * Usage: add rel="external" to <a> to make it open in new window.
		 */
		
		initExternalLinks();
		initPopUp();
		initPlayerPopUp();
		initMainReturn();
		initWebinarPlayerBtns();
		initEqualColumns(); //Added this to allow automaticly setting equal columns.
		
		/*
		 * MRD added for Event Navigation in the left nav. Trying to migrate all sites to this.
		*/
		initEventNavItems();
		initDownloadLinks();
		
	});

	function initDownloadLinks(){
		// add ga-custom-tracking to any download links
		$('.downloadable-customer-products .my-account a.download-link').click(function(){
			// look for event and track name inside of the table row
			var eventName = jQuery(this).parents('tr:first').find('.event-name').text();
			if (eventName == ''){ eventName = 'Unspecified Event'; }
			var trackName = jQuery(this).parents('tr:first').find('.track-name').text();
			if (trackName == ''){ trackName = 'Unspecified Track'; }
			// sku comes from the link
			var sku = $(this).attr('alt');
			gaTrackDownloadClick(eventName, trackName, sku, this.href);
			return openURLInNewWindow(this.href);
		});
	}
	
	function initWebinarPlayerBtns(){
		
		if(!$('body').hasClass('player-webcast-index')) return false;
		
		//var switchBtns = $('#webcast-header .switch-view-btns ul li');
		var $slideViewBtn = $('#btn-slide-view a');
		var $videoViewBtn = $('#btn-video-view a');
		
		$($slideViewBtn).click(function(){
			
			if(!$slideViewBtn.parent().hasClass('active')){
				$slideViewBtn.parent().addClass('active');
				$videoViewBtn.parent().removeClass('active');
			}else{
				$slideViewBtn.parent().removeClass('active');
				$videoViewBtn.parent().addClass('active');
			}
			return false;
		});
		
		$($videoViewBtn).click(function(){
			if(!$videoViewBtn.parent().hasClass('active')){
				$videoViewBtn.parent().addClass('active');
				$slideViewBtn.parent().removeClass('active');
			}else{
				$videoViewBtn.parent().removeClass('active');
				$slideViewBtn.parent().addClass('active');
			}		
			return false;
		});			
		
		
	}
	
	// ExternalLinks
	function initExternalLinks(){
		
		var $link = $('a[rel="external"]');
		$link.click( function(){
			
			return openURLInNewWindow(this.href);
			
			//window.open(this.href);
			//return false;
		});
	}

	// Player Pop Up 
	function initPopUp(){
		$('a[rel="player"]').click( function(){
			if(window.name != 'player'){ window.name = 'main'; }
			var link = this.href;			
			playerWindow = window.open(link, 'player', 'toolbar=0,scrollbars=0,location=1,statusbar=1,menubar=0,resizable=0,width=996,height=600,top=100,left=100');
			playerWindow;
			playerWindow.focus();
			return false;
		});
	}
	
	// Webcast Player Pop Up 
	function initPlayerPopUp(){
		$('a[rel="webcastplayer"]').click( function(){
			if(window.name != 'webcastplayer'){ window.name = 'main'; }
			var link = this.href;			
			playerWindow = window.open(link, 'webcastplayer', 'toolbar=0,scrollbars=0,location=1,statusbar=1,menubar=0,resizable=0,width=996,height=650,top=100,left=100');
			playerWindow;
			playerWindow.focus();
			return false;
		});
	}
	
	// Main
	function initMainReturn(){
		$('a[rel="main"]').click( function(){
			var link = this.href;			
			try{
				mainWindow = window.opener.location(link);
				window.opener.focus();
			}catch(err){
				mainWindow = window.open(link, 'main', '');
				mainWindow.focus();		
			}
			mainWindow;
			return false;
		});
	}
	
	/**
	 * This will make equal columns for elements that have the same class. You must begin the 
	 * class names with "equalCol_" then add a unique ending for each set of items that you want to equalize.
	 * EX: equalCol_homepageMiddelCol
	 */
	function initEqualColumns()
	{
		//get all elements that have a class like equalCol_NUMBER, EX: equalCol_1
		var equalColumnsArray = [];	//array
		var elementClass = '';
		var elementClassArray = [];
		$("[class*=equalCol_]").each(function(){	//Gets elements that have "equalCol_" in the class name. -MRD
			elementClass = $(this).attr('class');
			//Now explode the classes and make array of equalCol_ classes.
			elementClassArray = elementClass.split(" ");
			$.each(elementClassArray, function(){
				//Now build array of eqCol item classes.
				var match = (this +'').indexOf("equalCol_");
				if(match !== -1){
					//Have to cast as a string otherwise $.inArray does not work
					var classString = String(this);
					if($.inArray(classString, equalColumnsArray) === -1){
						equalColumnsArray.push(classString);	//Class String
					}				
				}
			}); 
		});
		
		//Build array of class names and then call .equalHeightColumns()
		var selectorString = "";
		$.each(equalColumnsArray, function(){
			selectorString = "."+this;
			if(selectorString.length > 0){
				$(selectorString).equalHeightColumns();
			}
		});
		
	}
	
	
/* ------------------    END Event Nav Expand/Collapse    ------------------  */
	function initEventNavItems(){		
		//This is the expand/collapse click event.
		$('.event-nav-toggle-category').click(function(){
			toggleEventNavItem($(this));
			return false;
		});
	}
	
	function toggleEventNavItem(el){
		//el.siblings('ul').toggle();		
		var childrenUl = el.siblings('ul');
		var openingChildren = true;
		
		if(childrenUl.hasClass('hide')){	//Unhide
			childrenUl.removeClass('hide');
			changeEventNavIcon(el, 'minus');
			
			//If this was the event clicked we need to unhide an extra UL
			if(el.hasClass('level-top')){
				childChildUl = childrenUl.find('ul.level1');
				childChildUl.removeClass('hide');
			}
		} else {	//Hide
			childrenUl.addClass('hide');
			changeEventNavIcon(el, 'plus');
		}
		
		return;
	}
	
	function changeEventNavIcon(el, changeTo)
	{
		el.children('img').each(function(){
			var img = $(this);
			
			if(changeTo == 'minus'){
				var src = img.attr("src").replace('plus', 'minus');
				img.attr("src", src);
			} else if(changeTo == 'plus'){
				var src = img.attr("src").replace('minus', 'plus');
				img.attr("src", src);
			}			
		});
		
		return;
	}
/* ---------------    END Event Nav Expand/Collapse -----------------  */

})(jQuery);


function openURLInNewWindow(link){
	win = window.open(link, '_blank', '');
	win.focus();
	return false;
}

// Google analytics custom tracking - note this must be defined in the global scope so flash has access to it
var ACCESS_TYPE_PREVIEW = 'preview';
var ACCESS_TYPE_STREAMING = 'streaming';
var ACCESS_TYPE_DOWNLOAD = 'download';	// not really used here!
var MEDIA_TYPE_AUDIO = 'audio';
var MEDIA_TYPE_VIDEO = 'video';

function gaBuildPagePath(eventName, trackName, itemSku, accessType, fileType, extras) {
	// set information
	if (typeof itemSku == undefined) { itemSku = ''; }
	var fullTrackName = (itemSku != '') ? (trackName + ' - ' + itemSku) : trackName;
	var parts = ['media', eventName, fullTrackName, accessType, fileType];
	if (extras != '') { parts.push(extras); }
	return '/' + parts.join('/');
}

// more GA stuff
function gaTrackDownloadClick(eventName, trackName, sku, filePath){
	try {
		if (typeof _gaq != 'undefined'){
			var fileType = (filePath.indexOf('mp3') > -1) ? MEDIA_TYPE_AUDIO : MEDIA_TYPE_VIDEO;
			var path = gaBuildPagePath(eventName, trackName, sku, ACCESS_TYPE_DOWNLOAD, fileType, '');
			_gaq.push(["_trackPageview", path]);
		} else {
			// handling?
			//alert('gaTrackDownloadClick gaq NOT DEFINED');
		}
	} catch (err) {
		// error
	}
}
	
function gaTrackStreaming(extras){
	try {
		if (typeof _gaq != 'undefined'){
			var eventName = 'Unspecified Event';
			var trackName = 'Unspecified Track';
			var fileType = 'Unspecified file type';
			var sku = 'Unspecified sku';
			var accessType = 'Unspecified access type';
			if (typeof flashVars != 'undefined'){
				trackName = gaScrubPath(flashVars['t']);
				eventName = gaScrubPath(flashVars['e']);
				fileType = (flashVars['v'].indexOf('mp3') > -1) ? MEDIA_TYPE_AUDIO : MEDIA_TYPE_VIDEO;
				sku = gaScrubPath(flashVars['sku']);
				accessType = (flashVars['a'] == '1') ? ACCESS_TYPE_STREAMING : ACCESS_TYPE_PREVIEW;
			}
			var path = gaBuildPagePath(eventName, trackName, sku, accessType, fileType, extras);
			_gaq.push(["_trackPageview", path]);
		} else {
			// handling?
			//alert('gaTrackStreaming gaq NOT DEFINED');
		}
	} catch (err) {
		// error
	}
}

function gaScrubPath(text){
	try {
		return text.replace(/\+/g, " ");
	} catch (err) { /* fallthrough */ }
	return '';
}


