$(document).ready(function(){	
	
	//New URL submit
	$('#url_form').submit(function (){ 
			var url = $('#url').val();
			var custom = $('#custom').val();
			
			$('#show_new').slideUp();
			
			//Run some validation
			if(url.match(/^(http(s?):\/\/|ftp:\/\/{1})?((\w+\.){1,})\w{2,}/) == null)
			{
				$('.error').hide();
				$('#url_form').prepend('<div class="error">Enter a valid URL to shorten</div>');
			}
			else if(custom.match(/^([a-zA-Z0-9_]){1,}$/) == null && custom != '')
			{
				$('.error').hide();
				$('#url_form').prepend('<div class="error">Enter valid custom URL</div>');
			}
			else
			{
				//Hide any errors
				$('.error').hide();
				$('#submit_btn').val('Shortening..');
				$.ajax({
					url: site_url+'ajax/new_url',
					type: 'POST',
					data: 'custom='+custom+'&url='+escape(url),

					success: function(result){
						
						if(result == 'error')
						{
							$('#submit_btn').val('Try Again');
							alert('There was an error and your URL could not be shortened.');
						}
						else if(result == 'taken')
						{
							$('#submit_btn').val('Try Again');
							$('#url_form').prepend('<div class="error">That custom URL has already been used</div>');
						}
						else
						{
							//Parse the resulting JSON
							var data = eval('('+result+')');
							//Change URL input value back
							$('#url').val('http://');
							//Fade in the wrapper to show the new url
							$('#show_new').fadeIn();
							//Fille the new URL input with the short URL
							$('#new_url').val(site_url+data.key).select();
							//Reset the shorten btn text
							$('#submit_btn').val('Shorten');
							//Add this link to the recent links shortened
							$('#recent_list').prepend('<li style="display:none" id="new-'+data.id+'"><a href="'+url+'" target="_blank">'+data.link_title+'</a><br /><span>Clicks: '+data.clicks+' Link: '+site_url+data.key+'</span></li>');
							$('#new-'+data.id).slideDown();
							//Show the recent list if it's not already being showed
							$('#recent_links').fadeIn();
							//Close the custom field and clear it
							$('#custom').val('');
							$('#custom_input').slideUp();
						}
					}
				});
			}
				
			return false; 
	});	
	
	//Select the value when the user clicks the new url
	$('#new_url').live('click',function (){ this.select();});
	
	//Clear the recent links
	$('#clear_recents').click(function (){
		$.get('/ajax/clear_recent');
		$('#recent_list').html('');
		$('#recent_links').fadeOut();
		return false;
	});
	
	//Switch the number of posts on the admin dashboard
	$('.show').click(function (){
		var show = this.id;
		$('#recent_list').html('');
		$('#spinner').fadeIn('fast');
		$('#recent_list').load(site_url+'ajax/get_links', {amount: show}, function(result){
			if(result != 'error')
			{
				$('#spinner').slideUp();
			}
		 });
		return false;
	});
	
	//Check for updates
	$('#update_check').click(function (){
		$.getJSON(site_url+'ajax/check_for_updates', function(data){ 
			if(data.status == 'none')
			{
				alert('There are no available updates');
			}
			else
			{
				if(confirm('URLi version '+data.version+' is available\n\nNotes:\n'+data.notes+'\n\nClick "ok" to get the update'))
				{
					window.location=data.url;
				}
			}
		});
		return false;
	});
	
	//Custom link slide down
	$('#custom_link').click(function (){
		$('#custom_input').slideDown();
		return false;
	});
});




