/*
	EXAMPLE USAGE::
	Initialise:
	windowManager = new AppWindowManagerClass();
	windowManager.name = 'windowManager';
	windowManager.init();
	
	New Window:
	appOptionsWindow = windowManager.newWindow();
	appOptionsWindow.setHeader("Einstellungen");
	appOptionsWindow.setSubHeader("Einstellungen f&uuml;r die Fotoalben.");
	appOptionsWindow.show();	
	
	New SearchWindow:
	searchWindow = windowManager.newSearchWindow('searchString');
	
	Close Winodw:
	appOptionsWindow.close();
*/

function AppWindowManagerClass() {
	this.init = function() {
		this.windows = new Array();
		currentZIndex = 1;
		this.windowPositionRelativeToMousePosition = new Array();
	}
	
	this.newSearchWindow = function(searchString) {
		currentZIndex++;
		
		var windowsCounter = this.windows.length;
		this.windows[windowsCounter] = new Array();
		this.windows[windowsCounter]["id"] = 'AppWindow' + windowsCounter;
		this.windows[windowsCounter]["zIndex"] = currentZIndex;
		this.windows[windowsCounter]["window"] = new SearchWindowClass();			
		this.windows[windowsCounter]["window"].searchString = searchString;		
		this.windows[windowsCounter]["window"].id = this.windows[windowsCounter]["id"];
		this.windows[windowsCounter]["window"].name = this.name + '.windows[' + windowsCounter + ']["window"]';
		this.windows[windowsCounter]["window"].eventHandler = this;
		this.windows[windowsCounter]["window"].init();
		
		return(this.windows[windowsCounter]["window"]);
	}
	
	this.newCustomSizeAppWindow = function(width, height, displayTabBar) {	
		currentZIndex++;
		var windowsCounter = this.windows.length;
		this.windows[windowsCounter] = new Array();
		this.windows[windowsCounter]["id"] = 'AppWindow' + windowsCounter;
		this.windows[windowsCounter]["zIndex"] = currentZIndex;
		this.windows[windowsCounter]["window"] = new AppWindowClass();
		this.windows[windowsCounter]["window"].id = this.windows[windowsCounter]["id"];
		this.windows[windowsCounter]["window"].name = this.name + '.windows[' + windowsCounter + ']["window"]';
		this.windows[windowsCounter]["window"].eventHandler = this;
		this.windows[windowsCounter]["window"].width = width;
		this.windows[windowsCounter]["window"].height = height;
		this.windows[windowsCounter]["window"].displayTabBar = displayTabBar;
		this.windows[windowsCounter]["window"].init();
		
		return(this.windows[windowsCounter]["window"]);		
	}
	
	this.newUploadSinglePhotoAppWindow = function() {	
		return(this.newCustomSizeAppWindow(670, 400, false));		
	}
	
	this.newPhotoGalleryWindow = function() {
		currentZIndex++;
		
		var windowsCounter = this.windows.length;
		this.windows[windowsCounter] = new Array();
		this.windows[windowsCounter]["id"] = 'AppWindow' + windowsCounter;
		this.windows[windowsCounter]["zIndex"] = currentZIndex;
		this.windows[windowsCounter]["window"] = new PhotoGalleryWindowClass();				
		this.windows[windowsCounter]["window"].id = this.windows[windowsCounter]["id"];
		this.windows[windowsCounter]["window"].name = this.name + '.windows[' + windowsCounter + ']["window"]';
		this.windows[windowsCounter]["window"].eventHandler = this;
		this.windows[windowsCounter]["window"].init();
		
		return(this.windows[windowsCounter]["window"]);
	}	

	this.newDashAppWindow = function(appId) {
		currentZIndex++;
		
		var windowsCounter = this.windows.length;
		this.windows[windowsCounter] = new Array();
		this.windows[windowsCounter]["id"] = 'AppWindow' + windowsCounter;
		this.windows[windowsCounter]["zIndex"] = currentZIndex;
		this.windows[windowsCounter]["window"] = new DashAppWindowClass(appId);				
		this.windows[windowsCounter]["window"].id = this.windows[windowsCounter]["id"];
		this.windows[windowsCounter]["window"].name = this.name + '.windows[' + windowsCounter + ']["window"]';
		this.windows[windowsCounter]["window"].eventHandler = this;
		this.windows[windowsCounter]["window"].init();
		
		return(this.windows[windowsCounter]["window"]);
	}	
	
	this.newWindow = function(type) {
		currentZIndex++;
		var windowsCounter = this.windows.length;
		this.windows[windowsCounter] = new Array();
		this.windows[windowsCounter]["id"] = 'AppWindow' + windowsCounter;
		this.windows[windowsCounter]["zIndex"] = currentZIndex;
		this.windows[windowsCounter]["window"] = new AppWindowClass();
		this.windows[windowsCounter]["window"].id = this.windows[windowsCounter]["id"];
		this.windows[windowsCounter]["window"].name = this.name + '.windows[' + windowsCounter + ']["window"]';
		this.windows[windowsCounter]["window"].eventHandler = this;
		this.windows[windowsCounter]["window"].init(type);
		
		return(this.windows[windowsCounter]["window"]);
	}
	
	this.getCurrentZIndex = function() {
		return(currentZIndex);
	}
	
	this.increaseZIndex = function() {
		currentZIndex++;
		return(currentZIndex);
	}	
}

