/*###########################
##########SIMPLE EXCHANGE$###
josephmoore 06-##############
###########################*/

exchange ={
	dict : new Object(),
	checkEmpty: /^\s+$/,
	//create hash of elements and associated text
	setElementHash: function(node)
	{
		//if node is a textNode and node is not empty
		if(node.nodeType == 3 && !this.checkEmpty.test(node.nodeValue))
		{	
			//if key exists push text
			if(this.dict[node.parentNode.nodeName])
			{
				this.dict[node.parentNode.nodeName].push(node.nodeValue)
			}
			//else create key and put value with key
			else
			{
				this.dict[node.parentNode.nodeName] = new Array();
				this.dict[node.parentNode.nodeName].push(node.nodeValue)
			}	
		}
		//if children exist step though tree recursively
		//creating key val[] pairs
		if(node.childNodes != null)
		{
			for(var i = 0; i < node.childNodes.length; i++)
			{
				exchange.setElementHash(node.childNodes.item(i));
			}
		}
	},
	getHash: function()
	{
		return this.dict;
	},
	//returns keys as key array
	getHashKeys: function()
	{
		var keyArray = new Array();
		for(var i in this.dict)
		{
			keyArray.push(i);
		}
		return keyArray;
	},
	//get document in iframe
	getFrameDocument: function(id)
	{
		var myFrame = document.getElementById(id);
		var frameWin = (myFrame.contentWindow || myFrame.contentDocument);
		if(frameWin.document)
		{
			var frameWin = frameWin.document
			return frameWin;
		}
	},
	//create hash from iframe doc
	getFrameHash: function(frameId, frameNode)
	{
		var frameWindow = this.getFrameDocument(frameId);
		var myNode = frameWindow.getElementsByTagName(frameNode)[0];
		this.setElementHash(myNode);
	},
	//copy hash and return copy
	copyDict: function(dictionary)
	{
		var dictCopy = new Object();
		for(var key in dictionary)
		{
			dictCopy[key] = new Array();
			for(i = 0; i < dictionary[key].length; i++)
			{
				dictCopy[key].push(dictionary[key][i]);
			}
		}
		return dictCopy;	
	},
	//switch elements between dictionaries
	makeSwitch: function(dictionary1,dictionary2)
	{
	//for key in dictionary 1
		for(var key in dictionary1)
		{
			//if dictionary 2 has dictionary 1 type key
			if(dictionary2[key])
			{
				//for key type value in dict that is in dictionary 1
				//write values to dictionary2
				for(val = 0; val < (dictionary1[key].length < dictionary2[key].length ? dictionary1[key].length : dictionary2[key].length);val++)
				{
					dictionary2[key][val] = dictionary1[key][val];
				}
			
			}
		}
		return dictionary2;
	},
	//switch text content between two documents. The switching of the contents is 
	//based on the availablity of the associated elements in the other document
	//so simular structures swap contents
	switchContents: function(frameId1, frameNode1, frameId2, frameNode2)
	{
/*		netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead"); 
		netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserWrite");*/
		
		var frameWindow1 = this.getFrameDocument(frameId1);
		var myNode1 = frameWindow1.getElementsByTagName(frameNode1)[0];
		this.setElementHash(myNode1);
		var dict1a = this.dict;
		var dict1b = this.copyDict(dict1a);
		
		//clear hash. is there a better way to do this? setting to null doesn't work.kluuuudgey
		this.dict = null;
		this.dict = new Object();
		
		var frameWindow2 = this.getFrameDocument(frameId2);
		var myNode2 = frameWindow2.getElementsByTagName(frameNode2)[0];
		this.setElementHash(myNode2);
		var dict2a = this.dict;
		var dict2b = this.copyDict(dict2a)

		dict2a = this.makeSwitch(dict1a,dict2a);
		dict1b = this.makeSwitch(dict2b,dict1b);	
			
		for(var key in dict2a)
		{
			for(i = 0; i < (dict2a[key].length < frameWindow2.getElementsByTagName(key).length ? dict2a[key].length : frameWindow2.getElementsByTagName(key).length);i ++)
			{
			  //try to append associated text to node in other doc
				try
				{
					frameWindow2.getElementsByTagName(key)[i].firstChild.nodeValue = dict2a[key][i];
				}
				catch(e)
				{
				  //if other doc has no textNode attached to element create one
				  //before appending text
				  try
				  {
				    var emptyText = document.createTextNode(" ");
				    frameWindow2.getElementsByTagName(key)[i].appendChild(emptyText);
				    frameWindow2.getElementsByTagName(key)[i].firstChild.nodeValue = dict2a[key][i];
				  }
				  catch(e){}
				}
			}
		}

		
		for(var key in dict1b)
		{
			for(i = 0; i < (dict1b[key].length < frameWindow1.getElementsByTagName(key).length ? dict1b[key].length : frameWindow1.getElementsByTagName(key).length); i ++)
			{
				try
				{
					frameWindow1.getElementsByTagName(key)[i].firstChild.nodeValue = dict1b[key][i];
				}
				catch(e)
				{
				  try
				  {
				    var emptyText = document.createTextNode(" ");
				    frameWindow2.getElementsByTagName(key)[i].appendChild(emptyText);
				    frameWindow2.getElementsByTagName(key)[i].firstChild.nodeValue = dict2a[key][i];
				  }
				  catch(e){}
			  }
			}
		}
		//kludgey clear object again.
		this.dict = null;
		this.dict = new Object();
	}
}

