if(!directoryIndex){
	var directoryIndex = "index.php";
}

if(!httpRoot && pathToRoot){
	var httpRoot = pathToRoot;
}


function showImage(path,w,h,caption){
	var sw = screen.width-20;
	var sh = screen.height-100;
	if(sw < w){
		var w1 = sw;
		h = Math.round(h*sw/w);
		w = w1;
	}
	if(sh < h){
		var h1 = sh;
		w = Math.round(w*sh/h);
		h = h1;
	}
	var querystring = "?path="+path;
	querystring += (caption != null) ? "&caption="+caption : "";
	var url = (path.substr(0,4) == "http") ? path : pathToRoot+"image.php"+querystring;
	window.open(url,"image","width="+w+",height="+h);
}

var itemSlideshowIndex = 0;

function showItemSlideShow(itemID,itemType,index){

	itemSlideshowIndex = (index) ? index: 0;

	var dataObj = {itemid: itemID, item_type: itemType};
	var action = "getitemimagesjson";
	var docObj = window.document;

	if(JSONSocket != null){
		JSONSocket.makeCall("itemslideshow",action,dataObj,displayItemSlideShow);
	}

}

function displayItemSlideShow(dataObject){
	var slideshowObj = new ImageSlideShowObject(dataObject);
	if(itemSlideshowIndex){
		slideshowObj.imagePointer = itemSlideshowIndex;
	}
	slideshowObj.displayContent();

}


function deleteItem(path,item,name){
	if(window.confirm("Are you sure you want to delete "+unescape(name))){
		document.location="?action=deleteItem&path="+path+"&item="+item;
		return true;
	}
	return false;
}

function deleteItemMedia(item,media,name){
	if(window.confirm("Are you sure you want to delete "+unescape(name))){
		document.location="?action=deleteItemMedia&item="+item+"&media="+media;
		return true;
	}
	return false;
}

function deletePageMedia(path,media,name){
	if(window.confirm("Are you sure you want to delete "+unescape(name))){
		document.location="?action=deletePageMedia&path="+path+"&media="+media;
		return true;
	}
	return false;
}

function deletePageUser(path,username,name){
	if(window.confirm("Are you sure you want to delete "+unescape(name))){
		document.location="?action=deletePageUser&path="+path+"&username="+username;
		return true;
	}
	return false;
}

function changePageUserAccess(select,path,username){
	var access = select.options[select.selectedIndex].value;
	document.location="?action=changePageUserAccess&path="+path+"&username="+username+"&access="+access;
}

var loadedJavascript = new Object();

function loadJS(file,callback,method){

	// split files and check we need to load them

	file = urlDecode(file);

	if(file.indexOf(",") != -1){
		var files = file.split(",");
	}else{
		var files = new Array(file);
	}

	var filesToLoad = new Array();
	var k = 0;

	var encodedCallback = (callback) ? urlEncode(callback) : "";

	var urlStubLength = String(httpRoot+"javascript/js-load.php?file=&callback="+encodedCallback).length;

	for (var i in files) {
		if(files[i] != "" && files[i] != " " && typeof(loadedJavascript[files[i]]) == "undefined"){
			if(filesToLoad[k]){
				fileLength = urlEncode(filesToLoad[k]+","+files[i]).length;
				if(fileLength+urlStubLength > 2000){// we have more than a URLs worth - start a new set
					k++;
				}
			}
			filesToLoad[k] = (filesToLoad[k] == null) ? files[i] : filesToLoad[k]+","+files[i];
			loadedJavascript[files[i]] = true;
		}
	}


	if(filesToLoad.length){// we have files to load
		for(i=0;i<filesToLoad.length;i++){
			file = httpRoot+"javascript/js-load.php?file="+urlEncode(filesToLoad[i]);
			if(i==filesToLoad.length-1){// this is the last batch - add the callback
				file += "&callback="+encodedCallback;
			}
			jsIncludeOnce(file,method);
		}
	}else if(callback != null){// nothing to load - run callback if it exists
		eval(callback);
	}


}


