
var Exchange = new Class({
  dict: {},
  checkEmpty:/^\s+$/,
	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++)
			{
				this.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 fWin = frameWin.document;
			return fWin;
		}
	  return null;
	},
	//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){
		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(); */
    delete this.dict;
		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.
		delete this.dict;
		this.dict={};
	}
});

//swap viewable iframe and control classes
//transfer height of iframe document to iframe


window.onload = function(){
	$('page1Control').addEvent('click',function(){
    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';
      }
  });
	$('page2Control').addEvent('click',function(){
    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';
      }
  });      
	$('input1').addEvents({
    'focus':function(){
       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';
        }
      },
      'keyup':function(evt){
        var event = new Event(evt);
        if(event.key == 'enter'){
          $('page1').src = "simple_proxy.php?url=" + $('input1').value; 
        }
      }
  });
	$('input2').addEvents({
    'focus':function(){
      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';
      }
    },
    'keyup':function(evt){
      var event = new Event(evt);
      if(event.key == 'enter'){
        $('page2').src = "simple_proxy.php?url=" + $('input2').value;
      }
    }
  });
	$('submit1').addEvent('click',function(){
    $('page1').src = "simple_proxy.php?url=" + $('input1').value;
  });
	$('submit2').addEvent('click',function(){
    $('page2').src = "simple_proxy.php?url=" + $('input2').value; 
  });
	
	$('page1').addEvent('load',function(){
         this.height = window.frames[this.name].document.height + 10;
  });
	$('page2').addEvent('load',function(){
         this.height = window.frames[this.name].document.height + 10;
  });
  var ex = new Exchange();
  $('submitExchange').addEvent('click',function(){
      ex.switchContents('page1','body','page2','body');
  });
}

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

