/**
 *  Nortel 2009 - hatko
 */

function dimensionObject() {
  this.width= 0;
  this.height = 0;
}

function positionObject() {
  this.left= 0;
  this.top = 0;
}

var chatWidth = 430;
var chatHeight = 170;

var firstTime = true;
var chatDocked = true;

var initialChatPosition = new positionObject();

var webDim = new dimensionObject();
var WAPosition = new positionObject();

window.onresize = onWindowResize;

var WEB_PLUGIN = "CWebPluginControl";

function findPosForTextchat(obj){
	var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
	
	this.X = obj.offsetLeft;
	this.Y = obj.offsetTop;
	while(obj.offsetParent){
		this.X=this.X+obj.offsetParent.offsetLeft;
		this.Y=this.Y+obj.offsetParent.offsetTop;
		if(obj==document.getElementsByTagName('body')[0]){
			break
		}
		else{
			obj=obj.offsetParent;
		}
	}
	if(isIE){
		this.X = this.X + 232;
		this.Y = this.Y + 199;
	}else{
		this.X = this.X + 231;
		this.Y = this.Y + 191;
	}
	
	return this
}

function findPosForSystemDialog(obj){
	var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
	
	this.X = obj.offsetLeft;
	this.Y = obj.offsetTop;
	while(obj.offsetParent){
		this.X=this.X+obj.offsetParent.offsetLeft;
		this.Y=this.Y+obj.offsetParent.offsetTop;
		if(obj==document.getElementsByTagName('body')[0]){
			break
		}
		else{
			obj=obj.offsetParent;
		}
	}
	if(isIE){
		this.X = this.X + 232;
		this.Y = this.Y + 224;
	}else{
		this.X = this.X + 231;
		this.Y = this.Y + 216;
	}
	
	return this
}

function getWebsiteDimensions()
{
	webDim.width = 0;
	webDim.height = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    webDim.width = window.innerWidth;
    webDim.height = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    webDim.width = document.documentElement.clientWidth;
    webDim.height = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    webDim.width = document.body.clientWidth;
    webDim.height = document.body.clientHeight;
  }
	return  webDim;
}

function onWindowResize(){
		if(chatDocked)
			xMoveTo('textchat', findPosForTextchat(document.getElementById('webAlive')).X, findPosForTextchat(document.getElementById('webAlive')).Y);
			
		xMoveTo('systemmessage', findPosForSystemDialog(document.getElementById('webAlive')).X, findPosForSystemDialog(document.getElementById('webAlive')).Y);
}


function checkSize()
{
    var viewportwidth;
    var viewportheight;
 
    // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
    if (typeof window.innerWidth != 'undefined')
    {
        viewportwidth = window.innerWidth;
        viewportheight = window.innerHeight;
    }
    // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

    else(typeof document.documentElement != 'undefined'
     && typeof document.documentElement.clientWidth !=
     'undefined' && document.documentElement.clientWidth != 0)
    {
        viewportwidth = document.documentElement.clientWidth;
        viewportheight = document.documentElement.clientHeight;
    }
    var avatarWidth = 800; //document.getElementById('avatar').style.width;
    var avatarHeight = 600; //document.getElementById('avatar').style.height;

    
    // Code for stage size and avatar app
    if (viewportheight < document.getElementById('avatar').offsetHeight){
		document.getElementById('avatar').style.marginTop = "0px"; 
	    document.getElementById('avatar').style.top = "0px"; 
    }else{
		document.getElementById('avatar').style.marginTop = -1* avatarHeight/2 + "px"; 
		document.getElementById('avatar').style.top = "50%"; 
	}
	
	if (viewportwidth < document.getElementById('avatar').offsetWidth){
		document.getElementById('avatar').style.marginLeft = "0px"; 
	    document.getElementById('avatar').style.left = "0px"; 
    }else{
		document.getElementById('avatar').style.marginLeft =  -1* avatarWidth/2 + "px"; 
		document.getElementById('avatar').style.left = "50%"; 
	}
}

