var features;
var currentFeature;

jQuery(document).ready(prepFeature);
function prepFeature() {
	jQuery.ajax({
		url:'/features.php',
		success: runFeatures,
		error: prepFeatureError,
		dataType: "xml",
		contentType:'text/xml'
	});
}

function prepFeatureError(XMLHttpRequest, textStatus, errorThrown) {
	alert(textStatus);
}

function runFeatures(data,textStatus,XMLHttpRequest) {
	features = jQuery(data).find('feature');
	currentFeature = 0;
	willAdvance();
}

function willAdvance(){
	// check to see if we're transitioning from an html block.
	$htmlblock = jQuery("#flash-stage .featureblock");
	if($htmlblock.length > 0) {
		$htmlblock.fadeOut('fast',advance);
		return;
	} 
	advance();
}

function advance() {
	showFeature(currentFeature);
	
	if((delay = features.eq(currentFeature).attr("runtime")))
	{
		setTimeout(willAdvance,delay);
	}
	
	currentFeature++;
	currentFeature =  currentFeature % features.length;
}

function showFeature(featureIndex) {
	$thisFeature = features.eq(currentFeature);
	switch($thisFeature.attr("type")) {
		case "swf":
			showSWF($thisFeature.attr("source"));
		break;
		case "html":
		default:
			showHTML($thisFeature.attr("source"));
	}
}

function showSWF(source) {
	$featureblock = jQuery("#html-stage").children('.featureblock');
	$featureblock.css("left","12px");
	$featureblock.animate({left:"1020px"},500,'swing').children('.imageblock').fadeOut('slow');
	
	$flash = jQuery("#flash-stage").children('#stage');
	$flash.css("display","none");
	var so = new SWFObject("flash/stage.swf", "stage", "1000", "287", "8", "#000000");
	so.useExpressInstall('flash/expressinstall.swf');
	so.addParam("base","flash/");
	so.addParam("name","stage");
	so.addVariable("movie",source);
	check = so.write("flash-stage");
	$("#flash-stage").fadeIn("slow");
}

function showHTML(source) {
	$("#flash-stage").fadeOut("slow");
	
	jQuery("#html-stage").load(source,'',htmlLoaded);
	jQuery("#html-stage").children('.featureblock');
	
}

function htmlLoaded(responseText, textStatus, XMLHttpRequest) {
	
	$featureblock = jQuery("#html-stage").children('.featureblock');
	$featureblock.css("left","-1020px");
	$featureblock.children(".imageblock").css("display","none");
	$featureblock.animate({left:"12px"},500,'swing').children('.imageblock').fadeIn('slow');
}

function stage_DoFSCommand(command, args) {
	switch(command) {
		case "seriesComplete":
		default:
			willAdvance();
	}
}

