//Global Vars Seup Document
var urlPathName = location.pathname;
var GoCount = 0;
var InitCount = 0;

var browser = navigator.userAgent.toLowerCase();
var users_browser = (browser.indexOf('iphone') == -1 && browser.indexOf('ipod') == -1);

if(urlPathName.indexOf("/WiiWare/") != -1)
{
	var site = location.host+'/WiiWare';
	var start = 'yes';
	var SelectCount = 47;
	var NameGame = 'Beaver Defense';
}

if(urlPathName.indexOf("/iPhone/") != -1)
{
	var site = location.host+'/iPhone';
	var start = 'yes';	
	var SelectCount = 43;
	var NameGame = 'Mobile Mayhem';
}

if(urlPathName.indexOf("/NintendoDS/") != -1)
{
	var site = location.host+'/NintendoDS';
	var start = 'yes';
	var SelectCount = 44;
	var NameGame = '';
}

//Function Setup New href <a>
function setupNewLinks(obj)
{
	var links = obj.getElementsByTagName("a");

	for (i = 0; i < links.length; i ++)
	{
		tmp = links[i].href; 
		tmp = tmp.replace("http://"+site, "");
		indexOfDot = 0;
		
		if(tmp.lastIndexOf(".") > 0) indexOfDot = tmp.lastIndexOf("."); 

		if(tmp.substring(0, 7) != "http://")

		{
			$(links[i]).addEvent('click', function(e) 
			{
				e.stop();
				getMovie().sendFromJS(this.name);
				
				markLinksWithHref(this.href);
				historyVar.getNewHistoryState(this.href.replace("http://"+site+"/#", ""));

				document.getElementById('NavObject').style.display = 'none';

				if(this.className.indexOf('Label') == -1)
				{
					this.className = "curPage";
	
					this.style.display = "none";	
				}
			});

			links[i].href = "#" + tmp;
		}
	}
}

//Setup New Link Sub Pages
function SubNewLinks(obj)
{
	var links = obj.getElementsByTagName("a");
	
	for (i = 0; i < links.length; i ++)
	{
		tmp = links[i].href; 
		tmp = tmp.replace("http://"+site, "");
		indexOfDot = 0;
		
		if(tmp.lastIndexOf(".") > 0) indexOfDot = tmp.lastIndexOf("."); 

		if(tmp.substring(0, 7) != "http://")
		{
			$(links[i]).addEvent('click', function(e) 
			{
				e.stop();
				getMovie().sendFromJS(this.name);

				markLinksWithHref(this.href);
				historyVar.getNewHistoryState(this.href.replace("http://"+site+"/#", ""));

				document.getElementById('NavObject').style.display = 'none';

				if(this.className.indexOf('Label') == -1)
				{
					this.className = "curPage";
	
					this.style.display = "none";	
				}
			});
			
			links[i].href = "#" + tmp;
		}
	}
}