////////////////////////////////////////////////////
//shortcut
function $(id){return document.getElementById(id);}

//swap viewable iframe and control classes
function choose()
{
	if(this.id == 'page1Control')
	{
	  $('page1Control').className = 'chosen';
	  $('page2Control').className = 'notChosen';
		$('page1').className = 'chosen';
		$('page2').className = 'notChosen';
	}
	if(this.id == 'page2Control')
	{
		$('page1Control').className = 'notChosen';
		$('page2Control').className = 'chosen';
		$('page1').className = 'notChosen';
		$('page2').className = 'chosen';
	}
}

function chooseParent()
{
  if(this.id == 'input1')
  {
	  $('page1Control').className = 'chosen';
	  $('page2Control').className = 'notChosen';
		$('page1').className = 'chosen';
		$('page2').className = 'notChosen';
  }
  if(this.id == 'input2')
  {
		$('page1Control').className = 'notChosen';
		$('page2Control').className = 'chosen';
		$('page1').className = 'notChosen';
		$('page2').className = 'chosen';
  }
}

function addEvent(elm, evType, fn, useCapture)
{
	if(elm.addEventListener)
	{
		elm.addEventListener(evType,fn,useCapture);
		return true;
	}
	else if (elm.attachElement)
	{
		var r = elm.attachEvent('on' + evType, fn);
	}
	else
	{
		elm['on' + evType] = fn;
	}
}
//transfer height of iframe document to iframe
function transferHeight()
{
/*	netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");*/
	this.height = window.frames[this.name].document.height + 10;
}
function changeLocation1()
{
  $('page1').src = "switch.php?url=" + $('input1').value;
}

function changeLocation2()
{
  $('page2').src = "switch.php?url=" + $('input2').value;
}

function input1Return(evt)
{
  if(evt.keyCode == 13)
  {
    changeLocation1();
  }
}

function input2Return(evt)
{
  if(evt.keyCode == 13)
  {
    changeLocation2();
  }
}

window.onload = function()
{
	control1 = $('page1Control');
	addEvent(control1,'click',choose,false);
		
	control2 = $('page2Control');
	addEvent(control2,'click',choose,false);
	
	Page1Input = $('input1');
	addEvent(Page1Input,'focus',chooseParent,false);
	addEvent(Page1Input,'keyup',input1Return,false);
	
	Page2Input = $('input2');
	addEvent(Page2Input,'focus',chooseParent,false);
	addEvent(Page2Input,'keyup',input2Return,false);
	
	Page1Submit = $('submit1');
	addEvent(Page1Submit,'click',changeLocation1,false);
	
	Page2Submit = $('submit2');
	addEvent(Page2Submit,'click',changeLocation2,false);
	
	frame1 = $('page1');
	frame2 = $('page2');
	addEvent(frame1,'load',transferHeight,false);
	addEvent(frame2,'load',transferHeight,false);
}

window.onresize = function()
{
	try
	{
		$('page1').height = window.frames['page1'].document.height + 10;
	}
	catch(e){}
	try
	{
		$('page2').height = window.frames['page2'].document.height + 10;
	}
	catch(e){}
}
	