var currentRegisterPage = 0;

$(document).ready(function() {
	$('form[name="searchAddress"]')																																																																																																																																																																																																																																																																																																																																																																																																																																																																																
		.submit(function() {
			return search(); 
		});
	$('form[name="searchRoute"]')
		.submit(function() { 
			return searchRoute(); 
		});


	$("#btnShowRouteSearch")
		.click(function() {switchMenu("#routeSearchDiv")});
		
	$("#btnShowAddressSearch")
		.click(function() {switchMenu("#addressSearchDiv")});

	$('#btnShowPrintRoute')
		.click(function() { printRoute(); });
	$('#btnPrintRoute')
		.click(function() { printRoute(); });


	/* setup focus effect */
	$('input.text, input.smalltext')
		.focus(function() { 
			if($(this).val() == $(this).prev('label').text()) {
				$(this).val("").addClass('active');
			}
		})
		.blur(function() { 
			if ($(this).val() == '') {
				$(this).val($(this).prev('label').text()).removeClass('active');
			}
		})
		.each(function() {
			$(this).val($(this).prev('label').text());
		});
	
	setupRegister();

	// intialize the map
	dkk_load();
	setTimeout("createFeaturedMarkers()",500);

	$.post('/ajax/doAction.php', { 'action' : 'index', 'q': 'null' });
});

$(document).unload(function() {
	// unload the map
	dkk_unload()
});

function switchMenu(action) {
	if (
			action != '#addressResultsDiv' && 
			action != '#addressSearchDiv' &&
			action != '#routeSearchDiv' &&
			action != '#routeResultsDiv'
				) {
		$.post('/ajax/doAction.php', { 'action' : action });
	}
	if(action == "#registerDiv" || action == "#addressResultsDiv" || action == "#addressSearchDiv") {
		$('#addressSearchDiv').show();
	} else {
		$('#addressSearchDiv').hide();
	}
	if (action == "#routeSearchDiv" || action == "#routeResultsDiv") {
		$('#routeSearchDiv').show();
	} else {
		$('#routeSearchDiv').hide();
	}
	if (action == "#registerDiv") {
		$('#createLogoDiv').hide();
		$('#infoBox').show();
		$('#registerDiv .scrollContainer')
			.css('width', ((327 + 24) * 3) + 'px' );
	} else {
		$('#createLogoDiv').show();
		$('#infoBox').hide();
		$('#registerDiv .scrollContainer')
			.css('width', ((327 + 24) * 1) + 'px' );
	}

	$('#registerDiv').hide();
	$('#routeResultsDiv').hide();
	$('#addressResultsDiv').hide();
	$('#registerCompleteDiv').hide();

	$(action).show();
}

function gotoRegisterPage(page) {
	if (currentRegisterPage == page) {
		return;
	}
	showRegisterPageButtons(page);
	currentRegisterPage = page;
	$("#registerDiv .scroll")
		.trigger('goto', [ page ]);
}

function showRegisterPageButtons(page) {
	var inpName 		= $('#registerDiv input[name="compname"]'),
		inpAddress 		= $('#registerDiv input[name="compaddress"]'),
		inpZip 			= $('#registerDiv input[name="compostalcode"]'),
		inpCity 		= $('#registerDiv input[name="compcity"]'),
		inpEmail 		= $('#registerDiv input[name="compemail"]'),
		inpPhone 		= $('#registerDiv input[name="compphone"]'),
		inpContact 		= $('#registerDiv input[name="comatt"]'),
		inpSite 		= $('#registerDiv input[name="compurl"]'),
		inpPreview 		= $('#registerDiv input[name="compimgurl"]'),
		inpCategories 	= $('#registerDiv input[name="compcategory"]'),
		inpNewCategory 	= $('#registerDiv input[name="compnewcategory"]'),
		inpKeywords 	= $('#registerDiv input[name^="compkeyword"]'),
		allFields = $([])
			.add(inpName).add(inpAddress).add(inpZip).add(inpCity).add(inpEmail).add(inpContact)
			.add(inpSite).add(inpPreview).add(inpCategories).add(inpNewCategory).add(inpKeywords),
		queryString = "";
	allFields.each(function() {
		queryString += '&' + $(this).attr('name') + '=' + $(this).val();
	});
	$.post('/ajax/doAction.php', { 'action' : '#btnRegister'+page, 'q' : queryString });
	
	if (page == 0) {
		$('#btnRegister1Next').show();
		$('#btnRegister2Previous').hide();
		$('#btnRegister2Next').hide();
		$('#btnRegister3Previous').hide();
		$('#btnDoRegister').hide();
	} else if (page == 1) {
		$('#btnRegister1Next').hide();
		$('#btnRegister2Previous').show();
		$('#btnRegister2Next').show();
		$('#btnRegister3Previous').hide();
		$('#btnDoRegister').hide();
	} else if (page == 2) {
		$('#btnRegister1Next').hide();
		$('#btnRegister2Previous').hide();
		$('#btnRegister2Next').hide();
		$('#btnRegister3Previous').show();
		$('#btnDoRegister').show();
	}
}

