var tReturnPage = "";
var iCount = 120;
var curFUN = "system.js";
var ErrorList = "";
var pageLoad = 0;


function globalError1() {
	//ShowNote();
	ErrorList = ErrorList + arguments[0] + " in " + curFUN + " at line " + arguments[2] + '\n'; 
	return true;
}

/*
		var tinyMCEImageList = new Array(
	["nice_sideways.JPG", "generalpics/nice_sideways.JPG"], 
	["sunfirewinter.JPG", "generalpics/sunfirewinter.JPG"]
	);
*/

//Above Is Post Information.

var activeEditors = 0;

HeadDefault = new Image;
HeadDefault.src = "graphics/gel-header-04.png";
HeadLoading = new Image(400,20);
HeadLoading.src = "graphics/load.gif";

function activateEditor(id) {
	curFUN = "activateEditor";
    //if (activeEditors == 0) {
	//activeEditors = 1;
    //LoadMCEopt();
	tinyMCE.execCommand('mceAddControl', false, id);
	//}
}
function LoadEditBar() {
	//deactivateEditors('elm1');
	//UpdateImgList();
	LoadMCEopt();
	activateEditor('elm1');
}
function deactivateEditors(id) {
	curFUN = "deactivateEditors";
    if (activeEditors == 1) {
        tinyMCE.execCommand('mceRemoveControl', false, id);
		activeEditors = 0;
		tinyMCE.idCounter = 0;
    }
}

function DisContent() {
	alert(document.getElementById('elm1').value);
}

function ReflectMCE(sPage){
	//tinyMCE.triggerSave('elm1');
	document.getElementById('elm1').value = tinyMCE.activeEditor.getContent();
	//tinyMCE.execCommand('mceRemoveControl', true, 'elm1');
	//tinyMCE.execCommand('mceRemoveControl', false, 'elm1');
	//alert(tinyMCE.getContent('elm1') + "5");
	makePOSTRequest("index.exe?content=save&pagename=" + sPage , "pagetext=" + escape(document.getElementById('elm1').value), 'contentbody');
}

function LoadMCEopt() {
	tinyMCE.init({
	// General options
		mode : "textareas",
		theme : "advanced",
		cleanup : false,
		plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount,advlist,autosave",

		// Theme options
		theme_advanced_buttons1 : "image,template,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
		theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
		theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,iespell,media,advhr,|,print",
		theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,pagebreak,restoredraft",
		theme_advanced_toolbar_location : "bottom",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true,

		// Example content CSS (should be your site CSS)
		content_css : "css/editor.css",

		// Drop lists for link/image/media/template dialogs
		template_external_list_url : "lists/template_list.js",
		external_link_list_url : "lists/link_list.js",
		//external_image_list_url : "lists/image_list.js",
		media_external_list_url : "lists/media_list.js",

		// Style formats
		style_formats : [
			{title : 'Bold text', inline : 'b'},
			{title : 'Red text', inline : 'span', styles : {color : '#ff0000'}},
			{title : 'Red header', block : 'h1', styles : {color : '#ff0000'}},
			{title : 'Example 1', inline : 'span', classes : 'example1'},
			{title : 'Example 2', inline : 'span', classes : 'example2'},
			{title : 'Table styles'},
			{title : 'Table row 1', selector : 'tr', classes : 'tablerow1'}
		],

		// Replace values for the template plugin
		template_replace_values : {
			username : "Some User",
			staffid : "991234"
		}

    });
}

function ShowLoad() {
    document.LinkMenu.src = HeadLoading.src;
}
function HideLoad() {
    document.LinkMenu.src = HeadDefault.src;
}

function openPage(sPage) {
	tReturnPage = sPage;
	iCount = 120;
		makePOSTRequest('index.exe','content=' + sPage,'contentbody');
}

function openEdit(sPage) {
    makePOSTRequest('index.exe','content=' + sPage,'contentedit');
  }
  
$(document).ready(function() {

//Set css in Firefox (Required to use the backgroundPosition js)
            $('#shutter1').css({backgroundPosition: '0px 0px'});
            $('#shutter2').css({backgroundPosition: '0px 0px'});
            $('#shutter3').css({backgroundPosition: '0px 0px'});
            $('#shutter4').css({backgroundPosition: '0px 0px'});

            //Animate the shutter
            $(".link").hover(function(){
                  $(this).parent().animate({backgroundPosition: '(200px 0px)'}, 500 );
                }, function() {
                  $(this).parent().animate({backgroundPosition: '(0px 0px)'}, 500 );
            });
         });

function HideNote(){
	document.getElementById("erroricon").className = "erroriconhide";
}

function ShowNote(){
	document.getElementById("erroricon").className = "erroriconshow";
}

function ShowError() {
	ErrorList = ErrorList + "I am still working out some BUGs!";
	window.alert(ErrorList);
	ErrorList = "";
	HideNote();
}

function DeletePhoto(sReturnPage) {
	if (confirm("Are you sure you want to DELETE this photo?")) { 
		openPage(sReturnPage + "&process=deletephoto&photo=" + escape(document.getElementById("currentpic").value));
	}
	
}
function UploadComplete(sReturnPage) {
	tReturnPage = sReturnPage;
	iCount = 0;
	setTimeout(UploadComplete2, 1000);
}
function UploadComplete2() {
	iCount++;
	if (iCount > 120) {
		alert("Photo Did Not Upload!");
		openPage(tReturnPage);
	} else {
		var myIFrame = document.getElementById("uploadframe");
	    var IFcontent = trim(myIFrame.contentWindow.document.body.innerHTML)
		if (IFcontent == "") {
			setTimeout(UploadComplete2, 1000);
		} else if (IFcontent == "1") {
			openPage(tReturnPage);
		} else {
			alert("'" + IFcontent + "': Server Has Reported An Error!");
			openPage(tReturnPage);
		}
	}
//	alert("'" + document.getElementById("uploadframe").innerHTML + "'");
}

// Removes leading whitespaces
function LTrim( value ) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}

// Removes ending whitespaces
function RTrim( value ) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}

// Removes leading and ending whitespaces
function trim( value ) {
	return LTrim(RTrim(value));	
}


window.onerror = globalError1;
