// init Objects
cWeb = new Object();
cWeb.o = new Object();

var mooDropDown = new Class({
	initialize: function(el){;
		// Setting Element
		this.oDropDownRoot = el;
		// Validate Objects
		if(!this.oDropDownRoot) {
			dbug.log('mooDropDown error: Element oRoot is not defined or not found');
			return;
		}
		this.setChildProperties(this.oDropDownRoot);
	},
	setChildProperties: function(oObj){
		var aChilds = oObj.getChildren();
		aChilds.each(function(oChild){
			if(oChild.tagName=='LI'){
				var oChildChilds = oChild.getChildren();
				oChildChilds.each(function(oChild2){
					if(oChild2.tagName=='UL'){
						oChild.firstChild.addClass('hasSubnavi');
						this.mooDropDown.prototype.setChildProperties(oChild2);
						//var fx1 = new Fx({duration:350, wait:false, onComplete:function(el){}});
						var fx1 = new Fx.Morph(oChild2, { 'duration': 350 });
						var fx2 = new Fx.Morph(oChild2, { 'duration': 350 });
						var fx3 = new Fx.Morph(oChild2, { 'duration': 350 });
						var fx4 = new Fx.Morph(oChild2, { 'duration': 350 });
									
						oChild.addEvent('mouseover',function(){
							if(window.ie || window.ie7)$$('select').setStyle('display','none');
							fx2.cancel();fx3.cancel();fx4.cancel();
							fx1.start({'opacity': '1','visibility':'visible'});
						});
						oChild.addEvent('mouseout',function(){
							if(window.ie || window.ie7)$$('select').setStyle('display','block');
							fx1.cancel();fx3.cancel();fx4.cancel();
							fx2.start({'opacity': '0','visibility':'hidden'});
						});
						oChild2.addEvent('mouseover',function(){
							if(window.ie || window.ie7)$$('select').setStyle('display','none');
							fx1.cancel();fx2.cancel();fx4.cancel();
							fx3.start({'opacity': '1','visibility':'visible'});
						});
						oChild2.addEvent('mouseout',function(){
							if(window.ie || window.ie7)$$('select').setStyle('display','block');
							fx1.cancel();fx3.cancel();fx3.cancel();
							fx4.start({'opacity': '0','visibility':'hidden'});
						});
					}
				});
			}
		});
	}
});
mooDropDown.implement(new Options, new Events);

window.addEvent('domready', function(){
	$$('.mooDownMenue').each(function(mooObj){
		new mooDropDown(mooObj);
	});
});