function setupRegister() {
    var scrollOptions = {
        target: '.scroll', 	
        items: '.registerPage',
        axis: 'x',
        cycle: false,
        force: true,
        duration: 500
    };

    $("#registerDiv").serialScroll(scrollOptions);

    $("#infoBox").click(function() {
    	$("#infoBox").hide();
    });

	$('#btnSignup')
		.click(function() { 
			gotoRegisterPage(0);
			switchMenu('#registerDiv');
		});
		
	$("#dlgSelectPreview").hide();
	
	$("#btnSelectPreview")
		.click(function() {
			IEprompt('Indtast din webside adresse her:', 'URL', function(url) {
				var url = "http://open.thumbshots.org/image.aspx?url="+url;
				$('#registerDiv input[name="compimgurl"]')
					.val(url);
				$('#registerDiv img.webpic').attr('src', url);
				$("#dlgSelectPreview").dialog('close');
			});
			$('#dlgSelectPreview').dialog('open');
		});

	$("#btnRegister1Next")
		.click(function() {
			gotoRegisterPage(1);
		});
	$("#btnRegister2Previous")
		.click(function() {
			gotoRegisterPage(0);
		});
	$("#btnRegister2Next")
		.click(function() {
			gotoRegisterPage(2);
		});
	$("#btnRegister3Previous")
		.click(function() {
			gotoRegisterPage(1);
		});

	$('form[name="register"]')
		.submit(function() { 
			return doRegister(); 
		});

	if (document.location.hash == '#registercomplete') {
		$('#registerCompleteDiv').show();
	}
		
	$('#registerDiv input[name="comppostalcode"]')
		.change(function() {
			if(postArray[$(this).val()]) {
				$('#registerDiv input[name="compcity"]')
					.val(postArray[$(this).val()]);
			}
		});

	$('#registerDiv input[name="compcity"]')
		.attr('readonly', 'readonly');
		
	$('#registerDiv input[name="compurl"]')
		.change(function() {
			var url = "http://open.thumbshots.org/image.aspx?url="+$(this).val();
			$('#registerDiv input[name="compimgurl"]')
				.val(url);
			$('#registerDiv img.webpic').attr('src', url);
		});

	var url = '/ajax/getCategories.php';
	$.getJSON(url, function(data) {
		var compcategory = $('#compcategory');
		compcategory
			.empty()
			.append($("<option/>")
			.text("Vælg branche"));
		$.each(data, function(i, option) {
			compcategory
				.append($("<option/>")
				.text(option.name)
				.val(option.id));
		});
	});
}

function doRegister() {
	return (checkRegister1() && checkRegister2() && checkRegister3());	
}


function checkField(fieldname, alertmessage, fieldpage) {
	var field = $('form[name="register"] input[name="'+fieldname+'"]');
	var page = $("#registerDiv .scroll");
	if(field.val() == "" || field.val() == field.prev('label').text()) {
		window.alert(alertmessage);
		field.focus();
		gotoRegisterPage(fieldpage);
		return false;
	}
	if (field.attr("type") == "checkbox")
	{
		if (field.attr("checked") == "")
		{
			window.alert(alertmessage);
			field.focus();
			gotoRegisterPage(fieldpage);
			return false;
		}
	}
	return true;
}

function checkRegister1() {
	if (!checkField('compname', 'Indtast venligst firmanavn', 0)) {
		return false;
	}
	if (!checkField('compaddress', 'Indtast venligst en adresse', 0)) {
		return false;
	}
	if (!checkField('comppostalcode', 'Indtast venligst et postnummer', 0)) {
		return false;
	}
	if (!checkField('compcity', 'Indtast venligst et bynavn', 0)) {
		return false;
	}
	if (!checkField('compemail', 'Indtast venligst en gyldig e-mail adresse', 0)) {
		return false;
	}
	if (!checkField('compphone', 'Indtast venligst et gyldigt telefonnummer', 0)) {
		return false;
	}
	if (!checkField('compatt', 'Indtast venligst dit navn', 0)) {
		return false;
	}
	if (!checkField('compurl', 'Indtast venligst url på din hjemmeside eks. www.internet.dk', 0)) {
		return false;
	}
	return true;	
}

function checkRegister2() {
	for (var i = 1; i <= 26; i++) {
		var kw = $('form[name="register"] input[name="compkeyword' + i + '"]');
		if (kw.val() == kw.prev('label').text()) {
			kw.val("");
		}
	}
	return true;
}

function checkRegister3() {
	var newCat = $('form[name="register"] input[name="compnewcategory"]');
	var categories = $('form[name="register"] #compcategory option:selected'); 
	if(newCat.val() == newCat.prev('label').text()) {
		newCat.val("");
	}
	if (categories.length < 1 && newCat.val() == "") {
		window.alert("Angiv venligst branchen for din virmsomhed");
		$('form[name="register"] #compcategory').focus();
		gotoRegisterPage(2);
		return false;
	}
	if (!checkField('conditions', 'Du mangler at acceptere at have læst vores betingelser', 2)) {
		return false;
	}
	return true;
}
