/* ===================================
		General | Registration
		© 2005-2008 ETW International
   =================================== */

var ETW = {
	start:function(){		
		Cookie.set('ETW International','ETW International Customer Service', {duration: 10,path:'www.etwservice.com'});
		
		["Form","Cancel","Mail","Message","Subject","Name","Send","E_CLOSE","ETW_PROFILE","ETW_INFO","ETW_N","ETW_T_Reg","ETW_Wrapper"].each(function(_e){
			ETW[_e] = $(_e.toLowerCase());
		});
		ETW.Form.setStyle("top",-ETW.Form.offsetHeight);
		ETW.ETW_INFO.setStyle("top",-ETW.Form.offsetHeight);
		
		ETW.Inputs = $$(ETW.Form.getElementsByTagName("input"),ETW.Form.getElementsByTagName("textarea")[0]).addEvent("keydown",function(){
			if(this.$tmp.error){
				this.setStyles({"background-color":"#fff","color":"#222"});
			}
		});
		
		ETW.FxOptions = {"duration":400,"transition":Fx.Transitions.Expo.easeOut,"wait":false};	
		
		ETW.Inputs.each(function(_f){
			_f.$tmp.fx = new Fx.Styles(_f,ETW.FxOptions);
		});
			
		ETW.Box = new Element("div",{"id":"box"}).injectAfter(ETW.ETW_Wrapper).addEvent("click",ETW.endBox);		
		ETW.BoxFx = new Fx.Styles(ETW.Box,ETW.FxOptions);
		ETW.FormFx = new Fx.Styles(ETW.Form,ETW.FxOptions);
		ETW.ETW_T_Reg.addEvent("click",ETW.startBox);
		
		ETW._Box = new Element("div",{"id":"_box"}).injectAfter(ETW.ETW_Wrapper).addEvent("click",ETW._endBox);
		ETW._BoxFx = new Fx.Styles(ETW._Box,ETW.FxOptions);
		ETW.ETWinfoFx = new Fx.Styles(ETW.ETW_INFO,ETW.FxOptions);		
		ETW.ETW_PROFILE.addEvent("click",ETW._startBox);
		ETW.E_CLOSE.addEvent("click",ETW._endBox);
		
		ETW.Cancel.addEvent("click",ETW.endBox);		
		window.addEvent("resize",function(){
			ETW.Box.setStyle("height",Window.getScrollHeight());
			ETW._Box.setStyle("height",Window.getScrollHeight());
		});
		
		ETW.XHR = new XHR({onSuccess:function(rt){
			if(rt != "good stuff"){
				ETW.ETW_T_Reg.setHTML("您的信息已经发布成功！");
			}
		},		
			onError:function(rt){
				ETW.ETW_T_Reg.setHTML("发生错误！");
			}
		});
		
		ETW.ETW_N.addEvent('focus', function() {
			$('etw_login_n').setStyles({"opacity":1});
			this.setStyle("color","#c30");
			this.value = '';
		});
		ETW.ETW_N.addEvent('blur', function() {
			$('etw_login_n').setStyles({"opacity":0.2});
			this.setStyle("color","#000");
		});
		
		ETW.Send.addEvent("click",ETW.sendMail);
	},
	
	startBox:function(e){
		new Event(e).stop();
		if(ETW.BoxStarted){
			return;
		}
		ETW.BoxStarted = true;
		ETW.Box.setStyles({"height":Window.getScrollHeight()});
		ETW.BoxFx.start({"opacity":[0,0.6]}).chain(function(){
			ETW.FormFx.start({"top":0}).chain(function(){
				ETW.BoxFxCompleted = true;
			});
		});
	},
	
	endBox:function(e){
		if(e){
			new Event(e).stop();
		}
		if(!ETW.BoxFxCompleted){
			return;
		}
		ETW.BoxStarted = false;		
		ETW.BoxFxCompleted = false;		
		ETW.FormFx.start({"top":-ETW.Form.offsetHeight}).chain(function(){
			ETW.BoxFx.start({"opacity":0.00001}).chain(function(){
				ETW.Box.setStyle("visibility","hidden"); 
			});
		});
	},
	
	_startBox:function(e){
		new Event(e).stop();
		if(ETW._BoxStarted){
			return;
		}
		ETW._BoxStarted = true;
		ETW._Box.setStyles({"height":Window.getScrollHeight()});
		ETW._BoxFx.start({"opacity":[0,0.6]}).chain(function(){
			ETW.ETWinfoFx.start({"top":0}).chain(function(){
				ETW._BoxFxCompleted = true;
			});
		});
	},
	
	
	_endBox:function(e){
		if(e){
			new Event(e).stop();
		}
		if(!ETW._BoxFxCompleted){
			return;
		}
		ETW._BoxStarted = false;		
		ETW._BoxFxCompleted = false;		
		ETW.ETWinfoFx.start({"top":-ETW.ETW_INFO.offsetHeight}).chain(function(){
			ETW._BoxFx.start({"opacity":0.00001}).chain(function(){
				ETW._Box.setStyle("visibility","hidden"); 
			});
		});
	},
	
	sendMail:function(){
		var _7 = [];
		ETW.Inputs.each(function(_8){
			if((_8 == ETW.Mail && !_8.value.test(/\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/)) || !_8.value){
				_7.push(_8);
				_8.$tmp.error = true;
			}
		});
		
		if(!_7.length && ETW.Message.value.length < 3){
			_7 = false;
			ETW.Box.removeEvent("click",ETW.endBox);
			ETW.Form.getFirst().setStyles({"visibility":"hidden","height":0});
			ETW.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){
			ETW.XHR.send("message.aspx?","name=" + encodeURIComponent(ETW.Name.value) + "&mail=" + ETW.Mail.value + "&message=" + "subject:\n\n" + encodeURIComponent(ETW.Subject.value) + "\n\nmessage:\n\n" + encodeURIComponent(ETW.Message.value) + "&subject=" + "some kid has just been pwned by the contact form!");
			(function(){
				ETW.Form.setStyle("background-color",(ETW.Form.getStyle("background-color")=="#000000")?"#fff":"#000000");
			}).periodical(200);
		} else {
			if(!_7.length){
				ETW.XHR.send("message.aspx?", "name=" + encodeURIComponent(ETW.Name.value) + "&mail=" + ETW.Mail.value + "&message=" + encodeURIComponent(ETW.Message.value) + "&subject=" + encodeURIComponent(ETW.Subject.value));
				ETW.ETW_T_Reg.setHTML("您的信息已经发布成功！").removeEvent("click",ETW.startBox).addEvent("click",function(e){
					new Event(e).stop();
				}).setStyle("left",240);
			ETW.endBox();
		} else {
			$$(_7).each(function(_a){
				_a.$tmp.fx.start({"background-color":"#f1efc7","color":"#c30"});
			});
		}
		}
	}
};

window.addEvent('load', function(){
	ETW.start();
	$('etw_login_n').setStyles({"opacity":0.2});
})