//----------------------------------------------------------------------------------------------------------------------------------------------
// Copyright (c) by Goncalo "gesf" Fontoura
// Custom javascript functions.
//----------------------------------------------------------------------------------------------------------------------------------------------

//----------------------------------------------------------------------------------------------------------------------------------------------
// Things to do as soon as the document is ready...
//----------------------------------------------------------------------------------------------------------------------------------------------

$(document).ready(function() {
	$('a[rel*=facebox]').facebox({ closeImage: 'http://i.urlms.com/close-tiny.gif' });
	
	Extra();
	CustomPointer();
	PreviewPage();

	$('#form').submit(function() {
		var data = 'create=true&' + $(this).serialize();			
		// $.facebox(function(data) {
			$.ajax({
            	type: 'POST',
            	url: '/create.php',
            	data: data,
            	error: function (event, XMLHttpRequest, ajaxOptions, thrownError) { alert('Ajax Error: ' + thrownError) },
				success: function(data){ $.facebox(data); }
           	})
		// });	
		return false;
	});
	
	var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");  
	jQuery.getScript(gaJsHost + "google-analytics.com/ga.js", function(){  
	  try {
	    var pageTracker = _gat._getTracker("UA-244286-11");
	    pageTracker._trackPageview();
	  } catch(err) {}
	});	

});

//----------------------------------------------------------------------------------------------------------------------------------------------
// Append the 'custom pointer information' html to the document
//----------------------------------------------------------------------------------------------------------------------------------------------

function CustomPointer() {
	var divcontent = '\
	<p class="icon_information">Add your own pointer</p>\
	<p>You can use a custom pointer, instead of the ones automatically generate by urlms.com.<br /><br />\
	For that, just type the desired pointer in the following field (e.g: john_xmas-2006).\
	Your custom pointer may only contain numbers, letter, dashes(-) and underscores(_).<br /><br />\
	If the url already exists in our database you wont be able to store your custom pointer\
	and the existing pointer for the given url will be returned.<br /><br />\
	Just close the window and hit the \'shorten\' button when you\'re done!<br /><br />\
	<input id="pointer" name="pointer" type="text" value="" /></p>';
	$('#advanced_content').html(divcontent);
}

//----------------------------------------------------------------------------------------------------------------------------------------------
// Append the 'preview page' information to the document
//----------------------------------------------------------------------------------------------------------------------------------------------

function PreviewPage() {
	var divcontent = '\
	<p class="icon_information">Preview a shorten URL!</p><p>\
	You can choose whether or not your short URL will instantly redirect to its destination,\
	or a page with information about the destination URL will be shown before redirecting.</p><br />\
	<p>Click <a href="http://urlms.com/XO3" class="external" target="_blank">here</a> for a preview page sample.</p>';
	$('#preview_content').html(divcontent);
}

//----------------------------------------------------------------------------------------------------------------------------------------------
// Additional stuff ...
//----------------------------------------------------------------------------------------------------------------------------------------------

function Extra(){ 
	var extra = '\
	<div id="advanced_content" style="display:none">&#160;</div>\
	<div id="preview_content" style="display:none">&#160;</div>\
	<div class="balloon" style="display:none">&#160;</div>';
	$('#content').append(extra);
	$('#alert span').click(function(){ 
		$(this).text('Loading ...'); 
		$(document).attr('location', 'http://' + window.location.hostname);
	});
	$('a.check').click(function(){
		checkbox = $('#preview');
		if(checkbox.val() == 'no') { 
			$(this).css('background-image', 'url(http://i.urlms.com/checked.gif)');
			checkbox.attr('value', 'yes');
		} else { 
			$(this).css('background-image', 'url(http://i.urlms.com/unchecked.gif)');
			checkbox.attr('value', 'no');
		}
	});
	$('input.field').mouseover(function(){ if($(this).val() == '') { $('.balloon').fadeIn('slow'); } });
	$('input.field').mouseout(function(){ $('.balloon').fadeOut('slow'); });	
	$('input.field').focus();
	$('a.usesite').click(function(){ $('input.field').val(document.referrer); });	
}