/* MODULE METHODS */

function moduleSwitcher(){

	this.modules = new Array();// array to hold DOM objects of modules
	this.activeModule = 0;// current visible module
	this.attributes = new Object();// array of attributes
	this.divsAligned = false;
	this.transitionStep = 5;
	this.cssToggle = "visibility";
	this.moduleOpacityArr = new Array();

	this.setAttribute = function(a,v){
		this.attributes[a] = v;
	}

	this.setAttributes = function(){
		for(var i=0;i<arguments.length;i+=2){
			this.attributes[arguments[i]] = arguments[i+1];
		}
	}

	this.getAttribute = function(a){
		return this.attributes[a];
	}

	this.addModule = function(obj){

		this.modules.push(obj);

		/*var opacity = (this.modules.length == 1) ? 100 : 0;// make first module opaque, the rest transparent

		this.moduleOpacityArr.push(opacity);

		setOpacity(obj,opacity);
		*/

	}

	this.showModule = function(i){

		this.activeModule = i;

		for(i=0;i<this.modules.length;i++){

			button = document.getElementById("moduleSwitcherButton"+this.getAttribute("id")+"_"+i);

			if(i == this.activeModule){

				if(this.cssToggle == "display"){
					this.modules[i].style.display = "block";
				}else{
					this.modules[i].style.visibility = "visible";
				}

				if(button != null){
					$(button).addClass("active");
					$(button).removeClass("inactive");
				}

			}else{

				if(this.cssToggle == "display"){
					this.modules[i].style.display = "none";
				}else{
					this.modules[i].style.visibility = "hidden";
				}

				if(button != null){
					$(button).addClass("inactive");
					$(button).removeClass("active");
				}

			}

		}


		//this.doTransition();

	}

	this.resetModules = function(){
		this.showModule(0);
	}

	/* disslove transition //suspended due to problem with flash elements in Mac Firefox
	this.alignDivs = function(){

		if(!this.divsAligned){

			var divWidth,divHeight,divTop;

			if(this.modules.length > 1){

				if(this.modules[0].offsetWidth){
					divWidth = this.modules[0].offsetWidth;
					divHeight = this.modules[0].offsetWidth;
				}else if(this.modules[0].style.pixelWidth){
					divWidth = this.modules[0].style.pixelWidth;
					divHeight = this.modules[0].style.pixelHeight;
				}

				var pos = findPos(this.modules[0]);
				divTop = pos[1];

				for(var i=1;i<this.modules.length;i++){

					this.modules[i].style.width = divWidth+"px";
					this.modules[i].style.top = divTop+"px";
					divHeight = Math.max(divHeight,((this.modules[0].offsetHeight) ? this.modules[0].offsetHeight : this.modules[0].style.pixelHeight));

				}

				this.modules[0].style.height = divHeight+"px";

				this.divsAligned = true;

			}

		}

	}


	this.doTransition = function(){

		this.alignDivs();

		var transitionsLeft = false;

		for(var i=0;i<this.modules.length;i++){

			if(i == this.activeModule){

				if(this.moduleOpacityArr[i] < 100){

					this.moduleOpacityArr[i] += this.transitionStep;

					transitionsLeft = true;

					setOpacity(this.modules[i],this.moduleOpacityArr[i]);
				}

			}else{

				this.modules[i].style.zIndex = 200+i;

				if(this.moduleOpacityArr[i] > 0){

					this.moduleOpacityArr[i] -= this.transitionStep;

					transitionsLeft = true;

					setOpacity(this.modules[i],this.moduleOpacityArr[i]);
				}
			}

		}

		if(transitionsLeft){
			var thisObj = this;

			window.setTimeout(function(){ thisObj.doTransition() },50);

		}

	}
	*/


}


/* AJAX methods */

