$(function(){
/* small gallery */
	el = {
		0 : -5,
		1 : -10,
		2 : -15,
		3 : -25,
		4 : -30,
		5 : -35,
		6 : -40
	}
	$(".sm_gal .one_el .inner").hover(
		function(){
			$(this).css({zIndex:1}).stop().animate({width:232, height:194, top:-18, left:el[$(this).parent().index()]}, 200);
			$("span", this).stop().animate({top:10}, 200);
		},
		function(){

			$(this).css({zIndex:0}).stop().animate({width:176, height:145, top:0, left:0}, 200);
			$("span", this).stop().animate({top:3}, 200);
		}
	);
/* header navigation */
	$(".nav ul > li").hover(
		function(){
			a = $("ul", this).length ? true : false;
			if(a){
				$("ul", this).show();
				$("ul li", this).animate({top:0}, 300);
				$("> a", this).addClass("hover");
			}
		},
		function(){
			a = $("ul", this).length ? true : false;
			if(a){
				_this = this;
				$("> a", this).removeClass("hover");
				$("ul li", this).animate({top:-31}, 300, function(){
					$("ul", _this).hide();
				});
			}
		}
	);
	setInterval(function(){
		$("div.header div.imgs img:last-child").fadeOut(1000, function(){
			$(this).prependTo("div.header div.imgs").show();
		});
	}, 5000);
});

function proccessForm(element, event_name) {
	var errors = '';
	data = new Array;
	$('#' + element + ' .formz').each(function() {
		data.push(new Array($(this).attr('label'), $(this).attr('value')));
		
		if ($(this).attr('value') == '' && $(this).attr('validation') == 'not_empty') {
			 errors += $(this).attr('error') + '\n';
		}
		if ($(this).attr('validation') == 'mail') {
			var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			if(reg.test($(this).attr('value')) == false) {
				errors += $(this).attr('error') + '\n';
			}
		}
		if ($(this).attr('validation') == 'phone') {
			 var reg = /^(\+)?([0-9]{1,2})?( )?([0-9]{10})$/;
			if(reg.test($(this).attr('value')) == false) {
				errors += $(this).attr('error') + '\n';
			}
		}
	});
	if (typeof(event_name) == 'undefined') {
		theme = $('#' + element + ' .theme').html();
	} else {
		theme = event_name;
	}
	if (errors.length > 0) {
		alert(errors);
		return false;
	}
	data = $.toJSON(data);
	$.ajax({
		type: "POST",
		url: "/content/contacts/",
		data: "data=" + data + "&theme=" + theme + "&send=true",
		success: function(msg){
			alert($('#' + element + ' .success').html());
			clear_form($('#' + element));
		}
	});
	return true;
}

function clear_form(form) {
    $(form).find(':input').each(function() {
        switch(this.type) {
            case 'password':
            case 'select-multiple':
            case 'select-one':
            case 'text':
            case 'textarea':
                $(this).val('');
                break;
            case 'checkbox':
            case 'radio':
                this.checked = false;
        }
    });

}

function auth() {
	$.ajax({
	type: "POST",
	url: "/content/auth/",
	data: "login=" + $('#login')[0].value + "&password=" + $('#password')[0].value,
	success: function(msg){
		if (msg != false) {
			if (msg == 'success') {
				window.location = '/content/cabinet/'; 
			} else {
				window.alert(msg);
			}
			return false;
		}
	}
	});
	return false;
}

function registerMe() {
	$.ajax({
		type: "POST",
		url: "/content/new_user/",
		data: "login=" + $('#reg_login')[0].value + "&fio=" + $('#reg_fio')[0].value + "&password=" + $('#reg_password')[0].value + "&password2=" + $('#reg_password2')[0].value + "&email=" + $('#reg_email')[0].value + "&phone=" + $('#reg_phone')[0].value,
		success: function(msg){
			if (msg.length <= 3) {
				window.alert('Поздравляем с успешной регистрацией!');
				window.location = '/content/cabinet/';
			} else {
				window.alert(msg);
			}
		}
	});
	return false;
}
