// STORM Javascript Library
// (C) 2006 Granite Media + Communications
// www.granite-media.com
// Portions (C) 2006 Richard Gomer and used with permission
// Portions (C) 2006 Lee Doel and used with permission
// All rights reserved.
// This file and its contents are protected by international copyright law and may NOT be copied or modified in part or in its entirety without
//  prior written permission from Granite Media + Communications


// ========================== Rich Text Editor ============================
//window.onload = init;
    
// rte_content holds the text that the rich text editors should be prepopualted with
var rte_content = new Array;

	function init(id, div, widget)
	{
		if(typeof id != 'undefined')
		{
		//alert('Activating ' + id + '\n Content: ' + rte_content[id]);
			document.getElementById(id).contentWindow.document.designMode = 'on';
			
			
			var ua = navigator.userAgent.toLowerCase();
			isIE = ((ua.indexOf("msie") != -1) && (ua.indexOf("opera") == -1)); 
			if (!isIE) 
			{
				// Disable CSS-based rich-text in mozilla
				document.getElementById(id).contentWindow.document.execCommand("styleWithCSS", false, false);
				
				if(typeof rte_content[id] != 'undefined') // Prepopulate with content if content has been specified
				{
				document.getElementById(id).contentWindow.document.body.innerHTML = rte_content[id];
				}
			}
			else
			{
				if(typeof rte_content[id] != 'undefined') // Prepopulate with content if content has been specified
				{
				var loading = document.createElement('loadingwin');
				
				var buttondiv = 'buttons_' + widget;
				//alert(buttondiv);
				var buttondiv = document.getElementById(buttondiv);
				buttondiv.style.display = 'none';
				
				loading.style.position = 'absolute';
				loading.style.top = '10%';
				loading.style.left = '10%';
				loading.style.width = '80%';
				loading.style.height = '150px';
				loading.style.border = '2px groove #def';
				loading.style.background = '#fff';
				loading.style.fontSize = '20px';
				loading.style.textAlign = 'center';
				loading.style.zIndex = '2000';
				loading.innerHTML = '<br /><br /><br /><img src="loading.gif" alt="Loading Icon" /> Loading...<br /><br /><div id="load_wrapper" style="position: static; float: left; width: 80%; margin-left: 10%; height: 15px; border: 1px solid #000;"><div id="load_bar" style=" position: static; float: left; margin: 1px; height: 13px; background: #fda110; width: 0;;"></div></div>';
				
				document.getElementById(div).appendChild(loading);
				
				rtecontent = rte_content[id].replace("\n\n", '');
				rtecontent = rtecontent.replace("\n", '');
				rtecontent = rtecontent.replace(/'/g, "\\'");
				var code = id + ".document.body.innerHTML = '" + rtecontent + "';loading.style.display = 'none'; buttondiv.style.display = 'block';";
				//alert(code);
				setTimeout(function(){eval(code);}, 3000);
				setTimeout('startloadbar(3000, 100)', 0);
				}
			}
		}
	}
	
	function startloadbar(time, steps)
	{
	loadbar_time    = (time / steps);
	loadbar_percent = 100 / steps;
	loadbar_width   = 0;
	barcount = 0;
	
	//alert(loadbar_percent + '% Every ' + loadbar_time + 'ms');
	
	loadbar();
	}
	
	function loadbar()
	{
	loadbar_width = loadbar_width + loadbar_percent;
	
	//alert(loadbar_time);
	
	bar = document.getElementById('load_bar');
	bar.style.width = loadbar_width + '%';
	
	barcount++;
	
	//alert(loadbar_width + '%');
	
		if(loadbar_width < 100)
		{
		setTimeout('loadbar()', loadbar_time);
		}
		else
		{
		//alert(barcount);
		}
	}
	
    function RichtText(what, id)
    {
		document.getElementById(id).contentWindow.document.execCommand(what, false, null); 
        document.getElementById(id).contentWindow.focus();
    }
	
	function headers(hType, id)
	{
		document.getElementById(id).contentWindow.document.execCommand('formatblock', false, hType); 
        document.getElementById(id).contentWindow.focus();
	}
	
	function url(id)
	{
		var szURL = prompt("Enter a URL:", "http://");
		if ((szURL != null) && (szURL != ""))
		{
		document.getElementById(id).contentWindow.document.execCommand("CreateLink",false,szURL);	
		}
	}
	
	function urlUnlink(id)
	{
		document.getElementById(id).contentWindow.document.execCommand("UnLink",false,null)
	}
	
	function SourceCode(mode, id, buttons)
	{
	var htmtext;
	var ua = navigator.userAgent.toLowerCase();
	isIE = ((ua.indexOf("msie") != -1) && (ua.indexOf("opera") == -1)); 

	if (isIE) {
		if(mode) {
			//htmtext=id.document.body.innerHTML;
			//id.document.body.innerText=htmtext;
			document.getElementById(buttons).style.display = 'none';
			
			var code = 'htmtext=' + id + '.document.body.innerHTML;';
			eval(code);
			var code = id + '.document.body.innerText = htmtext;';
			eval(code);
			
			}
			else {
			//htmtext=mytext.document.body.innerText;
			//mytext.document.body.innerHTML=htmtext;
			document.getElementById(buttons).style.display = 'block';
			
			var code = 'htmtext=' + id + '.document.body.innerText;';
			eval(code);
			var code = id + '.document.body.innerHTML = htmtext;';
			eval(code);
			}
		}
		else{
			if(mode){
			//alert('on');
			document.getElementById(buttons).style.display = 'none';
			htmtext = document.createTextNode(document.getElementById(id).contentWindow.document.body.innerHTML);
			document.getElementById(id).contentWindow.document.body.innerHTML = "";
			document.getElementById(id).contentWindow.document.body.appendChild(htmtext);
			}
			else{
			//alert('off');
			 document.getElementById(buttons).style.display = 'block';
			htmtext = document.getElementById(id).contentWindow.document.body.ownerDocument.createRange();
			htmtext.selectNodeContents(document.getElementById(id).contentWindow.document.body);
			document.getElementById(id).contentWindow.document.body.innerHTML = htmtext.toString();
			}
		}
	}


	function transfer(src, dest)
	{
	var ua = navigator.userAgent.toLowerCase();
	isIE = ((ua.indexOf("msie") != -1) && (ua.indexOf("opera") == -1));     
	
	var html = document.getElementById(src).contentWindow.document.body.innerHTML;
	
	//alert(html);
	document.getElementById(dest).value = html;
	}

// ========================== PopUp Editor ============================ 

var content = new Array();
var style = new Array();

// Add content to a div (div must be provided as an object)
function setcontent(obj, html)
{
var newdiv = document.createElement('newdiv');
//alert(html);
obj.innerHTML = '';
newdiv.innerHTML = html;
obj.appendChild(newdiv);
}

function hide(div, widget)
{
var obj = document.getElementById(div);
var editbutton = '<img src="edit.gif" class="edit-button" onclick="show(\'' + div + '\', \'' + widget + '\')" alt="edit" />';

//obj.style.height = '40px';
//obj.style.width = '70px';
//obj.style.overflow = 'hidden';

	if(typeof style[div] != 'undefined')
	{
		obj.style.width = style[div]['width'];
		obj.style.height = style[div]['height'];
		obj.style.position = style[div]['position'];
		obj.style.top = style[div]['top'];
		obj.style.left = style[div]['left'];
		obj.style.border = style[div]['border'];
		obj.style.background = style[div]['background'];
		obj.style.padding = style[div]['padding'];
		obj.style.overflow = style[div]['overflow'];
		obj.style.margin = style[div]['margin'];
		obj.style.padding = style[div]['padding'];
		obj.style.color = style[div]['color'];
		obj.style.zIndex = style[div]['zindex'];
		
		var needpreview_widget = new Array();
		var needpreview_div = new Array();
		
		//obj.innerHTML = editbutton;
		setcontent(obj, editbutton);
		
		// Make a request for the widget to be updated with a preview
		getwidget(widget, div);
		
		getpreview();
	}
	else
	{
	content[div] = obj.innerHTML;
	
	//obj.innerHTML = editbutton;
	setcontent(obj, editbutton);
	
	// Make a request for the widget to be updated with a preview
	getwidget(widget, div);
	}
}

function show(div, widget, preshow)
{
var obj = document.getElementById(div);

style[div] = new Array;

style[div]['width'] = obj.style.width;
style[div]['height'] = obj.style.height;
style[div]['position'] = obj.style.position;
style[div]['top'] = obj.style.top;
style[div]['left'] = obj.style.left;
style[div]['border'] = obj.style.border;
style[div]['background'] = obj.style.background;
style[div]['padding'] = obj.style.padding;
style[div]['overflow'] = obj.style.overflow;
style[div]['margin'] = obj.style.margin;
style[div]['padding'] = obj.style.padding;
style[div]['color'] = obj.style.color;
style[div]['zindex'] = obj.style.zIndex;

var width = 600;
var height = 450;

var top = (document.body.offsetHeight / 2) - (200 / 2) - 50;

	if(top < 10)
	{
	top = 10;
	}

var left = (document.body.offsetWidth / 2) - (width / 2);

scrollTo(top,0)

obj.style.width = width + 'px';
//obj.style.height = height + 'px';
obj.style.position = 'absolute';
obj.style.top = top + 'px';
obj.style.left = left + 'px';
obj.style.border = '2px groove #def';
obj.style.background = '#fff';
obj.style.color = '#000';
obj.style.padding = '10px';
obj.style.overflow = 'auto';
obj.style.margin = '10px';
obj.style.zIndex = '1000';

var contenthead = '<div style="text-align: right"><button onclick="hide(\'' + div + '\', \'' + widget + '\')" style="float: right;">Close</button></div><h1>Edit</h1><br /><br />';

//obj.innerHTML = contenthead + content[div];
setcontent(obj, contenthead + content[div]);

// Workaround for the rich-text-area which is magically disabled by being ripped out of the page and stitched back in
var iframes = new Array;
iframes = obj.getElementsByTagName('iframe');

i = 0;
while(typeof iframes[i] != 'undefined')
{
var id = iframes[i].getAttribute('id');

//alert('Found an iframe with ID ' + id);

// Timeout fixes failure to initialise in Firefox
setTimeout(function(){init(id, div, widget);}, 250);

i++;
}
// End of RTE workaround

}


// ========================== AJAX Bits ============================

var xmlhttp;
var target;
var needpreview_widget = new Array();
var needpreview_div = new Array();
var num = 0;
var page;

function getwidget(widget, div)
{
// Add a widget to the list of widgets that need getting
needpreview_widget[needpreview_widget.length] = widget;
needpreview_div[needpreview_div.length] = div;
}

function getpreview() 
{
// Get the widget that needs loading
widget = needpreview_widget[num];

// ... and the div that it will be placed into (this variable is used by the XML object status change function)
target = needpreview_div[num];

// Now increase num because we have finished with it (and the next widget needs pointing to for the next iteration
num++;

var url = "getwidget.php?widget=" + widget + "&page=" + page;

//alert('Making request for ' + url);

	if (window.XMLHttpRequest)
	{
	xmlhttp=new XMLHttpRequest();
	xmlhttp.onreadystatechange=xmlhttpChange;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
	}
	// IE
	else if (window.ActiveXObject)
	{
	//alert('Creating XML HTTP Request');
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	
		if (xmlhttp)
		{
		//alert('Requesting content');
		xmlhttp.onreadystatechange=xmlhttpChange;
		xmlhttp.open("GET",url,true);
		xmlhttp.send();
		}
	}
}

function xmlhttpChange()
{
	// if xmlhttp shows "loaded"
	if (xmlhttp.readyState==4)
	{
	// if "OK"
	if (xmlhttp.status==200)
	{
	//alert('Got 200 OK response for div "' + target + '"');
	
	document.getElementById(target).innerHTML = document.getElementById(target).innerHTML + xmlhttp.responseText;
	}
	else
	{
	//alert('Request failed for div "' + target + '"');
	document.getElementById(target).innerHTML = document.getElementById(target).innerHTML + "This item could not be previewed";
	}
		// Once a preview has been completed, start a new one
		if(typeof needpreview_widget[num] != 'undefined')
		{
		getpreview();  
		} 
   }
}
