// <on load>

var pageLoaded = false;
var profileType = "";
var userID = "";
var userName = "";
var userAvr = 0;
var SWFsArray = new Array();
var currContent = "miPlaceMain";
var lateAction = ""; //action to be performed after full load
var opaqueParams = {wmode:"opaque",bgcolor:skinColor,menu:"false"}

//<specific for special areas>
var showPictureChangeScreen = false;
//</specific for special areas>

//<navs>
SWFsArray["header"] = {filePath:"header.swf",divName:"header",width:980,height:163,className:"header",params:opaqueParams};
SWFsArray["navBar"] = {filePath:"profileNavBar.swf",divName:"navBar",width:352,height:325,className:"navBar",params:opaqueParams};
//</navs>
//<contents>
SWFsArray["miPlaceMain"] = {filePath:"miPlaceMain.swf",divName:"content",width:980,height:1350,className:"content",params:opaqueParams};
SWFsArray["hizPlaceMain"] = {filePath:"hizPlaceMain.swf",divName:"content",width:980,height:1150,className:"content",params:opaqueParams};
SWFsArray["miPlaceFriends"] = {filePath:"miPlaceFriends.swf",divName:"content",width:980,height:850,className:"content",params:opaqueParams};
SWFsArray["hizPlaceFriends"] = {filePath:"hizPlaceFriends.swf",divName:"content",width:980,height:390,className:"content",params:opaqueParams};
SWFsArray["miPlaceWallet"] = {filePath:"miPlaceWallet.swf",divName:"content",width:980,height:450,className:"content",params:opaqueParams};
SWFsArray["miPlaceMessages"] = {filePath:"miPlaceMessages.swf",divName:"content",width:980,height:600,className:"content",params:opaqueParams};
SWFsArray["miPlaceUploads"] = {filePath:"miPlaceUploads.swf",divName:"content",width:980,height:680,className:"content",params:opaqueParams};
SWFsArray["hizPlaceUploads"] = {filePath:"hizPlaceUploads.swf",divName:"content",width:980,height:390,className:"content",params:opaqueParams};
SWFsArray["miPlaceFavorites"] = {filePath:"miPlaceFavorites.swf",divName:"content",width:980,height:390,className:"content",params:opaqueParams};
SWFsArray["hizPlaceFavorites"] = {filePath:"hizPlaceFavorites.swf",divName:"content",width:980,height:390,className:"content",params:opaqueParams};
SWFsArray["miPlaceTree"] = {filePath:"miPlaceTree.swf",divName:"content",width:980,height:950,className:"content",params:opaqueParams};
SWFsArray["hizPlaceTree"] = {filePath:"hizPlaceTree.swf",divName:"content",width:980,height:950,className:"content",params:opaqueParams};
SWFsArray["notLogged"] = {filePath:"MiPlaceEmpty.swf",divName:"content",width:980,height:700,className:"content",params:opaqueParams};

//</contents>
//<popups>
SWFsArray["writePM"] = {filePath:"writePM.swf",divName:"normalPopup",width:520,height:300,className:"",params:opaqueParams,posY:300,posX:400};
SWFsArray["addFriend"] = {filePath:"addFriend.swf",divName:"normalPopup",width:520,height:270,className:"",params:opaqueParams,posY:300,posX:400};
//</popups>
var headerHeight = SWFsArray["header"].height;
var overallHeight = 0;

//correct skin folder:
for (i in SWFsArray){
	SWFsArray[i].filePath = skinFolder + SWFsArray[i].filePath;
}

// </on load>


function initPage(){
	setLoader("Plate");
	checkParams();
}

function firstTimeNavBar(){
	if (userID==-1){
		changeContent("notLogged");
		loadRest();
		return;
	}
	placeLoaderTop(headerHeight);
	changeCurrContent("PlaceMain");
		if (actionVar!=null){
			switch (actionVar){
				case "op":
					changeCurrContent(pageParams.wp);			
				break;
				case "pm":
					lateAction = "pm";
				break;
				default:break;
			}
		}
	enterTransitionMode(SWFsArray[currContent].height);
	embedSWF(SWFsArray["navBar"],headerHeight);
}