function WAProcessMessage(message)
{
  //split message paramters
  var WAhashAction =message.split(/WAPARAM/g);



  if (WAhashAction[0] == "#SelectAvatar")
  {
     checkSize();
	 document.getElementById("avatar").style.display = 'block';
  }
  if (WAhashAction[0] == "#SelectTextchat")
  {
  	xMoveTo('textchat', -900, findPosForTextchat(document.getElementById('webAlive')).Y);
	document.getElementById("textchat").style.display = 'block';
  }
  if (WAhashAction[0] == "#SelectSystemMessage")
  {
  	xMoveTo('systemmessage', -800, findPosForSystemDialog(document.getElementById('webAlive')).Y);
	document.getElementById("systemmessage").style.display = 'block';
  } 
  if (WAhashAction[0] == "#TextchatReady")
  {
  	window.setTimeout("xa.size('textchat', 0, 0, 0, 2, 0);", 100);
	window.setTimeout("xMoveTo('textchat', findPosForTextchat(document.getElementById('webAlive')).X, findPosForTextchat(document.getElementById('webAlive')).Y);", 200);
  }
   if (WAhashAction[0] == "#SystemMessageReady")
  {
	window.setTimeout("xa.size('systemmessage', 0, 0, 0, 2, 0);", 100);
	window.setTimeout("xMoveTo('systemmessage', findPosForSystemDialog(document.getElementById('webAlive')).X, findPosForSystemDialog(document.getElementById('webAlive')).Y);", 200);
  }
  
  
  if (WAhashAction[0] == "#ShowTextchat")
	showTextchat();

  if (WAhashAction[0] == "#HideTextchat")
  	hideTextchat();

  if (WAhashAction[0] == "#DockTextchat")
	dockChat();

  if (WAhashAction[0] == "#UndockTextchat")
	undockChat();
 
  if (WAhashAction[0] == "#ResizeTextchat")
	resizeChat(WAhashAction[1], WAhashAction[2])
 
   if (WAhashAction[0] == "#DragTextchat")
	onDrag(WAhashAction[1], WAhashAction[2])
 
  
  
  
  if (WAhashAction[0] == "#ShowSystemMessage")
  	showSystemMessage();

  if (WAhashAction[0] == "#HideSystemMessage")
  	hideSystemMessage();
 

 

  if (WAhashAction[0] == "#Web.Alive")
     document.getElementById("avatar").style.display = 'none'; 
     
  if (WAhashAction[0] == "#CustomizeAndBuy")
  {
     var WACustomizeBuySystem = WAGetMappingURL(WAhashAction[1]); 
	 window.open(WACustomizeBuySystem,'CustomizeAndBuy');
  }   
  if (WAhashAction[0] == "#LoadContent")
  {
	//debug
	//var newMessage= "<p> Message recieved (WA): " + WAhashAction[1] +  " " + WAhashAction[2]+ " " + WAhashAction[3]+"</p>";
	//document.getElementById('WAMessage').innerHTML=newMessage;
  	var newIFrame = document.createElement('iframe');
  	if(newIFrame )
	{
		if (WAhashAction[3] == "unTriggered"  )
		{
			//path and size of default footer document
			newIFrame.src = 'http://' + '__SERVERDOMAINNAME__' + '/sample/default.html';
			newIFrame.height="240px";
		}
		else //triggered case
		{
			//path and size of footer document mapped in function call WAGetMappingURLFooter
			newIFrame.src = WAGetMappingURLFooter(WAhashAction[1]);
			newIFrame.height="240px";
		}
		//common format
		newIFrame.id = WAhashAction[2];
		newIFrame.border="0";
		newIFrame.scrolling="no";
		newIFrame.margin="0px";
		newIFrame.frameBorder="0";
		newIFrame.allowtransparency="true";
		// make frame visible only when loaded since it will otherwise show up white
		newIFrame.setAttribute('style', 'visibility:hidden;');
		newIFrame.setAttribute('onload', 'this.style.visibility="visible";')
		
		//reload iframe, currently hardcoded name
		var WACurrentContent = document.getElementById('sampleContent');
		if (WACurrentContent)
			WACurrentContent.parentNode.replaceChild(newIFrame, WACurrentContent);
	}
  } 
}

var xa = new xAnimation(); // for all demos except para


function undockChat(){
	chatDocked = false;
	xMoveTo('textchat', findPosForTextchat(document.getElementById('webAlive')).X, Number(findPosForTextchat(document.getElementById('webAlive')).Y) - Number(15));
}

function dockChat()
{
	chatDocked = true;
	chatWidth = 430;
	chatHeight = 170;	
	xMoveTo('textchat', findPosForTextchat(document.getElementById('webAlive')).X, findPosForTextchat(document.getElementById('webAlive')).Y);
	xa.size('textchat', 430, 170, 250, 2, 0);
}

function showTextchat(){
	xa.size('textchat', chatWidth , chatHeight , 0, 2, 0);
}

function hideTextchat(){
	xa.size('textchat', 0, 0, 0, 2, 0);
}

function resizeChat(dragObjectWidth, dragObjectHeight){
	var newWidth = dragObjectWidth;
	var newHeight = dragObjectHeight;

	xa.size('textchat', newWidth, newHeight, 0, 2, 0);
	chatWidth = newWidth;
	chatHeight = newHeight;
}

function onDrag(posObjectX, posObjectY)
{
	
	var websiteSize = getWebsiteDimensions();
	var left = Number(xLeft('textchat')) + Number(posObjectX);
	var top = Number(xTop('textchat')) + Number(posObjectY);
		
	if(left >= websiteSize.width - chatWidth){
		left = websiteSize.width - chatWidth ;
	}else if(left <= 0){
		left = 0;
	}
	if(top >= websiteSize.height - chatHeight){
		top = websiteSize.height - chatHeight;
	}else if(top <= 0){
		top = 0;
	}
  xMoveTo('textchat', left, top);

}

// JS Function for system message dialog

function showSystemMessage(){
	xa.size('systemmessage', 430, 145, 0, 2, 0);
}

function hideSystemMessage(){
	xa.size('systemmessage', 0, 0, 0, 2, 0);
}