
// IE6(-) PNG Fix
var blank = new Image();
blank.src = '/images/blank.gif';
 
function fixPng(png) {
	// get src
	var src = png.src;
	// set width and height
	if (!png.style.width) { png.style.width = $(png).width(); }
	if (!png.style.height) { png.style.height = $(png).height(); }
	// replace by blank image
	png.onload = function() { };
	png.src = blank.src;
	// set filter (display original image)
	png.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
}

// ---------------------------------------------------------------
// -- Tweaks Sports Media Websites List Style --------------------
// ---------------------------------------------------------------


	function smwExpandCollapse(){
		if($('.sports-media-websites').length > 0) {
			$('.sports-media-websites li ul li:nth-child(2)').each(function(){
				var currentList = $(this).text();
				var listArray = currentList.split(", ");
				$(this).empty().prepend('<ul></ul>');
				for(i = 0; i < listArray.length; i++){
					$(this).children('ul').append('<li>' + listArray[i] + '</li>');
				}
			});
		}
		
		// expand & collapse
		$('~*:not(div)','.sports-media-websites li a').hide();
	
		$('.sports-media-websites li a').toggle(
			function(){
				$('~*:not(div)', this).slideDown(200);
			},
			function(){
				$('~*:not(div)', this).slideUp(200);
			}
		);
	}



// Dropdown functionality for "Other Audiences" in Modal
function modalDropdown(){
	$('#audience-options li:has(a#dropdown) ul').data('height',$('#audience-options li:has(a#dropdown) ul').height());
	$('#audience-options li:has(a#dropdown) ul').css({'height':'0px'});
	$('#audience-options li:has(a#dropdown)').hover(
		function(){
			$(this).children('ul').animate({height:$('#audience-options li:has(a#dropdown) ul').data('height')},{queue:false,duration:200});
		},
		function(){
			$(this).children('ul').animate({height:"0px" },{queue:false,duration:200});
		}
	);	
	$('#audience-options li:has(a#dropdown)').click(function(){
		return false;
	});	
}

// Dropdown functionality for "Other Audiences" in IE6
function modalDropdownIE6(){
	$('#audience-options li:has(a#dropdown) ul').data('height',$('#audience-options li:has(a#dropdown) ul').height());
	$('#audience-options li:has(a#dropdown) ul').css({'height':'0px'});
	$('#audience-options li:has(a#dropdown)').hover(
		function(){
			$(this).children('ul').animate({height:$('#audience-options li:has(a#dropdown) ul').data('height')},{queue:false,duration:200});
		},
		function(){
			$(this).children('ul').animate({height:"0px" },{queue:false,duration:200});
		}
	);	
	$('#audience-options li:has(a#dropdown)').click(function(){
		return false;
	});	
	$('#audience-options li:has(a#dropdown) ul li a').click(function(){
		var currentURL = $(this).attr('href');
		var regExp = /\s+/g;
		currentURL = currentURL.replace(regExp,'%20');

		$('#lhs').load(currentURL + " #modal", function(data){
			do_sIFR();
			modalDropdownIE6();
		});
		return false;
	});
}

// Binds all events necessary for Modal Navigation
function modalNavigation(dialog){
	// add 'current' class to current page in Dropdown
	var currentPage = $('#modal h2').text();
	$('#audience-options').find('a:contains("'+currentPage+'")').css({'border':'1px solid #ff0000'}).addClass('current');
	
	// Bind Dropdown Click Intercept
	$('#audience-options li ul li a').click(function(){
		var currentURL = $(this).attr('href');
		var regExp = /\s+/g;
		currentURL = currentURL.replace(regExp,'%20');
		modalNavigationUnloadLoad(dialog, currentURL);
		return false;
	});
	
	// Bind modal.close() to #escBtn
	$('#escBtn').click(function(){
		$.modal.close();
	});
	
	// hide Prev/Next if on appropriate page
	var listLength = $('#audience-options li ul li').size();
	var firstText = $('#audience-options li ul li:first-child a').text();
	var lastText = $('#audience-options li ul li:nth-child(' + listLength + ') a').text();
	var controlText = $('#audience-options a.current').text();
	if(controlText == firstText){
		$('#modal-arrow-prev').hide();
	} else if(controlText == lastText){
		$('#modal-arrow-next').hide();
	}
	
	// Bind Prev/Next Click Intercept
	$('.modal-arrow').click(function(){
		var currentPage = $('#audience-options a.current');
		if($(this).attr('id') == "modal-arrow-prev"){
			currentURL = currentPage.parent().prev().children('a').attr('href');
			var regExp = /\s+/g;
			currentURL = currentURL.replace(regExp,'%20');
			modalNavigationUnloadLoad(dialog, currentURL);
		} else {
			currentURL = currentPage.parent().next().children('a').attr('href');
			var regExp = /\s+/g;
			currentURL = currentURL.replace(regExp,'%20');
			modalNavigationUnloadLoad(dialog, currentURL);
		}
		return false;
	});
	
	
}

