/*global window, document, clearInterval, setInterval, dojo, CndSuggest */

/* Konfiguration fuer die Datumsauswahl */
function scwSetLanguage(){
	scwToday               = 'Heute:';
	scwClear               = 'Leeren';
	scwDrag                = 'click here to drag';
	scwArrMonthNames       = ['Januar','Februar','März','April','Mai','Juni','Juli','August','Septemper','Oktober','November','Dezember'];
	scwArrWeekInits        = ['So','Mo','Di','Mi','Do','Fr','Sa'];
	scwInvalidDateMsg      = 'Das eingebene Datum ist ungültig.\n';
	scwOutOfRangeMsg       = 'Das eingebene Datum ist außerhalb des zulässigen Bereiches.';
	scwDoesNotExistMsg     = 'Das eingebene Datum existiert nicht.';
	scwInvalidAlert        = ['Ungültiges Datum(',') ignoriert.'];
	scwDateDisablingError  = ['Fehler ',' ist kein Datumsobjekt.'];
	scwRangeDisablingError = ['Fehler ',	' sollte aus zwei Elementen bestehen.'];
}
var scwDateDisplayFormat = 'dd.mm.yy';
var scwDateOutputFormat  = 'DD. MMM YYYY';
var scwBaseYear = scwDateNow.getFullYear()-98;
var scwDropDownYears = 100;

