
MY = {};

MY.parseUri = function  (str) {
	var	o   = MY.parseUri.options,
	m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
	uri = {},
	i   = 14;

	while (i--) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});

	return uri;
};

MY.parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
		name:   "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};

MY.url = MY.parseUri( document.location );

MY.openDialog = function(dialog) {
	$(dialog).dialog("open");
}


$(function() {

});


$(document).ready(function() {
	
	$("button").button();
	$("a.btn").button();
	$("input[type=submit]").button();
	
	$("select.combobox").combobox();
	$("form.validate").validate();
	$("#tabs").tabs();
	$(".accordion").accordion({
		collapsible: true,
		navigation: true,
		autoHeight: false
	});
	$(".toggle").next().hide();
	$(".toggle").click(function(){
		$(".toggle span:first-child").toggleClass('ui-icon-triangle-1-s');
		$(this).next().toggle('fast');
	});
	

	$(".logoslide").cycle({
		delay: 2000,
		timeout: 6000,
		speed: 2000
	});

	$(".portalslide").cycle({
		delay: 2000,
		timeout: 6000,
		speed: 2000
	});

	$(".portalsliderole").cycle({
		delay: 2000,
		timeout: 6000,
		fx: 'custom',
		cssBefore: {top: -232},
		animIn:  {top: 0},
		animOut: {top: 232}
	});

	$('#navlist li').hover(
		function () {  
			//show its submenu  
			$('ul', this).fadeIn(100);  
		},
		function () {  
			//hide its submenu  
			$('ul', this).fadeOut(50);           
		}
	);

	$('input.q').focus(function(){
		$(this).val("");
	});
	
	// hide arrangor
	$("#kursarrangjor").change(function() {
		if ('0' == $(this).val()) {
			$(this).parent().next("div.field").show('fast');
		} else {
			$(this).parent().next("div.field").hide('fast');
		}
	})
	.change();

	// dialog
	$("div.MYdialog").each(function() {
		//Keep any defined width
		var dialogWidth = "650px";
		if($(this).css("width")){
			dialogWidth = $(this).css("width");
		}
		$(this).dialog({
			"position": ["center","center"],
			autoOpen: false,
			width: dialogWidth
		});

	});

	// dialog starter with popup
	$(".MYpopup").click(function(el) {
		var url = MY.parseUri( this.href );
		if( url.anchor && $("#"+url.anchor).hasClass("MYdialog")){
			MY.openDialog($("#"+url.anchor));
		}
		return false;
	});

	// url popup
	if (MY.url.anchor && $("#"+MY.url.anchor).hasClass("MYdialog")){
		MY.openDialog($("#"+MY.url.anchor));
	}

	// confirm
	$(".confirm").click(function(el) {
		var url = MY.parseUri( this.href );
		if( url.anchor && $("#"+url.anchor).hasClass("dialog-confirm")){
			$("#"+url.anchor).dialog("open");
		}
		return false;
	});

	$(".dialog-confirm").dialog({
		resizable: true,
		height:140,
		modal: true,
		autoOpen: false,
		buttons: {
			"Slett": function() {
				$(this).dialog( "close" );
				$(this).children("form").submit();
			},
			"Ikke slett": function() {
				$(this).dialog( "close" );
			}
		}
	});


});



	

