	/* 
	 * 0x.tc Javascript Library
	 * Controls basic functionality of the 0x.tc shortening service
	 * 
	 * Version: 0.1.921a
	 */
	
	
	ZeroClipboard.setMoviePath('http://0x.tc/lib/ZeroClipboard.swf');
	var hideDelayTimer = null;
	var beingShown = false;
	var shown = false;
	
	function take()
	{
		// create a date object
		var xDat = new Date();
		
		// make sure the url is encoded using encodeURICompontent
		// this makes sure the string can be decoded using urldecode() in PHP 	
		var xUrl = encodeURIComponent($("#xUrl").val());
		
		// empty return URL
		var rUrl = ""; 	
		
		if(shown == false)
		{				
			$.get("x?go=" + xUrl + "&t=" + xDat.getTime(),{},function(xml) {
				$('taken',xml).each(function(i) {
					if($(this).find("xCode").text() != 200)
					{
						// something went wrong, we expect a return code of 200
						// show a bubble with the error message and focus on input
						bubble('xPopupContent', $(this).find("xMessage").text(), 1, true);
						$("#xUrl").focus();
					}
					else
					{	
						// create unique variable values
						xID 	= xDat.getTime();
						xDiv	= 'take_' + xID;
						xBubble	= 'bubble_' + xID;
						rUrl 	= $(this).find("xUrl").text();
														
						// change the size of the input box				
						$('#xUrl').animate({
							width: '220px'
						}, 400, 'swing');
						$('.xLeftDiv').animate({
							width: '226px'
						}, 400, 'swing');
						
						// build new boxes
						$('#xContainer').append('<div id="xEquals">=</div>');
						$('#xContainer').append('<div class="xRightDiv"><form onsubmit="return false;">');
						$('#xContainer').append('<div class="formbg">');
						$('#xContainer').append('<input type="text" class="enterURLSmall" value="' + rUrl + '" readonly>');
						$('#xContainer').append('</div></div></form></div>');
						$('#xContainer2').append('<div class="xRightDiv"><a href="javascript:void(0);" id="' + xDiv + '" class="fg-button ui-state-default ui-corner-all"><span class="ui-icon ui-icon-heart"></span>Copy</a></div>');
						// $('#xContainer2').append('<a href="tweetie:' + rUrl + '">Tweetie</a>');
						//http://twitter.com/home?status=input
						
						// animate the new box
						$('.xRightDiv').animate({
							width: '226px'
						}, 400, 'swing');
						
						// make sure the input field is readonly - it's not making sense
						// to have users changing this value after a conversion
						$('#xUrl').attr('readonly', true);
						
						// build a reset button
						$('#bTake').html('<span class="ui-icon ui-icon-arrowreturnthick-1-w"></span>Reset');
						$('#bTake').attr('href','javascript:reset("'+xDiv+'")');
						
						// bind the copy button
						eval("var clip" + xID + " = new ZeroClipboard.Client();");
						eval("clip" + xID +  ".setText('" + rUrl + "');"); 
						eval("clip" + xID +  ".glue('" + xDiv + "');");
						
						// make sure resizing sets the flash copy at the right position
						$(window).bind('resize', function() {
							eval("clip" + xID +  ".reposition();");
						});
						
						// coda bubble complete message - sends a bubble once the copy has been completed
						// listen to 'complete'
						eval("clip" + xID +  ".addEventListener('complete', function(client, text) { bubble('xPopupContent', '" + rUrl + "<br />copied to clipboard', 2); } );");
						
						// action has been performed
						shown = true;
						
						// check for aditional parameters
						// dm: tweetie, twitter
						xGet = getUrlVars();
						if(xGet['dm'] == "tweetie") window.location = 'tweetie:' + rUrl;
						if(xGet['dm'] == "twitter") window.location = 'http://twitter.com/home?status=' + rUrl;
					}
					// update total count
					$("#xCount").html($(this).find("xCount").text());
				});
			});
		}	
	}
	
	// resetting all variables to its original values and size
	function reset(id)
	{
		$(window).unbind('resize');
		$('#bTake').html('<span class="ui-icon ui-icon-link"></span>Take it shorty!');
		$('#bTake').attr('href','javascript:take()');
		$('#xUrl').val('');
		$('#xUrl').attr('readonly', false);		
		$('.xLeftDiv').animate({
			width: '480px'
		}, 400, 'swing');
		$('#xUrl').animate({
			width: '474px'
		}, 400, 'swing');
		$('#xEquals').remove();
		$('.enterURLSmall').remove();
		$('.xRightDiv').remove();
		$('#swf'+id).remove();
		shown = false;
		
		setTimeout(function() {
			$('#xUrl').focus();
		}, 500);	
	}
	
	function bubble(div, content, version, error)
	{
		if(error == true)
			bgcolor = "#d00000";
		else
			bgcolor = "#117B1E";
			
		
		$("#" + div).html(content);
		
		// get positions
		if (version == 1) {
			var pos = $(".xLeftDiv").position();
			var bwidth = '480px';	
			var btop = 20;
		} else {
			var pos = $(".xRightDiv").position();
			var bwidth = '226px';
			var btop = 70;
		}
		$("#xPopup").css({
			top: pos.top - btop,
			left: pos.left,
			width: bwidth,
			display: 'block',
			'background-color': bgcolor
		})
		.animate({
			top: '-=20px',
			opacity: 1
		}, 400, 'swing', function() {
			setTimeout(function() {
				$("#xPopup").animate({
					top: '-=20px',
					opacity: 0
				}, 400, 'swing', function() {
					$("#xPopup").css('display', 'none');
				});
			}, 1000);	
		});
			
	}
	
	function result(div, content)
	{
		$("#" + div).html(content);
		
		// get positions
		var pos = $("#xUrl").position();		
		$("#xResult").css({
			top: pos.top,
			left: pos.left + 220,
			display: 'block'
		})
		.animate({
			left: '+=40px',
			opacity: 1
		}, 400, 'swing');
			
	}	
	
	$(function() 
	{ 
		$("#xUrl").focus();
		$("#xUrl").keypress(function(e)		
		{
			var code = (e.keyCode ? e.keyCode : e.which);
			if(code == 13) { take(); }
		});		
		$("#xPopup").css('opacity', 0);
		
		// jquery ui
		$(".fg-button:not(.ui-state-disabled)")
		.hover(
			function(){ 
				$(this).addClass("ui-state-hover"); 
			},
			function(){ 
				$(this).removeClass("ui-state-hover"); 
			}
		)
		.mousedown(function(){
				$(this).parents('.fg-buttonset-single:first').find(".fg-button.ui-state-active").removeClass("ui-state-active");
				if( $(this).is('.ui-state-active.fg-button-toggleable, .fg-buttonset-multi .ui-state-active') ){ $(this).removeClass("ui-state-active"); }
				else { $(this).addClass("ui-state-active"); }	
		})
		.mouseup(function(){
			if(! $(this).is('.fg-button-toggleable, .fg-buttonset-single .fg-button,  .fg-buttonset-multi .fg-button') ){
				$(this).removeClass("ui-state-active");
			}
		});
		
		// check if this is a get request
		var xGet = getUrlVars();
		if(xGet['bm'] == 'take')
		{
			url = xGet['url'];
			$("#xUrl").val(decodeURIComponent(url));
			take();
		}
	});
	
	// Read a page's GET URL variables and return them as an associative array.
	function getUrlVars()
	{
    	var vars = [], hash;
	    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
 
    	for(var i = 0; i < hashes.length; i++)
    	{
			hash = hashes[i].split('=');
			vars.push(hash[0]);
			vars[hash[0]] = hash[1];
   		}
 
    	return vars;
	}
	
	