var zeag={
	menu:{
		init: function(menuListId) {
			dojo.query('> ul', menuListId).forEach(zeag.menu.register);
		},
		register:function(ul){
			dojo.query('li.child > ul, li.child > div.nachricht', ul).forEach(function(e){
				if(dojo.hasClass(e, 'path')){
					dojo.addClass(e.parentNode, 'jsOpen');
					dojo.removeClass(e.parentNode, 'path');
				}else{
					dojo.style(e, 'display', 'none');
				}
				dojo.addClass(e, 'jsPath');
			});

			dojo.query('li > a', ul).forEach(function(e){
				if (dojo.hasClass(e, 'child') && e.parentNode.parentNode==ul) {
					dojo.connect(e, 'onclick', zeag.menu.switchstate);
				} else {
					//von thomas b. 'abbestellt'
					//dojo.connect(e, 'onclick', zeag.menu.href);
				}
			});
		},

		switchstate:function(evt){
			evt.preventDefault();
			var li=evt.target.parentNode;
			var ul=dojo.query('> ul, > div.nachricht', li)[0];
			if(dojo.style(ul, 'display')!='none') {
				zeag.menu.close(evt, li, ul);
			} else {
				zeag.menu.open(evt, li, ul);
			}
		},

		open:function(evt, li, ul){
			dojo.style(ul, 'height', '');
			dojo.style(ul, 'display', 'none');//ul.hide();

			dojo.fx.wipeIn({
				node: ul,
				duration: 200
			}).play();
			zeag.menu.closeOpen(li);
			dojo.addClass(li, 'jsOpen');
		},

		close:function(evt, li, ul){
			dojo.fx.wipeOut({
				node: ul,
				duration: 200
			}).play();
			dojo.removeClass(li, 'jsOpen');
		},

		href:function(evt){
			evt.preventDefault();
			var href=evt.target.href;
			zeag.menu.closeOpen(evt.target.parentNode.parentNode.parentNode);
			window.setTimeout(function(){
			window.location.href=href;
			}, 250);
		},
		closeOpen:function(li){
			var lastOpen=dojo.query('> li.jsOpen > a + ul, > li.jsOpen > a + div.nachricht', li.parentNode);
			if(lastOpen.length===1) {
				dojo.fx.wipeOut({
					node: lastOpen[0],
					duration: 200
				}).play();

				dojo.removeClass(lastOpen[0].parentNode, 'jsOpen');
			}
			dojo.query('li.current.path', li.parentNode).forEach(function(e){
				dojo.removeClass(e, 'path');
			});
		}
	},contact:{
		init:function(classname){
			dojo.query('a.'+classname).forEach(function(item){
				var div=dojo.query('> div.'+classname, item.parentNode);
				if(div.length===1){
					div = div[0];
					dojo.style(div, 'display', 'none');
					dojo.addClass(div, 'jscontrol');
					dojo.connect(item, 'onclick', function(evt){
						if(dojo.style(div, 'display')=='none'){
							dojo.fx.wipeIn({node:div,duration:100}).play();
						}else{
							dojo.fx.wipeOut({node:div,duration:100}).play();
						}
						evt.preventDefault();
					});
				}
			});
		}
	},

	suggest:{
		reader:{
			getRecords:function(json){
				try{return json.W3MAN.SEARCH.SUGGEST.LOOP[0]?json.W3MAN.SEARCH.SUGGEST.LOOP[0]:[];}catch(e){return[];}
			},getLabel:function(o){
				return o;
			},getValue:function(o){
				return o;
			}
		},handleFire:function(label, value){
			this.input.value=label;
			this.input.form.submit();
		}
	},

	anim:{
		reg:[],
		pulseFactor: 113.6,
		slideMaxW: 350,
		slideDuration: 250,
		slideInterval: 20,
		slideContW: null,
		animFn: null,
		animId: null,
		fnOp: function(evt){
			zeag.anim.doSlide(this.slideLi, true);
			zeag.anim.doPulse(this.pulseImg, true, this.pulseOSize, this.pulseTop, this.pulseLeft);
		},
		fnCl: function(evt){
			zeag.anim.doSlide(this.slideLi, false);
			zeag.anim.doPulse(this.pulseImg, false, this.pulseOSize, this.pulseTop, this.pulseLeft);
		},

		init: function(slideUlId, pulseUlId){
			var slider=document.getElementById(slideUlId);
			if (slider){
				zeag.anim.slideContW=slider.clientWidth>1?slider.clientWidth:slider.offsetWidth;
				var els=dojo.query('> li > a', slideUlId);
		
				var min=Math.floor(zeag.anim.slideContW/els.length*0.75);
				var max=Math.floor(min*5/3);
				var sum=0;
				var count=0;
				els.forEach(function(e){
					var w=++count===els.length?zeag.anim.slideContW-sum:Math.floor(max-Math.random()*(max-min+1));
					sum+=w;
					zeag.anim.register(e, pulseUlId, w);
				});
			} else {
				var pulse=document.getElementById(pulseUlId);
				dojo.query('> li > a > img', pulseUlId).forEach(function(e){
					var pulseTop=e.offsetTop;
					var pulseLeft=e.offsetLeft;
					var pulseOSize=e.offsetWidth;
					var fn=function(evt){
						console.log(evt);
						zeag.anim.doPulse(e, evt.type=='mouseover', pulseOSize, pulseTop, pulseLeft);
					};
					dojo.connect(e, 'onmouseover', fn);
					dojo.connect(e, 'onmouseout', fn);
				});
			}
		},

		register:function(e, pulseUlId, randW) {
			dojo.style(e.parentNode, 'width', randW+'px');
			var conf = {
				sW: randW,
				href: e.href,
				slideLi: e.parentNode,
				pulseImg: dojo.query('> li > a[href="'+e.href+'"] > img', pulseUlId)[0]
			};
			conf.pulseTop=conf.pulseImg.offsetTop;
			conf.pulseLeft=conf.pulseImg.offsetLeft;
			conf.pulseOSize=conf.pulseImg.offsetWidth;

			zeag.anim.reg.push(conf);

			dojo.connect(e, 'onmouseover', conf, zeag.anim.fnOp);
			dojo.connect(e, 'onmouseout', conf, zeag.anim.fnCl);

			dojo.connect(conf.pulseImg, 'onmouseover', conf, zeag.anim.fnOp);
			dojo.connect(conf.pulseImg, 'onmouseout', conf, zeag.anim.fnCl);
		},

		doPulse: function(trigger, increase, oSize, oTop, oLeft) {
			var anim=zeag.anim;
			var size = Math.round(oSize*anim.pulseFactor/100);
			dojo.animateProperty({ node: trigger, duration:200,
				properties: {
					width: { start: trigger.offsetWidth, end: (increase?size:oSize), unit:"px" },
					height: { start: trigger.offsetHeight, end: (increase?size:oSize), unit:"px" },
					top: { start: trigger.offsetTop, end: (increase?oTop-(size-oSize)/2:oTop), unit:"px" },
					left: { start: trigger.offsetLeft, end: (increase?oLeft-(size-oSize)/2:oLeft), unit:"px" }
				}
			}).play();
		},

		doSlide:function(trigger, open) {
			var anim=zeag.anim;
			clearInterval(anim.animId);

			var sC = Math.ceil(anim.slideDuration/anim.slideInterval);
			var p = [];

			for(var i=anim.reg.length-1;i>=0;i--) {
				var cfg=anim.reg[i];
				var cW = open ? Math.ceil((anim.slideContW-anim.slideMaxW)/(anim.reg.length-1)) : cfg.sW;
				if(cfg.slideLi!=trigger){
					var currW=cfg.slideLi.offsetWidth;
					p.push({
						e: cfg.slideLi,
						c: currW,
						s: (currW-cW)/sC,
						cW: cW
					});
				}
			}
//			anim.animId = setInterval(anim.peformSlide, anim.slideInterval, anim, trigger, open, p);
			anim.animId = setInterval(function(){anim.peformSlide(anim,trigger,open,p);}, anim.slideInterval);
		},

		peformSlide: function(anim, trigger, open, params){
			var ready=true;
			for(var i=params.length-1;i>=0;i--) {
				var p=params[i];
				p.c-=p.s;
				if(open && p.c>p.cW && p.s>0 || !open && p.c<p.cW && p.s<0){
					dojo.style(trigger, 'width', (trigger.offsetWidth+p.e.offsetWidth-Math.round(p.c))+"px");
					dojo.style(p.e, 'width', Math.round(p.c)+"px");
					ready=false;
				} else {
					dojo.style(trigger, 'width', (trigger.offsetWidth+p.e.offsetWidth-p.cW)+"px");
					dojo.style(p.e, 'width', p.cW+"px");
				}
			}
			if(ready){
				clearInterval(anim.animId);
			}
		}
	}
};

dojo.addOnLoad(function(){
	zeag.menu.init('linksCssMenu');
	zeag.contact.init('extramenu');
	zeag.anim.init('portalSlide', 'portalPulse');

	CndSuggest.prototype.handleFire=zeag.suggest.handleFire;
	CndSuggest.init(zeag.suggest.reader);

	dojo.query('input[name*="datum"]', 'form').onclick(function(evt){scwShow(evt.target,evt);});
});


