$(document).ready(function() {
	
	if ($('#homepage').length) {
	  $(window).resize(function() {
	    var height = ($(window).height() - $('#header').height() - $('#holder:first').height() - 32);
	    if (height > 120) {
	      $('#footer').height(height + "px");
      }
	  });
	  $(window).trigger('resize');
	}
	
	$(".external").attr("target", "_blank");
	
	var site_path = "";
	site_path = (document.location.host == "roy.local") 
		? document.location.protocol + '//' + document.location.host + "/sport927.com.au/httpdocs/" 
		: document.location.protocol + '//' + document.location.host + "/";
		// Pop Window
		
	$('.popup').click(function() {
		var window_width = 780;
		var window_height = 700;
		var left = (screen.width - window_width) / 2;
		var top = (screen.height - window_height) / 2;
		
		var popupWindow = window.open(this.href,'','width='+ window_width +',height=' + window_height +',resizable=yes,scrollbars=yes,menubar=yes,toolbar=no,location=no,screenX='+ left +',screenY='+ top +',left='+ left +',top='+ top);
		return false;
	});
	
	$('.popup_audio').click(function() {
		var window_width = 300;
		var window_height = 230;
		var left = (screen.width - window_width) / 2;
		var top = (screen.height - window_height) / 2;
		
		var popupWindow = window.open(this.href,'','width='+ window_width +',height=' + window_height +',resizable=yes,scrollbars=yes,menubar=yes,toolbar=no,location=no,screenX='+ left +',screenY='+ top +',left='+ left +',top='+ top);
		return false;
	});	
	
	$(".opaq").css('opacity', 0.7);
	$(".opaq_extreme").css('opacity', 0.2);
	$(".opaq_hard").css('opacity', 0.6);

	// only process links that have some where to go - this is more of a tool used during development.
	$("a").bind('click', function() {
		if ($(this).attr('href') == "") {
			return false;
		}
	});
	
	$(".disabled").each(function() {
		$(this).attr("disabled", "disabled");
	});
		
	$("form").bind("submit", function() { 
	
		if (jQuery(this).attr("action") == "") {
			alert('No action set; form can not be submitted.');
			return false;
		}
	});
	
	if ($("param").length > 0)
		$("param").attr("wmode", "transparent");
	else if ($("embed").length > 0)
		$("embed").attr("wmode", "transparent");


	$(".printThis").click(function() {
		window.print();
		return false;
	});
	
	if ($('.more_link').length > 0) {
		$('.more_link').each(function() {
			var txt = $(this).html(); 
			$(this).html(txt+'&nbsp;&#187;');
		});
	}
	
	$('.fancybox').fancybox({padding: 0});
	$('.fancybox_terms').fancybox({padding: 0});
	
	$('#searchbtn').click(function() {
		$("#sitesearch_form").submit();
		return false;
	});	
	

	//News Ticker //	
	var speed = 700; var pause = 10000;
	function slideLeft() { 
		$('ul#listticker li:first').animate({left:'-50'}, 5000, function() { removeFirst(); });};
	
	function removeFirst(){ $('ul#listticker li:first').fadeOut('slow', function() {addLast(this);});}
	function addLast(first){$(first).insertAfter('ul#listticker li:last').fadeIn('slow');}
	interval = setInterval(removeFirst, pause);
	
	//Race Results Slider //
	var fw = $("#headerResults div p").width();
	setTimeout('scrollRaces('+fw+')', 4500);
	
});


//Races Scroller
function scrollRaces(fw) {
	if($("#headerResults div p").length > 0) {
		var boxWidth = $("#headerResults").width();
		var scrollAmount = (boxWidth - fw);
		if(scrollAmount < 0) {
			$('#headerResults p').animate({ 'left':scrollAmount-10}, 3000, 'linear', function() { setTimeout('startRaceScroll('+fw+')', 2000) });	
		}
	}
}

function startRaceScroll(fw) {
		$('#headerResults div p').fadeOut('slow', function() { 
			$(this).css({'left':'0px'}); 
		})
		.fadeIn('slow', function() {
			setTimeout('scrollRaces('+fw+')', 2500);
		});	
		
}

//Window.load is a Fix for Safari when it doesn't know the height of images (and doc.ready is called). 
//Col Heights were being mis-calculated in Safari.
 
$(window).load(function() {
	setColHeights();
	setMenuPad();
	
});

function setColHeights() {

	if ($(".layoutCol").length > 1) {
	
		var maxHeight = 10;
	
		$(".layoutCol").each(function() {
			if ($(this).height() > maxHeight)
				maxHeight = $(this).height();
		});
		
		$(".layoutCol").each(function() {
			$(this).height(maxHeight);
		});
		
	}
	
	right_h = $("#rightcol").height() + 10;
	fp_h = $("#feature_podcast").height();
	op_h = $("#other_podcast").height();
	
	if(right_h > fp_h) { 
		$('#other_podcast').height(right_h);
		$('#other_podcast_inner').height(right_h - 20);
	}
	
	return true;
}

function setMenuPad() {

		var fullwidth = $("#nav_main").width();
		var ulwidth = $("#nav_main ul").width(); 
		var elements = $("#nav_main ul li").size();
		var pads = Math.floor((fullwidth - ulwidth) / (elements*2)); 
		pads = Math.floor(pads);
	
		$("#nav_main ul li a").each(function() {
			$(this).css({'padding-left': pads, 'padding-right': pads});
		});
		
		var diff = $('#nav_main').width() - $('#nav_main ul').width();
		$("#nav_main ul li a").last().css({'padding-right': (pads+diff)-1 });
		
		$("#nav_main ul").fadeIn('1000');
		
}

// Retrieve the value of the cookie with the specified name.
function GetCookie(sName)
{
  // cookies are separated by semicolons
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {
    // a name/value pair (a crumb) is separated by an equal sign
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0]) 
      return unescape(aCrumb[1]);
  }
  // a cookie with the requested name does not exist
  return null;
}


