$.fn.extend({
	login: function () {
		if (this) {
			$(this).submit(function(){
				result = true;
				if (!$(this).find('input[name=login]').validatorMail()){result = false;}
				if($('#loginform').attr('action') == '/plugins/ajax/login.php'){
					if (!$(this).find('input[name=password]').validatorLength(5,20)){result = false;} 
				}
				return result;
			});
			
			$(this).find('#remember-password-show a').click(function(){
				$('#loginform input.i-p').removeClass('error');
				$('#remember-password-show, #login-password, #login-button').slideUp(200);
				$('#remember-password-close, #remember-button').slideDown(200);
				$('#loginform').attr('action', '/plugins/ajax/forgot.php')
				
				return false;
			});
			
			$(this).find('#remember-password-close a').click(function(){
				$('#loginform input.i-p').removeClass('error');
				$('#remember-password-close, #remember-button, #remember-password-message').slideUp(200);
				$('#remember-password-show, #login-password, #login-button').slideDown(200);
				$('#loginform').attr('action', '/plugins/ajax/login.php')
				
				return false;
			});
		}
	},
	registration: function () {
		if (this) {
			$(this).submit(function(){
				result = true;
				
				if (!$(this).find('input[name=registration_'+'first_name]').validatorLength(2,100)){result = false;} 
				if (!$(this).find('input[name=registration_'+'last_name]').validatorLength(2,100)){result = false;} 
				if (!$(this).find('input[name=registration_'+'email]').validatorMail()){result = false;}
				if (!$(this).find('input[name=registration_'+'password]').validatorLength(5,20)){result = false;} 
				if($(this).attr('checked')){
					if (!$(this).find('input[name=registration_'+'vk_id]').validatorPhone()){result = false;} 
				}
				if(!result)
				{
					$('#registration_form_error').html('Пожалуйста, заполните все поля!').show();
				}
				else
				{
					$('#registration_form_error').html('').hide();
				}
				
				return result;
			});
			
			$('#registration_district').load('data/registration_district/'+$('#registration_district').attr('rel')+'/tpl-geo_regions/',function(){
				$('#registration_district select').change(function(){
					$('#registration_city').load('data/registration_city/'+$('#registration_district select').val()+'/'+$('#registration_city').attr('rel')+'/tpl-geo_cities/');
				});
				$('#registration_district select').change();
			});
			
			$(this).find('input[name=registration_'+'vk]').change(function(){
				if($(this).attr('checked')){
					$("#vkid").show();
				}else{
					$("#vkid").hide();
				}
			});
			
			if($(this).attr('checked')){
				$("#vkid").show();
			}
		}
	},
	addcode: function () {
		if (this) {
			$(this).submit(function(){
				result = true;
				
				if (!$(this).find('input[name=addcode_'+'code]').validatorLength(2,100)){result = false;} 
				
				if(!result)
				{
					$('#addcode_form_error').html('Пожалуйста, введите код!').show();
				}
				else
				{
					$('#addcode_form_error').html('').hide();
				}
				
				return result;
			});
		}
	},
	profile: function () {
		if (this) {
			$(this).submit(function(){
				result = true;
				
				errortext = 'Пожалуйста, заполните все поля!';
				
				if (!$(this).find('input[name=profile_'+'first_name]').validatorLength(2,100)){result = false;} 
				if (!$(this).find('input[name=profile_'+'last_name]').validatorLength(2,100)){result = false;} 
				if(result && $(this).find('input[name=profile_'+'old_password]').val().length > 0){
					if (!$(this).find('input[name=profile_'+'old_password]').validatorLength(5,20)){result = false;} 
					if (!$(this).find('input[name=profile_'+'new_password]').validatorLength(5,20)){result = false;} 
					if (!$(this).find('input[name=profile_'+'new_password2]').validatorLength(5,20)){result = false;} 
					if(!result){
						errortext = 'Пожалуйста, заполните все поля для смены пароля!';
					}else if($(this).find('input[name=profile_'+'new_password2]').val() != $(this).find('input[name=profile_'+'new_password]').val()){
						result = false;
						errortext = 'Пароль и его повторение не верны!';
					}
				}
				
				if(!result)
				{
					$('#profile_form_error').html(errortext).show();
				}
				else
				{
					$('#profile_form_error').html('').hide();
				}
				
				return result;
			});
		}
	}
});

