var siteFocussedForm = '';
var selectedTab = '';

$(window).resize(function(){
	if($('.tab-bg'))
	{
		selectTab(selectedTab, false);
	}
	
	$('body').css('overflow', 'scroll');
});

$(window).scroll(function () {
	$('#copyright').css('bottom','5px');
	$('#copyright').css('right','5px');
});

function initTabs()
{
	selectTab('', true);
	
	$('.tab').bind('click', function(){
		if(selectedTab !== $(this).attr('id'))
		{
			selectedTab = $(this).attr('id');
			selectTab(selectedTab, true);
		}
	});
	
}
function selectTab(which, animation)
{
	if(!which || which == '')
		which = 'tab-worldwide-access';
	
	var firstTabPos = $('#'+which).offset();
    
	if(animation == true)
	{
		$('.tab').css('border','1px solid transparent');
		$('#'+which).css('border','1px solid #777777');

		$('#'+which).css('-moz-border-radius','3px');
		$('#'+which).css('border-radius','3px');
		setTabContent(which);
		/*$('.tab-bg').animate({
			left: firstTabPos.left-15,
			top: firstTabPos.top-9
		}, 200, function() {
			setTabContent(which);
			if($('.tab-bg').css('display') == 'none')
			{
				$('.tab-bg').css('display', 'inline');
			}
		});*/
	}
	else
	{
		$('.tab-bg').css('left',firstTabPos.left-15);
		$('.tab-bg').css('top',firstTabPos.top-9);
	}
    
    
}

function setTabContent(which)
{
	var title;
	var description;
	if(which == 'tab-worldwide-access')
	{
		title = 'Worldwide Access';
		description = 'Thanks to the data storing on the <span class="highlight">Internet</span> and the <span class="highlight">browser independency</span>, it is possible to connect to your ideas from everywhere, regardless whether you are at work or at home!';
	}
	if(which == 'tab-speed')
	{
		title = 'Speed';
		description = 'During the development of MindPosting it has been emphasised on speed. First of all the application itself has been kept quite simple and an <span class="highlight">AutoLogin</span> technique has been developed (you can get the URL in the Settings window after login) which offers a fast way to access to your wall without login!';
	}
	if(which == 'tab-user-friendliness')
	{
		title = 'User Friendliness';
		description = 'Thanks to the JQuery library, it was possible to create a <span class="highlight">self-explanatory interaction</span>. You can move your post-its to the desired position, resize and delete them and create new ones. Each function can be also executed with <span class="highlight">Key-Shortcuts</span>.';
	}
	if(which == 'tab-anonymity')
	{
		title = 'Anonymity';
		description = 'To register and make use of this service no type of private data is needed. You only have to set a <span class="highlight">username and a password</span>, that’s it! So, what are you waiting for?!';
	}
	
	$('#tab-title').fadeTo(100, 0, function(){
		$('#tab-title').html(title);
		$(this).fadeTo(100, 1);
	});
	$('#tab-description').fadeTo(100, 0, function(){
		$(this).html(description);
		$(this).fadeTo(100, 1);
	});
}

function showLogin()
{
	
	/*
	var element = "<table id='login_table' width='100%' height='100%'><tr><td>";
	element += "<center><div id='login_box'>";
	element += "<table>";
	element += "<tr><td colspan='2'><input type='text' class='login_field' id='login_user_field' value='' /></td></tr>";
	element += "<tr><td colspan='2'><input type='password' class='login_field' id='login_pw_field' value='' /></td></tr>";
	element += "<tr><td><div id='login_newmember'></div></td><td><div id='login_submit'></div></td></tr>";
	element += "</table>";
	element += "</div></center>";
	element += "</td></tr></table>";
	
	$('body').append(element);
	
	$('.mindposting-title').css('display', 'block');
	
	*/
	
	$(":input").focus(function (){
		fId = $(this).attr("id");
		if(fId == 'login_user_field' || fId == 'login_pw_field')
		{
			siteFocussedForm = 'login';
		}
		if(fId == 'register_user_field' || fId == 'register_pw_field' || fId == 'register_pw2_field')
		{
			siteFocussedForm = 'register';
		}
	}).blur(function (){
		siteFocussedForm = '';
	});
	
	$('#login_user_field').focus();
	
	$('#main-table').keyup(function(e) {
		if(e.keyCode == 13) {
			if(siteFocussedForm == 'login')
				submitLogin();
			if(siteFocussedForm == 'register')
				registerMember();
			
		}
	});
	$('#login_submit').bind("click", function(e){
		submitLogin();
	});
	
	$('#register_submit').bind("click", function(e){
		registerMember();
	});
	
	initTabs();
	//showRegister();
	
	//self.location.href='index.php';
}

