var baseURL = "";
indx = location.href.indexOf("moc");    
baseURL = location.href.substr(0,indx);
var url=location.href;
if(url&&url.length>0&&url.indexOf('/moc')!=-1)
	baseURL=url.substr(0,url.indexOf('/moc')+1);
else if(url&&url.length>0&&url.indexOf('\\moc')!=-1)
	baseURL=url.substr(0,url.indexOf('\\moc')+1);
else if(url&&url.length>0&&url.indexOf('\\index.php')!=-1)
	baseURL=url.substr(0,url.indexOf('\\index.php')+1);
else if(url&&url.length>0&&url.indexOf('/index.php')!=-1)
	baseURL=url.substr(0,url.indexOf('/index.php')+1);
else if(url&&url.length>0&&url.lastIndexOf('/')!=-1)
	baseURL=url.substr(0,url.lastIndexOf('/')+1);
else if(url&&url.length>0&&url.lastIndexOf('\\')!=-1)
	baseURL=url.substr(0,url.lastIndexOf('\\')+1);

var isReady = false;
var homePageReadyCalled = false;


function GetBaseURL()
{
	return baseURL;
}

function watchitoo_OnLogin(username, nickname, certificate, accountType) 
{

	//alert("watchitoo_OnLogin. accountType="+accountType);
	if (accountType == 'Member') {
		//var u = username; /* for scope*/
		//var n = nickname;
		//var c = certificate;
		
		//if (document.getElementById("welcomeMessage")) {
		//	SetNickname(nickname);
		//}
		// redirect to member page
		
		//Check if the user have hase
		if(window.location['hash'].length) {
			//Check if the user try to see anything on #MyStuff - we should redirect him to #HomPage
			var reg = '^#MyStuff(.*)$';
			var MyStuffmatchs = window.location['hash'].match(reg);

			if(MyStuffmatchs && MyStuffmatchs[1] && MyStuffmatchs[1].length>0) {
				var pathname = window.location['pathname'];
				
				//Check if the users look on /X/index.php, if so, remove the index.php from the path
				var reg = '^(.*)index.php$';
				indexMatchs = pathname.match(reg);
				if( indexMatchs && indexMatchs[1] && indexMatchs[1].length>0) //set the new path without index.php
					pathname = indexMatchs[1];
				
				var newUrl = window.location['protocol'] + '//' + window.location['hostname'] + pathname + 'member/#HomePage' + MyStuffmatchs[1];
				window.location = newUrl;
			}
		} else
			window.location="member/";
	} else if(accountType == 'ProMember')
		window.location="pro/";

	//getSWF("HomePage").SetUsername(username,certificate,nickname,accountType);
	
}

function watchitoo_OnLogout() {
	//alert("watchitoo_OnLogout");
	self.parent.clearWelcome();
	//getSWF("HomePage").Signout();
}

function watchitoo_OnLoginFailed(errorCode, description) {
	//alert("watchitoo_OnLoginFailed");
	//getSWF("HomePage").onLoginFailed(errorCode,description);
	/*$('#signinError').css( {'display': 'block'} );
	$('#signinError').html(description);*/
	$('#loginLoader').hide();
}

function watchitoo_OnStateChange(state) {
	//alert("state="+state);
	if(state=="loggedOut")
	{
		// added these lines to ensure logout gets processed
		self.parent.clearWelcome();
		//getSWF("HomePage").Signout();
	
		// added this line to create a guest account if not logged in
		//setTimeout("CreateGuestAccount()",100);
	}
}

function watchitoo_OnRegister(params) {
	//getSWF("HomePage").onRegisterResult('success','', params);
	// redirect to member page
	window.location="member/";
}

function watchitoo_OnRegisterError(errorCode, description, params) {
	//getSWF("HomePage").onRegisterResult('error', description, params);
}

function watchitoo_OnError(text) {
	alert(text);
}

$(document).ready(function() {
	//this is jQuery's init function - being run when the DOM is ready. (before the images are loaded)
	$('#signout').click(function(){
		// added these lines to ensure logout gets processed
		self.parent.clearWelcome();
		Logout();
		//login.logout();
	});

});