function AppWindowClass() {
	this.init = function(type) {
		if(type) {
			this.type = type;
		}
		
		if(!this.width && !this.height) {
			if(this.type=='narrow') {
				this.width = 733;
				this.height = 717;
			} else {
				this.width = 915;
				this.height = 730;				
			}
		}

		this.buildContainer();
		this.box = document.getElementById(this.id + 'windowContainer');		
		
		scrollPosition = getCurrentScrollPosition();
		
		topPos = scrollPosition["top"] + 135;
			
		if(topPos < 170) {
			topPos = 170;
		}
		
		topPos = topPos + Math.floor(Math.random()*100);			
			
		this.box.style.top = topPos + "px";	
		leftPos = Math.ceil((scrollPosition["width"] / 2) - (this.width / 2)) + Math.floor(Math.random()*60);
		
		if(leftPos < 0) {
			leftPos = 10;
		}
		
		this.box.style.left = leftPos + "px";		
	}
	
	this.show = function() {
		this.box.style.zIndex = this.eventHandler.getCurrentZIndex() + 1;
		this.box.style.display = 'block'; 
	}

	this.close = function() {
		this.box.style.display = 'none';
		this.box.innerHTML = '';
	}

	this.setHeader = function(headerString) {
		document.getElementById(this.id + 'ContainerHeader').innerHTML = headerString;
	}

	this.setSubHeader = function(headerString) {
		document.getElementById(this.id + 'ContainerSubHeader').innerHTML = headerString;
	}

	this.dragWindow = function(evt) {
		var mousePosition = getMousePosition(evt);
		
		windowManager.draggedWindow = this.box;	
		windowManager.windowPositionRelativeToMousePosition = getElementPositionRelativeToMousePosition(document.getElementById(this.id + 'windowContainer'), mousePosition);
		
		// Set Event-Handler for Dragging
		eval("document.onmousemove = " + this.name + ".mousemoveEventHandler;");
		eval("document.onmouseup = " + this.name + ".dropWindow;");
		  	
		// Disable Text-Selection
		disableTextSelection(document.getElementsByTagName("body")[0]);
	}
	
	this.dropWindow = function() {	
		document.onmousemove = null;
		document.onmouseup = null;

		enableTextSelection(document.getElementsByTagName("body")[0]);
		
		windowManager.draggedWindow = false;
	}

	this.mousemoveEventHandler = function(evt) {
		var mousePosition = getMousePosition(evt);
		
		mousePosition["x"] = mousePosition["x"] - windowManager.windowPositionRelativeToMousePosition["x"];
		mousePosition["y"] = mousePosition["y"] - windowManager.windowPositionRelativeToMousePosition["y"];

		// Set DragBox-Position
		windowManager.draggedWindow.style.top = mousePosition["y"] + "px";
		windowManager.draggedWindow.style.left = mousePosition["x"] + "px";	
	}
	
	this.setToTop = function() {
		this.box.style.zIndex = windowManager.increaseZIndex();
	}

	this.setWindowClassName = function(className) {
		document.getElementById(this.id + 'windowContainer').className = 'AppWindowContainer ' + className;
	}
	
	this.buildContainer = function() {	
		var windowContainer = document.createElement("DIV");
		windowContainer.id = this.id + 'windowContainer';
		windowContainer.className = 'AppWindowContainer';
		if(this.type!='noSize') {
			windowContainer.style.width = this.width + 'px';
			windowContainer.style.height = this.height + 'px';
		}
		eval("windowContainer.onclick = function() { " + this.name + ".setToTop(); };");
		
		windowDragBar = document.createElement("DIV");
		windowDragBar.id = this.id + 'windowDragBar';
		windowDragBar.className = 'windowDragBar';
		if(this.type!='noSize') {
			windowDragBar.style.width = (this.width - 60) + 'px';
		}
		eval("windowDragBar.onmousedown = function(event) { " + this.name + ".dragWindow(event); };");
			
		var windowBackground = document.createElement("DIV");
		windowBackground.className = 'windowBackground';
		
		if(this.type!='noSize') {
			windowBackground.style.width = this.width + 'px';
		}
		
		if(!this.displayTabBar && this.type!='noSize') {
			windowBackground.style.height = (this.height - 30) + 'px';
		}
		
		// CloseLink + Container
		var closeLinkContainer = document.createElement("DIV");
    	closeLinkContainer.id = this.id + "CloseLink";
    	closeLinkContainer.className = "AppWindowContainerCloseLink";
	
 		var closeLink = document.createElement("A");
    	closeLink.href = "JavaScript:" + this.name + ".close();";
    	closeLink.innerHTML = "X";
    	closeLinkContainer.appendChild(closeLink); 	

    	windowContainer.appendChild(windowBackground);  	
		windowContainer.appendChild(windowDragBar);
    	windowContainer.appendChild(closeLinkContainer);
    	// End CloseLink
	    	
    	// Header
		var containerHeader = document.createElement("DIV");
    	containerHeader.id = this.id + "ContainerHeader";
    	containerHeader.className = "AppWindowContainerHeader";

		// Sub-Header
		var containerSubHeader = document.createElement("DIV");
		containerSubHeader.id = this.id + "ContainerSubHeader";
		containerSubHeader.className = "AppWindowContainerSubHeader";
		containerHeader.appendChild(containerSubHeader);	
		
		var containerContent = document.createElement("DIV");
		containerContent.id = this.id + 'containerContent';
		containerContent.className = 'containerContent';
		if(this.type!='noSize') {
			containerContent.style.width = (this.width - 40)+ 'px';
			containerContent.style.height = (this.height - 92) + 'px';
		}
		
		// Add Header
    	windowContainer.appendChild(containerHeader);  
    	windowContainer.appendChild(containerSubHeader);  	
		windowContainer.appendChild(containerContent);
		
		document.getElementsByTagName("body")[0].appendChild(windowContainer);

		this.contentBox = document.getElementById(this.id + 'containerContent');	
	}
}
