
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 : "";
	window.open(pathToRoot+"image.php"+querystring,"image","width="+w+",height="+h);
}

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




/* 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.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){
				
				this.modules[i].style.display = "block";
				
				button.className = "active";
				
			}else{
				
				this.modules[i].style.display = "none";
				
				button.className = "inactive";
				
			}
			
		}
				
				
		//this.doTransition();
		
	}
	
	/* 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+"index.php";	// 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+"index.php?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);
	
	
}