/* change.js - Javascript file for Change 1.0. 

Description: Setup for Change 1.0. Displays and Handles Iframe.

Requirements: ajax.js;

Outline: 
1. Setup Variables
2. Swap Function
3. H1 Handle
4. Iframe
5. Change Buttons

*/

//Setup Variables
var textDiv = '';
var textLink = '';
var textBody = '';
var textArea = '';
var content = '';
var buttonP = '';
var sourceButton = '';

function setVariables(){//Sets Variable when Iframe is called
	textDiv = document.getElementById("textDiv");//The Div containing the entire change form
	textLink = document.getElementById("textLink");//The text show to users
	textBody = document.getElementById("textBody");//The IFRAME
	textArea = document.getElementById("textArea");//Source Textare
	//var content = '';//Iframe Edit Window
	content = (textBody.contentWindow || textBody.contentDocument);
	if (content.document) content = content.document;	
}

//Swap Function
function swapItems(array){//Takes an array and toggles them to show or not 					
	for(i=0;i<array.length;i++){
		var item = document.getElementById(array[i]);
 		item.className = (item.className == 'hidden' ? 'show' : 'hidden');	 			
	}
}

function toggleEs(array){
	array.each(function(item){
		toggleE(item);					
	});
}

function toggleE(item){//Takes an array and toggles them to show or not 			
 		item.className = (item.className == 'hidden' ? 'show' : 'hidden');	 			
}



//H1 Handling


function clickHeader(){
	$('h1_input').value=$('h1').innerHTML.replace(/&amp;/g,'&');
	toggleEs([$('h1'),$('h1_form')]);
	$('h1').style.display='none';
}


function toggleH1(){	
	var hOnes=['h1','h1_form'];
	swapItems(hOnes);
}

function submitH1(pId){//Submit the H1, update text, swap back to normal
var text= document.getElementById('h1_input').value.replace(/&/g,"%26");
new Ajax('header_write.php',{
method:'post',
data:'p='+ pId +'&text='+text,
update:$('h1')}).request();
toggleEs([$('h1'),$('h1_form')]);
$('h1').style.display='block';
}
		
function submitForm(){//Updates Database,Displays Text
	var bodyText = content.body.innerHTML.replace(/&/g,"%26");;
	var url = "page_write.php";
	var parameters = "p_id="+pID+"&text="+bodyText;
	var changeElement = document.getElementById('textLink');
	var type = 1;
	makeXMLRequest(type,url, parameters,changeElement);	
	var nText = textLink.innerHTML;
	content.body.innerHTML = nText;
	var swapArr = ['textDiv','textLink','textBody','change_a'];	
	swapItems(swapArr)	
}

function showWYS(){
  generate_wysiwyg('textLink');
}

        