function toggle(targetId) {
	if (document.getElementById) {
		target = document.getElementById(targetId);
		if (target.style.display == "none"){
			target.style.display = "";}
		else {
			target.style.display = "none";
		}
	}
}

function pspCheck() {
	return (navigator.userAgent.indexOf('PlayStation Portable') != -1);	
}

function trim(stringToTrim) {
	return stringToTrim.replace("/^\s+|\s+$/g","");
}

function downloadFormSubmit(form) {
	if (form && form.data)
		form.submit();
}

function ghostSelect(onSuccess) {
	if (pspext && pspext.SelectGhost() >= 0)
		window.location = onSuccess;		
}

function ghostSubmit() {
	downloadFormSubmit(document.selectGhostForm);
}	

function gridSelect(onSuccess) {
	if (pspext && pspext.SelectGrid() >= 0)
		window.location = onSuccess;	
}

function gridSubmit() {
	downloadFormSubmit(document.selectGridForm);
}	

function getPspext() {
	if (document["pspext"] && document["pspext"].SaveData)
		return document["pspext"];
	else
		return null;
}

function saveDownload(form, saveFunction, onSuccess, onFailure) {
	if (form && saveFunction) {	
		if (getPspext())	{
			if (saveFunction(form.memoryStickLocation.value, form.description.value, form.data.value, form.mac.value) > 0)
				window.location = onSuccess;
			else
				window.location = onFailure+"?statusCode="+result;	
		}
		else {
			alert(form.memoryStickLocation.value + form.description.value);
			window.location = onSuccess;
		}
	}
}

function ghostDownload(onSuccess, onFailure) {
	saveDownload(document.downloadGhostForm, 
				 function(memoryStickLocation, description, data, mac) {return getPspext() ? getPspext().SaveGhost(memoryStickLocation, description, data, mac) : 0;}, 
		 		 onSuccess, 
		 		 onFailure);
}

function skinDownload(onSuccess, onFailure) {
	saveDownload(document.downloadSkinForm,
				 function(memoryStickLocation, description, data, mac) {return getPspext() ? getPspext().SaveShipSkin(memoryStickLocation, description, data, mac) : 0;},
				 onSuccess,
				 onFailure);
}

function datablastDownload(onSuccess, onFailure) {
	saveDownload(document.downloadDatablastForm,
				 function(memoryStickLocation, description, data, mac) {return getPspext() ? getPspext().SaveDatablast(memoryStickLocation, description, data, mac) : 0;},
				 onSuccess,
				 onFailure);
}

function gridDownload(onSuccess, onFailure) {
	saveDownload(document.downloadGridForm,
				 function(memoryStickLocation, description, data, mac) {return getPspext() ? getPspext().SaveGrid(memoryStickLocation, description, data, mac) : 0;},
				 onSuccess,
				 onFailure);
}

function initPage() {
}




function teamInfoBoxSwitch(id1,id2) 
{
		
		$(id2).addClass('hidden');		
		$(id1).removeClass('hidden');
}	


function animateTeamGraph(speedVal, accVal, handlingVal, shieldVal)
{
	
	targetWidth = 58;
	
	$('#speedSpacer').animate({
		'width' : speedVal * targetWidth
		}, 'slow');
	
	$('#speedBar').animate({
		'width' : speedVal * targetWidth + 20
		}, 'slow');
	
	$('#accSpacer').animate({
		'width' : accVal * targetWidth
		}, 'slow');
	
	$('#accBar').animate({
		'width' : accVal * targetWidth + 20
		}, 'slow');
	
	$('#handlingSpacer').animate({
		'width' : handlingVal * targetWidth
		}, 'slow');
	
	$('#handlingBar').animate({
		'width' : handlingVal * targetWidth + 20
		}, 'slow');
	
	$('#shieldSpacer').animate({
		'width' : shieldVal * targetWidth
		}, 'slow');
	
	$('#shieldBar').animate({
		'width' : shieldVal * targetWidth + 20
		}, 'slow');
}


function resetGraphs()
{

	$('#speedSpacer').width(0);
	$('#speedBar').width(0);
	$('#accSpacer').width(0);
	$('#accBar').width(0);
	$('#handlingSpacer').width(0);
	$('#handlingBar').width(0);
	$('#shieldSpacer').width(0);
	$('#shieldBar').width(0);

}


function insertSpinner(id)
{

	$(id).text('').append('<img src="/resource/images/site/loadingSpinnerMedium.gif" height="24" width="24" alt="loading" />');


}


