/*
INCLUDES
**********************************************/
// specify which javascripts to include on the webpage
// include the javascript 'f' on the webpage
//js('/files/javascript/SCRIPTNAME.js');
js('/files/javascript/jquery.autocomplete.min.js');
//js_defer('/files/javascript/pngfix.js');
/*
ON LOAD
*********************************************/
// jQuery 'onReady' script - runs when the document have been loaded, but before images
$(function(){
	toggleMainMenu();
	chooseLanguage();
	loadPostalcode();
	ie6Fix_zindex();
});

function resizeBanner() {
	$('.bannerbg').each(function() {
		alert($(this).parent().width() + 'px / ' + $(this).parent().height() + 'px');
		//$(this).parent().css('width', t.width() + 'px');
		//$(this).parent().css('height', t.height() + 'px');
	});
}

function ie6Fix_zindex() {
	var zIndexNumber = 1000;
	$('div').each(function() {
		$(this).css('zIndex', zIndexNumber);
		zIndexNumber -= 10;
	});
}

function chooseLanguage()
{
	// Er vi på forsiden?
	if($('#haschosenlang').length > 0)
	{
		// Må vi skifte profil eller er det allerede gjort?
		if ($('#haschosenlang').val() == 0)
		{
			// Variabler
			var profileid;
			var currentProfileid;
			var language;
			var dansk = "dansk";
			var engelsk = "english";
			var svensk = "svensk";
			var norsk = "norwegian";
			var finsk = "finsk";
			
			// Find browsersprog
			if (navigator.appName == 'Netscape')
			{
				language = navigator.language;
			}
			else
			{
				language = navigator.browserLanguage;
			}

			var code = language.substring(0,2); // Sæt browsersprog
			
			// Passer vores profil allerede til browsersproget?
			currentProfileid = $('#currentcountry').val().toLowerCase();
			
			// AJAX: Hent profilid fra serveren
			$.get(
				"/dynamic.aspx?data=checkprofile",
				"",
				function(data) {
				
					//profileid = data.substring(data.indexOf("<body id='pageDynamic'>") + 23, data.indexOf("</body>")).toLowerCase();
					
					// Behandl data
					if (code == "da") // Dansk
					{
						if (currentProfileid != dansk)
						{
							// Skift profilid
							$.get(
								"/dynamic.aspx?data=checkprofile&profileid=" + dansk,
								"",
								function(data) { },
								"html"
							);
							// Redirekt
							var loc = "http://" + window.location.hostname + "/default.aspx?profile=" + dansk;
							if (window.location != loc)
							{
								window.location = "/default.aspx?profile=" + dansk;
							}
						}
					}
					else if (code == "en") // Engelsk
					{
						if (currentProfileid != engelsk)
						{
							// Skift profilid
							$.get(
								"/dynamic.aspx?data=checkprofile&profileid=" + engelsk,
								"",
								function(data) { },
								"html"
							);
							// Redirekt
							var loc = "http://" + window.location.hostname + "/default.aspx?profile=" + engelsk;
							if (window.location != loc)
							{
								window.location = "/default.aspx?profile=" + engelsk;
							}
						}
					}
					else if (code == "sv") // Svensk
					{
						if (currentProfileid != svensk)
						{
							// Skift profilid
							$.get(
								"/dynamic.aspx?data=checkprofile&profileid=" + svensk,
								"",
								function(data) { },
								"html"
							);
							var loc = "http://" + window.location.hostname + "/default.aspx?profile=" + svensk;
							if (window.location != loc)
							{
								window.location = "/default.aspx?profile=" + svensk;
							}
						}
					}
					else if (code == "fi") // Finsk
					{
						if (currentProfileid != finsk)
						{
							// Skift profilid
							$.get(
								"/dynamic.aspx?data=checkprofile&profileid=" + finsk,
								"",
								function(data) { },
								"html"
							);
							var loc = "http://" + window.location.hostname + "/default.aspx?profile=" + finsk;
							if (window.location != loc)
							{
								window.location = "/default.aspx?profile=" + finsk;
							}
						}
					}
					else if (code == "no") // Norsk
					{
						if (currentProfileid != norsk)
						{
							// Skift profilid
							$.get(
								"/dynamic.aspx?data=checkprofile&profileid=" + norsk,
								"",
								function(data) { },
								"html"
							);
							var loc = "http://" + window.location.hostname + "/default.aspx?profile=" + norsk;
							if (window.location != loc)
							{
								window.location = "/default.aspx?profile=" + norsk;
							}
						}
					}
					else
					{
						// Skift profilid
						$.get(
							"/dynamic.aspx?data=checkprofile&profileid=" + engelsk,
							"",
							function(data) { },
							"html"
						);
						var loc = "http://" + window.location.hostname + "/default.aspx?profile=" + engelsk;
						if (window.location != loc)
						{
							window.location = "/default.aspx?profile=" + engelsk;
						}
					}
				},
				"html"
			);
		}
		else
		{
		}
	}
}

//Postalcode control
function loadPostalcode()
{
	if ($.isFunction($(this).autocomplete)) {
		$('.jsPostalCode')
			.autocomplete("/dynamic.aspx?data=citynames", {
				formatItem: function(row, i, max) {
					return row[0].replace("_", " ");
				},
				extraParams: {
					country: $('input[name="countryhidden"]').val()
				},
				max: 4,
				delay: 100,
				width: '200px'
			})
			.result(function(event, data, formated){
				var result = data[0].split("_");
				$(".jsPostalCode").val(result[0].replace(/<!DOCTYPE.*?>/gi,''));
				if ($(".jsPostalCode").val() != "") {
					$(".jsCity").val(result[1]);
				} else {
					$(".jsCity").val("");
				}
			})
			.keydown(isNumberKey)
		;
	
		$(".jsCity, .jsCity2").attr("readonly", true).addClass("disabled");
		
		$('.jsPostalCode2')
			.autocomplete("/dynamic.aspx?data=citynames", {
				formatItem: function(row, i, max) {
					return row[0].replace("_", " ");
				},
				extraParams: {
					country: $('input[name="countryhidden"]').val()
				},
				max: 4,
				delay: 100,
				width: '200px'
			})
			.result(function(event, data, formated){
				var result = data[0].split("_");
				$(".jsPostalCode2").val(result[0].replace(/<!DOCTYPE.*?>/gi, ""));
				if ($(".jsPostalCode2").val() != "") {
					$(".jsCity2").val(result[1]);
				} else {
					$(".jsCity2").val("");
				}
			});
	}
} //end postalcode

function isNumberKey(evt){
	//var charCode = (evt.which) ? evt.which : event.keyCode
	var charCode = (window.event)? event.keyCode : evt.which;

	if ((charCode > 47 && charCode < 58) || (charCode > 95 && charCode < 106) || (charCode == 8) || (charCode == 9) || (charCode == 12) || (charCode == 27) || (charCode == 37) || (charCode == 38) || (charCode == 39) || (charCode == 46)){
		return true;
	}
	return false;
}