//Function Select Element CSS
function markLinksWithHref(linkHref)
{	
	var links = document.getElementById("Header").getElementsByTagName("a");

	if(navigator.appName == 'Microsoft Internet Explorer') linkHref = linkHref.replace("http://"+site+"/", "");

	for (i = 0; i < links.length; i ++)
	{
		var SetScreenBoo1 = links[i].href.indexOf("screenshots");
		var SetScreenBoo2 = linkHref.indexOf("screenshots");
	
		if(links[i].href == linkHref && SetScreenBoo1 == SetScreenBoo2 || links[i].href == linkHref+"/" && SetScreenBoo1 == SetScreenBoo2)
		{
			var SetName = links[i].name;

			if(SetName == 3 || SetName == 4 || SetName == 10)
			{
				if(links[i].className.indexOf('Label') == -1)
				{
					links[i].style.display = 'none';
					links[i].className = "curPage";		
				}

				document.getElementById('Block30').style.display = 'block';
				document.getElementById('Block40').style.display = 'none';
				document.getElementById('Block50').style.display = 'none';

				if(getMovie().sendFromJS != undefined)
				{
					getMovie().sendFromJS(SetName);
				}
			}
			else if(SetName == 5 || SetName == 6 || SetName == 7)
			{
				if(links[i].className.indexOf('Label') == -1)
				{
					links[i].style.display = 'none';
					links[i].className = "curPage";		
				}

				document.getElementById('Block30').style.display = 'none';
				document.getElementById('Block40').style.display = 'block';
				document.getElementById('Block50').style.display = 'none';

				if(getMovie().sendFromJS != undefined)
				{
					getMovie().sendFromJS(SetName);
				}
			}
			else if(SetName == 8 || SetName == 9 || SetName == 12)
			{
				if(links[i].className.indexOf('Label') == -1)
				{
					links[i].style.display = 'none';
					links[i].className = "curPage";		
				}

				document.getElementById('Block30').style.display = 'none';
				document.getElementById('Block40').style.display = 'none';
				document.getElementById('Block50').style.display = 'block';

				if(getMovie().sendFromJS != undefined)
				{
					getMovie().sendFromJS(SetName);
				}
			}
			else
			{
				if(links[i].className.indexOf('Label') == -1)
				{
					links[i].style.display = 'none';
					links[i].className = "curPage";		
				}

				document.getElementById('Block30').style.display = 'none';
				document.getElementById('Block40').style.display = 'none';
				document.getElementById('Block50').style.display = 'none';

				if(getMovie().sendFromJS != undefined)
				{
					getMovie().sendFromJS(SetName);
				}
			}
			
			//alert(SetName + ' = AU 1; ' + SetScreenBoo1 + ' = ' + SetScreenBoo2);
		}
		else if(links[i].href != linkHref && SetScreenBoo1 != SetScreenBoo2 || links[i].href != linkHref+"/" && SetScreenBoo1 != SetScreenBoo2)
		{
			var SetName = links[i].name;
			
			if(links[i].className.indexOf('Label') == -1)
			{
				links[i].style.display = 'block';
				links[i].className = "pp";		
			}
			
			//alert(SetName + ' = AU 2; ' + SetScreenBoo1 + ' = ' + SetScreenBoo2);
		}
		else if(links[i].href != linkHref && SetScreenBoo1 == SetScreenBoo2 || links[i].href != linkHref+"/" && SetScreenBoo1 == SetScreenBoo2)
		{
			var SetName = links[i].name;
			
			if(links[i].className.indexOf('Label') == -1)
			{
				links[i].style.display = 'block';
				links[i].className = "pp";		
			}
			
			//alert(SetName + ' = AU 2; ' + SetScreenBoo1 + ' = ' + SetScreenBoo2);
		}
	}
}

//Function Setup Strt & Click Object
function checkAnchorOnload()
{
	var urlPathName = location.pathname;
	var urlAnchor = location.hash;

	if(start == 'yes')
	{
		if(urlPathName != "/WiiWare/" && urlPathName.indexOf("/WiiWare/") != -1) 
		{
			location.href = "http://"+site+"/#"+urlPathName.replace("/WiiWare","");
		}
		else if(urlPathName != "/iPhone/" && urlPathName.indexOf("/iPhone/") != -1) 
		{
			location.href = "http://"+site+"/#"+urlPathName.replace("/iPhone","");
		}
		else if(urlPathName != "/NintendoDS/" && urlPathName.indexOf("/NintendoDS/") != -1) 
		{
			location.href = "http://"+site+"/#"+urlPathName.replace("/NintendoDS","");
		}
		else if(urlAnchor != "" && urlAnchor.indexOf("#/") != -1)
		{
			HTMLRequest(urlAnchor.replace("#/", ""));
		}
	}
	else if(start == '')
	{
		if(urlPathName == "/WiiWare/" && urlPathName.indexOf("/WiiWare/") != -1) 
		{
			if(urlAnchor == "" || (urlAnchor == "#" && navigator.appName == 'Microsoft Internet Explorer')) 
			{
				HTMLRequest("http://"+site+"/");
			}
			else if(urlAnchor.indexOf("#/") != -1) 
			{
				HTMLRequest(urlAnchor.replace("#/", ""));
			}
		}
		else if(urlPathName == "/iPhone/" && urlPathName.indexOf("/iPhone/") != -1) 
		{
			if(urlAnchor == "" || (urlAnchor == "#" && navigator.appName == 'Microsoft Internet Explorer')) 
			{
				HTMLRequest("http://"+site+"/");
			}
			else if(urlAnchor.indexOf("#/") != -1) 
			{
				HTMLRequest(urlAnchor.replace("#/", ""));
			}
		}
		else if(urlPathName == "/NintendoDS/" && urlPathName.indexOf("/NintendoDS/") != -1) 
		{
			if(urlAnchor == "" || (urlAnchor == "#" && navigator.appName == 'Microsoft Internet Explorer')) 
			{
				HTMLRequest("http://"+site+"/");
			}
			else if(urlAnchor.indexOf("#/") != -1) 
			{
				HTMLRequest(urlAnchor.replace("#/", ""));
			}
		}
	}
}



//Function HTTL Request

function HTMLRequest(href)