function firstTimeContent(){
		embedSWF(SWFsArray[currContent],headerHeight);
		loadRest(); //shouldn't be here. make event.
}

function loadRest(){
	initDE();
	//hide loader:
	enable();
	pageLoaded = true;
	runAction();
}

function runAction(){
	switch (lateAction){
		case "pm":
			showPopup("writePM",{posX:400,posY:200,toIds:pageParams.pmuid})	
		break;
		default:break;
	}
}

function stepInit(step){
	switch(step){

		case "header":
		firstTimeNavBar();
		break;
		case "navBar":
		firstTimeContent();
		break;
		case "content": exitTransitionMode();
		if (!pageLoaded){
			loadRest();
		}
		break;
		case "popup": forcePopupSet();
		break;
		case "DE": DEFirstLoad();
		break;
		default:break;
		
	}
}

function changeContent(newContent){
	changeCurrContent(newContent);
	enterTransitionMode(SWFsArray[currContent].height);	
	embedSWF(SWFsArray[currContent],headerHeight);
}

function enterTransitionMode(contentHeight){
	showLoader(contentHeight);
	placeFooter(contentHeight+headerHeight);
	overallHeight = contentHeight+headerHeight
}

function exitTransitionMode(){
	hideLoader();
}

function checkParams(){
	if (pageParams["uid"]!=undefined){
		//entered userID for profile - check info for that user, for use in all SWFs
		userID = Number(pageParams.uid);
		profileType = (userID==sessionVars.uid)?"mi":"hiz";
		requestUserInfo();
	} else {
		if (sessionVars.s == "n"){
			//user not logged in - show default "you're not logged"
			userID = -1;
			embedSWF(SWFsArray["header"],0);
			return;
		}
		//if no parameters - go to MyPlace
		userID = sessionVars.uid;
		profileType = "mi";
		requestUserInfo();
	}

}

function requestUserInfo(){
	ajaxpack.postAjaxRequest("/app/mbUsr.php", "cmd=user&uid="+userID, receiveUserInfo, "txt")	
}

function receiveUserInfo(){
	var ajaxResponse = ajaxpack.ajaxobj;
	if (ajaxResponse.readyState == 4){ //if request of file completed
		if (ajaxResponse.status==200 || window.location.href.indexOf("http")==-1){ //if request was successful or running script locally
				var responseArr = responseStringToArray(ajaxResponse.responseText);
				if (responseArr["data"]=='0,,0'){
					//if user not found - return to main page
					backToMainPage();
				}else{
					//user OK - continue page load
					var userInfoArr = responseArr["data"].split(",");
					userName = userInfoArr[1];
					userAvr = userInfoArr[2];
					for (i in SWFsArray){
					SWFsArray[i].extraVars = {pType:profileType,profileUserID:userID,profileUserName:userName,profileUserAvr:userAvr};
					}
					document.title = "Mobilhood - "+userName+"'s profile";
					embedSWF(SWFsArray["header"],0);
				}
		}
	}	
}

function backToMainPage(){
	navigate("home",{})
}

function changeCurrContent(newContent){
	currContent = profileType+newContent;
}

function specialAreaLoaded(areaName){
	switch (areaName){
		case "pictureChooser":
			if(showPictureChangeScreen){
				choosePicture();
			}
		break;
		default:break;
	}
}

function choosePicture(){
	if (profileType=="mi"){
		if (currContent=="miPlaceMain"){
			document.getElementById("content").choosePicture();
		} else {
			showPictureChangeScreen = true;
			changeContent("PlaceMain");
		}
	}
}

function refreshNavbarPic(){
		document.getElementById(SWFsArray["navBar"].divName).refreshPicture();
}

function refreshFavs(){
	var tbSWF = document.getElementById("content");
	if (tbSWF){
		tbSWF.refreshFavs();
	}
}