function toggleNewsBody(id) {
	
	
	if($('#body'+id).is(":hidden"))
	{
		
		//if you've clicked on a link that's hidden, then
		//hide the other news articles that might be visible		
		$('.newsBodyRevealed').slideUp("normal").removeClass('newsBodyRevealed').addClass('newsBody');		
		
		//then slide down the body of the chosen news item
		$('#body'+id).slideDown("normal").removeClass('newsBody').addClass('newsBodyRevealed');	
	
	}
	else {
	
		//if the news item was already visible, then hide it
		$('#body'+id).slideUp("normal").removeClass('newsBodyRevealed').addClass('newsBody');		
	
	}
	
	
}
	

function toggleLogo(id) {
	
	
	
	if($(id).is(":hidden"))
	{
		
		
		//then slide down the body of the chosen item
		$(id).slideDown(1200).removeClass('teamLogoContainer').addClass('teamLogoContainerRevealed');	
	
	}
	else {
	
		//if item was already visible, then hide it
		$(id).slideUp("slow").removeClass('teamLogoContainerRevealed').addClass('teamLogoContainer');		
	
	}
	
	
}

	
function imageSwitch(id, newSrc)
{
	
	if($(id))
	{
	
		$("#mainImage").fadeOut('fast', function() { 
			
			$("#mainImage").attr("src", newSrc).fadeIn('fast');
			});
	
	}

}	

	

function goToChanged() {
	
		
	if($('#goTo').attr('value') == 4)
	{
	
		$('#filterContainer #accountName').fadeTo("slow", 1).attr('disabled', '');
	
	}
	else
	{
	
		$('#filterContainer #accountName').fadeTo("slow", 0.2).attr('disabled', 'disabled');
	
	}


}

function gameModeChanged() {
	
		
	if($('#gameMode').attr('value') == 'RANKED')
	{
		$('#sortColumn').attr('disabled', 'disabled').hide();		
		$('#rankType').attr('disabled', '').show();
		$('#rankType').fadeTo("slow", 0.2).attr('disabled', 'disabled');
		$('#speedClass').fadeTo("slow", 0.2).attr('disabled', 'disabled');
		$('#track').fadeTo("slow", 0.2).attr('disabled', 'disabled');	
		
	}
	else if($('#gameMode').attr('value') == 'ZONE'){
	
		$('#rankType').fadeTo("slow", 0.2).attr('disabled', 'disabled');
		$('#speedClass').fadeTo("slow", 0.2).attr('disabled', 'disabled');
		$('#track').fadeTo("slow", 1).attr('disabled', '');	

		$('#rankType').attr('disabled', 'disabled').hide();
		$('#sortColumn').show().attr('disabled', '');
		
	
	}
	else if($('#gameMode').attr('value') == 'SPEED_LAP'){
		
		$('#sortColumn').attr('disabled', 'disabled').hide();
		$('#rankType').attr('disabled', '').show();
		$('#rankType').fadeTo("slow", 0.2).attr('disabled', 'disabled');
		$('#speedClass').fadeTo("slow", 1).attr('disabled', '');
		$('#track').fadeTo("slow", 1).attr('disabled', '');	
	
	}
	else 
	{
		$('#rankType').fadeTo("slow", 1).attr('disabled', '');
		$('#speedClass').fadeTo("slow", 1).attr('disabled', '');
		$('#track').fadeTo("slow", 1).attr('disabled', '');	
		$('#sortColumn').attr('disabled', 'disabled').hide();
		$('#rankType').attr('disabled', '').show();
		
		
	
	}
	
	


}

	
$(document).ready(function(){

			
		if($('itemContainer'))
		{
			$(".itemContainer:nth-child(even)").addClass("dark");
		}
		
	
		
		
		// $("h3").prepend('');
		
		//$('.subNavCurrent').addClass('visible');
		
		if($('#filterContainer'))
		{
			goToChanged();
			gameModeChanged();
		}
		
			
		if($('.galleryItem'))
		{
		
			$('.galleryItem img').fadeTo("normal", 0.7);
		
		}
		
		
		$('.galleryItem').hover(
	      function () {
	        $(this).find('img').fadeTo("normal", 1);
	      }, 
	      function () {
	        $(this).find('img').fadeTo("normal", 0.7);
	      }
	    );
		
	
	
	if($('.teamLogoContainer'))
	{
		
		toggleLogo('.teamLogoContainer')		
	
	}
		
	
	
	//fix for navigation element width in IE6
	
	if($.browser.msie && ($.browser.version == 6))
	{

		// $('#introPanel p').append(', MSIE 6.0');		
		$('#l1').css("marginLeft", "0");
	}
	
		
	});	
	