// NomadDS.js
// Gregory Dean 2009

// Communicate with swf and resize <div> on-the-fly
var gtargetX=10;
var gtargetY=10;

var gNomadWidth=41;
var gNomadHeight=70;
var gCanFloat=true;
var gBrowserDetected=false;

var ns = (navigator.appName.indexOf("Netscape") != -1);
var d = document;
var t;

function detectBrowser(){
	var browser=navigator.appName;
	var b_version=navigator.appVersion;
	var version=parseFloat(b_version);
	if(b_version.match('MSIE 8.0')){
		//alert('Browser: '+b_version.match('MSIE 8.0'));
		gCanFloat=true;
  	}else{
		gCanFloat=true;
  	}
	gBrowserDetected = true;
	return gCanFloat;
}

function dumpBrowserDetails(){
	var x = navigator;
	document.write("CodeName=" + x.appCodeName);
	document.write("<br />");
	document.write("MinorVersion=" + x.appMinorVersion);
	document.write("<br />");
	document.write("Name=" + x.appName);
	document.write("<br />");
	document.write("Version=" + x.appVersion);
	document.write("<br />");
	document.write("CookieEnabled=" + x.cookieEnabled);
	document.write("<br />");
	document.write("CPUClass=" + x.cpuClass);
	document.write("<br />");
	document.write("OnLine=" + x.onLine);
	document.write("<br />");
	document.write("Platform=" + x.platform);
	document.write("<br />");
	document.write("UA=" + x.userAgent);
	document.write("<br />");
	document.write("BrowserLanguage=" + x.browserLanguage);
	document.write("<br />");
	document.write("SystemLanguage=" + x.systemLanguage);
	document.write("<br />");
	document.write("UserLanguage=" + x.userLanguage);
}

function Nomad_GetWidth()
{
	var x = 0;
	if (self.innerHeight){
		x = self.innerWidth;
	}else if (document.documentElement && document.documentElement.clientHeight){
		x = document.documentElement.clientWidth;
	}else if (document.body){
		x = document.body.clientWidth;
	}
	return x;
}
 
function Nomad_GetHeight(){
	var y = 0;
	if (self.innerHeight){
		y = self.innerHeight;
	}else if (document.documentElement && document.documentElement.clientHeight){
		y = document.documentElement.clientHeight;
	}else if (document.body){
		y = document.body.clientHeight;
	}
	return y;
}

function Nomad_GetWindowWidth(){
	var winW = 640;
	if (parseInt(navigator.appVersion)>3) {
		if (navigator.appName=="Netscape") {
	  		winW = window.innerWidth-16;
	 	}
	 	if (navigator.appName.indexOf("Microsoft")!=-1) {
	  		winW = document.body.offsetWidth-20;
			//winW = document.body.offsetWidth;
	 	}
	}
	return(winW);
}

function Nomad_GetWindowHeight(){
	var winH = 480;
	if (parseInt(navigator.appVersion)>3) {
		if (navigator.appName=="Netscape") {
	  		winH = window.innerHeight-16;
	 	}
	 	if (navigator.appName.indexOf("Microsoft")!=-1) {
	  		winH = document.body.offsetHeight-20;
	 		//winH = document.body.offsetHeight;
		}
	}
	return(winH);
}

// --------------------------------------------------------------------
// Nomad_getFlashMovie()
// --------------------------------------------------------------------
function Nomad_getFlashMovie(movieName) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}

// --------------------------------------------------------------------
// Nomad_callToActionscript()
// --------------------------------------------------------------------
function callToActionscript(str){
	Nomad_getFlashMovie("NomadDS").sendToActionscript(str);
}

// --------------------------------------------------------------------
// Nomad_sendToJavaScript()
// --------------------------------------------------------------------
function sendToJavaScript(val){
	//alert(val);
}

// --------------------------------------------------------------------
// Nomad_Float()
// --------------------------------------------------------------------
function Nomad_Float(id, sx, sy){
	if(!gBrowserDetected)detectBrowser();
	sy = sy - 20;
	var el=d.getElementById?d.getElementById(id):d.all?d.all[id]:d.layers[id];
	var px = document.layers ? "" : "px";
	window[id + "_obj"] = el;
	if(d.layers)el.style=el;
		el.cx = el.sx = sx;el.cy = el.sy = sy;
	el.sP=function(x,y){
		if(y < 0){
			var ys = ((Nomad_GetHeight() - getNomadHeight())-40);
			this.style.left=x+"px";
			this.style.top=ys+"px";		
		}else{
			this.style.left=x+"px";
			this.style.top=y+"px";
		}	
	};
	el.floatIt=function(){
		clearTimeout(t);
		
		if(!gCanFloat) return el;
		
		var pX, pY;
	
		pX = (this.sx >= 0) ? 0 : ns ? innerWidth : 
		document.documentElement && document.documentElement.clientWidth ? 
		document.documentElement.clientWidth : document.body.clientWidth;
		pY = ns ? pageYOffset : document.documentElement && document.documentElement.scrollTop ? 
		document.documentElement.scrollTop : document.body.scrollTop;
		if(this.sy<-180) {
			pY += ns ? innerHeight : document.documentElement && document.documentElement.clientHeight ? 
			document.documentElement.clientHeight : document.body.clientHeight;
		}
		if(sy > 1){
			sy = ((Nomad_GetHeight() - getNomadHeight())-20);
		}
		this.cx += (pX + (Nomad_GetWidth() - getNomadWidth()) - this.cx)/8;
		this.cy += (pY + (Nomad_GetHeight() - getNomadHeight()) - this.cy - 20)/8;
		this.sP(this.cx, this.cy);
		// Nomad_Float("nomadDiv",document.body.clientWidth-getNomadWidth(),document.body.clientHeight-getNomadHeight()).floatIt(); //
		t = setTimeout(this.id + "_obj.floatIt()", 40);
	}
	return el;
}


