$(document).ready(function() {
	/**
	 * Creates dialog for the login
	 */
	$('.sign_in').click(function() {
		
		// Make Ajax request to fetch selected  dialog content
		$.ajax({
		  url: "/site/signinselect",
		  global: false,
		  type: "GET",
		  dataType: "html",
		  cache: false,
		  async:false,
		  success: function(responseText) {
			 $("#sign_in_dialog").html(responseText);
		  },
			error: function() {
			 $("#sign_in_dialog").html("<p>Webserver error!</p>");
			}
		});

		$('#sign_in_dialog').dialog({
			'title':'Logga in',
			'autoOpen':false,
			'modal':true,
			'minWidth':580,
			'position':['auto', 10],
			close: function(ev, ui) { $(this).dialog("destroy"); }
			
		});
		$("#sign_in_dialog").dialog("open");
		
		

		$('#sign_in_dialog').live('keyup', function(e){
		  if (e.keyCode == 13) {
			$(':button:last').click();
		  }
		});

	});
	
	// register to access this function
	$('.employeraccess').click(function() {
		
		// Make Ajax request to fetch selected dialog content
		$.ajax({
		  url: "/site/employeraccess",
		  global: false,
		  type: "GET",
		  dataType: "html",
		  cache: false,
		  async:false,
		  success: function(responseText) {
			 $("#sign_in_dialog").html(responseText);
		  },
			error: function() {
			 $("#sign_in_dialog").html("<p>Webserver error!</p>");
			}
		});

		$('#sign_in_dialog').dialog({
			'title':'Logga in',
			'autoOpen':false,
			'modal':true,
			'minWidth':580,
			'position':['auto', 10],
			close: function(ev, ui) { $(this).dialog("destroy"); }
			
		});
		$("#sign_in_dialog").dialog("open");

	});
	
	
	
	

});
	/**
	 * Loads content into dialog
	 * path to view
	 * type applicant or employer
	 */
	function loadContent(path,type) {
		
		$.ajax({
		  url: path,
		  global: false,
		  type: "GET",
		  dataType: "html",
		  cache: false,
		  async:false,
		  success: function(responseText) {
			 
			 $("#sign_in_dialog").hide().html(responseText).slideToggle('fast', function() {
				setButtons(path,type);
				$("#login_username").focus();
			  });
			 
		  },
			error: function() {
			 $("#sign_in_dialog").html("<p>Webserver error!</p>");
			}
		});
	}

	/**
	 * Sets the buttons for the dialog when the user has selected login
	 */
	function setButtons(path,type) {
		var buttons = $("#sign_in_dialog").dialog("option", "buttons"); // getter
		if (type=='applicant') {
			$.extend(buttons, { 'Logga in som arbetsökande':function(){send(path,type);}});
		} else if (type=='employer') {
			$.extend(buttons, { 'Logga in som arbetsgivare':function(){send(path,type);}});
		}
		$("#sign_in_dialog").dialog("option", "buttons", buttons); // setter
	}
	
	function send(path,type) {
		// Make Ajax request to fetch selected bookings dialog content
		data = $("#sign_in_form").serialize();
		$.ajax({
			url: path,
			global: false,
			type: "POST",
			dataType: "html",
			cache: false,
			data: data,
			async:false,
			success: function(responseText) {
				if (responseText.substr(0,10).indexOf("[__DONE__]") >= 0) {
					// Close dialog
					$("#sign_in_dialog").dialog("close");
					if(responseText.length > 10){
						$(location).attr('href',responseText.substr(10,responseText.length));
					}else{
						location.reload();
					}
				} else {
					$("#sign_in_dialog").html(responseText);
				}
			},
			error: function() {
				$("#sign_in_dialog").html("<p>Webserver error</p>");
			}
		});
	}
