var rcktBrowserVer;
var BASE_URL = "http://cds.rocketon.com/flash/client/";
var manualHeight = false;
var throughMode = 0;
var forceDemo = 1; 
var instanceID = "ROCKETON";
var rckt_targeturl = window.location.href;

function getDemoMode()
{
	return(forceDemo);
}

function getInstanceID()
{
	return(instanceID);
}

function setDivPosition()
{
	var bName = navigator.appName;
	var position = "fixed";
	rcktBrowserVer = parseInt(navigator.appVersion);
	
	if(bName == "Microsoft Internet Explorer")
	{
		var isIE = true;
		if(rcktBrowserVer < 7)
		{
			position = "absolute";
		}
		else if(!(document.compatMode && document.compatMode != "BackCompat"))
		{
			position = "absolute";
		}
	}
	
	if(position == "absolute")
	{
		window.onscroll=handleScroll;
	}
	
	return(position);
}

function createForm(name, meth, action) 
{
	var newForm = null;

	if(     (document.createElement) 
		&&  (newForm = document.createElement('form')))
	{
		newForm.name = name;
		newForm.action = action;
		newForm.method = meth;
	}
	return newForm;
}

function createInputElem(type, name, value) 
{
	var inputElem = document.createElement('input');
	inputElem.type = type;
	inputElem.name = name;
	inputElem.value = value;
	return inputElem;
}

function getFormDOM(text) 
{
	var formDoc;
	if(typeof DOMParser != "undefined") 
	{
		var parser = new DOMParser();
		formDoc = parser.parseFromString(text, "text/xml");
	}
	else if(typeof ActiveXObject != "undefined") 
	{
		formDoc = new ActiveXObject("MSXML2.DOMDocument"); 
		formDoc.async = "false";
		formDoc.loadXML(text); 
	}  
	else 
	{
		var url = "data:text/xml;charset=utf-8," + encodeURIComponent(text);
		var request = new XMLHttpRequest( );
		request.open("GET", url, false);
		request.send(null);
		formDoc = request.responseXML;
	}
	return formDoc;
}

function submitForm(formText) 
{ 
	var formDoc = getFormDOM(formText);
	var formAttr = formDoc.getElementsByTagName('form');
	var formName = 'w00t';
	var formMethod = formAttr[0].getAttribute('method');
	var formAction = formAttr[0].getAttribute('action');
	var newForm = createForm(formName, formMethod, formAction);
	var formParams = formAttr[0].getElementsByTagName('input');

	for(var x = 0; x < formParams.length; x++)
	{
		var param = formParams[x];
		var input = createInputElem('hidden', 
									param.getAttribute('name'), 
									param.getAttribute('value'));
		newForm.appendChild(input);
	}

	document.body.appendChild(newForm);
	newForm.submit();    
}