cWeb.Autocompleter = new Class({
	Implements: [Options,Chain],
	options: {
		basicPath:'',
		url:'',
		commiturl:'',
		fadedurration:200,  					// time for animation
		fps:50,									// frames per second
		startAtChar:1,							// count of chars when the first search hat so start
		delayToHideBox:3000,					// time in ms to hide box after a search 
		delayToHideBoxAfterMouseOut:1000,		// time in ms to hide box after leaving the Box with the mouse 
		delayToSearch:200						// delay to start a new search while typing
	},
	initialize: function(elInput,elBox,elList,options){
		this.setOptions(options);

		// check data
		if($type(elInput) == 'element'){
			this.elInput = elInput;
		}else{
			// exit quietly 
			return false;
		}
		if($type(elBox) == 'element'){
			this.elBox = elBox;
		}else{
			// exit quietly 
			return false;
		}
		if($type(elList) == 'element'){
			this.elList = elList;
		}else{
			// exit quietly 
			return false;
		}
		if(!this.options.url.length){
			// exit quietly 
			return;
		}
		
		// init Data
		var request = new Request.HTML({			
			url: this.options.basicPath + this.options.url,
			method: 'get',
			onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){				
				this.data = responseElements[1].innerHTML.split(',');
			}.bind(this)
		}).send();
		
		this.bBoxVisible = false; 
		
		this.elBox.setStyle('opacity',0);
		this.fxHideBox = new Fx.Morph(
			this.elBox,
			{
				fps:this.options.fps,
				duration: this.options.fadedurration,
				onComplete:(function(el){
					if(el.get('opacity')){
						this.bBoxVisible = true;
					}else{
						this.bBoxVisible = false,
						this.elBox.setStyle('display','none')
					}
				}).bind(this)
			});
		
		this.hideboxDelay = 0;
		this.bHideBox = false;
		
		this.searchDelay = 0;
		this.bStartSearch = false;
		
		this.currSelection = -1;
		
		this.elInput.addEvent('keyup', this.checkForSearch.bindWithEvent(this));
		this.elInput.addEvent('focus', this.focus.bindWithEvent(this));
		this.elInput.addEvent('blur', this.blur.bindWithEvent(this));
		
		$(this.elInput.form).addEvent('submit', this.commit.bindWithEvent(this));
		
		this.defaultString = this.elInput.get('value');
		
		this.elInput.set('autocomplete','off');
		
	},
	checkForSearch:function(evnt){		
		switch(evnt.code){
			case 38:
				// onKey cursor up
				this.hideboxDelay = $clear(this.hideboxDelay);
				this.mark(-1);
			break;
			case 40:
				// onKey cursor down
				this.hideboxDelay = $clear(this.hideboxDelay);
				this.mark(1);
			break;
			case 27:
				// onKey Esc
				this.bHideBox = true;
				this.hideBox(false);
			break;
			default:
				this.searchDelay = $clear(this.searchDelay);
				if(this.elInput.value.length >= this.options.startAtChar){
					this.searchDelay = this.startSearch.delay(this.options.delayToSearch,this,this.elInput.value);
				}
		}
	},
	mark:function(addSel){
		if(this.bBoxVisible){
			var aEl = this.elList.getElements('a');
			aEl.removeClass('sel');
			if($defined(aEl[this.currSelection+addSel])){
				aEl[this.currSelection+addSel].addClass('sel');
				this.currSelection += addSel;
			}
		}
	},
	mousemark:function(evnt,i){
		if(this.bBoxVisible){
			var aEl = this.elList.getElements('a');
			aEl.removeClass('sel');
			if($defined(aEl[i])){
				aEl[i].addClass('sel');
				this.currSelection = i;
			}
		}
		
	},
	click:function(evnt){
		evnt = new Event(evnt);
		evnt.stop();
		this.commit();
	},
	commit:function(evnt){
		if($defined(evnt)){
			evnt = new Event(evnt);
			evnt.stop();
		}
		var aEl = this.elList.getElements('a');
		var sCrit = this.elInput.get('value');
		if(this.currSelection >= 0 && this.bBoxVisible){
			if($defined(aEl[this.currSelection])){
				var sCrit = aEl[this.currSelection].get('href');
			}
		}
		window.location.href = this.options.basicPath + this.options.commiturl + sCrit;
		
	},
	focus:function(evnt){
		var sVal = this.elInput.get('value');
		if(!sVal.length || sVal == this.defaultString){
			this.elInput.set('value','');
		}
	},
	blur:function(evnt){
		var sVal = this.elInput.get('value');
		if(!sVal.length){
			this.elInput.set('value',this.defaultString);
		}
	},
	startSearch:function(criteria){
		this.fxHideBox.pause();
		var aRes = [];
		var sCurr = '';
		for(var i = 0; i< this.data.length; i++){
			sCurr = this.data[i].toString();
			if(sCurr.test(criteria, "i"))
				aRes.push(sCurr);
		};
		this.displayResult(aRes);
	},
	displayResult:function(content){
		
		this.currSelection = -1;
		this.elList.getElements('a').removeClass('sel');
		
		this.elList.empty();
		
		// generate HTMLElements
		var eA = 0;
		for(var i = 0; i< content.length; i++){
			eA = 0;
			eA = new Element('a',{'href':content[i]}).set('html',content[i]);
			this.elList.adopt(eA);
			eA.addEvent('click', this.click.bindWithEvent(this));
			eA.addEvent('mouseenter', this.mousemark.bindWithEvent(this,i));
		}
		
		this.bBoxVisible = true; 
	
		this.elBox.setStyle('display','block');
		
		this.fxHideBox.start({'opacity':1})

		
		this.hideboxDelay = $clear(this.hideboxDelay); 
		this.bHideBox = true;
		this.hideboxDelay = this.hideBox.delay(this.options.delayToHideBox,this,false);
	},
	outBox:function(){
		this.hideboxDelay = $clear(this.hideboxDelay); 
		this.bHideBox = true;
		this.hideBox.delay(this.options.delayToHideBoxAfterMouseOut,this,false);
	},
	overBox:function(){
		this.hideboxDelay = $clear(this.hideboxDelay); 
		this.bHideBox = false;
	},
	hideBox:function(force){
		if(this.bHideBox || force){
			this.fxHideBox.start({'opacity':0});
		}
	}
});


