$(function() {
	var qs = window.location.search.substring(1);	
	if (qs == "do=login"){
		openModal('#modalContent');
	}
	$('#cityMenu').change(function() {
		var city = $('#cityMenu :selected').val();
		if(city != "nochange") {
			document.location='http://www.finditlocal.co.uk/'+city;
		}
	});
	$('#indexLoad').html('<div align="center"><img src="image/loading.gif" alt="Loading..." /></div>');	   
	$("#indexLoad").load('indexLoad.php');
	$(".navLink").click(function() {
	    $('#indexLoad').html('<div align="center"><img src="image/loading.gif" alt="Loading..." /></div>');
		var letter = $(this).attr('id');
		$('#indexLoad').load('indexLoad.php?show='+letter);
	});
	function displayErrorMessage(msg) {
		$('#modalBody').html('<div id="errorMessage" style="display:none"><p>'+msg+'</p></div>');
		$('#errorMessage').fadeIn(1500);
		$('<p style="display:none" id="closeTag">Press the Escape (Esc) key or click on the X in the corner to close the window.</p>').appendTo('#modalBody');
		$('#closeTag').delay(5000).fadeIn(600);
		
	}
	function openModal(elementID) {
		$(elementID).modal({
			opacity:85,
			overlayId:"modalBG",
			containerId:"modalContainer",
			dataId:"modalData",
			escClose:true,
			closeClass:"closeMe",
			focus:true,
			onOpen: function(e) {
				e.overlay.fadeIn('slow', function() {
					e.container.slideDown('slow', function(){
						e.data.fadeIn('fast');
					});
				});
			},
			onShow:function(e){
 			  setTimeout(function(){e.data.find("input#emailAddress").focus();},1500);
            },
			onClose: function(e) {
				e.data.hide(1, function() {
					e.container.slideUp(200, function() {
						e.overlay.fadeOut(500, function() {
							$.modal.close();
						});
					});
				});
			}
		});
	}

	$('.openModal').click(function(e) {
		e.preventDefault();						   
		openModal('#modalContent');
	});
	$("#loginForm").submit(function(e){
		e.preventDefault();
		if (!$('.modalError').is(':hidden')) {
			$('.modalError').hide();
		}
		var getHeight = $('#modalContainer').css('height');
		if (getHeight == "270px") {
			$('#modalContainer').css({height:'250px'});
		}
		$('#loginButton').hide();
		$('#loadingIcon').fadeIn(1000);
		var formData = $(this).serialize();
		
		$.ajax({
			type:"POST",
			url:"takeLogin.php",
			data:formData,
			success: function(response){
				
				if ($.isNumeric(response)){
					if (response == 1){
						document.location='http://www.finditlocal.co.uk/account/';
					} else if (response == 2){
						$('#modalContainer').css({height:'270px'});
						$('.modalError span').html('INVALID LOGIN - Please check your credentials and try again.');
						$('.modalError').fadeIn(1000);
						$('#loadingIcon').hide();
						$('#loginButton').fadeIn(1000);
						$('#password').val('');
						if (!$('#emailAddress').val()) {
							$('#emailAddress').focus();
						} else{
							$('#password').focus();
						}
					} else if (response == 3){
						$('#modalContainer').css({height:'270px'});
						$('.modalError span').html('ACTIVATION ERROR - Your account has not been activated yet. Please check your email.');
						$('.modalError').fadeIn(1000);
						$('#loadingIcon').hide();
						$('#loginButton').fadeIn(1000);
						if (!$('#emailAddress').val()) {
							$('#emailAddress').focus();
						} else{
							$('#password').focus();
						}
					}
				} else {
					var linkCheck = response.substr(0,3);
					if (linkCheck == "URL") {
						var stringLength = response.length;
						var getLink = response.substr(3,stringLength);
						document.location=getLink;
					} else {
						displayErrorMessage(response);
					}
				}
			}
		});
		
	});
});