function resizeDiv(pWidth, pHeight)
{
	var app = document.getElementById("rocketonAppDiv");
	
	if(app)
	{
		if(pWidth == -1)
		{						
			setHeight();
			if(app.style.position == "absolute")
			{
				   var top = (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;

				app.style.top = top + 'px';
			}
			app.style.width = '100%';
	
			if(app.style.height == '100%')
			{
				window.onresize = null;
			}
			else
			{
				window.onresize = handleResize;
			}

			setFocus();
		}
		else
		{
			if(app.style.position == "absolute")
			{
				var myHeight;

				if (self.innerHeight) // all except Explorer
				{
					myHeight = self.innerHeight;
				}
				else if (document.documentElement && document.documentElement.clientHeight)
					// Explorer 6 Strict Mode
				{
					myHeight = document.documentElement.clientHeight;
				}
				else if (document.body) // other Explorers
				{
					myHeight = document.body.clientHeight;
				}
				var top = (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;

				app.style.top = (myHeight - pHeight) + top + 'px';
				
				window.onresize = handleResize;
			}
			
			app.style.width = pWidth + 'px';
			app.style.height = pHeight + 'px';
		}
	}   
}

function handleResize()
{
	var app = document.getElementById("rocketonAppDiv");
	var top = (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
	
	if(app)
	{
		if(app.style.height == "100%")
		{
			app.style.top = top + 'px';
		}
		else
		{
			var myHeight;
	
			if(rcktBrowserVer < 7 && app.style.height.split('px')[0] > 100)
			{
				setHeight();
			}

			if (self.innerHeight) // all except Explorer
			{
				myHeight = self.innerHeight;
			}
			else if (document.documentElement && document.documentElement.clientHeight)
				// Explorer 6 Strict Mode
			{
				myHeight = document.documentElement.clientHeight;
			}
			else if (document.body) // other Explorers
			{
				myHeight = document.body.clientHeight;
			}
			
			var top = (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;

			app.style.top = (myHeight - parseInt(app.style.height)) + top + 'px';
		}
	}
}

function handleScroll()
{
	var app = document.getElementById("rocketonAppDiv");
	var top = (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
	
	if(app)
	{
		if(app.style.height == "100%")
		{
			app.style.top = top + 'px';
		}
		else
		{
			var myHeight;

			if (self.innerHeight) // all except Explorer
			{
				myHeight = self.innerHeight;
			}
			else if (document.documentElement && document.documentElement.clientHeight)
				// Explorer 6 Strict Mode
			{
				myHeight = document.documentElement.clientHeight;
			}
			else if (document.body) // other Explorers
			{
				myHeight = document.body.clientHeight;
			}
			
			var top = (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;

			app.style.top = (myHeight - parseInt(app.style.height)) + top + 'px';
		}
	}
}

function localRocketonAppClick()
{	
	if(readCookie('rocketonLaunchpad080725'))
	{
		window.location.href = "http://www.rocketon.com/a/launchpad.php";
	}
	else
	{
		localRocketonApp();
	}
}

  function detectMouseCoordinates(e)
  { 
    var posx,posy; 

    posx = 0; 
    posy = 0; 
    
    if(!e) var e=window.event; 
    
    if(e.pageX || e.pageY)
    { 
      posx=e.pageX; 
      posy=e.pageY; 
    } 

    else if(e.clientX || e.clientY)
    { 
      posx = e.clientX;// + document.documentElement.scrollLeft; 
      posy = e.clientY;// + document.documentElement.scrollTop; 
    } 

//    alert(posx + ":" + posy + ":" + e.clientX + ":" + e.clientY + ":" + document.documentElement.scrollLeft + ":" + document.documentElement.scrollTop);

    var embed = document["ROCKETON_EMBED"];
    if(embed)
    {
      embed.mouseClick(posx, posy);
//    alert('X=' + posx+' Y=' + posy); 
    }
  } 

  function handleKeyDown(e)
  { 
    var embed = document["ROCKETON_EMBED"];
    if(embed)
    {
      embed.keyDown(e.keyCode, e.shiftKey, e.ctrlKey, e.altKey, e.metaKey);
    }
  }
 
  function handleKeyUp(e)
  { 
    var embed = document["ROCKETON_EMBED"];
    if(embed)
    {
      embed.keyUp(e.keyCode, e.shiftKey, e.ctrlKey, e.altKey, e.metaKey);
    }
  }

function localRocketonApp(language, baseURL)
{
	if(baseURL != null)	
	{
		BASE_URL = baseURL;
	}

	var pageBody = document.getElementsByTagName("body")[0];
	if(!pageBody)
	{
		pageBody = document.getElementsByTagName("frameset")[0];
	}
			
	var appDiv = document.getElementById("rocketonAppDiv");
	if(!appDiv)
	{
		appDiv = document.createElement("div");
		appDiv.id = 'rocketonAppDiv';
		
		if(pageBody)
		{
			pageBody.appendChild(appDiv);
		}

		appDiv = document.getElementById("rocketonAppDiv");
	}
	
	if(appDiv)
	{
		appDiv.style.position=setDivPosition();
		appDiv.style.left='0px';
		appDiv.style.zIndex='1000000';
		appDiv.style.height='50px';
		appDiv.style.width='50px'; 
				
		if(appDiv.style.position == "absolute")
		{
			appDiv.style.top = '0px';
		}
		else
		{
			appDiv.style.bottom = '0px';
		}
				
		if(appDiv && pageBody)
		{
			var rocketonObject = document["ROCKETON_EMBED"];
			
			if(!rocketonObject)
			{
				var flashEmbed = "";
	
	
				if (AC_FL_RunContent == 0)
				{
					alert("This page requires AC_RunActiveContent.js.");
				} 
				else
				{
					var params = "";
					var referrerid = readCookie('referrerid');
					var partnerid = readCookie('partnerid');

                    if(document.attachEvent)
                    {
                      // document.attachEvent('onclick', detectMouseCoordinates);
                      document.attachEvent('onkeyup', handleKeyUp);
                      document.attachEvent('onkeydown', handleKeyDown);
                    }
                    if(document.addEventListener)
                    {
                      // document.addEventListener('click', detectMouseCoordinates, false);
                      document.addEventListener('keyup', handleKeyUp, false);
                      document.addEventListener('keydown', handleKeyDown, false);
                    }
				
					if(referrerid.length > 0)    
					{
						params += '&ReferrerID=' + referrerid; 
					}
					if(partnerid.length > 0)    
					{
						params += '&PartnerID=' + partnerid; 
					}
                    if(language != null)
                    {
						params += '&Language=' + language; 
                    }
				
//                  if(navigator.appName == "Microsoft Internet Explorer")
//                  {
//		                throughMode = 1;
//		            } 
                    
					flashEmbed = RCKT_FL_RunContent(
						'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
						'width', '100%',
						'height', '100%',
						'src', BASE_URL + 'ROCKETON_Loader',
						'quality', 'autohigh',
						'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
						'align', 'middle',
						'AutoStart', 'true',
						'loop', 'true',
						'scale', 'noscale',
						'wmode', 'transparent',
						'devicefont', 'false',
						'id', 'ROCKETON_EMBED',
						'bgcolor', '#ffffff',
						'name', 'ROCKETON_EMBED',
						'menu', 'true',
						'allowFullScreen', 'false',
						'allowScriptAccess','always',
						'movie', BASE_URL + 'ROCKETON_Loader',
						'FlashVars', 'BaseURL=' + BASE_URL + '&PostRegistrationURL=http://www.rocketon.com/a/launchpad.php&ForceActive=1&ThroughMode=' + throughMode + '&ForceDemo=' + forceDemo + '&CurrentURL=' + encodeURIComponent(rckt_targeturl) + params,
						'salign', ''
						); //end AC code
						appDiv.innerHTML = flashEmbed;
                  
						resizeDiv(50, 50);
				}
			}
			else
			{
				var rocketonObject = document["ROCKETON_EMBED"];
				rocketonObject.setMode(true);
			}
			
		}
	}
}

function readCookie(name) 
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i = 0; i < ca.length; i++) 
	{
		var c = ca[i];
		while(c.charAt(0)==' ')
		{
	        c = c.substring(1,c.length);
	    }
		if(c.indexOf(nameEQ) == 0)
		{
		    return c.substring(nameEQ.length,c.length); 
        }
	}
	return "";
}

function getURLParam(name)
{  
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
    var regexS = "[\\?&]"+name+"=([^&#]*)";  
    var regex = new RegExp(regexS);  
    var results = regex.exec(window.location.href);  
    
    if(results == null)    
    {
        return "";      
    }
    else
    {
        return results[1];
    }
}

function setFocus()
{
	var app = isIE?window.document["ROCKETON_EMBED"]:0;
	if(app)
	{
		window.focus();
		app.focus();
	}
}

function setHeight()
{
	var myHeight;
	var app = document.getElementById("rocketonAppDiv");
	
	if (self.innerHeight) // all except Explorer
	{
		myHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
	{
		myHeight = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		myHeight = document.body.clientHeight;
	}
	
	if(app)
	{	 
		if(rcktBrowserVer < 7)
		{
			app.style.height = myHeight + "px";
			manualHeight = true;
		}
		else
		{
			app.style.height = "100%";
		}
	}
}