function SessionProxy(){}

SessionProxy.UpdateSession = function(mark, username, community, persist){
	// update the Session cookie
	document.cookie= "SMSession="+mark+"|"+username+"|"+community+"; path=/;";

	// if persist, we need a Persistence cookie to remember
	if(persist=="Y"){
		document.cookie= "SMPersist=Y; path=/; max-age=" + (60*60*24*30);
	}else{
		document.cookie= "SMPersist=; path=/; max-age=0";
	}

}

SessionProxy.GetSessionInfo = function(){
	if(!SessionProxy.sessionInfo){
		var cc = SessionProxy.GetCC();
		var cookie_str = document.cookie;
		var beginIndex = cookie_str.indexOf("SMSession",0);
		if(beginIndex==-1){
			SessionProxy.sessionInfo = cc;
		}else{
			var endIndex = cookie_str.indexOf(";",beginIndex);
			endIndex = endIndex==-1 ? cookie_str.length : endIndex;
			var session_str = cookie_str.substring(beginIndex,endIndex);
			session_str = session_str.split("=")[1];
			session_str = session_str.split("|");
			var mark = session_str[0];
			var uusername = session_str[1];
			var community = session_str[2];
			var persist="N";
			if(session_str.length>3) persist = session_str[3];
			
			if(persist=="Y"){
				// bad cookie
				SessionProxy.sessionInfo = cc;
			}else{
				// check for new cookie
				beginIndex = cookie_str.indexOf("SMPersist=Y",0);
				persist = beginIndex==-1 ? "N" : "Y";
							
				SessionProxy.sessionInfo = "mark=" + mark + "&uusername=" + uusername + "&community=" + community + "&persist=" + persist + "&" + cc;
			}
		}
	}

	return SessionProxy.sessionInfo;
}

SessionProxy.GetCC = function(){
	if(!SessionProxy.cc){
		var cc="";
		// check query string
		var search = window.location.search.toLowerCase();
		var loc = search.indexOf("cc");
		var cookie_str = document.cookie;
		var beginIndex = cookie_str.indexOf("SMCC",0);
		if(loc!=-1){
			loc+=3;
			var end = search.indexOf("&",loc);
			if(end==-1) end = search.length;
			cc = search.substring(loc,end);
			
			var cookie_str = "SMCC="+cc+"; ";
		
			// update cookie
			var edate = new Date();
			edate.setDate(edate.getDate()+90);
			cookie_str += "expires=" + edate.toUTCString();
			
			document.cookie = cookie_str;
	
		// else check cookie
		}else if(beginIndex!=-1){
			var endIndex = cookie_str.indexOf(";",beginIndex);
			endIndex = endIndex==-1 ? cookie_str.length : endIndex;
			cc = cookie_str.substring(beginIndex,endIndex).split("=")[1];
		}
		SessionProxy.cc = "cc=" + cc + "&";
	}

	return SessionProxy.cc;
}

SessionProxy.GetProxyID = function(){
	if(!SessionProxy.proxyID){
		var date = new Date();
		SessionProxy.proxyID = date.getTime();
	}
	return "proxyID=_" + SessionProxy.proxyID;
}

function SMPortal(){}

SMPortal.Init = function(partnerCode, defaultColour){

	defaultColour = (defaultColour!=undefined) && (defaultColour!=null) ? defaultColour : "#ffffff";

	SMPortal.PartnerCode = partnerCode;
	SMPortal.DefaultColour = defaultColour;

	var modules_array = SMPortal.ScanDocument();
	// create the proxy swf
	var proxyElement = document.createElement("div");

	var proxy = document.body.appendChild(proxyElement);
	proxy.id = "SM_proxy";
	proxy.style.width=1;
	proxy.style.height=1;
	SMPortal.InitDiv(proxy,"proxy.swf",SessionProxy.GetSessionInfo() + SessionProxy.GetProxyID());
	// initialize each module
	for(var i=0; i<modules_array.length; i++){
		SMPortal.InitModuleDiv(modules_array[i].div,modules_array[i].path);
	}

}