function registerMember()
{
	var user = $('#register_user_field').val();
	var pw = $('#register_pw_field').val();
	var pw2 = $('#register_pw2_field').val();
	
	if(user == '' || pw == '' || pw2 == '')
	{
		showMessage('Please fill all the fields!', 0);
	}
	else if(pw !== pw2)
	{
		showMessage('Passwords are not equal!', 0);
	}
	else if(user.length < 3)
	{
		showMessage('Username too short (min. 3 characters)!', 0);
	}
	else if(pw.length < 5)
	{
		showMessage('Password too short (min. 5 characters)!', 0);
	}
	else if(!specialCharsValidation(user))
	{
		showMessage('Username contains special characters!', 0);
	}
	else
	{
		$.ajax({
			   type: "POST",
			   url: "./php/login.php",
			   data: "todo=checknewmember&user="+user+"&pw="+pw,
			   dataType:"json",
			   success: function(data){
					if(data['checknewmember']['msg'] == "1")
					{
						showMessage('Successfully registered! You can now login!', 1);
						clearSiteFields();
					}
					else
					{
						showMessage(data['checknewmember']['details'], 0);
					}
			   }
			 });
	}
	
}

function submitLogin(al)
{
	var parms;
	if(!al) {
		var user = $('#login_user_field').val();
		var pw = $('#login_pw_field').val();
		parms = 'user='+user+'&pw='+pw;
	}
	else {
		parms = 'al='+al;
	}
	
	$.ajax({
		   type: "POST",
		   url: "./php/login.php",
		   data: "todo=checklogin&"+parms,
		   dataType:"json",
		   success: function(data){
				if(data['login']['msg'] == "1")
				{
					$('body').html("");
					addNeededDivs();
					setLoading(1);
					loadSettings();
					loadData();
					bindKeys();
					hideMessage();
					$('.mindposting-title').css('display', 'none');
					
				}
				else
				{
					//showLogin();
					showMessage("Wrong username or password!", 0);
				}
		   }
		 });
	
	/*$.post("./php/login.php?todo=checklogin&"+parms,
	        function(data){
				if(data['login']['msg'] == "1")
				{
					$('#login_table').remove();
					setLoading(1);
					loadSettings();
					loadData();
					bindKeys();
					hideMessage();
				}
				else
				{
					showMessage("Wrong username or password!", 0);
				}
	    	});*/
}


function addNeededDivs()
{
	// CSS
	var element = '<link id="main-css" rel="stylesheet" type="text/css" href="./css/main.css" />';
	element += '<link id="loading-css" rel="stylesheet" type="text/css" href="./css/loading.css" />';
	element += '<link id="settings-css" rel="stylesheet" type="text/css" href="./css/settings.css" />';
	
	$('head').append(element);
	$('#site-main-css').remove();
	$('#site-tabs-css').remove();
	
	
	// DIVS
	element = "<div id='loading-mask'><table width='100%' height='100%'><tr><td><center><div id='loading-gif-big'></div></center></td></tr></table></div>";
	element += "<div id='loading-small'><div id='loading-gif-small'></div></div>";
	element += "<div id='message'></div>";
	
	element += "<div class='logout'></div>";
	element += "<div class='add'></div>";
	element += "<div class='save'></div>";
	element += "<div class='settings'></div>";
	element += "<div class='theme'></div>";
	element += "<div class='bin'></div>";
	element += "<div class='change'></div>";
	element += "<div class='info'><div id='info-title'></div><div id='info-body'></div></div>";
	element += "<div class='mindposting-title'></div>";

	$('body').append(element);
	
	
}
