var FckEditorLoaded = false;


var TableName;
var PageName;
var DivName;

var NumberofHotSpots = 0;
var HotSpotArray = new Array();

/* 	This is an experiment .. we dont want the user the have to paste lots stuff in the header.
	So we are going to include all external .js files  with this call 
	Remember .. order can count .. css first!
 */

addCSS("/cms/css/style.css");
addJavascript("/cms/LoadContent.js");
addJavascript("/cms/GetDivContent.js");
addJavascript("/cms/SaveContent.js");
addJavascript("/cms/js/x_core.js");
addJavascript("/cms/fckeditor/fckeditor.js");
addJavascript("/cms/test.js");
GetTableName();
GetPageName();


/******************************************************* End Init **********************************************************************/

/* 	Load Page Function, searches for all Divs with cms_x (x being a number)
	It then calls GetDivContent which populates the divs from the sql server.
	So when this is finished, our cms data should be on the page!
 */

function LoadNewPage(){

	var URL;
	var DivArray = new Array();
	var n;
	
	
	var divs = document.getElementsByTagName('div');
	for (var i = 0; i < divs.length; i++) {
		if (divs[i].className=="main"){	
		//if (divs[i].getAttribute('id').indexOf('cms_') != -1){
			DivName = divs[i].getAttribute('id');
			URL = "cms/GetDivContent.asp?TableName=" + TableName + '&DivName=' + DivName + '&PageName=' + PageName;
			GetDivContent(URL,DivName);
		}
	}
	var theBody = document.getElementsByTagName('BODY')[0];
	var newdiv = document.createElement('div');
	newdiv.setAttribute('id', 'popupMask');
	theBody.appendChild(newdiv);	
}

function hide(){
	document.getElementById("popupMask").style.display = "none";
	document.getElementById("popup").style.display = "none";
	el = document.getElementById('popup');
	el.parentNode.removeChild(el) ;

}

function startEdit1(EditDivID){
//	alert(EditDivID);

	//document.getElementById("popupMask").style.display = "block";
	var theBody = document.getElementsByTagName('BODY')[0];

	var newdiv = document.createElement('div');
	newdiv.setAttribute('id', 'popup');
	theBody.appendChild(newdiv);
	DivName = 'popup';
	//test('/cms/LoadFCK.asp?TableName=' + TableName + '&PageName=' + PageName + '&DivName=' + EditDivID,'popup')
	testit('cms/t.asp?TableName=' + TableName + '&PageName=' + PageName + '&DivName=' + EditDivID,'popup')
}

function Do_It(PageName,TableName,DivName){
	
	var oEditor = FCKeditorAPI.GetInstance('FCKeditor1') ;
	var DivContent = oEditor.EditorDocument.body.innerHTML;
	FCK_sendRequest(PageName,TableName,DivName,DivContent);
	hide();


}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function replaceAll(oldStr,findStr,repStr) {
  var srchNdx = 0;  // srchNdx will keep track of where in the whole line
                    // of oldStr are we searching.
  var newStr = "";  // newStr will hold the altered version of oldStr.
  while (oldStr.indexOf(findStr,srchNdx) != -1)  
                    // As long as there are strings to replace, this loop
                    // will run. 
  {
    newStr += oldStr.substring(srchNdx,oldStr.indexOf(findStr,srchNdx));
                    // Put it all the unaltered text from one findStr to
                    // the next findStr into newStr.
    newStr += repStr;
                    // Instead of putting the old string, put in the
                    // new string instead. 
    srchNdx = (oldStr.indexOf(findStr,srchNdx) + findStr.length);
                    // Now jump to the next chunk of text till the next findStr.           
  }
  newStr += oldStr.substring(srchNdx,oldStr.length);
                    // Put whatever's left into newStr.             
  return newStr;
}

function addJavascript(jsname) {
var th = document.getElementsByTagName('head')[0];
var s = document.createElement('script');
s.setAttribute('type','text/javascript');
s.setAttribute('src',jsname);
th.appendChild(s);
} 

function addCSS(CSSname) {
var th = document.getElementsByTagName('head')[0];
var s = document.createElement('link');
s.setAttribute('type','text/css');
s.setAttribute('rel','stylesheet');
s.setAttribute('href',CSSname);
th.appendChild(s);
} 


function CreateHotSpotDivs(){
	/*
	This create the hot spot edit graphic for each div starting with cms_  
	or in laymans terms
	'Edit Mode On'
	*/

	var DivID;
	var n = 0;	
	var divs = document.getElementsByTagName('div');
	
	for (var i = 0; i < divs.length; i++) {
		//alert(divs[i].getAttribute('id'));
		if (divs[i].className=="main"){
		//if (divs[i].getAttribute('id').indexOf('cms_') != -1){
			n++;
			DivName = divs[i].getAttribute('id');
			var ele = xGetElementById(DivName);
			var divleft = xLeft(ele) + xWidth(ele);	
			var divtop = xTop(ele);	
			var newdiv = document.createElement('div');
			newdiv.setAttribute('id', 'hotspot' + n);
			newdiv.setAttribute('class','hotspot');
			newdiv.style.position = "absolute";
			newdiv.style.width = 16;
			newdiv.style.height = 16;
			newdiv.style.left = (divleft-8) + 'px';
			newdiv.style.top = (divtop-8) + 'px';
			newdiv.style.background = "#fff";
			newdiv.style.border = "1px solid #000";
			newdiv.innerHTML = "<a href=javascript:startEdit1('" + DivName + "');><img src='cms/images/edit.gif' width='16' height='16' border='0'/></a>"
			document.getElementById('workarea').appendChild(newdiv); 
			xZIndex(newdiv,999);
			HotSpotArray[n] = 'hotspot' + n;
			NumberofHotSpots = n;
		}
	}
}

function DestroyHotSpotDivs(){
	/*
	This destroys the hot spot edit graphic divs on the page 
	or in laymans terms
	'Edit Mode Off'
	*/
	for (var n = 1; n <= NumberofHotSpots; n++) {
		el = document.getElementById(HotSpotArray[n]);
		el.parentNode.removeChild(el) ;
	}
	NumberofHotSpots=0;
}

function GetPageName(){
	/*
	Get the name of the calling page ... we need to do this so we can get the right page content from the database.
	Get PageName
	*/
	var url = document.URL;
	PageName =(url.substring(url.lastIndexOf("\\")+1,url.length)) // for windows
	PageName =(url.substring(url.lastIndexOf("\/")+1,url.length)) // for *nix 
	var metaArray = document.getElementsByTagName ('META');
	for (var i=0; i<metaArray.length;i++){
		if (metaArray[i].name == "PAGENAME")
			PageName = metaArray[i].content.toLowerCase();
	}
}

function GetTableName(){
	/*
	Get the name of the website 
	Play with it ... ie www.bikeparts.com.au and change it to www-bikeparts-com-au , it will then give us the name of the TABLE in the CMS Database to get our data from.
	Get TableName
	*/

	TableName = document.domain;
	if (TableName.indexOf("new.") != -1)
	{
		TableName = TableName.substring(4,TableName.length).toLowerCase();
	}
	TableName = replaceAll(TableName,".","-");
}


