function callbackfunc(f){
  	$.ajax({
		type: "GET",
		url: "/names.nsf?Login&requesttype=ajax",
		dataType: "html",
		data: f,
		success: function(html, textStatus){
			if (textStatus.toLowerCase() == "success") {
				var loginstatus = $("#loginstatus",$(html)).children().eq(0).val();
				if (loginstatus && loginstatus != "") {
		  			var loginerrormessage = $("#loginstatus",$(html)).children().eq(1).val();
		  			promptForLogin(loginerrormessage);
		  		} else {
		  			window.location.reload();
		  		}
  			}
		}
	});
}

function promptForLogin(errorMessage) {
	var errorHTML = "";
	if (errorMessage) {
		errorHTML = '<tr><td colspan="2"><p>' + errorMessage + '</p></td></tr>';
	}
	if ($("#login").attr('href') == '?Login') {
		var html = '<div id="logincontainer" title="Login"><table id="logintable" cellspacing="0" cellpadding="0" border="0">' +
			errorHTML +
			'	<tr>' +
			'	<td><label>Brugernavn</label></td>' +
			'	<td><input type="text" name="Username" /></td>' + 
			'	</tr>' +
			'	<tr>' +
			'	<td><label>Kodeord</label></td>' +
			'	<td><input type="password" name="Password" /></td>' +
			'	</tr>' +
			'</table></div>';	
		$("body").append(html);
		$("#logincontainer").dialog({
			width: 360,
			modal: true,
			close: function() {
				$("#logincontainer").remove();
			},
			position: ['center',100],
			buttons: {
				"Annuller": function(){
					$(this).dialog("close");
				},
				"Ok": function() {
					var f = {
						username: $("input[name='Username']").val(),
						password: $("input[name='Password']").val()
					};
					callbackfunc(f);
					$(this).dialog("close");
				}
			},
			resizable: false
		});
	} else {
		location.href = siteMgr.getAppDbURL() + '?Logout&RedirectTo=' + location.href;
	} 

}

/*
 * Validation functions
 */
function toggleErrorClass(fieldName, isError) {
	if (isError) {
		$("[name='" + fieldName + "']").addClass("error");
	} else {
		$("[name='" + fieldName + "']").removeClass("error");
	}
}

function toggleSelectError(fieldName, isError) {
	if (isError) {
		$("[name='" + fieldName + "']").wrap('<span style="display:inline-block" class="error"></span>');
	} else {
		$parent = $("[name='" + fieldName + "']").parent();
		$parent.before($("[name='" + fieldName + "']")).remove();
	}
}

function minLength(str, mlength) {
	if (str.length >= mlength) {
		return true;
	} else {
		return false;
	}
}

function isAlphabet(str) {
	var alphaExp = /^[a-zA-Z\u00E6\u00F8\u00E5\u00C6\u00D8\u00C5]+$/;
	if (str.match(alphaExp)) {
		return true;
	} else {
		return false;
	}
}

function isNumeric(num) {
	var numericExpression = /^[0-9]+$/;
	if (num.match(numericExpression)) {
		return true;
	} else {
		return false;
	}
}

function emailValidator(str){
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(str.match(emailExp)){
		return true;
	} else {
		return false;
	}
}

function isZip(num) {
	var emailExp = /^[0-9]{4}$/;
	if(num.match(emailExp)){
		return true;
	} else {
		return false;
	}
}
/*
 * Validation functions end
 */

function cleanupFlash() {
	$("#videocontainer").empty();
}

function showVideoCallback() {
	$("#videocontainer").dialog( 'destroy' );
	$("#videocontainer").remove();
}

function showThisVideo(videoId, videoTitle) {
	var vTitle = "Video";
	var vTitleSupplied = false;
	if (typeof videoTitle == "string") {
		vTitle = videoTitle;
		vTitleSupplied = true;
	}
	$("body").append('<div id="videocontainer" style="display:none" title="' + vTitle + '"><div id="p_' + videoId + '"></div></div>');
	$.getScript("http://webtvadmin.semaphor.dk/semaphor/flv.nsf/MediaLookupByIdPlayerJS/" + videoId + "?open&command=videoinabox", 
		function(){
			var movieInfo = embedVideo();
			//var movieInfo = ["",""];
			if (!vTitleSupplied) {
				$("#videocontainer").attr("title", unescape(movieInfo[0]));
			}
			var dialogWidth = parseInt($("#videocontainer").children().eq(0).attr("width")) + 22;

			$("#videocontainer").dialog({
				close: showVideoCallback,
				modal: true,
				resizable: false,
				position: ['center',50],
				buttons: {
					"Luk": function(){
						$(this).dialog("close");
					}
				},
				width: dialogWidth + "px",
				beforeclose: cleanupFlash
			});
			if (unescape(movieInfo[1]) != "") {
				$(".ui-dialog-buttonpane").prepend('<div style="width:80%;float:left;margin:0.5em 0.4em 0.5em 0;padding:0.2em 0.6em 0.3em;">' + unescape(movieInfo[1]) + '</div>');
			}
			//$("#videocontainer").dialog("open");
		}
	);
	return false;
}

$(function() {
	$(".contentcol img").parent("a").css("border","none");
	$(".top-item-login a").bind('click', function() {
		promptForLogin(); 
		return false;
	});
	$(document).bind("keyup", function(e) {
		switch (e.which) {
			case 13:
				if ($("#logincontainer").dialog('isOpen')) {
					$("button:contains('Ok')").click();
					
				}
				break;
		}
	});
});
$(window).load(function() {
	$("[onclick*='showThisVideo']").each(function(i, element) {
		var $element = $(element); 
		if ($element.children("img").size() > 0) {
			var top = ($element.children("img").eq(0).height() - 48)/2;
			var left = ($element.children("img").eq(0).width() - 48)/2;
			var $span = $('<span class="play-icon" style="top:' + top + 'px;left:' + left + 'px"></span>').bind("click", $element.attr('onclick'));
			$element.wrap('<div style="position:relative;width:' + $element.width() + 'px"></div>').before($span);
		}
	});
	$(".play-icon").ifixpng();
});


