document.observe('dom:loaded', function(){
	$$('#menu>ul>li').each(function(e){
		e.observe('mouseover', function(){
			$$('#menu>ul>li').each(function(e){
				clearTimeout(e.menutimeout);
				if (e != this) e.removeClassName('hover');
				else e.addClassName('hover');
			}.bind(this));
		});
		e.observe('mouseout', function(){
			this.menutimeout=setTimeout(function(){this.removeClassName('hover');}.bind(this), 500);
		});
	});

	$$('#menu>ul>li li').each(function(e){
		e.observe('mouseover', function(){
			this.addClassName('hover');
		});
		e.observe('mouseout', function(){
			this.removeClassName('hover');
		});
	});

	$$('.special').each(function(e){
		if (e.hasClassName('flap'))
		{
			var c=e.childNodes;

			for (var a=0; a < c.length; a++)
			{
				Element.extend(c[a]);
				if (c[a].nodeType == 1 && c[a].hasClassName('header')) var h=c[a];
				if (c[a].nodeType == 1 && c[a].hasClassName('content')) var c=c[a];
			}
			if (h && c)
			{
				h.setStyle({cursor:'pointer'});
				c.flap=new specialflap(c);
				h.observe('click', function(){c.flap.toggle();}.bind(c));
			}
		}
		e.insertBefore(new Element('div', {'class':'bottom'}), e.firstChild);
	});

	$$('.contentbox').each(function(e){
		new contentbox(e);
	});

	if (swgallery)
		$$('.gallery').each(function(g){
			var i=g.select('img');
			if (i.length <= 1) return;
			var x=new swgallery(i,{
				/*style:[
					{left:'690px'},
					{left:'0px'},
					{left:'-690px'}
				]*/
				duration2next:700
			});

			/*var i=7000;
			var tf=function(){
				x.next();
			};
			var t=setInterval(tf, i);

			g.observe('mouseenter', function(){
				clearInterval(t);
			});

			g.observe('mouseleave', function(){
				t=setInterval(tf, i);
			});*/

			var prev=new Element('div', {style:'position:absolute; top:0; left:0; width:50%; height:100%; cursor:pointer; z-index:2; '});
			prev.addClassName('prev');
			prev.observe('click', function() { x.prev(); });
			prev.observe('mousedown', function(e) { e.stop(); });
			var prevarrow=new Element('div');
			prev.appendChild(prevarrow);
			g.appendChild(prev);
			prevarrow.setStyle({opacity:0});
			prev.observe('mouseenter',function(){
				prevarrow.tween({opacity:0.7});
			});
			prev.observe('mouseleave',function(){
				prevarrow.tween({opacity:0});
			});

			var next=new Element('div', {style:'position:absolute; top:0; right:0; width:50%; height:100%; cursor:pointer; z-index:2; '});
			next.addClassName('next');
			next.observe('click', function() { x.next(); });
			next.observe('mousedown', function(e) { e.stop(); });
			var nextarrow=new Element('div');
			next.appendChild(nextarrow);
			g.appendChild(next);
			nextarrow.setStyle({opacity:0});
			next.observe('mouseenter',function(){
				nextarrow.tween({opacity:0.7});
			});
			next.observe('mouseleave',function(){
				nextarrow.tween({opacity:0});
			});
		});
});

Event.observe(window, 'load', function() {
	preload_images();
});

var preload_images=function() {
	var i,images=[
		['menu_grafik_1.png'],
		['menu_kontakt_1.png'],
		['menu_multimedia_1.png'],
		['menu_ueberuns_1.png'],
		['menu_web_1.png'],
		['drop_off.png'],
		['drop_bottom_off.png'],
		['drop_on.png'],
		['drop_bottom_on.png']
	];

	for(var i=0; i<images.length; i++)
	{
		images[i][1]=new Image;
		images[i][1].src='css/img/'+images[i][0];
	}
}



