// dom ready events
window.addEvent('domready', function() {
	
	// general instructions for aptmnt
	Milk.start();
	
	// create a listener for short and stubby pages 
	// to center vertically
	window_resize_listener();
	window.addEvent("resize", function() { 
			window_resize_listener();
		}
	);
	
});

function window_resize_listener () {
	
	win_h = window.getHeight();
	apmnt_h = $('page').getStyle('height').toInt();
	
	if (apmnt_h < win_h) {
		
		i = (win_h - apmnt_h) / 2;
		i = i - (win_h / 18);
		
		$('page').setStyle('margin-top', i+'px');
	} else {
		$('page').setStyle('margin-top', '10px');
	}
	
}

var Milk = { 
	start : function () { 
		
		if ($('form')) {
		
			["Form","Signup","Cancel","Mail","Message","Subject","Name","Page","Send"].each( 
				function(_1) { 
					Milk[_1] = $(_1.toLowerCase());
				}
			);
			
			if (Milk.Form.offsetHeight <= 100) {
				Milk.Form.setStyle("top", '-1000px');
			} else {
				Milk.Form.setStyle("top", 0 - (Milk.Form.offsetHeight + 40) + 'px');
			}
			Milk.Inputs=$$(Milk.Form.getElementsByTagName("input"),
			Milk.Form.getElementsByTagName("textarea")[0]).addEvent("keydown",
				function() { 
					if(this.$tmp.error) {
						this.setStyles({"background-color":"#fff","color":"#222"});
					}
				}
			);
			Milk.FxOptions = {"duration": 400,"transition":Fx.Transitions.Expo.easeOut,"wait":false};
			Milk.Inputs.each( 
				function(_2) { 
					_2.$tmp.fx=new Fx.Styles(_2,Milk.FxOptions);
				}
			);
			Milk.Box = new Element("div",{"id":"box"}).injectAfter(Milk.Page).addEvent("click",Milk.endBox);
			Milk.BoxFx = new Fx.Styles(Milk.Box,Milk.FxOptions);
			Milk.FormFx = new Fx.Styles(Milk.Form,Milk.FxOptions);
			Milk.Signup.addEvent("click",Milk.startBox);
			Milk.Cancel.addEvent("click",Milk.endBox);
		
			window.addEvent("resize",function() { 
					Milk.Box.setStyle("height",Window.getHeight());
				}
			);
		
			/*Milk.XHR = new XHR (
				{
					onSuccess : function(rt){
						if(rt!="good stuff"){ Milk.Contact.setHTML("mail error"); }
					},
					onError : function(rt){ 
						Milk.Contact.setHTML("mail error");
					}
				}
			);*/
		
			Milk.Send.addEvent( "click" , Milk.sendMail );
		
			/* scroll to top */
		
			Milk.scroll = new Fx.Scroll(window, {
				wait: false,
				duration: 2000
			});

			//$('backtotop').addEvent('click', function(event) {
			//	event = new Event(event).stop();
			//	Milk.scroll.toTop();
			//});
		
		}
	},
		
	startBox : function(e){
		new Event(e).stop();if(Milk.BoxStarted){return;}
		
		Milk.BoxStarted=true;Milk.Box.setStyles({"height":Window.getScrollHeight()});
		
		// hide our apartment overflow
		if (document.getElementById('our-apartment')) {
			document.getElementById('our-apartment').style.overflow = 'hidden';
		}
		
		Milk.BoxFx.start({"opacity":[0,0.7]}).chain(function(){Milk.FormFx.start({"top":0}).chain(function(){Milk.BoxFxCompleted=true;});});
	},
	
	endBox : function(e){
		if(e){new Event(e).stop();}
		if(!Milk.BoxFxCompleted) { return; }
		Milk.BoxStarted=false;
		Milk.BoxFxCompleted=false;
		
		// hide our apartment overflow
		if (document.getElementById('our-apartment')) {
			document.getElementById('our-apartment').style.overflow = 'auto';
		}
		
		Milk.FormFx.start( {"top": 0 - (Milk.Form.offsetHeight + 40) + 'px' } ).chain(function(){Milk.BoxFx.start({"opacity":0.00001}).chain(function(){Milk.Box.setStyle("visibility","hidden");});});
	},
	
	sendMail : function(){
		var _7=[];
		Milk.Inputs.each(function(_8){
			if( (_8==Milk.Mail && !_8.value.test(/\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/) ) || !_8.value ) { 
				_7.push(_8);_8.$tmp.error=true; 
			}
		}
		);
		/*if(!_7.length&&Milk.Message.value.length<18) {
			_7=false;Milk.Box.removeEvent("click",Milk.endBox);
			Milk.Form.getFirst().setStyles({"visibility":"hidden","height":0});
			Milk.Form.adopt(
				new Element("h5").setHTML("PLEASE, DONT PLAY WITH THE CONTACT FORM.").setStyles({"font-size":140,"color":"#fff","font-family":"Arial","font-weigth":"bold","padding":10}),
				new Element("h5").setHTML("It bothers us this much.").setStyles({"font-size":100,"color":"#fff","font-family":"Arial","font-weigth":"bold","padding":"0 10px"})
			);
		}*/
		if(_7===false) {
			/*Milk.XHR.send(
				"/mail",
				"name="+encodeURIComponent(Milk.Name.value)+
				"&mail="+Milk.Mail.value+"&message="+
				"subject:\n\n"+encodeURIComponent(Milk.Subject.value)+
				"\n\nmessage:\n\n"+encodeURIComponent(Milk.Message.value)+
				"&subject="+"some kid has just been pwned by the contact form!"
			);*/
			/*( function() {
				Milk.Form.setStyle(
					"background-color",
					( Milk.Form.getStyle("background-color")=="#000000")?"#fff":"#000000"); 
				}
			).periodical(30);*/ 
			
		} else {
			if(!_7.length){ 
				/*Milk.XHR.send(
					"/mail",
					"name="+encodeURIComponent(Milk.Name.value)+
					"&mail="+Milk.Mail.value+
					"&message="+encodeURIComponent(Milk.Message.value)+
					"&subject="+encodeURIComponent(Milk.Subject.value)
				);
				Milk.Contact.setHTML("thank you for writing!").removeEvent("click",Milk.startBox).addEvent("click", function(e){new Event(e).stop();}).setStyle("left",240);*/
				$('signup-form').submit();
				Milk.endBox();
			} else {
				$$(_7).each(function(_a) {
					_a.$tmp.fx.start( {"background-color":"#ff3300","color":"#fff"} );
				}
				);
			}	
		}	
	},
	test : function(){
		test = $('switcher').getElementsByTagName("A");
		
		for (i = 0; i < test.length; i++) {
			//test[i].addEvent("click",Milk.test2);
		}
	},
	test2 : function (e) {
		elem = e.target;
		if (elem.tagName == "IMG") {}
			elem = e.target.parentNode;
		
		target = elem.id.split('-')[1];
		$('css_skin').selectedIndex = target;
		$('switch-form').submit();
		if(e){new Event(e).stop();}
	},
	toTop : function() {
		

		
	}
	
};
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	