$(function(){
	if($('.wallpaper-show-wallpaper ul a').length){
		$('.wallpaper-show-wallpaper ul a').click(function(){
			_gaq.push(['_trackEvent', 'Gifts', 'Download', 'Wallpapers']);
		});
	}
	if($('.wallpaper-show-screensaver .pp-screensaver a').length){
		$('.wallpaper-show-screensaver .pp-screensaver a').click(function(){
			_gaq.push(['_trackEvent', 'Gifts', 'Download', 'Screensaver']);
		});
	}
	if($('.wallpaper-show-widget .pp-widget a').length){
		$('.wallpaper-show-widget .pp-widget a').click(function(){
			_gaq.push(['_trackEvent', 'Gifts', 'Download', 'Widget']);
		});
	}
	
	if($('.b-topregcode form').length) {
		$('.topregcode_open_link').click(function(){
			$(this).parent().toggleClass('active-topregcode');
			
			if($(this).parent().hasClass('active-topregcode')){
				window.scroll(0,0);
			}else{
				$.historyLoad('close-login');
			}
			
			return false;
		});
		$('.topregcode-extend .i-p').toggleVal({
			populateFrom: 'title'
		});
	}
	
	if($('a.wallpapers-show').length) {
		$('a.wallpapers-show').click(function(){
			$('.wallpaper-show-wallpaper').css({top:$(this).offset().top-120, left:$(this).offset().left-180}).fadeIn(400);
			return false;
		});
	}
	if($('.wallpaper-show-wallpaper a.close').length) {
		$('.wallpaper-show-wallpaper a.close').click(function(){
			$('.wallpaper-show-wallpaper').fadeOut(400);
			return false;
		});
	}
	if($('a.screensaver-show').length) {
		$('a.screensaver-show').click(function(){
			$('.wallpaper-show-screensaver').css({top:$(this).offset().top-120, left:$(this).offset().left-90}).fadeIn(400);
			return false;
		});
	}
	if($('.wallpaper-show-screensaver a.close').length) {
		$('.wallpaper-show-screensaver a.close').click(function(){
			$('.wallpaper-show-screensaver').fadeOut(400);
			return false;
		});
	}
	if($('a.widget-show').length) {
		$('a.widget-show').click(function(){
			$('.wallpaper-show-widget').css({top:$(this).offset().top-120, left:$(this).offset().left-180}).fadeIn(400);
			return false;
		});
	}
	if($('.wallpaper-show-widget a.close').length) {
		$('.wallpaper-show-widget a.close').click(function(){
			$('.wallpaper-show-widget').fadeOut(400);
			return false;
		});
	}
	
	if($('.pp-msg a.close').length) {
		$('.pp-msg a.close').click(function(){
			$('.pp-msg').fadeOut(400);
			$('.overlay-msg').fadeOut(400);
			return false;
		});
		$('.overlay-msg').click(function(){
			$('.pp-msg').fadeOut(400);
			$('.overlay-msg').fadeOut(400);
			return false;
		});
	}
	
	if($('.b-pmdetails').length) {
		$('.b-pmdetails h3 a').click(function(){
			$('.b-pmdetails').removeClass('pmdetails-edit-active').toggleClass('pmdetails-viewprof-active');
			return false;
		});
		$('.b-pmdetails .close').click(function(){
			$('.b-pmdetails').removeClass('pmdetails-edit-active pmdetails-viewprof-active');
			return false;
		});
		$('.b-pmdetails .pmdetails-edit').click(function(){
			$('.b-pmdetails').removeClass('pmdetails-viewprof-active').addClass('pmdetails-edit-active');
			return false;
		});
	}
	
	if($('#loginform').length){
		$('#loginform').login();
	}
	if($('#registration').length){
		$('#registration').registration();
	}
	if($('#addcode').length){
		$('#addcode').addcode();
	}
	if($('#profile').length){
		$('#profile').profile();
	}
	
	if($('#plantation-map').length) {
		swfobject.embedSWF("/webroot/delivery/flash/rr_map_plantation.swf", "plantation-map", "790", "430", "9.0.0", false, {xml_path: '/action/plantation/data/tpl-action_plantation_xml/?ie=' + Math.random()}, {wmode: 'transparent', quality: 'high', background:"#ffffff"});
	}
});

function showLoginError(){
	$('#loginform input.i-p').addClass('i-p-error');
} 
function showForgotInfo(text){
	$('#remember-password-message').html(text).slideDown(200);
}

function showRegistrationError(text){
	$('#registration_form_error').html(text).show();
} 
function showProfileError(text){
	$('#profile_form_error').html(text).show();
	$('input[name=profile_'+'old_password]').val('');
	$('input[name=profile_'+'new_password]').val('');
	$('input[name=profile_'+'new_password2]').val('');
} 

function showAddCodeError(text){
	$('#addcode_form_error').html(text).show();
} 

function showPlant(id){
	$('.f-map-cnt').load('data/'+id+'/tpl-action_plantation_plant/');
} 
function showDeposit(id){
	$('.f-map-cnt').load('data/'+id+'/tpl-action_plantation_deposit_and_woodland/');
} 
function showWoodland(id){
	$('.f-map-cnt').load('data/'+id+'/tpl-action_plantation_deposit_and_woodland/');
} 

function hidePlant(){
	$('.f-map-cnt').html('');
} 
function hideDeposit(){
	$('.f-map-cnt').html('');
} 
function hideWoodland(){
	$('.f-map-cnt').html('');
} 
