/* 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 = '';
var h1Input = '';

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;	
	buttonP = document.getElementById("buttonP");//Change Buttons
	sourceButton = document.getElementById("sourceButton");//View Source Button
}

//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';
}



//IFRAME 	
function showIframe(){//Show Iframe, Hide Text, Start Counter
	setVariables();
	var swapArr = ['textDiv','textLink','textBody','change_a'];
	swapItems(swapArr);		
	content.designMode = "on";
	i=0;		
	var interval = setInterval("countLines()",100);
	content.body.focus();
} 

function countLines(){//Counts to continue to increase size of IFRAME
	var bodyHeight = (content.body.scrollHeight || content.body.offsetHeight);
	if(bodyHeight > 300){
		textBody.style.height = bodyHeight+'px';
		}
	if(i<1){//If first run, setup font and height
		iText = content.body.innerHTML;
		content.body.innerHTML = iText;	
		content.body.innerHTML = textLink.innerHTML;
		textBody.style.height = '300px';
		i++;
	}	
}

//Buttons Row   	
   		
function doBold(){//Adds/Deletes Bold 
	content.execCommand('bold', false, null);
	setTimeout(function() { textBody.contentWindow.focus(); }, 100);
}

function doItalic(){//Adds/Deletes Italics
	content.execCommand('italic', false, null);
	setTimeout(function() { textBody.contentWindow.focus(); }, 100);
}

function doUnderline(){//Adds/Deletes Underline
	content.execCommand('underline', false, null);
	setTimeout(function() { textBody.contentWindow.focus(); }, 100);
}

function doStrikethrough(){//Adds/Deletes Underline
	content.execCommand('strikethrough', false, null);
	setTimeout(function() { textBody.contentWindow.focus(); }, 100);
}

function doCreateLink(){//Shows Popup to add/delete Link
	var url = "createLink.html";
	window.open(url,'popup','width=300,height=100');	  
}       
   
function doUnlink(){//Unlinks Text
	content.execCommand('Unlink', false, null);
	setTimeout(function() { textBody.contentWindow.focus(); }, 100);
}
   
var viewSource={//Shows/Hides Source of Text
	on:function(){
	countSource();
	textArea.value = content.body.innerHTML;
	var swapArr = ['buttonP','textArea','sourceButton','textBody'];
	swapItems(swapArr);	
	},off:function(){ 	   	
	content.body.innerHTML =textArea.value;
	var swapArr = ['buttonP','textArea','sourceButton','textBody'];
	swapItems(swapArr);	
 	}
}
 	
function countSource(){//Gets right height of textarea 		
	if(content.body.offsetHeight > 300){
		textBody.style.height = (content.body.scrollHeight || content.body.offsetHeight)+'px';
	}
}

function picPop() {//Displays Image Popup
	window.open( "image_form.php", "myWindow", "status = 1, height = 300, width = 300, resizable = 1,scrollbars=1" );
}
	
function restoreForm(){//Hide IFrame,show Text
	var swapArr = ['textDiv','textLink','textBody','change_a'];
	swapItems(swapArr);
} 
			
function submitForm(){//Updates Database,Displays Text
	var bodyText = content.body.innerHTML;
	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)	
}
        