$(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('Shrink it!');
							//Add this link to the recent links shortened
							$('#recent_list2').prepend('<li 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_links2').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_list2').html('');
		$('#recent_links2').fadeOut();
		return false;
	});
	
	//Switch the number of posts on the admin dashboard
	$('.show').click(function (){
		var show = this.id;
		$('#recent_list2').html('');
		$('#spinner').fadeIn('fast');
		$('#recent_list2').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;
	});
});

(function ($) {

$.fn.maxlength = function (settings) {

    if (typeof settings == 'string') {
        settings = { feedback : settings };
    }

    settings = $.extend({}, $.fn.maxlength.defaults, settings);

    function length(el) {
    	var parts = el.value;
    	if ( settings.words )
    		parts = el.value.length ? parts.split(/\s+/) : { length : 0 };
    	return parts.length;
    }
    
    return this.each(function () {
        var field = this,
        	$field = $(field),
        	$form = $(field.form),
        	limit = settings.useInput ? $form.find('input[name=maxlength]').val() : $field.attr('maxlength'),
        	$charsLeft = $form.find(settings.feedback);

    	function limitCheck(event) {
        	var len = length(this),
        	    exceeded = len >= limit,
        		code = event.keyCode;

        	if ( !exceeded )
        		return;

            switch (code) {
                case 8:  // allow delete
                case 9:
                case 17:
                case 36: // and cursor keys
                case 35:
                case 37: 
                case 38:
                case 39:
                case 40:
                case 46:
                case 65:
                    return;

                default:
                    return settings.words && code != 32 && code != 13 && len == limit;
            }
        }


        var updateCount = function () {
            var len = length(field),
            	diff = limit - len;

            $charsLeft.html( diff || "0" );

            // truncation code
            if (settings.hardLimit && diff < 0) {
            	field.value = settings.words ? 
            	    // split by white space, capturing it in the result, then glue them back
            		field.value.split(/(\s+)/, (limit*2)-1).join('') :
            		field.value.substr(0, limit);

                updateCount();
            }
        };

        $field.keyup(updateCount).change(updateCount);
        if (settings.hardLimit) {
            $field.keydown(limitCheck);
        }

        updateCount();
    });
};

$.fn.maxlength.defaults = {
    useInput : false,
    hardLimit : true,
    feedback : '.charsLeft',
    words : false
};

})(jQuery);

$(document).ready(function() {
	   $("#sidebar li.ab").css({'text-shadow' : '1px 1px #000'});
	   $("#sidebar li.ac").css({'text-shadow' : '1px 1px #000'});
	   $("#content").css({'text-shadow' : '1px 1px #d7d7d7'});
	   $("#footer").css({'text-shadow' : '1px 1px #d7d7d7'});
	   $("a.show").css({'text-shadow' : '0px 0px #d7d7d7'});
           $("a.show").stop().fadeTo("slow", 1.0);						                  
           $("a.show").stop().hover(function(){
	   $(this).stop().fadeTo("slow", 0.3);
		 },function(){
	   $(this).stop().fadeTo("slow", 1.0);
	});
           $("#menu ul li a").stop().fadeTo("slow", 1.0);						                  
           $("#menu ul li a").stop().hover(function(){
	   $(this).stop().fadeTo("slow", 0.5);
		 },function(){
	   $(this).stop().fadeTo("slow", 1.0);
	});
           $(".wp-image").stop().fadeTo("slow", 1.0);						                  
           $(".wp-image").stop().hover(function(){
	   $(this).stop().fadeTo("slow", 0.5);
		 },function(){
	   $(this).stop().fadeTo("slow", 1.0);
	});
           $("#submit_btn").stop().fadeTo("slow", 1.0);						                  
           $("#submit_btn").stop().hover(function(){
	   $(this).stop().fadeTo("slow", 0.9);
		 },function(){
	   $(this).stop().fadeTo("slow", 1.0);
	});
     });
    if (document.images) {
        img1 = new Image();
        img1.src = "http://cmin.us/images/loading.gif";
   }

    function createTarget(t){
     window.open("", t, "toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=600,height=420,top=110,left=350'");
     return true;
   }

