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uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i;
valid_email = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i;
valid_username = /^[a-z0-9_]{3,25}$/;

shorten = function()	{
	$.ajax({
		type: 'GET',
		cache: true,
		dataType: 'json',
		url: BASE_URL+'shorten',
		data: {long: $('#long').val()},
		beforeSend: function(){
			if (!valid_url.test($('#long').val()))
				return false;

			$('.motto').animate({ letterSpacing:"20px" }, 1200, "swing");
			$('#short-cont').slideUp('fast');
			$('#preview').attr('href', '');
		},
		success: function(data){
			if (data.error)	{
				alert(data.msg);
				$('.motto').animate({ letterSpacing:"0px" }, 100, "swing");
			}
			else	{
				$('.motto').animate({ letterSpacing:"0px" }, 100, "swing", function(){
					$('#short-cont').slideDown('fast').find('#short').val(BASE_URL+data.short);
					$('a[rel=facebook]').attr('href', 'http://www.facebook.com/sharer.php?u='+BASE_URL+data.short+'&title='+data.title);
					//@todo: truncate 
					$('a[rel=twitter]').attr('href', 'http://twitter.com/home?status='+data.title+' '+BASE_URL+data.short);
					$('a[rel=myspace]').attr('href', 'http://www.myspace.com/index.cfm?fuseaction=postto&t='+data.title+'&u='+BASE_URL+data.short);
					$('a[rel=stumbleupon]').attr('href', 'http://www.stumbleupon.com/submit?url='+BASE_URL+data.short);
				});
			}
		},
		error: function(){
			$('.motto').animate({ letterSpacing:"0px" }, 100, "swing");
		}
	});
};

homepage_tab = function(elm)	{
	$this = $(elm);
	if (elm.href.split('#')[1]!='latest-links' && elm.href.split('#')[1]!='popular-links' && elm.href.split('#')[1]!='latest-users')
	{
		$('.tabs li').removeClass('active');
		$('#tab-content').html("This feature has not been activated yet.");
		$this.parent().addClass('active');
		return;
	}
	
	$.ajax({
		type: 'POST',
		cache: true,
		url: BASE_URL+"ajax/?action=getSix&type="+elm.href.split('#')[1],
		beforeSend: function(){
			$('#tab-content').html("<div style=\"text-align:center;padding-top:45px;\"><img src=\""+STATIC_URL+"img/colorbox/loading.gif\"/></div>");
		},
		success: function(data){
			$('.tabs li').removeClass('active');
			$('#tab-content').html(data);
			$this.parent().addClass('active');
		},
		error: function(){
			alert("Error");
		}
	});
};

join_form = function (e) {
	e.preventDefault();
	var $form = this;
	$.ajax({
		type: 'POST',
		cache: true,
		url: $form.action,
		data: $($form).serialize(),
		beforeSend: function(){
			$('#signup-form form span').css('display', 'none');
			if (!valid_username.test($form.username.value))
			{
				$('#signup-form form input[name=username]').next().html("Username is not valid").css('display', 'block');
				return false;
			}
			if (!valid_email.test($form.email.value))
			{
				$('#signup-form form input[name=email]').next().html("Email is not valid").css('display', 'block');
				return false;
			}
			if ($form.password.value.length<=2 || $form.password.value.length>8)
			{
				$('#signup-form form input[name=password]').next().html("Password length must be min. 3, max. 8").css('display', 'block');
				return false;
			}
			if ($form.password2.value!=$form.password.value)
			{
				$('#signup-form form input[name=password2]').next().html("Passwords don't match").css('display', 'block');
				return false;
			}
		},
		success: function(data){
			if(data=='ok')
			{
				$('#signup-form form').hide();
				$('#signup-form .thanks').show();
			} 
			else if(data=='email_exist')
			{
				$('#signup-form form input[name=email]').next().html("This email address is already in use.").show();
			}
			else if(data=='username_exist')
			{
				$('#signup-form form input[name=username]').next().html("This username is already in use.").show();
			}
			else
			{
				$('#signup-form .system-error').html("There was some error during registration.").show();
			}
		},
		error: function(){
			$('#signup-form .system-error').html("There was some error during registration.").show();
		}
	});
};

login_form = function (e) {
	$('#login-form form span').css('display', 'none');
	if (!valid_email.test(this.email.value))
	{
		$('#login-form form input[type=text]').next().css('display', 'block');
		$.fn.colorbox.resize();
		return false;
	}
	if (this.password.value.length==0)
	{
		$('#login-form form input[type=password]').next().css('display', 'block');
		$.fn.colorbox.resize();
		return false;
	}
};

like_link = function(e)	{
	e.preventDefault();
	var elm = this;
	$.ajax({
		type: 'POST',
		cache: true,
		url: BASE_URL+'links/like/?id='+$(elm).parent().parent().attr('id'),
		beforeSend: function(){
			
		},
		success: function(data){
			if (data=='login_required')	{
				alert('You must be login to perform this action.');
			}
			else	{
				$(elm).hide();
				if (data>0)	{
					$(elm).parent().find('.like_count').html(data + ' likes');
				}
			}
		},
		error: function(){
			alert("Error");
		}
	});
};

follow_unfollow = function(e){
	e.preventDefault();
	var action_ = $(this).hasClass('follow') ? 'follow' : 'unfollow';
	var user_id = $(this).attr('user_id');
	$this = $(this);
	$.ajax({
		type: 'POST',
		cache: true,
		url: BASE_URL+"users/"+action_+"/?id="+user_id,
		beforeSend: function(){

		},
		success: function(data){
			if (data=='login_required')	{
				alert('You must be login to perform this action.');
			}
			else if (data==1 && action_=='follow')	{
				$this.removeClass('follow').addClass('unfollow');
				$('#follow').html('<img src="'+STATIC_URL+'img/minus.png"/>un-follow').css('color', '#b33737');
			}
			else if (data==1 && action_=='unfollow')	{
				$this.removeClass('unfollow').addClass('follow');
				$('#follow').html('<img src="'+STATIC_URL+'img/plus.png"/>follow').css('color', '#80CA24');
			}
			else	{
				alert("There was something wrong.");
			}
		},
		error: function(){
			alert("There was something wrong.");
		}
	});
};