var ajaxContentObjects = new Array();

function ajaxShowContent(divId,ajaxContentIndex,url,returnFunction){

	var div = document.getElementById(divId);

	if(div != null){

		var html = ajaxContentObjects[ajaxContentIndex].response;
		div.innerHTML = html;
		ajaxContentObjects[ajaxContentIndex] = false;
		evalScripts(divId);

	}

	if(returnFunction != null){
		returnFunction();
	}
}

function ajaxLoadContent(divId,url,returnFunction){
	var ajaxContentIndex = ajaxContentObjects.length;
	ajaxContentObjects[ajaxContentIndex] = new sack();
	ajaxContentObjects[ajaxContentIndex].requestFile = url;	// Specifying which file to get
	ajaxContentObjects[ajaxContentIndex].onCompletion = function(){ ajaxShowContent(divId,ajaxContentIndex,url,returnFunction); };	// Specify function that will be executed after file has been found
	ajaxContentObjects[ajaxContentIndex].runAJAX();		// Execute AJAX function
}



var ajaxServerProcesses = new Array();

function ajaxServerStatus(ajaxServerIndex,returnFunction){

	if(returnFunction != null){
		returnFunction();
	}
}

function ajaxServerProcess(action,params,returnFunction){
	var ajaxServerIndex = ajaxServerProcesses.length;
	ajaxServerProcesses[ajaxServerIndex] = new sack();
	ajaxServerProcesses[ajaxServerIndex].method = "POST";
	ajaxServerProcesses[ajaxServerIndex].setVar("action",action);
	if(params != null){
		params = params.split("&");
		for(var i=0;i<params.length;i++){
			params[i] = params[i].split("=");
			ajaxServerProcesses[ajaxServerIndex].setVar(params[i][0],params[i][1]);
		}
	}
	ajaxServerProcesses[ajaxServerIndex].requestFile = pathToRoot+directoryIndex;	// Specifying which file to get
	ajaxServerProcesses[ajaxServerIndex].onCompletion = function(){ ajaxServerStatus(ajaxServerIndex,returnFunction); };	// Specify function that will be executed after file has been found
	ajaxServerProcesses[ajaxServerIndex].runAJAX();		// Execute AJAX function
}

var jsonServerProcesses = new Array();

function jsonServerProcess(action,params,callback){

	var url = pathToRoot+directoryIndex+"?action="+action;

	if(params != null){
		url += "&"+params;
	}

	if(callback != null){
		url += "&callback="+callback;
	}

	var jsonServerIndex = jsonServerProcesses.length;

	jsonServerProcesses[jsonServerIndex] = new JSONscriptRequest(url);
	jsonServerProcesses[jsonServerIndex].buildScriptTag();
	jsonServerProcesses[jsonServerIndex].addScriptTag(); ;
}


function evalScripts(divId){
	var div = document.getElementById(divId);
	var x = div.getElementsByTagName("script");

	for(var i=0;i<x.length;i++){
		if(window.execScript){// IE method
			window.execScript(x[i].text);
		}else{
			window.eval(x[i].text);
		}
	}
}

function startDashboardProcess(msg,type){
	var msgDiv = document.getElementById("DTDDashboardMessage");
	if(msgDiv != null){
		msgDiv.innerHTML = msg;
		msgDiv.style.display = "block";
		var contentDiv = document.getElementById("DTDDashboardContents");
		contentDiv.style.display = "none";
		if(type != null){
			msgDiv.className = type;
		}else{
			msgDiv.className = "";
		}

	}
}

function endDashboardProcess(){
	var msgDiv = document.getElementById("DTDDashboardMessage");
	if(msgDiv != null){
		msgDiv.innerHTML = "";
		msgDiv.style.display = "none";
		msgDiv.className = "";
		var contentDiv = document.getElementById("DTDDashboardContents");
		contentDiv.style.display = "block";
	}
}

