var isIE8Plus = false;
var isIE7 = false;
var isIE6 = false;
if (Prototype.BrowserFeatures.SelectorsAPI && Prototype.Browser.IE) {
	isIE8Plus = true;
}
if (navigator.appVersion.indexOf ("MSIE 7") > -1) {
	isIE7 = true;
}
else if (navigator.appVersion.indexOf ("MSIE 6") > -1) {
	isIE6 = true;
}
var isMainStageSet = false;
var currentHash = "";

function SetMainStage () {
	$('changing-the-world').hide ();
	$("powerdish").hide ();
	$("by-infinia").hide ();
	$('top-nav').hide ();
	$('main-pane-container').show ();
	
	$('powerdish').setStyle ({ left: "94px", top: "52px"});
	$('by-infinia').setStyle ({ left: "205px", top: "110px"});
	$('top-nav').setStyle ({ left: "514px", top: "21px"});
	$('powerdish').appear({duration: 0.5, queue: 'end'});
	$('by-infinia').appear({duration: 0.5, queue: 'end'});
	$('top-nav').appear({duration: 0.5, queue: 'end'});
	
	isMainStageSet = true;
}
function ChangeBodyContent (id) {
	$("main-body").update ($("c-" + id).innerHTML);
	$$("#main-body-nav li").each (
		function (o, i) {
			o.removeClassName ("current");	
		}
	);
	$(id).addClassName ("current");
	currentHash = "#content=" + id;
	if (Prototype.Browser.Opera == true) {
		var marginTop = parseInt ($("main-pane-container").getStyle ("margin-top"), 10);
		var paddingTop = parseInt ($("main-pane-container").getStyle ("padding-top"), 10);
		$('main-body-footer-container').style.top = $("main-pane-container").getHeight () + marginTop + paddingTop + "px";
	}
	else if (Prototype.Browser.IE == true && isIE8Plus == false) {
		var h = $("main-body").getHeight ();
		if (isIE7 === true) {
			if (h < 270) {
				$("main-body").setStyle ({minHeight: "270px"});	
			}
		}
		else if (isIE6 === true) {
			if (h < 270) {
				$("main-body").setStyle ({height: "270px"});	
			}
		}
		$('main-body-footer-container').style.top = $("main-body").getHeight () + 66 + 20 + "px";
	}
}

var isMainReady = 0;
function GoMain () {
	isMainReady++;
	if (isMainReady > 2) {
		SetMainStage ();
		ChangeBodyContent ("overview");										   
	}
	else if (isMainReady > 1) {
		isMainReady++;
		window.setTimeout('GoMain ()', 4000);
	}
}
function ChangingTheWorld () {
	$('by-infinia').fade({duration: 1, afterFinish: GoMain});
	$('changing-the-world').appear({duration: 1, afterFinish: GoMain});
}

Event.observe(window, 'hashchange', function () {
	if (location.hash != currentHash) {
		var hash = location.hash.split ("#content=");
		ChangeBodyContent (hash[1]);										   
	}
});


Event.observe(document, 'dom:loaded', function() {
	$("main-body-nav").observe ("click", function (e) {
		e.stop ();
		var id = e.element ().up ("li").id;
		ChangeBodyContent (id);
		$("main-body").focus ();
		location.hash = "#content=" + id;
	});
	$("main-body").observe ("mouseover", function (e) {
		var a = e.findElement ("a");
		if (a) {
			a = $(a);
			var container = a.up (".body-thumbnails");
			if (container) {
				e.stop ();
				var img = $(a).down ("img");
				new Effect.Morph($(img), {
					style: 'width: 159px; height: 86px;',
					duration: 0.1
				});
			}
		}
	});
	$("main-body").observe ("mouseout", function (e) {
		var a = e.findElement ("a");
		if (a) {
			a = $(a);
			var container = a.up (".body-thumbnails");
			if (container) {
				e.stop ();
				var img = $(a).down ("img");
				new Effect.Morph($(img), {
					style: 'width: 79px; height: 43px;',
					duration: 0.1
				});
			}
		}
	});
});

Event.observe(window, 'load', function() {
	if (isIE6 == true) {
		SetMainStage ();
		ChangeBodyContent ("overview");										   
	}
	else {
		if (location.hash.indexOf ("#content=") > -1) {
			$('background').show ();
			var hash = location.hash.split ("#content=");
			SetMainStage ();
			ChangeBodyContent (hash[1]);										   
		}
		else {
			$('background').appear({duration: 3.0});
			$('powerdish').appear({duration: 1.0, queue: 'end'});
			$('by-infinia').appear({duration: 0.5, queue: 'end'});
			$('top-nav').appear({duration: 0.2, queue: 'end', afterFinish: function () {window.setTimeout('ChangingTheWorld ();', 1000)}});
			
			$("powerdish").observe ("click", function (e) {
				SetMainStage ();
				ChangeBodyContent ("overview");										   
			});
		}
	}
});