var contentbox=Class.create({
	initialize:function(obj){
		if (!obj.hasClassName('contentbox')) obj.addClassName('contentbox');
		var b=new Element('div', {'class':'body'});
		while(obj.hasChildNodes()) b.appendChild(obj.firstChild);

		obj.appendChild(new Element('div', {'class':'top'}));
		obj.appendChild(b);
		obj.appendChild(new Element('div', {'class':'bottom'}));
	}
});




var specialflap=Class.create({
	initialize:function(obj){
		this.obj=obj;

		// create flap container
		this.fc=new Element('div', {style:'overflow:hidden; '});
		this.obj.parentNode.insertBefore(this.fc, this.obj)
		if (this.obj.hasClassName('hidden'))
		{
			this.fc.setStyle({height:0});
			this.obj.setStyle({display:'block'});
		}
		this.fc.appendChild(this.obj);

		this.motion=new motion([this.fc], {duration:300});
	},

	toggle:function(){
		if (this.fc.getHeight() == 0)
			this.open();
		else
			this.close();
	},

	open:function(){
		this.motion.start([{height:this.obj.getHeight()+'px'}]);
	},

	close:function(){
		this.motion.start([{height:'0px'}]);
	}
});





var callback={
	send:function(e){

		// validate values
		var fields=Form.serialize(e, true);
		if (fields.name == '') { alert('Bitte geben Sie Ihren Namen an.'); return; }
		if (fields.phone == '') { alert('Bitte geben Sie Ihre Rufnummer an.'); return; }

		// send request
		new Ajax.Request('?mm=site_sternwerk:callback', {
			method:'post',
			parameters:fields,
			postBody:Object.toQueryString(fields),
			onSuccess:function(transport){
				eval('var r='+transport.responseText+';');
				//alert(transport.responseText);
				var f=this.parentNode.flap;
				this.parentNode.update(r.output);
				f.open();
				setTimeout(function(){this.close();}.bind(f), 5000);
			}.bind(e)
		});
	}/*,

	timechange:function(e){
		var day=e.select('.day select')[0];
		var time=e.select('.time select')[0];

		if (day.getValue() == 'heute')
		{
			//for ()

			alert(time.options);


			//alert(time.getValue().match(/^[0-9]{2}/));

			var d=new Date();
			var x=(d.getHours() < 10 ? '0' : '')+d.getHours()+':00 - '+((d.getHours()+1) < 10 ? '0' : '')+(d.getHours()+1)+':00';
			alert(x);
		}

		//alert(Form.getElements(e));
	}
*/
};






var contact={
	send:function(e){

		// validate values
		var fields=Form.serialize(e, true);
		if (fields.name == '') { alert('Bitte geben Sie Ihren Namen an.'); return; }
		if (fields.mail == '') { alert('Bitte geben Sie Ihre E-Mail-Adresse an.'); return; }
		if (fields.message == '') { alert('Bitte geben Sie eine Nachricht ein.'); return; }

		// send request
		new Ajax.Request('?mm=site_sternwerk:contact', {
			method:'post',
			parameters:fields,
			postBody:Object.toQueryString(fields),
			onSuccess:function(transport){
				eval('var r='+transport.responseText+';');
				var p=this.parentNode;
				var h=p.getHeight();
				p.setStyle({height:h+'px'});
				p.motion=new motion([p]);
				var n=new Element('div').update(r.output);
				p.update(n);
				p.motion.start([{height:n.getHeight()+'px'}]);
			}.bind(e)
		});
	}
};





var setclickableteaser=function(obj) {
	if (!obj) obj=document.body;
	Element.extend(obj);
	(obj.select('.item')).each(function(area) {
		var mehr = area.select('.more')[0];
		if (mehr)
			setclickablearea(area, mehr);
	});
};

var setclickablearea=function(area, alias) {
	area.setStyle({cursor: 'pointer'});
	area.observe('click', function(){window.location = alias.href;});
	area.select('a,object').each(function(b){
		b.observe('click', function (e) {
			window.location = this.href;
			e.stop();
		}.bindAsEventListener(b));
	});
};

document.observe('dom:loaded', function(){setclickableteaser();});

