webdesk = function(options) {
	var dom = $(options.dom);		
	var self = this;	
	self.start = function() {
		self.LoadStyleSheet('style/default.css');
		self.getBrowser(function(){
				self.domObject = dom;
				self.isLoggedIn();
			}
		);
	};
	
	self.config = {}; //system config
	self.extConfig = new Array(); //extension configs
	self.domObject = ""; // the main super parent container/domObject used
	self.browserInfo = {
		'ismobile':false
	};
	s_ = {} //system extension namespace
	
	self.getBrowser = function(callBack){
		//look for standar browsers
		$.each($.browser, function(i, val) {
			if(val === true) {
				self.browserInfo.type = i+"_"+$.browser.version.substr(0,3);
			}
		});
		
		//look for iphone
		var agent=navigator.userAgent.toLowerCase();
		var is_iphone = (agent.indexOf('iphone')!=-1);
		if(is_iphone) { 
		//if(true) { 
			self.browserInfo.type =  "iphone";
			self.browserInfo.ismobile = true;
			self.LoadScript("javascript/browser_specifik/iphone.js", function(){
				_iphone();
				callBack();
			});
			return;
		}
		callBack();
		return;
	}
	
	self.isLoggedIn = function(){
		$.post("server/server.php"+"?="+Math.floor(Math.random()*1000000), {"action":"isLoggedIn"}, function(data){
			if(data.loggedIn) {
				self.loadDesk(data.user);
			} else {
				self.loginPage();
			}
		}, "json");
	};

	self.loginPage = function() {
		self.LoadTemp("templates/login.html", {}, function(data){
			self.domObject.html(data); //add login form
			
			//login if enter pushed during entering password
			$('input[name=password]').keypress(function(e){
				if(e.which == 13) {
					self.submitLoginForm();
				}
			})
			//login on press of "login" btn
			self.domObject.find('.submitBtn').click(function(){
				self.submitLoginForm();
			});
		});
	};
	
	self.submitLoginForm = function(){
		var username = $('input[name=username]').attr("value");
		var password = $('input[name=password]').attr("value");
		$.post("server/server.php"+"?="+Math.floor(Math.random()*1000000), {'action':'logIn','username':username,'password':password}, function(data){
			if(data.succes) {
				self.loadDesk(data.user);
			} else {
				$('.warning').html(data.error);
				$('.warning').show();
			}
		}, "json");
	}
	
	self.queryServer = function(options, callback, type, error) {
		type = (type == undefined) ? "text":type;
		var data = (options.data != undefined ) ? options.data:{};
		data.action = options.action;
		self.domObject.trigger('queryServerStart', data);
		$.ajax({
		   'type': "POST",
		   'dataType':type,
		   'url': "server/server.php"+"?="+Math.floor(Math.random()*1000000),
		   'data': data,
		   'success': function(returnData){
				self.domObject.trigger('queryServerSuccess', data);
				callback(returnData);
		   },
		   'error': function(XMLHttpRequest, textStatus, errorThrown) {
				self.domObject.trigger('queryServerFaileed', errorThrown);
				if(error != undefined) {
					error();
				}
		   }
		 });
	}
	
	self.loadConfig = function(callback) {
		$.post("javascript/config.json", {}, function(data){
			self.config = data;
			//do callback
			if(data == "[object Object]") {
				callback();
			}
		}, "json");

	};
	
	self.loadDesk = function(user) {
		self.empty();
		self.loadConfig(function(){
			self.config.user = user;
			self.loadExtensions();
		});
	}
	
	/*
	*FIXME
	*load and initialize after load
	*/
	
	self.loadExtensions = function(){
		$.post("server/server.php"+"?="+Math.floor(Math.random()*1000000), {'action':'getExtensions'}, function(data){
			self.extensionsAmount = 0;
			$.each(data.baseExtensions, function(i, v){
				self.extensionsAmount++;
			});
			self.loadExtensions.loadExtensionChain(data.baseExtensions, 'javascript/extensions/' ,0);
		}, "json");
	}
	self.extensionsAmount = 0;
	
	self.loadExtensions.loadExtensionChain = function(extensions, baseFolder, theMysteriusX) {
		var extName = "";
		var extVal = "";
		$.each(extensions, function(index, value) {
			if(value != undefined) {
				extName = index;
				extVal = index;
			}
		});
		delete extensions[extName];
		//loadExtension
		if(extName != "" || extVal != "") {
			$.post(baseFolder+extVal+"/"+extName+".js"+"?="+Math.floor(Math.random()*1000000), {}, function(data){//get js
				eval(data);
				$.post(baseFolder+extVal+"/"+"config.json"+"?="+Math.floor(Math.random()*1000000), {}, function(data){//get config
					//append config to initObject
					
					func = eval(data.initOn);
					func.config = data;
					//init function if defined
					if(data.initOnStart == 1) {
						self.loadExtensions.loadExtensionChain.inits.push(func);
					}
					self.extConfig[extVal] = data;
					theMysteriusX++;
					self.loadExtensions.loadExtensionChain(extensions, baseFolder, theMysteriusX);
				},"json");
			},"text");
			return true;
		} else {
			$.each(self.loadExtensions.loadExtensionChain.inits, function(i, v){
				v();
			});
			return false;
		}		
	};
	self.loadExtensions.loadExtensionChain.inits = [];
	
	self.includeJs = function(path, callback) {
		if(self.includeJs.cache[path] == undefined) {
			$.post(path, {}, function(data){
				eval(data);
				self.includeJs.cache[path] = data;
				callback();
			}, "text");
		} else {
			callback();
		}
	}
	self.includeJs.cache = new Array();
	
	/*
	*Templater
	*loads and caches templates in a buffer
	*/
	self.LoadTemp = function(path, replace, callback, forceNew, removeSelector) {
		if(removeSelector != null) {
			if($("#cacheBox").find(removeSelector).length <= 0) {
				//$("#cacheBox").find(removeSelector).remove();
			}
		}
		if(forceNew || self.LoadTemp.cachedFiles[path] == undefined) {
			$.post(path+"?="+Math.floor(Math.random()*1000000), {}, function(data){
					self.LoadTemp.cachedFiles[path] = data;
					//$("#cacheBox").append( self.LoadTemp.cachedFiles[path] );
					var replacedString = self.LoadTemp.replaceContentHolders(data, replace);
					callback(replacedString);
			}, "text");
		} else {
			var replacedString = self.LoadTemp.replaceContentHolders(self.LoadTemp.cachedFiles[path], replace);
			callback(replacedString);
		}
	}
	self.LoadTempFromCache = function(path, replace){
		if(self.LoadTemp.cachedFiles[path] != undefined) {
			return replacedString = self.LoadTemp.replaceContentHolders(self.LoadTemp.cachedFiles[path], replace);
		} else {
			return false;
		}
	}
	self.LoadTemp.replaceContentHolders = function(string, contentHolders) {
		var newString = string;
		var replaced = false;
		$.each(contentHolders, function(index, value) {
			replaced = true;
			if(value == undefined || value == null) {
				value = "";
			}
			newString = newString.replace('<$'+index+'$>',value);
			while(newString != newString.replace('<$'+index+'$>',value)) {
				newString = newString.replace('<$'+index+'$>',value);
			}
		});
		if(replaced) {
			string = newString;
		}
		return string;
	}
	self.LoadTemp.cachedFiles = new Array();
	
	/*
	*inlude script
	*loads the given script
	*/
	self.LoadScript = function(path, callback) {
		$.post(path+"?="+Math.floor(Math.random()*1000000), {}, function(data){
			eval(data);
			self.LoadScript.loadedScripts[path] = true;
			if(callback != undefined) {
				callback();
			}
		}, "text");
	}
	self.LoadScript.loadedScripts = new Array();
	
	self.Loadfile = function(path, callback, force) {
		if(self.Loadfile.loadedFiles[path] != undefined && force != true) {
			callback(self.Loadfile.loadedFiles[path]);
			return;
		}
		$.post(path+"?="+Math.floor(Math.random()*1000000), {}, function(data){
			self.Loadfile.loadedFiles[path] = data;
			callback(data);
		}, "text");
	}
	self.Loadfile.loadedFiles = new Array();
	
	/*
	*inlude lib
	*loads the given lib
	*/
	self.Loadlib = function(libName, callback) {
		if(self.config.libraries[libName] != undefined && self.Loadlib.loadedlibs[libName] != true) {
			$.post(self.config.libraries[libName].file+"?"+(Math.random() * 300), {}, function(data){
				eval(data);
				self.Loadlib.loadedlibs[libName] = true;
				if(callback != undefined) {
					callback();
				}
			}, "text");
			return;
		}
		callback();
		return;
	}
	self.Loadlib.loadedlibs = new Array();
	
	/*
	*inlude styleSheeth
	*loads the given stylesheet into the header
	*/
	self.LoadStyleSheet = function(path, force) {
		if(self.LoadStyleSheet.LoadedStyles[path] != true || force === true) {
			self.LoadStyleSheet.LoadedStyles[path] = true;
			$('head').append('<link rel="stylesheet" type="text/css" href="'+path+"?"+(Math.random() * 300)+'" />');
		}
	}
	self.LoadStyleSheet.LoadedStyles = new Array();
	
	/*
	*Window
	*moved to sepperate library
	*keps for compatabillety
	*/
	self.window = function(info) {s_.windows(info);};
	self.window.windowFocus = function(theWindow){s_.windows.windowFocus(theWindow);};
	self.window.init = function(){s_.windows.init();};
	self.window.setName = function(window, name){s_.windows.setName(window, name);};	
	/*
	*Gives dom item the highest z-index
	*/
	
	s_.bringToFront = function(object){
		object.css('z-index', s_.bringToFront.HZIndex);
		object.find(".fileSurface").css('z-index', s_.bringToFront.HZIndex);
		s_.bringToFront.HZIndex++;
	};
	s_.bringToFront.HZIndex = 10;
	
	/*
	*File surface, for windows etc
	*being reinstated to extension, look for "filesurface"
	*/
	
	self.filesurface = function(options, displayType) {
		s_.fileSurface(options, displayType);
	};
	self.filesurface.dropTo = null;
	self.filesurface.dropFrom = null;
	self.filesurface.MoveMe = null;
	self.filesurface.moveFile = function() {
		s_.fileSurface.moveFile();
	}
	self.fileName = function(url) {
		url = url.split("/");
		return url.pop();
	}
	self.imageResize = function(imgPath, name, height, width) {
		var path = "server/image/image.php/"+name+"?width="+width+"&height="+height+"&image="+self.config.domain.baseDomain+imgPath;
		return path;
	}
	
	/*
	*Decide what extension to use 
	*when clicking a file on a filesurface
	*/
	self.filesurface.openFileClickEvent = function(object) {
		s_.fileSurface.openFileClickEvent(object);
	};
	/*
	*Loadbox
	*/
	self.loadTemplate = function(path) {
		var responds = "";
		$.post(path, {}, function(data){
			self.domObject.trigger(path, data);
		}, "text");
	};
	
	self.loading = function(loading, msg) {
		if(loading && $(".loadBox").attr("class") == undefined) {
			insert = "";
			if(msg != undefined && msg != "") {
				var insert = "</p>"+msg+"<p>";
			}
			self.domObject.append('<table class="loadBox"><tr></td>Loading <span class="msg">('+insert+')</span></td></tr></table>');
		} else if(loading && msg != undefined && msg != "" && $(".loadBox").attr("class") != undefined) {
			$(".loadBox .msg").html('('+msg+')');
		} else if(!loading) {
			$(".loadBox").remove();
		}
	};
	
	/*
	*Desktop library
	*/
	
	self.saveFile = function(path, data, callback){
		$.post("server/server.php"+"?="+Math.floor(Math.random()*1000000), {action:"saveFile", file:path, content:data}, function(data){
			if(data.succes) {
				self.edited = false;
				callback(data);
			}
		}, "json");
	}
	

	/*
	*destroy desk
	*/
	
	self.empty = function() {
		self.domObject.html("");
	};
	
	//start up when done instantiating
	self.start();
}