// Processes any Modal navigational requests (unloads current, calls requested, loads new)
function modalNavigationUnloadLoad(dialog, currentURL){
	// unload current data & call new
	dialog.container.fadeOut(500, function(){			
		dialog.container.empty().append('<div id="modal-storage"></div>');
		$('#modal-storage').load(currentURL + " #modal", function(){
			dialog.container.prepend('<div id="modal-header"></div><a class="modalCloseImg" title="Close" id="escBtn">Close</a>')
			dialog.container.append('<a href="#" id="modal-arrow-prev" class="modal-arrow" title="Prev"></a><a href="#" id="modal-arrow-next" class="modal-arrow" title="Next"></a>')
			dialog.container.fadeIn(500);
			do_sIFR();
			modalNavigation(dialog);
			modalDropdown();
		});
	});
}



// parses a string for the queried 'name'
function getUrlParams(name,string){
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec(string);
	if( results == null ){
		return "";
	} else {
		return results[1];
	}
}




function initHomepageSliders(){
	//$('#landing2 #featured > li').css({'outline':'1px solid #ff0000'}).scrollable();
	$('#marketers').scrollable({
		side: 5,
		loop: true,
		clickable: false
	});
	$('#partners').scrollable({
		side: 4,
		loop: true,
		clickable: false
	});
}




$(document).ready(function() {

// ---------------------------------------------------------------
// -- IE6(-) PNG Fix ---------------------------------------------
// ---------------------------------------------------------------

	var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
	if (badBrowser) {
		
		$('#torque').before('<div style="visibility:hidden;">Hello</div>').after('<div style="visibility:hidden;">Hello</div>');
		
		$('img[src$=.png]').each(function() {
			if (!this.complete) {
				this.onload = function() { fixPng(this) };
			} else {
				fixPng(this);
			}
		});
	}



// ---------------------------------------------------------------
// -- Home Cycling Background Images -----------------------------
// ---------------------------------------------------------------
	if($('#landing').length > 0){
		//var containerHeight = $('ul#bgimg').height();
		
		$('#landing ul#bgimg').innerfade({ 
			animationtype: 'fade',
			timeout: 4000,
			speed: 1000,
			containerheight: '317px'
		}); 
	}

	initHomepageSliders();
// ---------------------------------------------------------------
// -- "Sub-nav" Hover & Click ------------------------------------
// ---------------------------------------------------------------

/*
	$('.sidebar-module').hover(
		function(){
			$(this).children('*').addClass('hover');
			$(this).css({'cursor':'pointer'});
		},
		function(){
			$(this).children('*').removeClass('hover');
			$(this).css({'cursor':''});
		}
	);
	
	$('.sidebar-module').click(function(){
		if(this.id == "ceo"){
			$blogurl = $(this).find("a").attr("href");
			window.open($blogurl, '_new');
		}else{
			window.location = $(this).find("a").attr("href");
		}
		return false;
	});
*/

// ---------------------------------------------------------------
// -- Modal Window -----------------------------------------------
// ---------------------------------------------------------------
if (!badBrowser){
	$('#tab-anchors ul li a').click(function() {
		var currentURL = $(this).attr('href');
		var regExp = /\s+/g;
		currentURL = currentURL.replace(regExp,'%20');

		if($('#modal-storage').length == 0){
			$('body').append('<div id="modal-storage"></div>');
		}
		
		$('#modal-storage').load(currentURL + " #modal", function(){
			$('#modal-storage').modal({
				position: [35,],
				closeHTML: '<a class="modalCloseImg" title="Close">Close</a>',
				onOpen: simplemodal.open,
				onShow: simplemodal.show,
				onClose: simplemodal.close
			});
		});
		return false;
	});
} else {
	// IE6
	$('#tab-anchors ul li a').click(function() {
		var currentURL = $(this).attr('href');
		var regExp = /\s+/g;
		currentURL = currentURL.replace(regExp,'%20');

		$('#lhs').load(currentURL + " #modal", function(data){
			do_sIFR();
			modalDropdownIE6();
		});
		return false;
	});
}



	var simplemodal = {
		message: null,
		open: function(dialog){
			dialog.overlay.fadeIn(500, function () {
				dialog.data.show();
				dialog.container.before('<div id="modal-loading" style="display:none;"></div>');
				$('#modal-loading').show();
				dialog.container.prepend('<div id="modal-header"></div>');
				dialog.container.append('<a href="#" id="modal-arrow-prev" class="modal-arrow" title="Prev"></a><a href="#" id="modal-arrow-next" class="modal-arrow" title="Next"></a>');
				dialog.container.fadeIn(500);
				do_sIFR();
				modalNavigation(dialog);
				modalDropdown();
				$(document).one('keydown.simplemodal', function (e) {
					if (e.keyCode == 27){
						$.modal.close();
					}
				});
			});
		},
		close: function(dialog){
			$('#modal-loading').remove();
			dialog.container.fadeOut(500, function () {
				dialog.overlay.fadeOut(500, function () {
					$.modal.close();
					$('#modal-storage').remove();
				});
			});
		}
	};



// ---------------------------------------------------------------
// -- Form Tweaks ------------------------------------------------
// ---------------------------------------------------------------
if (!badBrowser){
	$('#contact-form select').form({
		container: 'select-container',
		preselectText: 'Please Select',
		speed: 250
	});

	$('#smw select').form({
		container: 'select-container',
		preselectText: 'Select a Category',
		speed: 250,
		ajaxSubmit: true
	});
}

// ---------------------------------------------------------------
// -- inits ------------------------------------------------------
// ---------------------------------------------------------------
smwExpandCollapse();
modalDropdown();
});// --- End jQuery Document.Ready ---