SMPortal.ScanDocument = function(){
	var smDivs_array = new Array();
	var divs_array = document.getElementsByTagName('div');
	for(var i=0; i<divs_array.length; i++){
		var div = divs_array[i];
		var divId_str = div.id;
		if(divId_str.lastIndexOf("SM_",0)==0){
			// strip of the SM_
			var path_str = divId_str.substr(3)
			var d = new Object();
			d.div = div;
			d.path = path_str;
			smDivs_array.push(d);
		}
	}
	return smDivs_array;
}

SMPortal.InitModuleDiv = function(div, module){

	// For dev only
	var port = window.location.port;

	var partner = SMPortal.PartnerCode;

	var server = "http://en.sitemakerlive.com" + ( port ? ':' + port : '' );
	var modulePath = "/partner/" + partner + "/modules/";

	var host = window.location.protocol + "//" + window.location.host;

	var params = SessionProxy.GetSessionInfo() + SessionProxy.GetProxyID() + "&module=" + module + "&host=" + host;
	var path = server + modulePath + "PortalContainer.swf";
	
	SMPortal.InitDiv(div, path, params, server);
}

SMPortal.InitDiv = function(div, path, params, server){

	div.innerHTML="";
	
	var bgcolour;

	// get background colour
	if(div.style.backgroundColor.indexOf("#")!=-1){
		bgcolour = div.style.backgroundColor;
	}else if(div.style.backgroundColor.indexOf("rgb(")!=-1){
		var rgbString = div.style.backgroundColor;
		rgbString = rgbString.substring(4,rgbString.length-1);
		var rgbArray = rgbString.split(",");
		bgcolour = "#" + convertToHex(rgbArray[0]) + convertToHex(rgbArray[1]) + convertToHex(rgbArray[2]);
	}else{
		bgcolour = SMPortal.DefaultColour;	
	}
	
	var width = "100%";
	var height = "100%";
	width = div.clientWidth; // Set actual value for firefox 1.0.7
	var id = "";

	// ideally, sniff browser and put in only one of object or embed
	var html_str = '<OBJECT CLASSID="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" CODEBASE="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,0,0" ID="' + id + '" WIDTH="' + width + '" HEIGHT="' + height + '">';
	html_str += '<PARAM NAME="MOVIE" VALUE="' + path + '">';
	html_str += '<PARAM NAME="QUALITY" VALUE="best">';
	html_str += '<PARAM NAME="MENU" VALUE="false">';
	html_str += '<PARAM NAME="SEAMLESSTABBING" VALUE="false">';
	if(server) html_str += '<PARAM NAME="BASE" VALUE="' + server + '">';	
	html_str += '<PARAM NAME="FLASHVARS" VALUE="' + params + '">';
	html_str += '<PARAM NAME="BGCOLOR" VALUE="' + bgcolour + '">';
	html_str += '<EMBED SRC="' + path + '" ';
	html_str += 'NAME="' + id + '" ' ;
	html_str += 'QUALITY="high" ';
	html_str += 'WIDTH="'+width+'" ';
	html_str += 'HEIGHT="'+height+'" ';
	html_str += 'MENU="false" ';
	if(server) html_str += 'BASE="' + server + '" ';	
	html_str += 'FLASHVARS="' + params + '" ';
	html_str += 'BGCOLOR="' + bgcolour + '" ';
	html_str += 'TYPE="application/x-shockwave-flash" ';
	html_str += 'PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash">';
	html_str += '</EMBED>';
	html_str += '</OBJECT>';
	
	div.innerHTML = html_str;
}

function convertToHex(num){
	if(num==0){
		return "00";
	}
	return "0123456789ABCDEF".charAt((num-num%16)/16)+"0123456789ABCDEF".charAt(num%16);
}

