var minimize;				
var links = new Array();	//array of links for the feature

function firstRun(doRun){
	$.get("feature/ps-features.xml",{},function(xml){
		$('feature',xml).each(function(i){
			imgAlt = $(this).find("alt").text();
			imgName = $(this).find("name").text();
			imgURL = $(this).find("location").text();
			imgLink = $(this).find("destination").text();
			links.push(imgLink);
			$("#ps-images").append("<img id='"+i+"' name='"+imgName+"' alt='"+imgAlt+"' src='"+imgURL+"'>");
		});
		$('#ps-images').cycle({
		    fx: 'fade',
			timeout: 6000,
			random: 1,
			before: onBefore,
			after: onAfter
		});
	});
	if(doRun){
		$('#images').cycle('pause');
		autoPlay();
	}else{
		$('#images').cycle('resume');
	}
	$('#ps-togglePlayerOn').click(function(){
		showPlayer();
	});
	$('#ps-togglePlayerOff').click(function(){
		hidePlayer();
	});
}
function autoPlay(){
	showPlayer();
	minimize = setTimeout(function(){hidePlayer();},33000); //hidesPlayer after autoPlay
}
function showPlayer(){
	$('#ps-images').cycle('pause');
	$('#ps-videoOverlayBackground').animate
	    ({"width": "toggle", "height": "toggle"}, 'slow', function(){
			$('#ps-videoOverlayPlayer').animate
				({"opacity": "toggle"}, 'slow');
		});
	$('#ps-images').fadeTo('slow', .2);
	$('#ps-togglePlayerOff').show();
	$('#ps-togglePlayerOn').fadeOut('fast');
	$('#ps-applyNow').fadeOut('fast');
}
function hidePlayer(){
	if(minimize>0){
		clearTimeout(minimize);
		minimize = 0;
	}
	$('#ps-images').cycle('resume');
	$('#ps-videoOverlayPlayer').hide('fast');
	$('#ps-videoOverlayBackground').hide('fast');
	$('#ps-images').fadeTo('slow', 1);
	$('#ps-togglePlayerOff').hide();
	$('#ps-togglePlayerOn').fadeIn('slow');
	$('#ps-applyNow').fadeIn('slow');
}
function onBefore(){
	$("#ps-slideDescription").fadeOut("slow");
}
function onAfter(){
	document.getElementById("ps-slideDescription").innerHTML = "";
    $('#ps-slideDescription').append("<a href='"+links[this.id]+"'>"+this.alt+"</a>");
	$("#ps-slideDescription").fadeIn("slow");
}
function loadImage(name){
	document.getElementById("ps-imageDiv").innerHTML = "";
	document.getElementById("ps-imageDiv").style.display = "none";
	var img = new Image();
	$(img).attr("src",name).load(function(){
		$("#ps-imageDiv").append(img);
		$("#ps-imageDiv").fadeIn("slow");
	});
}