{
	$('ajaxloaded').fade(1,0.15);

	var r = new Request({
		method: 'get',
		url: href,
		data: "ajax=yes",
		onSuccess: function(responseText)
		{
			var pasturlAnchor = location.hash;
			if(NameGame == 'Beaver Defense')
			{
				var NameGameTitle = 'Tower Defense game';	
			}
			else
			{
				var NameGameTitle = 'RTS';
			}
			var urlAnchor = 'Robocalypse - ' + NameGame + ' - An Innovative ' + NameGameTitle + ' for ' + site.replace(location.host+'/',"") + ' by Vogster Entertainment | ' + location.hash + ' ';
			
			var titleAnchor = urlAnchor.replace("#/","");
			var Title = titleAnchor.replace(".html","");
			var TitleEnd = Title.replace("/"," > ");
			var TitleEndEnd = TitleEnd.replace("/"," > ");
			var TitleEndEndEnd = TitleEndEnd.replace("/"," > ");
			var TitleEndEndEndEnd = TitleEndEndEnd.replace("_"," ");
			
			document.title = TitleEndEndEndEnd;

			if(pasturlAnchor == "" || (pasturlAnchor == "#" && navigator.appName == 'Microsoft Internet Explorer') || pasturlAnchor.indexOf("home.html") != -1) 
			{
				if(urlAnchor.indexOf("home.html") != -1)
				{
					document.getElementById('NavObject').style.display = 'block';
					document.getElementById('ajaxloaded').innerHTML = responseText;
					document.getElementById('ClassFooterId').className = 'Home';

					if(navigator.appName == 'Microsoft Internet Explorer')
					{
						markLinksWithHref(window.location.href);
						document.title = TitleEndEndEndEnd.replace("#/home.html","");
						$('ajaxloaded').fade('in');
					}
					else
					{
						markLinksWithHref(window.location.href);
						document.title = TitleEndEndEndEnd + 'home';
						$('ajaxloaded').fade('in');
					} 
				}
				else
				{
					document.getElementById('NavObject').style.display = 'block';
					document.getElementById('ajaxloaded').innerHTML = responseText;
					document.getElementById('ClassFooterId').className = 'Home';
	
					if(navigator.appName == 'Microsoft Internet Explorer')
					{
						markLinksWithHref(window.location.href.replace("#","")+'#/home.html');
						document.title = TitleEndEndEndEnd + 'home';
						$('ajaxloaded').fade('in');
					}
					else
					{
						markLinksWithHref(window.location.href+'#/home.html');
						document.title = TitleEndEndEndEnd + 'home';
						$('ajaxloaded').fade('in');
					} 
				}
			}
			else
			{
				var urlAnchor = location.hash;	

				document.getElementById('NavObject').style.display = 'block';
				document.getElementById('ajaxloaded').innerHTML = responseText;
				document.getElementById('ClassFooterId').className = 'SubPage';

				markLinksWithHref(window.location.href);
				$('ajaxloaded').fade('in');

				if(document.getElementById('PageScNav') != undefined && urlAnchor.indexOf("screenshots") != -1)
				{
					SubNewLinks(document.getElementById('PageScNav'));

					if(getMovie().sendFromJS != undefined)
					{
						getMovie().sendFromJS('8');
					}
				}
				else if(document.getElementById('PageScNav') != undefined && urlAnchor.indexOf("wallpapers") != -1)
				{
					SubNewLinks(document.getElementById('PageScNav'));

					if(getMovie().sendFromJS != undefined)
					{
						getMovie().sendFromJS('12');
					}
				}
				else if(document.getElementById('PageScNav') != undefined && urlAnchor.indexOf("trailers") != -1)
				{
					SubNewLinks(document.getElementById('PageScNav'));

					if(getMovie().sendFromJS != undefined)
					{
						getMovie().sendFromJS('9');

					}
				}
			}
		},
		onFailure: function()
		{
			window.location.href = "";
			HTMLRequest("home.html")
			//alert('Errore :(');
		}
	});
	r.send();	
}

//Function Setup History
function historyHandler() 
{
	var stateVar = "nothin'", displayDiv = document.getElementById("ajaxloaded");

	this.getNewHistoryState = function(currentState) 
	{
		var newVal = currentState;
		unFocus.History.addHistory(newVal);
	};
	
	this.historyListener = function(historyHash) 
	{
		stateVar = historyHash;
		checkAnchorOnload();
	};

	unFocus.History.addEventListener('historyChange', this.historyListener);
	this.historyListener(unFocus.History.getCurrent());
};



//Setup History Var
var historyVar;

//Function Initialize Script
function init()
{
	setupNewLinks(document.getElementById('Header'));
	historyVar = new historyHandler();
	start = '';
}

