/*
 +-----------------------------------------------------------------------+
 | js/browse.class.js	                                                 |
 |                                                                       |
 | This file is part of the Phusion Webengine                            |
 | Copyright (C) 2008-2009, Roman Gruber                                 |
 | Licensed under the GNU GPL                                            |
 |                                                                       |
 | PURPOSE:                                                              |
 |   Browse-Class                                                        |
 |                                                                       |
 +-----------------------------------------------------------------------+
 | Author: Roman Gruber <roman@phusion.ch>                               |
 +-----------------------------------------------------------------------+
*/




function browse(path){
	this.retele = opener_id;
	this.path = path;
	this.files = document.getElementById("files");
	this.items = new Array();
	this.type = "name";
	this.old_type = "";
	this.view = "list";
	this.old_view = "list";
	this.tree_show = 0;
	this.choose = "file";
	this.feeder;
	registerObjectEventHandler(this.files, "click", this, "this.unselect_all_items()", true);
}

browse.prototype={
	getfiles:function(){
		if(this.choose == "file"){
			this.width = this.files.offsetWidth;
			this.height = this.files.offsetHeight;
			this.pos = getpos(this.files);
			this.left = this.pos[1];
			this.top = this.pos[0];
			this.loading = document.createElement("div");
			this.loading.style.backgroundColor = "#EEEEEE";
			this.loading.style.position = "absolute";
			this.loading.style.top = this.top;
			this.loading.style.left = this.left;
			this.loading.style.width = this.width;
			this.loading.style.height = this.height;
			this.loading.innerHTML = "<table style='width:100%; height:100%; vertical-align:middle; text-align:center;'><tr><td><img src='../img/icons/load.gif'><br><font>Loading...</font></td></tr></table>";
			this.loading.id = "loading";
			document.body.appendChild(this.loading);
			setopacity("loading", 8);

			if(this.feeder != ""){
				this.js = getxml(this.feeder+"?action=getfiles&path="+this.path);
			}else{
				alert("Feeder-Callback is not set.");
			}

			//alert(this.js);
			eval(this.js);

			this.create();
			this.change_view();
			this.resort();

			document.body.removeChild(this.loading);
		}
	},

	up:function(){
		this.path = this.self["pre"];
		this.getfiles();
	},

	enter_dir:function(c){
		this.path = this.items[c]["data"]["path"];
		this.getfiles();
	},

	choose_dir:function(path){
		alert(path);
		this.retele.value = path;
		window.close();
	},

	enter_item:function(c){
		if(this.self['webpath'] != ""){
			alert(this.self['webpath']+this.items[c]["data"]["name"]);
			this.retele.value = this.self['webpath']+this.items[c]["data"]["name"];
			window.close();
		}
	},

	select_item:function(c){
		this.unselect_all_items();
		addclass(this.items[c], "selected");

		document.getElementById("submit").value = "";
		if(this.self['webpath'] != ""){
			if(this.items[c]["data"]["type"] == "file"){
				document.getElementById("submit").value = this.self['webpath']+this.items[c]["data"]["name"];
			}
		}
	},

	unselect_all_items:function(){
		var i=0;
		for(var item in this.items){
			removeclass(this.items[i], "selected");
			i++;
		}
	},

	resort:function(type){
		if(type){
			this.old_type = this.type;
			this.type = type;
		}

		function bysize(a, b) {
			if((a)["data"]["size"] - (b)["data"]["size"] == 0){
				return (a)["data"]["size"]+" "+(a)["data"]["name"] > (b)["data"]["size"]+" "+(b)["data"]["name"];
			}else{
				return (a)["data"]["size"] - (b)["data"]["size"]
			}
		}
		
		function byname(a, b) {
			return (a)["data"]["type"]+" "+(a)["data"]["name"] > (b)["data"]["type"]+" "+(b)["data"]["name"];
		}

		function bydate(a, b) {
			return (a)["data"]["date"] > (b)["data"]["date"];
		}

		function bytype(a, b) {
			return (a)["data"]["type"]+" "+(a)["data"]["name"].match(/\.[A-Z,a-z,0-9]*$/)+" "+(a)["data"]["name"] > (b)["data"]["type"]+" "+(b)["data"]["name"].match(/\.[A-Z,a-z,0-9]*$/)+" "+(a)["data"]["name"];
		}

		if(this.type == "name"){
			this.items.sort(byname);
		}else if(this.type == "size"){
			this.items.sort(bysize);
		}else if(this.type == "date"){
			this.items.sort(bydate);
		}else if(this.type == "type"){
			this.items.sort(bytype);
		}

		if(this.old_type == this.type){
			this.items.reverse();
			if(this.type.match(/^r/)){
				this.type = this.type.replace(/^r/, "");
			}else{
				this.type = "r"+this.type;
			}
		}

		if(document.getElementById("sort_"+this.old_type.replace(/^r/, ""))){
			document.getElementById("sort_"+this.old_type.replace(/^r/, "")).src = "../img/icons/nothing.gif";
		}

		if(document.getElementById("sort_"+this.type.replace(/^r/, ""))){
			if(this.type.match(/^r/)){
				document.getElementById("sort_"+this.type.replace(/^r/, "")).src = "../img/icons/arrow_down.gif";
			}else{
				document.getElementById("sort_"+this.type).src = "../img/icons/arrow_up.gif";
			}
		}
		this.list();
	},

	list:function(){
		this.files.innerHTML = "";
		var i=0;
		
		for(var item in this.items){
			removeObjectEventHandler(this.items[i], "click", this.items[item]["callee"]["click"], true);
			removeObjectEventHandler(this.items[i], "dblclick", this.items[item]["callee"]["dblclick"], true);
			if(this.items[item]["data"]["type"] == "dir"){
				this.items[item]["callee"]["dblclick"] = registerObjectEventHandler(this.items[i], "dblclick", this, "this.enter_dir('"+i+"')", true);
				this.items[item]["callee"]["click"] = registerObjectEventHandler(this.items[i], "click", this, "this.select_item('"+i+"')", true);
			}else if(this.items[item]["data"]["type"] == "file"){
				this.items[item]["callee"]["dblclick"] = registerObjectEventHandler(this.items[i], "dblclick", this, "this.enter_item('"+i+"')", true);
				this.items[item]["callee"]["click"] = registerObjectEventHandler(this.items[i], "click", this, "this.select_item('"+i+"')", true);
			}else if(this.items[item]["data"]["type"] == "link"){
				this.items[item]["callee"]["click"] = registerObjectEventHandler(this.items[i], "click", this, "this.select_item('"+i+"')", true);
			}
			this.files.appendChild(this.items[i]);
			i++;
		}
	},

	create:function(){
		this.files.innerHTML = "";
		this.items = new Array();
		var i=0;
		for(var item in this.data){
			this.items[i] = document.createElement("div");
			this.items[i]["data"] = this.data[item];
			this.items[i].className = 'item';
			this.items[item]["callee"] = new Array();
			i++;
		}
	},

	change_view:function(view){
		this.old_view = this.view;
		if(view){
			this.view = view;
		}

		var i=0;
		var o=0;
		for(var item in this.items){
			removeclass(this.items[i], this.old_view);
			if(this.view == "list"){
				this.items[i].title = this.items[i]["data"]["name"];
				this.items[i].innerHTML = "<table><tr><td><img src='../img/icons/16x16/"+this.items[item]["data"]["icon"]+"'></td><td>&nbsp;<font>"+this.items[item]["data"]["name"]+"</font></td></tr></table>";
				addclass(this.items[i], "list");
			}else if(this.view == "detail"){
				this.items[i].title = "Dateiname: "+this.items[i]["data"]["name"]+", Groesse: "+this.items[i]["data"]["hrsize"];
				this.items[i].innerHTML = "<table><tr><td class'font_name'><img src='../img/icons/16x16/"+this.items[item]["data"]["icon"]+"'>&nbsp;<font>"+this.items[item]["data"]["name"]+"</font></td><td class='font_size'><font>"+this.items[item]["data"]["hrsize"]+"</font></td><td class='font_type'><font>"+this.items[item]["data"]["type"]+"</font></td><td class='font_date'><font>"+this.items[item]["data"]["hrdate"]+"</font></td></tr></table>";
				addclass(this.items[i], "detail");
			}else if(this.view == "thumbs"){
				this.items[i].title = this.items[i]["data"]["name"];
				if(this.items[item]["data"]["name"].match(/(\.jpg)|(\.gif)|(\.png)$/i)){
					this.items[i].innerHTML = "<table><tr><td><img src='open_file.php?path="+this.items[item]["data"]["path"]+"&PHPSESSID="+phpsessid+"&session_user="+session_user+"'></td></tr></table><table><tr><td><font>"+this.items[item]["data"]["name"]+"</font></td></tr></table>";
				}else{
					this.items[i].innerHTML = "<table><tr><td><img src='../img/icons/48x48/"+this.items[item]["data"]["icon"]+"'></td></tr><tr><td><font>"+this.items[item]["data"]["name"]+"</font></td></tr></table>";
				}
				addclass(this.items[i], "thumbs");
			}else if(this.view == "tiles"){
				this.items[i].title = this.items[i]["data"]["name"];
				this.items[i].innerHTML = "<table><tr><td style='width:48;'><img src='../img/icons/48x48/"+this.items[item]["data"]["icon"]+"'></td><td><font>"+this.items[item]["data"]["name"]+"</font><br><font class='font_size'>"+this.items[item]["data"]["hrsize"]+"</font></td></tr></table>";
				addclass(this.items[i], "tiles");
			}
			i++;
		}

		if(document.getElementById("view_"+this.old_view)){
			//removeclass(document.getElementById("view_"+this.old_view), "selected_view");
			document.getElementById("view_"+this.old_view).src = "../img/icons/nothing.gif";
		}

		if(document.getElementById("view_"+this.view)){
			//addclass(document.getElementById("view_"+this.view), "selected_view");
			document.getElementById("view_"+this.view).src = "../img/icons/dot_active.gif";
		}
	},

	tree:function(tree){
		this.treediv = document.getElementById('tree');
		var width = 200;

		if(this.tree_show == 0){
			window.outerWidth += width;
			window.moveBy(-200, 0);
			this.treediv.style.width = width+"px";
			this.tree_show = 1;

			ajaxFunction("tree.php?folder="+this.path, "tree");
		}else if(this.tree_show == 1){
			window.outerWidth -= width;
			window.moveBy(200, 0);
			this.treediv.style.width = "0px";
			this.tree_show = 0;
		}
	},

	handleEvent:function(event, func){
		eval(func);
	}

}