// --------------------------------------------------------------------
// Nomad_SetFlashTransParent()
// --------------------------------------------------------------------
function Nomad_SetFlashTransParent(){
	var objarr,i;
	objarr=document.getElementsByTagName('object');
	for(i in objarr){
		var prm=document.createElement("param")
		var nm = document.createAttribute("name");
		nm.nodeValue = "wmode";
		prm.setAttributeNode(nm);    
		var vl = document.createAttribute("value");
		vl.nodeValue = "Transparent";
		prm.setAttributeNode(vl);
		try{
			objarr[i].appendChild(prm);
			var clone = objarr[i].cloneNode(true);
			objarr[i].parentNode.replaceChild(clone, objarr[i]); 	
		}catch(Error){
		} 
	}	
}

// --------------------------------------------------------------------
// Nomad_SetTransEmbed()
// --------------------------------------------------------------------
function Nomad_SetTransEmbed(){
	objarr=document.getElementsByTagName('param');
	for(i in objarr){
		if(objarr[i].name == "wmode"){
			objarr[i].value = "Transparent";
		}
	}
	var embedarr,i;
	embedarr=document.getElementsByTagName('embed');
	for(i in embedarr){ 
		//alert(embedarr[i].name);
		var wm = document.createAttribute("wmode");
		wm.nodeValue = "Transparent";
		try{
			embedarr[i].setAttributeNode(wm); 
			var clone = embedarr[i].cloneNode(true);
			embedarr[i].parentNode.replaceChild(clone, embedarr[i]);   
		}catch(Error){
		}    
	}

}

// --------------------------------------------------------------------
// alertFromSWF()
// --------------------------------------------------------------------
function alertFromSWF(message){
	alert(message);
}
		
// --------------------------------------------------------------------
// setFlashWidth()
// --------------------------------------------------------------------
function setFlashWidth(divid, newW){
	document.getElementById(divid).style.width = newW+"px";
	
}
	
// --------------------------------------------------------------------
// setFlashHeight()
// --------------------------------------------------------------------
function setFlashHeight(divid, newH){
	document.getElementById(divid).style.height = newH+"px";		
}

function getTargetX(){
	return gtargetX;	
}

function getTargetY(){
	return gtargetY;	
}

function getNomadWidth(){
	return gNomadWidth;	
}

function getNomadHeight(){
	return gNomadHeight;	
}


function setatt(val)
{
	tp = val;
}

// --------------------------------------------------------------------
// resizeStage()
// --------------------------------------------------------------------
function resizeStage(w, h){
	var position_pad = 10;
	
	//alert("resizeStage(w:"+w+", h:"+h+")");
	
	var x = 0;
	var y = 0;
	var scrW = Nomad_GetWindowWidth();
	var scrH = Nomad_GetWindowHeight();
	
	//alert("screenSize(w:"+scrW+", h:"+scrH+")");
	
	
	document.getElementById('nomad').style.bottom = "-"+(position_pad+h)+"px";
	document.getElementById('nomad').style.right = "-"+(position_pad+w)+"px";
	document.getElementById('nomad').style.width = w+"px";
	document.getElementById('nomad').style.height = h+"px";
	gNomadWidth = w;
	gNomadHeight = h;
	gtargetX = (position_pad+w)*-1;
	gtargetY = (position_pad+h)*-1;
}

// --------------------------------------------------------------------
// setFlashSize()
// --------------------------------------------------------------------
function setFlashSize(divid, newW, newH){
	alert("setFlashSize()");
	setFlashWidth(divid, newW);
	setFlashHeight(divid, newH);
}
	
// --------------------------------------------------------------------
// canResizeFlash()
// --------------------------------------------------------------------
function canResizeFlash(){

	var ua = navigator.userAgent.toLowerCase();
	var opera = ua.indexOf("opera");

	if( document.getElementById ){
		if(opera == -1){
			//alert("Can resize");
			return true;
		}else if(parseInt(ua.substr(opera+6, 1)) >= 7){
			//alert("Can resize");
			return true;
		}
	}
	//alert("Can not resize");
	return false;
}


// --------------------------------------------------------------------
// methods to call flash randomly and using round robin
// --------------------------------------------------------------------



function setFlashVars()
{

		checkCookie();			

		var swfno = getCookie('swfno');

		if(swfno < swfArray.length)
		{
		swfno = parseInt(swfno) + 1;


		}
		else
		{
		swfno = 1;
		}
		flashvars.swf = swfArray[swfno-1];
		if(method=="random")
		{
		var randomnumber=Math.floor(Math.random()*swfArray.length);
		flashvars.swf = swfArray[randomnumber];
		}

		setCookie('swfno',swfno,5); 

}

function checkCookie()
{
	swfno=getCookie('swfno');
	if (swfno==null && swfno=="")
	  {
	setCookie('swfno',1,5);
	  }
}


function getCookie(c_name)
{
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
		{
		c_start=c_start + c_name.length+1;
		c_end=document.cookie.indexOf(";",c_start);
		if (c_end==-1) c_end=document.cookie.length;
		return unescape(document.cookie.substring(c_start,c_end));
		}
	  }
	return "";
}


function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

// --------------------------------------------------------------------
// end methods
// --------------------------------------------------------------------