/*
*	@author: Ronny Hartmann-Schmidt
*/

$(document).ready(function(){
	
	$(".colorbox").colorbox({initialWidth:400,initialHeight:200});

	$("#short-url-target").val("")
	$("#text-area-hint-id").fadeIn("slow");
	
	$("#text-area-hint-id").click(function() {
		$("#text-area-hint-id").fadeOut("slow");
		$("#short-url-target").focus();
	});
	
	$("#short-url-target").click(function() {
		$("#text-area-hint-id").fadeOut("slow");
	});
	
	$("#short-url-target").keyup(function(){
		if ($("#short-url-target").val().indexOf(window.location) != -1) {
			$(".button-shorten span").html("Get long URL");
		}
		else {
			$(".button-shorten span").html("Shorten URL");
		}
	});
	
	$("#short-url-target").focusout(function() {
		if($("#short-url-target").val() === "") {
			$("#text-area-hint-id").fadeIn("slow");
		};
	})
	
	var howToCounter = 1;
	$("#how-to-use ul li").each(function() {
		$(this).addClass("item-" + howToCounter);
		howToCounter += 1;
	})
	
	if($("#previous-items-content").html().indexOf("</table>") != -1 || $("#previous-items-content").html().indexOf("</TABLE>") != -1) {
		$("#previous-items-main").slideDown("slow");
	}
	
	$('.toolbox').tooltip({
		showURL: false,
		track: true,
		delay: 100,
		fade: 250
	});	
	
	$('.toolbox-static').tooltip({
		showURL: false,
		track: false,
		delay: 100,
		fade: 250
	});	
	
	$(".button").mouseover(function(){
		$(this).addClass("button-active");
	});
	
	$(".button").mouseout(function(){
		$(this).removeClass("button-active");
	});
	
	function keyWatch(e) {
	  var keyCode=(e)? e.which :event.keyCode;
	  if(keyCode==13) {shortenURL();} ;
	}
	
	document.onkeypress=keyWatch;

});

function shortenURL() {
	
	if( $("#short-url-target").val().indexOf(window.location) != -1) {
		var timestamp = new Date().getTime();
		$.ajax({
			url: window.location + "functions/getLongURL.php", 
			type: "POST",
			data: "shorturlstub=" + $("#short-url-target").val().replace("" + window.location + "s", "") + "&timestamp=" + timestamp,
			success: function(html){
				$("#short-url-target").val(unescape(html));
				$(".button-shorten span").html("Shorten URL");
			}
		});
	}
	else {
		if($("#short-url-target").val() != "") {
	
			$("#short-url-target").animate( { color: "#FFFFDF" }, "fast" )
		
			$("#loader-overlay-id").fadeIn("slow");
			
			var timestamp = new Date().getTime();
			
			$.ajax({ 
				url: window.location + "functions/generateShortURL.php", 
				type: "POST",
				data: "longurl=" + escape($("#short-url-target").val()) + "&timestamp=" + timestamp,
				success: function(html){
					
					$("#loader-overlay-id").fadeOut("fast");
					
					if(html.indexOf("valid") == -1 && html != "") {
						$("#short-url-target").val(window.location + "s" + html);
						$("#button-twitter").fadeIn("slow");
						
						$.ajax({
							url: window.location + "functions/getFormerShortURLs.php", 
							type: "POST",
							data: "timestamp=" + timestamp,
							success: function(html){
								$("#previous-items-content").html(html);
								$("#previous-items-main").slideDown("slow");
								$('.toolbox').tooltip({
									showURL: false,
									track: true,
									delay: 100,	
									fade: 250
								});
								$(".button-shorten span").html("Get long URL");
							}
						
						});
					} else {
						$("#short-url-target").val("Please enter a valid URL");
					}
					$("#short-url-target").animate( { color: "#AFAFAF" }, "fast" )
		
		      	}
			});
		}
	}
}

function getShortURL(id) {
	$("#text-area-hint-id").fadeOut("slow");
	$("#button-twitter").fadeIn("slow");
	$("#short-url-target").val( window.location + $("#old-shortURL-container-" + id).html());
	$(".button-shorten span").html("Get long URL");
	window.scrollTo(0, 0);
}

function visitSite(id) {
	window.location =  $("#old-longURL-container-" + id).html();
}

function shareOnTwitter() {
	newWindow = window.open("http://twitter.com/?status=" + $("#short-url-target").val(), "Twitter", "width=800,height=500,left=100,top=200");
	newWindow.focus();
}