//Function Anime Open
function PreloaderBig(item)
{
	var imgs,i;
	var imgs = document.getElementById('ajaxloaded').getElementsByTagName('div');
	
	for(i=0; i<imgs.length; i++)
	{
		if(imgs[i].className == 'WiiWareImage' || imgs[i].className == 'NintendoDSImage' || imgs[i].className == 'iPhoneImage')
		{
			$(imgs[i]).fade(1,0.15);
		}

		if(i == imgs.length - 1)
		{
			document.getElementById('ImLoader').src = item;
			document.getElementById('ImgBig').style.visibility = 'visible';

			document.getElementById('SetDownload').href = item;
		}
	}
}

//Function Anime Closs
function PreloaderBigClose()
{
	var imgs,i;
	var imgs = document.getElementById('ajaxloaded').getElementsByTagName('div');

	for(i=0; i<imgs.length; i++)
	{
		if(imgs[i].className == 'WiiWareImage' || imgs[i].className == 'NintendoDSImage' || imgs[i].className == 'iPhoneImage')
		{
			$(imgs[i]).fade(0.15,1);
		}
		
		if(i == imgs.length - 1)
		{
			document.getElementById('ImLoader').src = '/templates/default/images/blank.gif';
			document.getElementById('ImgBig').style.visibility = 'hidden';
			document.getElementById('SetDownload').href = '';
		}
	}
}

//Function Preloader Images
function simplePreload()
{
	var GroupLoader = '';
	var args = simplePreload.arguments;

	document.imageArray = new Array(args.length);
	
	for(var i=0; i<args.length; i++)
	{
		document.imageArray[i] = new Image;
		document.imageArray[i].onload = function () 
		{
			SetEndPreloader();
		};
		document.imageArray[i].src = args[i];
	}
}

//Function Set End Preloader
function SetEndPreloader()
{
	GoCount++;
	var PercentLoaded = Math.round(GoCount*(100/SelectCount));

	if(PercentLoaded >= 100)
	{
		document.getElementById('Counter').innerHTML = '<span>100%</span>';	
	}
	else
	{
		document.getElementById('Counter').innerHTML = '<span>' + PercentLoaded + '%</span>';	
	}

	if(GoCount >= SelectCount)
	{
		var urlPathName = location.pathname;
		var urlAnchor = location.hash;

		if(urlAnchor.indexOf("#/") != -1 || urlAnchor == '' || (urlAnchor == "#" && navigator.appName == 'Microsoft Internet Explorer'))
		{
			document.getElementById('Counter').innerHTML = '<span>100%</span>';
			document.getElementById('MainBody').style.visibility = 'visible';
			document.getElementById('Platforms').style.visibility = 'visible';
			document.body.style.overflow = 'visible';
			$('Preloader').fade('out');	
		}
	}
}

//Set Top Position Anime Platforms Element
function SetPlatformScroll()
{	
	var SetResalt = (getClientHeight() - 108)/2;
	var ScrollTopSet = getBodyScrollTop();
	var SetBottomScroll = document.getElementById('MainBody').offsetHeight - 505;
	var SetResultUI =  (document.getElementById('MainBody').offsetHeight - 108)/2;

	var el = $('Platforms');

	if(getClientHeight() >= document.getElementById('MainBody').offsetHeight)
	{
		if(SetResultUI <= 335)
		{
			el.tween('top', '335');	
		}
		else
		{
			el.tween('top', SetResultUI);
		}	
	}
	else
	{
		if(getBodyScrollTop() >= SetBottomScroll && getBodyScrollTop() > 335)
		{
			el.tween('top', (document.getElementById('MainBody').offsetHeight - 505));
		}

		else if(getBodyScrollTop() <= SetBottomScroll && getBodyScrollTop() <= 335)
		{
			var SetCy = getClientHeight() - (108 + 315*2);

			if(SetCy >= 0)
			{
				el.tween('top', (SetResalt + getBodyScrollTop()));	
			}
			else
			{
				el.tween('top', '335');		
			}	
		}
		else if(getBodyScrollTop() <= SetBottomScroll && getBodyScrollTop() > 315)
		{
			el.tween('top', (SetResalt + getBodyScrollTop()));	
		}	
	}
}

if(users_browser != true)
{
}
else
{
	setInterval("SetPlatformScroll()", 1000);
}

//
function getBodyScrollTop()
{
	return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}

//
function getClientHeight()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
}

//Window Ready
window.addEvent('domready', function() 
{
	if(users_browser != true)
	{
		
	}
	else
	{
		init();	
	}
});