var prefix = document.location.host == "playauditorium.com" ? "www." : "" ;
if(prefix == "www."){
	document.location.href = "http://" + prefix + document.location.href.substr(7);
}



$(document).ready(function()
{
	// init page
	if(!init_page()){
		window.parent.location = "http://www.playauditorium.com"
		return;
	}
	// linkage for focus removal
	$("#header #UserLoginForm input").bind("focus",focus);
	// replace text element with password element for secure login
	$("#UserPassword").focus(function(){
		//$(this).clone().attr("type", "password").insertBefore(this).focus();
		$(document.createElement("input"))
			.attr("name", $(this).attr("name"))
			.attr("id", $(this).attr("id"))
			.attr("type", "password")
			.insertBefore(this)
			.focus();
		$(this).remove();
	});
});

// setting webroot destination
function setWebroot(path)
{
	window.webroot = path;
}

// remove initial value on focus
function focus()
{
	$(this).attr("value","");
	$(this).unbind("focus",focus);	
}

// siFR
function init_page()
{
	//Check for iFrame
	var isInIFrame = (window.location != window.parent.location) ? true : false;
	if (isInIFrame)
	{
		return false;
	}

	// sIFR
	if(typeof sIFR == "function")
	{
		sIFR.replaceElement("h2", window.webroot + "swfs/bauhaus-light.swf", "#4a4a4a", "#4a4a4a", "#a1eca1", "transparent", 0, 0, 0, 0, "", "upper", "transparent");
		sIFR.replaceElement("h1", window.webroot + "swfs/bauhaus-light.swf", "#e5e5e5", "#e5e5e5", "#a1eca1", "transparent", 0, 0, 0, 0, "", "", "transparent");
	};
	// forms
	$("form.ajax").ajaxForm(function(response){
		$("#content #ajax").html(response)
		init_page();
	});
	// ajax links
	$("a.ajax").click(function(){
		$("#content #ajax").load($(this).attr("href"),null,function(){
			init_page();
		});
		$(this).blur();
		return false;
	});
	
	$(".screenshots_1 .img_list a").click(function(){
		$("#screenshot_1")
			.html("<img src='" + $(this).attr("href") + "'/>")
			.show()
			.click(function(){
				$(this).hide();
			});
		
		$(this).blur();
		return false;
	});
	
	$(".screenshots_2 .img_list a").click(function(){
		$("#screenshot_2")
			.html("<img src='" + $(this).attr("href") + "'/>")
			.show()
			.click(function(){
				$(this).hide();
			});
		
		$(this).blur();
		return false;
	});
	
	$("a.solution:not(.complete)").click(function(){
		load_level($(this).attr('rel'));
		$(this).blur();
	});
	
	$("a.complete").click(function(){
		act_complete($(this).attr('rel'));
		$(this).blur();
	});
	
	return true;
	
}


function load_level(params){
	var game = document.getElementById("game_swf");
	
	if(game){
		if (game.loadSolutionLevel){
			game.loadSolutionLevel(params);
		}else{
			setTimeout("load_level("+params+"')", 50);
		}
	}
}

function act_complete(params){
	var game = document.getElementById("game_swf");
	
	if(game){
		if (game.loadActComplete){
			game.loadActComplete(params);
		}else{
			setTimeout("act_complete("+params+"')", 50);
		}
	}
}



function showSnapshot(level_id, solution_id){
	$('#l_'+level_id).fadeOut('medium', function(){
		$('#l_'+level_id).load(window.webroot + "external/solution_view/" + solution_id, null, function(){
			$(this).fadeIn('medium');
		});
	})
	
}


function actAvailable(act_id, act_number, playlist_id){
	//trace('actAvailable ' + act_id + "," + act_number + ", " + playlist_id);
	$('#a_'+act_id).load(window.webroot + "external/act_view/" + act_id + "/" + act_number + "/" + playlist_id, null, function(){
		init_page();
	});
}

// ajaxLoad - used by flash to do ajax load requests
function ajaxLoad(url)
{
	$("#content #ajax").load(url,null,function(){
		init_page();
	});	
}

function cleartrace(){
	$("#tracer").html("");
}


function trace(output){
	if (!document.getElementById("tracer")){
		$(document.createElement("div"))
			.attr("id", "tracer")
			.prependTo("#content")
	}
	$("#tracer").html($("#tracer").html() + "<br/>" + output);
}