function dashboardProcess(proc,msg){

	startDashboardProcess(msg,"loading");
	ajaxServerProcess(proc,null,endDashboardProcess);


}



function LightBox(){

	this.attributes = new Object();
	this.imageArr = new Object();
	this.opacityTable = new Object();
	this.mainImage;
	this.imageSet = [];
	this.pointer = 0;
	this.onClickFunction = null;

	this.setAttribute = function(a,v){
		this.attributes[a] = v;
	}

	this.setAttributes = function(){
		for(var i=0;i<arguments.length;i+=2){
			this.attributes[arguments[i]] = arguments[i+1];
		}
	}

	this.getAttribute = function(a){
		return this.attributes[a];
	}

	this.getContainer = function(){
		if(id = this.getAttribute("id")){
			return document.getElementById("lightbox"+id);
		}
		return false;
	}

	this.setMainImage = function(id){

		this.imageArr[id] = document.getElementById(id);

		this.mainImageID = id;

		this.opacityTable[id] = 100;

	}

	this.addImage = function(id,src,width){
		this.imageSet.push({id: id, src: src, width: width});
	}

	this.nextImage = function(){
		this.pointer = (this.pointer + 1 >= this.imageSet.length) ? 0 : this.pointer+1;
		this.loadImage(this.imageSet[this.pointer].id, this.imageSet[this.pointer].src, this.imageSet[this.pointer].width);
		this.mainImageID = this.imageSet[this.pointer].id;
	}

	this.previousImage = function(){
		this.pointer = (this.pointer - 1 < 0) ? this.imageSet.length-1 : this.pointer-1;
		this.loadImage(this.imageSet[this.pointer].id, this.imageSet[this.pointer].src, this.imageSet[this.pointer].width);
		this.mainImageID = this.imageSet[this.pointer].id;
	}

	this.loadImage = function(id,src,width){
		var container,i;

		if(this.imageArr[id] == null){

			if(container = this.getContainer()){

				var newDiv = document.createElement("div");
				newDiv.id = id;
				newDiv.className = "lightboxSlide";
				newDiv.style.width = "100%";//this.getAttribute("viewportWidth")+"px";
				//newDiv.style.height = this.getAttribute("viewportHeight")+"px";
				newDiv.style.position = "absolute";
				newDiv.style.textAlign = "center";
				//var pos = findPos(this.mainImage);
				newDiv.style.left = "0px";
				newDiv.style.top = "0px";
				setOpacity(newDiv,0);
				this.opacityTable[id] = 0;
				var thisObj = this;
				if(this.onClickFunction != null){
					newDiv.onclick = function(){thisObj.onClickFunction(id);};
				}

				var imageHTML = "<img src=\""+src+"\" class=\"inlineImage\" style=\"";
				imageHTML += (width) ? "width: "+width+";" : "";
				imageHTML += (this.getAttribute("borderWidth")) ? " border-width: "+this.getAttribute("borderWidth")+"px;" : "";
				imageHTML += "\" />";

				newDiv.innerHTML = imageHTML;
				container.appendChild(newDiv);

				this.imageArr[id] = newDiv;
			}

		}

		this.currentImage = id;

		this.doTransition();

	}

	this.unloadImage = function(){

		this.currentImage = this.mainImageID;

		this.doTransition();

	}


	this.doTransition = function(){

		var transitionInProgress = false;

		for(i in this.opacityTable){

			if(i == this.currentImage){

				if(this.opacityTable[i] < 100){

					this.opacityTable[i] += 10;

					transitionInProgress = true;

				}

			}else{

				if(this.opacityTable[i] > 0){

					this.opacityTable[i] -= 10;

					transitionInProgress = true;

				}

			}

		}

		if(transitionInProgress){

			for(i in this.opacityTable){

				setOpacity(i,this.opacityTable[i]);

			}



			var thisObj = this;

			window.setTimeout(function(){ thisObj.doTransition() },50);

		}

	}

}

