
/**
* display Package
* created by Ingo Taraske (zoo2002.de)
* V. 0.22 (29-05-2006)
* 
**/

display = new Object();




/**
* display Package
* created by Ingo Taraske (zoo2002.de)
*
* 
**/

display.Window = Class.create();

display.Window = {
	
	initialize: function(properties){
		
		this.id = properties.id;
		this.isVisible = null;
		this.isLoose = false;
		this.oldMousePos = null;
		
		// setup window Hashlist
		this.windows = new Hash();
		this.windowKeys = new Array();
		
		if(properties.parent)
			this.parent = properties.parent;
		
		if(properties.isLoose)
			this.isLoose = properties.isLoose;
		
		if(properties.className)
			this.className = properties.className;
		
		this.container = cDOM({
			tag: 'div',
			id: this.id,
			className: this.className+' window',
			fixedId: true});
			
			if(properties.showTitleBar != false){
			
				this.titlebar = cDOM({
					tag: 'div',
					id: 'titlebar',
					className: 'titlebar'});
				
				this.titlebarMouseDown = event.getObserver(this.titlebar, 'onmousedown');
				this.titlebarMouseDown.addListener(this.onmousedown, this, this.onmousedown, 'startDragging');
		
			}

		
		if(this.titlebar && properties.showTitle != false){
			this.titlebarText = cDOM({
				tag: 'div',
				id: 'titlebarText',
				className: 'titlebarText'});
				
			
			this.titlebarText.innerHTML = properties.id;
			
			this.titlebar.appendChild(this.titlebarText);
		
		}
		
		if(this.titlebar && properties.showButtons != false){
			this.titlebarCloseButton = cBUTTON({
				innerHTML: 'x',
				command: 'close',
				id: 'titlebarCloseButton',
				className: 'titlebarButton',
				notify: this.onWindowButtonClick,
				target: this});
				
			this.titlebar.appendChild(this.titlebarCloseButton);

			this.titlebarToggleButton = cBUTTON({
				innerHTML: '_',
				command: 'toggle',
				id: 'titlebarToggleButton',
				className: 'titlebarButton',
				notify: this.onWindowButtonClick,
				target: this});
				
			this.titlebar.appendChild(this.titlebarToggleButton);
		}
		
		if(this.titlebar){
		this.titlebar.appendChild(cDOM({
			tag: 'div',
		className: 'divClear'}));}
		
		if(properties.showScrollBars != false){
			
			this.showScrollBars = properties.showScrollBars;
			this.scrollDownBar = cDOM({tag: 'div', className: 'scroll'});
			this.scrollDownBar.id = 'scrollDownBar'+getRandom();
			var link = cLINK({target: this, notify: this.onWindowButtonClick, className: 'scroll', command: 'scrollDown', innerHTML:'down'});
			link.parent = this;
			
			this.scrollDownBar.appendChild(link);
				
			
			this.scrollUpBar = cDOM({tag: 'div', className: 'scroll'});
			this.scrollUpBar.id = 'scrollUpBar'+getRandom();
			var link = cLINK({target: this, notify: this.onWindowButtonClick, className: 'scroll', command: 'scrollUp', innerHTML:'top'});
			link.parent = this;
			
			this.scrollUpBar.appendChild(link);
		}
		
		
		this.content = cDOM({
			tag: 'div',
			id: 'content',
			className: 'content'});
		this.content.isVisible = true;
		
		if(properties.showTitleBar != false)
		this.container.appendChild(this.titlebar);
		
		if(properties.showScrollBars != false)
		this.container.appendChild(this.scrollDownBar);
		
		this.container.appendChild(this.content);
		
		if(properties.showScrollBars != false)
		this.container.appendChild(this.scrollUpBar);
		
		// a Model window/dialog needs a blocker
		if(properties.isModal){
			this.blocker = cDOM({tag: 'div', className: 'blocker'});
			this.blocker.appendChild(this.container);
		}
		
		if(properties.appendToParent && properties.parent){
			
			if(properties.isModal)
				var element = this.blocker;
			else
				var element = this.container;
			
			var parent = properties.parent;
			var first = parent.firstChild;
			if(properties.appendAsFirst && first)
				parent.insertBefore(element, first);
			else
			parent.appendChild(element);
		}
		
		// hide if necessary
		if (properties.isVisible == false)
		this.hide();
		
		else
		this.show();

		this.customInitialize(properties);
	},
	
	onmousedown: function(e, target, command, pars){
		target.oldMousePos = getMouseXY(e);
		
		target.titlebarMouseMove = event.getObserver(BODY(), 'onmousemove');
		target.titlebarMouseMove.addListener(target.onmouse, target, target.onmouse, 'drag');
		
		target.titlebarMouseUp = event.getObserver(BODY(), 'onmouseup');
		target.titlebarMouseUp.addListener(target.onmouse, target, target.onmouse, 'stopDragging');
	},
	
	onmouse: function(e, target, command, pars){
		
		switch(command){
			
			case 'stopDragging':
			if(target.titlebarMouseMove){
				if(target.titlebarMouseMove.hasListener(target.onmouse))
					target.titlebarMouseMove.removeListener(target.onmouse);
			}
			break;
			
			case 'drag':
			var mouseNow = getMouseXY(e);
			var difx = target.oldMousePos[0] - mouseNow[0];
			var dify = target.oldMousePos[1] - mouseNow[1];
			
			var array = Position.cumulativeOffset(target.container); 
			oldx = array[0];
			oldy = array[1];
			
			var positioning;
			if((positioning = getStyle(target.container, 'position'))
				== 'relative' || null)
			target.container.style.position = 'absolute';
			
			var floating;
			if((floating = getStyle(target.container, 'float'))
			!= null)
			target.container.style['float'] = 'none';
			target.container.style['margin'] = 'auto';
			
			var y;
			if((y = oldy - dify) < 0 || mouseNow[1] < 0)
				y = 0;
			
			var x;
			if((x = oldx - difx) < 0 || mouseNow[0] < 0)
				x = 0;
			
			target.container.style.top = y+'px';
			target.container.style.left = x+'px';
			target.oldMousePos = mouseNow;
			break;
		}
	},
	
	show: function(){
		if (this.isVisible == false){
			Element.toggle(this.container);
			
			if(this.scrollDownBar && this.scrollUpBar){
			Element.toggle(this.scrollDownBar);
			Element.toggle(this.scrollUpBar);
			}
		}

		this.isVisible = true;
	},
	
	hide: function(){
		if(this.isVisible == true || this.isVisible == null){
			Element.toggle(this.container);
			
			if(this.scrollDownBar && this.scrollUpBar){
			Element.toggle(this.scrollDownBar);
			Element.toggle(this.scrollUpBar);
			}
		}
		
		this.isVisible = false;
	},
	
	toggleContent: function(){
		Element.toggle(this.content);
		Element.toggle(this.scrollDownBar);
		Element.toggle(this.scrollUpBar);
		if(this.content.isVisible == true)
			this.content.isVisible = false;
		else
			this.content.isVisible = true;
	},
	
	/**
	* to add content in front, expects DOM elements
	*/
	addFirst: function(content){
		if(isArray(content)){
			for(var i = 0; i<content.length; i++){
					if(parent.firstChild)
						document.insertBefore(content[i], this.content.firstChild);
					else
						this.content.appendChild(content[i]);
			}
		}
		else
			this.content.appendChild(content);
	},
	
	/**
	* to append DOM elements to content
	*/
	add: function(content){
		if(isArray(content)){
			for(var i = 0; i<content.length; i++)
				this.content.appendChild(content[i]);
		}
		else
			this.content.appendChild(content);
		
	},
	
	clearContent: function(){
	this.content.innerHTML = '';},
	
	addWindow: function(properties){
		properties.object.parent = this;
		this.content.appendChild(properties.object.container);
		this.windowKeys.append(properties.id, true);
		this.windows.add(properties.id, properties.object);
	},
	
	getWindow: function(properties){
		var answer = null;
		if(answer = this.windows.has(properties.id))
			return answer;
		else
			return null;
	},
	
	// to get Array of Window Keys
	getWindowArray: function(){
    return  this.windowKeys;
	},
	
	removeWindow: function(properties){
		if(!(child = this.windows.has(properties.id)))
			return false;
		
		if(!child.isModal)
			this.content.removeChild(child.container);
		else
			this.content.removeChild(child.blocker);
		
		this.windows.del(properties.id);
		this.windowKeys.remove(properties.id);
		return true;
	},
	
	removeWindows: function(){
		var result = true;
		while(this.windowKeys.length > 0 && result == true){
			var key = this.windowKeys[0];
			result = this.removeWindow({id: key});
		}
	},
	
	destroy: function(){
		this.removeWindows();
		
		if(this.parent){
			if(this.parent.removeWindow){
				this.parent.removeWindow({id: this.id});
				return;
			}
			
			if(this.parent.removeChild){
				if(!this.isModal)
				this.parent.removeChild(this.container);
				else
				this.parent.removeChild(this.blocker);
				return;
			}
		}
	},
	
	onWindowButtonClick: function(e, target, command, pars){
		
		switch(command){ 
		
		case 'close':
			target.destroy();
			break;
		
		
		case 'toggle':
		
			target.toggleContent();
			break;
		
		case 'scrollDown':
			Element.scrollTo($(target.scrollUpBar));
			break;
			
		case 'scrollUp':
			Element.scrollTo($(target.scrollDownBar));
			break;
		}
	}
};

