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

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

/*
	AJAX
*/
$.getHTML = function(url, data, callback) {
	$.post(url, data, callback, "html");
};

var Global = {
	
	init: function(){
		this.Pseudo.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');
			});
		}
	}
	/* events */
	, E: {
		change: function(o,e){
			$(o).keyup(e).mouseup(e).blur(e).change(e);
		}
	}
	/* sets */
	, S: {
		url: function(s){
			return encodeURIComponent(s);
		}
	}
	, Check: {
		email: function(email){
			var regexp = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
			//var regexp = /^[\w-](\.?[\w-])*(\+?\w*)?@[\w-](\.?[\w-])*\.[a-z]{2,}$/i;
			if(!$.trim(email).length)
				return false;
			
			if(!regexp.test(email))
				return false;
			
			return true;
		}
		, login: function(login){
			var regexp = /^[a-z0-9_]{3,25}$/i;
			
			if(!regexp.test(login))
				return false;
			
			return true;
		}
		, password: function(pass1){
			if(!pass1.length || pass1.length < 3)
				return false;
			
			return true;
		}
		, password_confirm: function(pass1, pass2){
			if(!this.password(pass1))
				return false;
			
			if(pass1 != pass2)
				return false;
			
			return true;
		}
		, captcha: function(s){
			var regexp = /^[0-9]{4}$/i;
			if(!regexp.test(s))
				return false;
			
			return true;
		}
		, dateSyntax: function(d,m,y){
			
			var change = function(){
				this.value = $(this).val().replace(/[^\d]+/,'');
			};
			Global.E.change($(d), function(){
				this.value = $(this).val().replace(/[^\d]+/,'');
				if(this.value > 31)
					this.value = 31;
			});
			Global.E.change($(m), function(){
				this.value = $(this).val().replace(/[^\d]+/,'');
				if(this.value > 12)
					this.value = 12;
			});
			Global.E.change($(y), function(){
				this.value = $(this).val().replace(/[^\d]+/,'');
				if(this.value > 1996)
					this.value = 1996;
				if(this.value.length > 3 && this.value < 1900)
					this.value = 1900;
			});
		}
	}
	, URL: {
		reload: function(s){
			window.location = window.location.href.replace( /#.*/, "") + s;
		}
	}
};

var Basket = {
	init: function(){
		this.$Content = $('#basket_info');
	}
	, order: function(article_id, event){
		$.postJSON('/json/', 'request=basket&faction=order&article_id='+article_id+'&', function(response){
			event(response);
		});
	}
	, update: function(){
		var me = this;
		$.getHTML('/ajax/','request=basket&faction=update', function(html){
			me.$Content.html(html);
			//event(response);
		});
	}
};
