$(document).ready(function() {
	//Showing and hiding hints
	if($('#username').val()=='username') {
		$('#username').val('');
	}
	if($('#password').val()=='password') {
		$('#password').val('');
	}
	$('#username').bind("focus", function(e) {
		$(this).addClass('textfocus');
		if($(this).parent().hasClass('inline')) {
			$(this).prev().addClass("focus")
			$(this).prev().removeClass("hastext")
		}
	});
	$('#username').bind("blur", function(e) {
		$(this).removeClass('textfocus');
		if($(this).parent().hasClass('inline')&&$(this).val()=='') {
			$(this).prev().removeClass("focus")
		}
	});
	$('#password').bind("focus", function(e) {
		$(this).addClass('textfocus');
		if($(this).parent().hasClass('inline')) {
			$(this).prev().addClass("focus")
			$(this).prev().removeClass("hastext")
		}
	});
	$('#password').bind("blur", function(e) {
		$(this).removeClass('textfocus');
		if($(this).parent().hasClass('inline')&&$(this).val()=='') {
			$(this).prev().removeClass("focus")
		}
	});
	setTimeout(function() {	if($('#username').parent().hasClass('inline')&&$('#username').val()!='') {			$('#username').prev().addClass('hastext');		}	},100);
	setTimeout(function() {		if($('#password').parent().hasClass('inline')&&$('#password').val()!='') {			$('#password').prev().addClass('hastext');		}	},100);

/*	function(){if($('#username').parent().hasClass('inline')&&$(this).val()!='') {$('#username').prev().addClass('hasText');}}
	function(){if($('#password').parent().hasClass('inline')&&$(this).val()!='') {$('#password').prev().addClass('hasText');}}
*/	//Updating dashboard URL
	$('#username').bind("keyup", function(e) {
		username = $(this).val();
		if(username.length>0) {
			var usernameRegEx = /^([\w_-]+)$/;
			if(usernameRegEx.test(username)==false) {
				reportBadUsername();
			} else {
				$.getJSON("/includes/php/ajax/checkuserexists.php?username="+username, 
					function(response) { 
						if(response.existence.exists=='true') {
   						$('span#predictedtitle').html('You can log into your dashboard at <a href="http://jotapp.com/' + response.existence.username+'">http://jotapp.com/' + response.existence.username+'</a>');
							$('span#predictedurl').addClass('unavailable');
							$('input#signupinput').addClass('unavailable');
						} else {
							$('span#predictedtitle').html('Your dashboard will be at:');
							$('span#predictedurl').html('http://jotapp.com/' + response.existence.username);
							$('span#predictedurl').removeClass('unavailable');
							$('input#signupinput').removeClass('unavailable');
						}
				});
			}
		}
	});
	
	//Edit note
	$('.edit a').bind("click", function(e) {
		var _id=$(this).closest('ul').attr("id").substring('note-functions-'.length);
		$('#note-functions-' + _id).hide();
		$('#note-content-' + _id).hide();
		var editform = $("<form id=\"edit-form-" + _id + "\" action=\"" +'/ajax'+$(this).attr('href')+ "\"><textarea id=\"note_content\" name=\"note_content\">" + $('#note-content-' + _id).html() + "</textarea><input type=\"hidden\" name=\"note_id\" value=\"" + _id + "\" /><input type=\"submit\" class=\"submit\" value=\"submit\"/><input type=\"button\" class=\"cancel\" value=\"cancel\"/></form>");
		$('#note-info-' + _id).append(editform);
		$('#edit-form-' + _id).bind("submit", function(f) {
			//This needs to callback to update the entire div#note-id.
			$.post($(this).attr('action'), $(this).serialize(), refreshNote, "json");
			$('#note-functions-' + _id).show();
			$('#note-content-' + _id).show();
			$(this).closest('form').remove();
			return false;
		});
		$('#edit-form-' + _id + ' input.cancel').bind("click", function(f) {
			$('#note-functions-' + _id).show();
			$('#note-content-' + _id).show();
			$(this).closest('form').remove();
		});
		return false;
	});
	
	//Create Ticket Skeleton
	$('#lighthouse_access').hide();
	$('#ticket-list-config').hide();
	$('#system_name').bind("change", function(e) {
		if($(this).val()=='trac') {
			$('#trac_access').show();
			$('#lighthouse_access').hide();
		} else {
			$('#trac_access').hide();
			$('#lighthouse_access').show();
		}
	});
	
	//Edit Ticket Skeleton
	$('#ticket-list a.configure').live("click", function(e) {
		var _id=$(this).closest('li').attr("id").substring('skeleton-'.length);
		$('#ticket-list').hide();
		$('#ticket-list-config').show();
		$('#ticket-list-config').load('/ajax' + $(this).attr('href'), bindForm);
		return false;
	});
	bindForm = function (responseText, textStatus, XMLHttpRequest) {
	 	$('#ticketform').bind("submit", function(f) {
			$.post($(this).attr('action'), $(this).serialize());
			$('#ticket-list').show();
			$('#ticket-list').load('list-ajax.html');
			$(this).closest('form').remove();
			$('#ticket-list-config').hide();
			return false;
		});
		$('#ticketform input.cancel').bind("click", function(f) {
			$('#ticket-list').show();
			$('#ticket-list-config').hide();
			$(this).closest('form').remove();
		});
		
	}
	
	
	//Share username suggest 
	$('#share_username').bind("change", function(e) {
		username = $(this).val();
		if(username.length>0) {
			var usernameRegEx = /^([\w_-]+)$/;
			if(usernameRegEx.test(username)) {
				$.getJSON("/includes/php/ajax/checkuserexists.php?username="+username, 
					function(response) { 
						if(response.existence.exists=='true') {
	   						$('p#outcome').html('');
							$('#share_submit').removeClass('unavailable');
						} else {
	   						$('p#outcome').html('No such user');
							$('#share_submit').addClass('unavailable');
						}
				});
			}
		}
	});
	
	$('#colour-list li').bind("click", function(e) {
		$('#colour-list li.active').removeClass('active');
		$.get('/includes/php/ajax/setusercolour.php',{colour:$(e.target).html()});
		$(e.target).addClass('active');
	});
	
	//Username autocomplete
	if($('.userautocomplete').length) {
		options = { serviceUrl:'/includes/php/ajax/usernames.php' };
		a = $('.userautocomplete').autocomplete(options);
	}

/*  var feedback_widget_options = {};
  feedback_widget_options.display = "overlay";  
  feedback_widget_options.company = "jot";
  feedback_widget_options.placement = "left";
  feedback_widget_options.color = "#222";
  feedback_widget_options.style = "problem";
  feedback_widget_options.container = "feedback_container";
  var feedback_widget = new GSFN.feedback_widget(feedback_widget_options);
*/	
});
refreshNote = function(data, textStatus) {
	$('#note-' + data.note.id).load('/ajax/' + data.note.username + '/note-info/'+data.note.id+'/');
}