cWeb.Timeline = new Class({
	Implements: [Events, Options, Chain],
	options: {
		leftBttClass:'.scrollBttL',
		rightBttClass:'.scrollBttR',
		bttOffOpacity:0.3,
		jumpWidth:115,
		scrollerTop:380,
		scrollVisibleCount:6,
		windowManager:null
	},
	initialize: function(el,options){
		this.setOptions(options);
		this.el = $(el);
		
		this.el.getElement(this.options.leftBttClass).addEvent('click',this.move.bindWithEvent(this,-1));
		this.el.getElement(this.options.rightBttClass).addEvent('click',this.move.bindWithEvent(this,1));
		
		this.oScroll = new Fx.Scroll(this.el.getElement('.timelinewrap'),{transition: Fx.Transitions.Sine.easeInOut,duration:500});
		
		this.windows = new Array();
		this.currOpen = null;
		this.currScrollPos = null;
		
		this.elScroll = this.el.getElement('.scroller');
		this.startPos = this.elScroll.getPosition();
		
		this.el.setStyle('height',this.el.getSize().y);
		
		this.el.getElements('.tlObj').each(function(oE,i){
			this.windows[i] = cWeb.o.WindowManager.add({
				content:$('timelineDet'+oE.get('rel'))
			});
			oE.addEvent('click', (function(e){
				new Event(e).stop();
				cWeb.o.WindowManager.open(this.windows[i]);
				this.currOpen = i;
			}).bind(this));
		},this);
		
		/*
		cWeb.o.WindowManager.addEvent('open',(function(){
			this.elScroll.setStyles({'z-index':10001,'position':'fixed',top:this.startPos.y-document.getScroll().y});
			
			this.elScroll.morph({top:this.options.scrollerTop});
		}).bind(this));
		cWeb.o.WindowManager.addEvent('afterclose',(function(){
			this.elScroll.setStyles({'z-index':1,top:0,'position':'relative'});
		}).bind(this));
		cWeb.o.WindowManager.addEvent('beforeclose',(function(){
			this.elScroll.retrieve('morph').addEvent('complete',(function(el){
				if(el.getStyle('top').toInt() != this.options.scrollerTop)
					el.setStyles({'z-index':1,top:0,'position':'relative'});
			}).bind(this));
			this.elScroll.morph({'top':this.startPos.y-document.getScroll().y});
		}).bind(this));
		*/
		cWeb.o.WindowManager.addEvent('keypress',(function(evnt,window){
			if(window.current){
				if(evnt.key == 'left'){
					this.openNext(-1)
				}
				if(evnt.key == 'right'){
					this.openNext(1)
				}
			}
		}).bind(this));
		
		this.renderArrows();
	},
	move: function(evnt,dir){
		if( (this.windows.length > this.currScrollPos + dir + this.options.scrollVisibleCount-1 ) && (this.currScrollPos + dir >= 0) ){
			this.currScrollPos += dir;
			var oCoor = this.el.getElement('.timelinewrap').getScroll();
			this.oScroll.start(oCoor.x + (this.options.jumpWidth * dir),0);
			this.renderArrows();
		}
	},
	openNext: function(dir){
		if( (this.windows.length > this.currOpen + dir ) && (this.currOpen + dir >= 0) ){
			this.currOpen += dir;
			cWeb.o.WindowManager.open(this.windows[this.currOpen]);
		}
	},
	renderArrows: function(){
		if(this.currScrollPos <= 0){
			this.el.getElement(this.options.leftBttClass).setStyle('opacity',this.options.bttOffOpacity);
			this.el.getElement(this.options.rightBttClass).setStyle('opacity',1);
		}else if(this.currScrollPos + this.options.scrollVisibleCount >= this.windows.length){
			this.el.getElement(this.options.leftBttClass).setStyle('opacity',1);
			this.el.getElement(this.options.rightBttClass).setStyle('opacity',this.options.bttOffOpacity);
		}else{
			this.el.getElement(this.options.leftBttClass).setStyle('opacity',1);
			this.el.getElement(this.options.rightBttClass).setStyle('opacity',1);
		}
	}
});