display.navItem = Class.create();
display.navItem.prototype = {
		
		initialize: function(item, level, toExtend){
				
				var title = item.title;
				
				this.id = title+'NavItem';
				
				this.container = cDOM({tag: 'div'});
				
				
				
				var label;
				if(level > 0){
				label = '&gt; '+title;
				className = 'navItemSub';
				}
				
				else{
				label = title;
				className = 'navItem';
				}
				
				this.img = 'menu-off-'+item.img+'.gif';
				this.img_on = 'menu-on-'+item.img+'.gif';
				this.img_vis = 'menu-visited-'+item.img+'.gif';
				
				//use DW function for image preload
				MM_preloadImages('menu/'+this.img, 'menu/'+this.img_on, 'menu/'+this.img_vis);
				
				// space reserved for the navigation item
				this.core = cDOM({tag: 'div', 'className': className});
				
				this.element = cDOM({tag: 'img', src: 'menu/'+this.img, 'id': this.id, className: 'navLink'});
				
				if(item.url)
				this.url = item.url;
				
				
				var pars = new Hash();
				pars.add('id', this.id);
				pars.add('url', item.url);
				
				// onClick Event Handler
				events.getObserver(this.element
				, 'onclick').addListener(this.onClickAction, this
				, this.onClickAction, 'navigateItem', pars);
				
				events.getObserver(this.element
				, 'onmouseover').addListener(this.onMouseOverAction, this
				, this.onMouseOverAction, 'highlight', pars);
				
				events.getObserver(this.element
				, 'onmouseout').addListener(this.onMouseOutAction, this
				, this.onMouseOutAction, 'highlightOff', pars);
				
				this.core.appendChild(this.element);
				
				// for the navigation item's subItems
				this.extension = cDOM({tag: 'div', className: 'navExtension'});
				
				
				this.container.appendChild(this.core);
				this.container.appendChild(this.extension);
				
				this.item = item;
				this.isExtended = false;
				
				if(toExtend == true)
				this.extend();
			
		}, 
		
		extend: function(){
			if(this.item.subItems){
						level = 1;
							
							for( var j = 0; j < this.item.subItems.length; j++){
								var element = new display.navItem(this.item.subItems[j], level);
								this.extension.appendChild(element.container);
							}
					
					//this.oldElementClass = this.element.className;
					//this.element.className = 'navItemActive';
					this.element.src = 'menu/'+this.img_vis;
					}
					
					this.isExtended = true;
					addNavMemory(this.id, this.id);
					
		},
		
		onClickAction: function(e, target, command, pars){
			
			switch(command){
			
			case 'navigateItem':
				
				if(target.url){
				window.location = target.url;
				}
				
				if(target.isExtended == false){
					target.extend();
				}
				
				else{
					var child
					while(child = target.extension.firstChild){
					target.extension.removeChild(child);
					}
					if(target.oldElementClass)
					target.element.className = target.oldElementClass;
					target.element.src = 'menu/'+target.img;
					target.isExtended = false;
					
					var id = pars.has('id');
					removeNavMemory(id);
				}
				
			break;
			}
			
		},
		
		onMouseOverAction: function(e, target, command, pars){
			
			switch(command){
			
			case 'highlight':
				if(!target.isExtended)
				target.element.src = 'menu/'+target.img_on;
			break;
			}
			
		},
		
		onMouseOutAction: function(e, target, command, pars){
			
			switch(command){
			
			case 'highlightOff':
				if(!target.isExtended)
				target.element.src = 'menu/'+target.img;
			break;
			}
			
		}
	
	};
	
	
display.ShiftNavigation = Class.create();
display.ShiftNavigation.prototype = {
	
	customInitialize: function(properties){
		this.render(properties);
	},
	
	render: function(properties){
		this.clearContent();
		
		var list;
		if(list = properties.list){

			for(var i= 0; i < list.length; i++){
				var item = list[i];
				
				var level = 0;
				
				var toExtend = false;
				//check if item is in memory to be expanded
				if(properties.navMemory){
					for(var j = 0; j< properties.navMemory.length; j++){
						if(properties.navMemory[j] == item.title+'NavItem')
						toExtend = true;
					}
				}
				
				var navItem = new display.navItem(item, level, toExtend);
				
				this.content.appendChild(navItem.container);
			}
			
			this.content.appendChild(cDOM({tag: 'div', className: 'divClear'}));
		}
	}
};
Object.extend(display.ShiftNavigation.prototype, display.Window);


