jQuery.fn.selText = function() {
    var obj = this[0];
    if ($.browser.msie) {
        var range = obj.offsetParent.createTextRange();
        range.moveToElementText(obj);
        range.select();
    } else if ($.browser.mozilla || $.browser.opera) {
        var selection = obj.ownerDocument.defaultView.getSelection();
        var range = obj.ownerDocument.createRange();
        range.selectNodeContents(obj);
        selection.removeAllRanges();
        selection.addRange(range);
    } else if ($.browser.safari) {
        var selection = obj.ownerDocument.defaultView.getSelection();
        selection.setBaseAndExtent(obj, 0, obj, 1);
    }
    return this;
}

var api = 'http://sam.tl/';

var trigger;

function shortLink() {
	var url = $('input').val();
	if(url!='') {
		trigger = true;
		$('button').html('<img src="static/load.gif" /><strong>Shortening</strong> your long link');
		
		$.post("api.php", {
		    url: url
		}, function (data) {
		
			if(data!=false) {
				var newLink = api + data;
								
				$('body').append('<div id="overlay"><div class="link">'+api+'<strong>'+data+'</strong></div></div>');
				$('#overlay').selText();
				$('#overlay .link').append('<div class="desc"><strong>Congratulations!</strong> Your link has been shortened. Press <strong class="highlight">'+shortcut+'</strong> to copy it or <strong>Escape</strong> to generate a new one.</div>');
				$('button').html('Shortened!');
			} else {
				$('button').html('The URL does not appear to be valid.');
				trigger = false;
			}
		});
	} else {
		$('button').html("We can't shorten <strong><em>nothing</em></strong>, we're not that good!");
	}
}

$(document).ready(function(){
	$('#bookmark').attr("href", "javascript:void(location.href='"+api+"api?url='+encodeURIComponent(location.href)+'&bookmark=true')").removeAttr('title');
	$('#con').html('<input type="text" placeholder="Sam! Shrink that Link" /><button><strong>Click</strong> or press <strong>Enter</strong> to shorten the link</button>');
	
	if(navigator.platform=='MacIntel') {
		shortcut = '⌘ + C';
	} else {
		shortcut = 'Ctrl + C';
	}
	
	buttonText = $('button').html();
	$('input').focus();
	$('body').append('<div class="tooltip">Drag the bookmarklet to your bookmarks!</div>');
	
	$('#bookmark').click(function() {
		$('.tooltip').html('Drag is the keyword!');
		return false;
	});
	
	$("#bookmark").hover(
	   function() {
	      $('.tooltip').fadeIn(200);
	   },
	   function() {
	      $('.tooltip').fadeOut(200);
	   }
	);
	
	if($('#overlay').length>0) {
		$('input').blur();
		$('.desc').append('Press <strong class="highlight">'+shortcut+'</strong> to copy it or <strong>Escape</strong> to generate a new one.');
		$('#overlay').selText();
	}
	
	
	$(document).keyup(function(e) {
	  if (e.keyCode == 27) { 
	  	$('#overlay').remove(); 
	  	$('input').val('').focus();
	  	$('button').html(buttonText);
	  	trigger = false;
	  }
	});
	
	$('button').click(function() {
		shortLink();
	});
	
	$('input').keypress(function(e) {
		if(e.which == 13 && trigger!=true) {
			shortLink();
		}
	});		
});	