cWeb.WindowManager = new Class({
	Implements: [Options,Events],
	options: {
		onOpen:$empty,
		onClose:$empty,
		onKeyPress:$empty,
		defaults:{
			width:300,
			height:300,
			modal:false,
			content:null
		},
		modalWrp:'modalWrp',
		modalOpacity:0.6,
		windowWrp:'windowWrp',
		clWindow:'window'
	},
	initialize: function(options){
		this.setOptions(options);
		
		this.windows = new Hash();
		
		this.current = null;
		
		if(!(this.oWrp = $(this.options.windowWrp))){
			// generate wrapper if not existent
			this.oWrp = new Element('DIV',{'id':this.options.windowWrp,styles:{'width':1,'height':1,'position':'absolute'}}).inject($(document.body),'bottom');
		}
		if(!(this.oModal = $(this.options.modalWrp))){
			// generate modal wrapper if not existent
			var tmpHeight = this.getTimelinePageSize();
			this.oModal = new Element('DIV',{'id':this.options.modalWrp,styles:{'height':tmpHeight[1],'display':'none','opacity':0}}).inject(this.oWrp,'before');
			this.oModal.get('tween').addEvent('complete',(function(el){
				if(!el.getStyle('opacity')){
					el.setStyle('display','none');
					this.fireEvent('close',this);
				}
			}).bind(this));
		}
		
		this.options.defaults.clWindow = this.options.clWindow;
		$(document).addEvent('keydown',this._keyEvent.bindWithEvent(this));
	},
// Custom-extension for Timeline	
getTimelinePageSize: function(){
 	var xScroll, yScroll;
		
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	return [pageWidth,pageHeight];

	
},	
	
	
	getEl: function(){
		return this.oWrp;
	},
	open: function(name){
		if(name != this.current){
			if(this.current){
				this.get(this.current).close(true);
			}else{
				this.fireEvent('open',this);
			}
			this.current = name;
			this.get(name).open(true);
		}
	},
	close: function(){
		this.fireEvent('beforeclose',this);
		if(this.get(this.current) && !this.get(this.current).isModal()){
			this.fireEvent('afterclose',this);
		}
		this.current = null;
	},
	add: function(options,name){
		var merged = $merge(this.options.defaults,options);
		var winname = this.options.clWindow + '_' + this.windows.getLength();
		if($defined(name)){
			winname = name;
		}
		merged.name = winname;
		this.windows.set(winname, new cWeb.Window(this,merged));

		return winname;
	},
	get: function(name){
		if(this.windows.has(name)){
			return this.windows.get(name);
		}else{
			return false;
		}
	},
	openModal: function(){
		this.oModal.setStyle('display','block');
		this.oModal.fade(this.options.modalOpacity);
	},
	closeModal: function(){
		this.oModal.fade('out');
	},
	_keyEvent: function(evnt){
		this.fireEvent('keypress',[evnt,this]);
		if(evnt.key == 'esc' && this.current){
			this.get(this.current).close(false);
		}
	}
});

cWeb.Window = new Class({
	Implements: [Events, Options, Chain],
	options: {
		top:null,
		width:300,
		height:300,
		relativ:null,
		content:null,
		modal:false,
		name:null
	},
	initialize: function(manager,options){
		this.setOptions(options);
		
		this.manager = manager;
		this.name = this.options.name;
		if(!this.options.content){
			// exit if no content is defined
			return void(0);
		}
		this.generate();
		
		return this;
	},
	generate: function(){
		var elWrp1,elWrp2;
		var content = this.options.content.dispose();
		var close = new Element('A',{'class':'close','href':'javascript:void(0);'});
		
		this.wrp = new Element('DIV',{
			'class':this.options.clWindow,
			styles:{
				width:this.options.width,
				//height:this.options.height,
				top:(this.options.top) ? this.options.top : window.getSize().y/2-this.options.height/2,
				left:window.getSize().x/2-this.options.width/2,
				position:'absolute',
				opacity:0
			}
		});
		
		elWrp1 = new Element('DIV',{'class':this.options.clWindow + '1'}).inject(this.wrp);
		elWrp2 = new Element('DIV',{'class':this.options.clWindow + '2'}).inject(this.wrp);
		this.el = new Element('DIV',{'class':this.options.clWindow + '_cont'}).inject(this.wrp);
		close.inject(elWrp1,'before');
		this.el.adopt(content);
		
		new Element('DIV',{'class':'clear'}).inject(this.wrp,'bottom');
		new Element('DIV',{'class':'clear'}).inject(this.el,'bottom');
		
		
		
		this.wrp.get('tween').addEvent('complete',function(el){if(!el.getStyle('opacity')){el.dispose();}});
		
		close.addEvent('click',this.close.bind(this,false));
	},
	open: function(){
		if(this.options.modal){
			this.manager.openModal();
		}
		this.wrp.inject(this.manager.getEl(),'bottom');
		this.wrp.fade('in');
	},
	close: function(byOpen){
		if(!$defined(byOpen))byOpen=false;
		if(this.options.modal){
			this.manager.closeModal();
		}
		if(!byOpen){
			this.manager.close();
		}
		this.wrp.fade('out');
	},
	isModal: function(){
		return this.options.modal;
	}
});
