$(document).ready(function () {
	Global.init();
	//Sub.init();
});

$.postJSON = function(url, data, callback) {
	$.post(url, data, callback, "json");
};
$.formPOST = function(oForm, callback){
	$.postJSON(oForm.getAttribute('action'), $(oForm).serialize(), callback);
};

var Global = {
	
	init: function(){
		this.Pseudo.init();
		PlaceHolder.init();
	}
	, Pseudo: {
		init: function(oElement){
			var pseudo = !oElement ? $('.pseudo') : $('.pseudo', oElement);
			$(pseudo).mouseover(function(){
				$(this).addClass('pseudo_hover');
			});
			$(pseudo).mouseout(function(){
				$(this).removeClass('pseudo_hover');
			});
		}
	}
	
};


var PlaceHolder = {
	init: function(){
		this.ClassEmpty = 'empty';
		this.setParams($('input[title]'));
		this.setParams($('textarea[title]'));
		this.isInit = true;
	}
	, setParams: function(aEl){
		var me = this;
		if(!aEl || !aEl.length)
			return;
		for(var i = 0, l = aEl.length; i < l; i++){
			aEl[i].d_value = aEl[i].title;
			aEl[i].d_type = aEl[i].type;
			aEl[i].d_name = aEl[i].name;
			aEl[i].e_class = this.ClassEmpty;
			aEl[i].SaveOriginal = (aEl[i].value == aEl[i].title);
			
			$(aEl[i]).focus(function(){ return me.focus(this) });
			$(aEl[i]).blur(function(){ return me.blur(this) });
			$(aEl[i]).keydown(function(){ return me.keydown(this) });
			if(!aEl[i].value)
				this.blur(aEl[i]);
		}
	}
	, focus: function(oEl){
		if (!oEl.SaveOriginal && oEl.d_value == oEl.value){
			oEl.value = '';
			oEl.name = oEl.d_name;
			if(oEl.type != 'textarea' && !$.browser.msie){
				oEl.focus();
			}
		} else {
			this.keydown(oEl);
		}
	}
	, keydown: function(oEl){
		$(oEl).removeClass(this.ClassEmpty);
	}
	, blur: function(oEl){
		var me = this;
		if (oEl.value == '' || oEl.d_value == oEl.value){
			if(oEl.type == 'password' && !oEl.value){
				if($.browser.msie){
					var el = document.createElement('<input type="text" name="name">');
				} else {
					var el = document.createElement('input');
					el.type = 'text';
					el.name = 'name';
				}
				var parent = oEl;
				parent.name = '';
				el.value = parent.d_value;
				el.d_value = parent.d_value;
				el.d_name = parent.d_name;
				el.className = this.ClassEmpty;
				el.parent = parent;
				var evtFocus = function(){
					parent.style.display = '';
					parent.name = this.d_name;
					this.style.display = 'none';
					parent.parentNode.removeChild(this);
					parent.focus();
				};
				$(el).focus(evtFocus);
				oEl.parentNode.insertBefore(el, parent);
				parent.style.display = 'none';
			} else {
				if(oEl.type != 'textarea' && oEl.type == 'password')
					oEl.type = 'text';
				oEl.value = oEl.d_value;
				oEl.name = '';
				$(oEl).addClass(this.ClassEmpty);
			}
		} else {
			this.keydown(oEl);
		}
	}
};

var Sub = {
	init: function(){
		this.oForm = $('form#subscribe');
		if(!this.oForm)
			return false;
		
		this.email = $('input.email', this.oForm);
		this.submit = $('input[type=submit]', this.oForm);
		if(!this.email.length){
			this.submit.disabled = true;
		}
		var me = this;
		var email_change = function(){ me.check_email(); };
		$(this.email).keyup(email_change).change(email_change).focus(email_change).blur(email_change);
		this.bSubmit = false;
		email_change();
	}
	, check_email: function(){
		var regexp = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
		if(!regexp.test(this.email[0].value)){
			this.submit[0].disabled = true;
			return false;
		}
		this.submit[0].disabled = false;
		return true;
	}
	, sub: function(oForm){
		var me = this;
		
		if(!this.check_email())
			return false;
		
		this.submit[0].disabled = this.bSubmit = true;
		$.formPOST(oForm, function(data){
			try {
				/*me.submit[0].disabled = me.bSubmit = false;
				$(me.oForm).html(response.r);*/
			} catch (e) {
				//me.submit[0].disabled = me.bSubmit = false;
			}
		});
		setTimeout(function(){
			$(me.oForm).hide();
			$('#ok').fadeIn(300);
		}, 500);
		return false;
	}
};
