﻿//-------------------------------------
// styleInputBoxes
//-------------------------------------
function styleInputBoxes() {
	//
	var inputBoxHeight = 14;
	//
	var $InputBox = $(this);
	var $span = $("<span />");
	var $label = $InputBox.parent("label");
	$span.addClass($InputBox.attr("type"));
	$span.css(inputBoxGetBgPosition(this.checked, 1));
	// bind inputBox events
	$InputBox.before($span);
	$InputBox.change(inputBoxClear);
	$InputBox.bind("error", inputBoxError);
	// hide inputBox
	$InputBox.css("display", "none");
	// bind span events
	// In Firefox the label tag gets the mouseup event !!
	if (!$span.parent().is("label")) {
		$span.mouseup(inputBoxCheck);
	}
	$span.hover(inputBoxOver, inputBoxClear);
	$span.mousedown(inputBoxDown);
	// bind label event
	if($span.parent().is("label")){
		$span.parent().mousedown(inputBoxDown);
		if($.browser.msie){
			$span.parent().mouseup(inputBoxCheck);
		}
	}
	// bind document event
	$(document).mouseup(inputBoxClear);

	function inputBoxGetBgPosition(checked, state) {
		var multi = state + ((checked) ? 3 : 0) - 1;
		return {
			"background-position": "0 -" + (inputBoxHeight * multi) + "px"
		}
	}
	//-------------------------------------
	// inputBox methods
	//-------------------------------------
	function inputBoxClear() {
		var $InputBoxes = $(":checkbox, :radio");
		$InputBoxes.map(function() {
			var $InputBox = $(this);
			var $span = $InputBox.prev();
			$span.css(inputBoxGetBgPosition($InputBox[0].checked, 1));
		});
	}
	function inputBoxOver() {
		var $span = ($(this).is("label")) ? $(this).children("span") : $(this);
		var $InputBox = $span.next();
		$span.css(inputBoxGetBgPosition($InputBox[0].checked, 2));
	}
	function inputBoxDown() {
		var $span = ($(this).is("label")) ? $(this).children("span") : $(this);
		var $InputBox = $span.next();
		$span.css(inputBoxGetBgPosition($InputBox[0].checked, 3));
	}
	function inputBoxError() {
		var $InputBox = $(this);
		var $span = $InputBox.prev();
		$span.css(inputBoxGetBgPosition(false, 6));
	}
	function inputBoxCheck() {
		var $span = ($(this).is("label")) ? $(this).children("span") : $(this);
		var $InputBox = $span.next();
		$InputBox[0].checked = !$InputBox[0].checked;
		$InputBox.click()
	}
}
//-------------------------------------
// styleSelectBoxes
//-------------------------------------
//var selectWidth = 152;
var selectWidth;
var selectHeight = 22;
function styleSelectBoxes() {
	var $SelectBox = $(this);
	var $label = $SelectBox.parent("label");

	var styleWidth = parseInt($SelectBox.css("width").substr(0,$SelectBox.css("width").length-2))+10;	
	selectWidth = (!isNaN(styleWidth)) ? Number(styleWidth) : $SelectBox.outerWidth()+10;

	$SelectBox.addClass("styled");
	$SelectBox.css("width", selectWidth + "px");
	$SelectBox.css("height", selectHeight + "px");

	$Options = $SelectBox.children("option");
	var ActiveString = $Options.filter(":selected").text();
	var $Outer = $("<span />");
	$Outer.addClass("selectOuter");
	var $Inner = $("<span />");
	$Inner.addClass("selectInner");
	$Inner.css("height", selectHeight + "px");
	
	var $Text = $('<span />');
	$Text.addClass("selectText");
	$Text.text(ActiveString);

	$Inner.append($Text);
	$Outer.append($Inner);
	$SelectBox.before($Outer);
	$Outer.append($SelectBox);
	//
	$Outer.css("width", selectWidth + "px");
	$Outer.css("height", selectHeight + "px");
	//
	$SelectBox.focus(selectFocus);
	$SelectBox.blur(selectBlur);
	$SelectBox.mouseover(selectOver);
	$SelectBox.mouseout(selectOut);
	
	$SelectBox.change(selectChange);

	function selectFocus() {
		var $SelectBox = $(this);
		var $Inner = $SelectBox.prev();
		var $Text = $Inner.children("span");
		$Inner.attr("class", "selectInner_focus");
		$Options = $SelectBox.children("option");
		var index = $Options.index(":selected") + 1;
		$SelectBox.keydown(function(event) {
			if (event.keyCode == 37 || event.keyCode == 38) {
				index = (index > 0) ? index - 1 : 0;
			} else if (event.keyCode == 39 || event.keyCode == 40) {
				index = (index + 1 < $Options.length) ? index + 1 : index;
			}
			$Text.text($Options.eq(index).text())
		});
	}
	function selectBlur() {
		var $SelectBox = $(this);
		var $Inner = $SelectBox.prev();
		$Inner.attr("class", "selectInner");
		$SelectBox.mouseout();
	}
	function selectOver() {
		var $SelectBox = $(this);
		$SelectBox.parent("span").attr("class", "selectOuter_over");
	}
	function selectOut() {
		var $SelectBox = $(this);
		$SelectBox.parent("span").attr("class", "selectOuter");
	}
	function selectChange() {
		var $SelectBox = $(this);
		var $Text = $SelectBox.prev().children("span");
		$Text.text($SelectBox.children("option").filter(":selected").text());
	}
}


//-------------------------------------
// styleFileInput
//-------------------------------------

var fileHeight = 33;

function styleFileInput() {
	var $FileInput = $(this);
	var $label = $FileInput.parent("label");
	fileWidth = $FileInput.width();

	$FileInput.addClass("styled");
	$FileInput.css("width", fileWidth + "px");
	$FileInput.css("height", fileHeight + "px");
	$FileInput.attr("size", parseInt((fileWidth-50)/14));

	var $Outer = $("<div />");
	$Outer.addClass("fileOuter");
	var $Inner = $("<div />");
	$Inner.addClass("fileInner");
	$Inner.css("height", fileHeight + "px");
	
	var $Text = $('<span class="fileText" />');	
	var $Button = $('<span class="fileButton">Gennemse</span>');

	
	var fileText = (String($FileInput.val()) != "") ? $FileInput.val() : "\r \n Vælg fil";
	$Text.text(fileText);

	
	$FileInput.before($Outer);
	$Outer.append($Inner);
	$Outer.append($FileInput);	
	//	
	$Outer.css("width", fileWidth + "px");
	$Outer.css("height", fileHeight + "px");	
	//
	$Inner.append($Button);	
	$Text.css("width", (fileWidth - 14 - $Button.outerWidth()) + "px");
	$Inner.append($Text);
	//
	$FileInput.focus(fileFocus);
	$FileInput.blur(fileBlur);
	$FileInput.mouseover(fileOver);
	$FileInput.mouseout(fileOut);
	$FileInput.change(fileChange);

	function fileFocus() {
		var $FileInput = $(this);
		var $Text = $FileInput.prev().children("span:eq(1)");
		$Text.attr("class", "fileText_focus");
	}
	function fileBlur() {
		var $FileInput = $(this);
		var $Text = $FileInput.prev().children("span:eq(1)");
		$Text.attr("class", "fileText");
		$FileInput.mouseout();
	}
	function fileOver() {
		var $FileInput = $(this);
		$FileInput.parent("div").attr("class", "fileOuter_over");
	}
	function fileOut() {
		var $FileInput = $(this);
		$FileInput.parent("div").attr("class", "fileOuter");
	}
	function fileChange() {
		var $FileInput = $(this);
		var $Text = $FileInput.prev().children("span:eq(1)");
		$Text.text($FileInput.val());
	}
}
//-------------------------------------
// styleFileInput
//-------------------------------------

var inputWidth = 287;
var inputHeight = 25;

function styleTextInput() {
	var $TextInput = $(this);
	var $label = $TextInput.parent("label");
	$TextInput.focus(textFocus);
	$TextInput.blur(textBlur);

	function textFocus() {
		$(this).toggleClass("inputFocus");
		if($(this).val() == this.defaultValue){  
			$(this).val("")
		}
	}
	function textBlur() {
		$(this).toggleClass("inputFocus");
		if($(this).val() == ""){  
			$(this).val(this.defaultValue)
		}
	}
}
//-------------------------------------
// styleFormFields
//-------------------------------------


function styleFormFields() {
	$("input:checkbox").each(styleInputBoxes);
	$("input:radio").each(styleInputBoxes);
	$("select").each(styleSelectBoxes);	
	$("input:file").each(styleFileInput);
	$("input:text, textarea").not("#search").each(styleTextInput);
}



//-------------------------------------
// TopSearchBox
//-------------------------------------
function setSearchBoxEvents(){
	
	var $Input = $('div#headerSearchInput input');
	$Input.focus(function() {  
		if(this.value != this.defaultValue){  
			this.select();  
		}
		$("#headerSearch").attr("class","headerSearchBGActive");
	});  
	$Input.blur(function() {  
		$("#headerSearch").attr("class","headerSearchBG");
	});
	var $Button = $("#headerSearchButtonMapArea");
	$Button.hover(
		function () {
			imgBehavior($("#headerSearchButtonImg"), 'over')
		}, 
		function () {
			imgBehavior($("#headerSearchButtonImg"), 'out')	
		}
	);	
	$Button.click(searchSubmit);
}
function searchSubmit(){
	var $Input = $('div#headerSearchInput input')
	if($Input.val() != "" && $Input.val() != $Input[0].defaultValue){
		var searchString = $Input.val().replace(/^\s+|\s+$/g, "");
		window.location.href = URL+"/resultat?search="+encodeURIComponent(searchString);
	}
}
//-------------------------------------
// Ligthbox
//-------------------------------------
function setLigthboxEvents(){
	var $Input = $('.lightBoxInputContainer input:text, .lightBoxInputContainer input:password');
				
	$Input.map(function(){
		$(this).attr("hasFocus","false")
		if ($(this).val() != ''){  
			$(this).css("color","#585858");
			$(this).prev().hide()
		} 		
	});
		
	var $Span = $('.lightBoxInputContainer span');
	
	$Span.mousedown(function(event){
		$(this).next().focus()
	});
	
	$Input.focus(function(event) {
		$(this).css("color","#585858");
		$(this).prev().hide()
		$(this).attr("hasFocus","true")
		$(this).parent('.lightBoxInputContainer').attr("class","lightBoxInputContainer lightBoxInputBg_focus");
		this.select()
	}); 
	
	$Input.blur(function(event) {
		$(this).attr("hasFocus","false")
		if (this.value == ''){  
			$(this).css("color","");
			$(this).prev().show()
		} else  {
			$(this).prev().hide()
		}
		$(this).parent('.lightBoxInputContainer').attr("class","lightBoxInputContainer lightBoxInputBg");
	});
	
	$Input.bind("error",function(){
		$(this).parent('.lightBoxInputContainer').attr("class","lightBoxInputContainer lightBoxInputBg_focus");
	});
	
	
	$('.lightBoxInputContainer input:password').keypress(function(event) {
		if (event.keyCode == 13) {
			var $form = $(this).parents("form");
			var $Span = $(this).parent().parent().siblings(".lightBoxHeading");
			$(this).parent().parent().next().children("a").click();
			return true
		} 
	});

	var $Button = $(".lightBoxButton a.grayButton");
	
	$Button.map(function(){
		$(this).attr("mshref",$(this).attr("href").split(":")[1]);
		$(this).attr("href","javascript:void(0)")
	});
	
	$Button.click(function(event) {
		var $form = $(this).parents("form");
		var $Span = $(this).parent().siblings(".lightBoxHeading");
		checkAndSubmitLoginForm($(this).parent().prev(), $(this).attr("mshref"), $Span[0]);
	});
}
//-------------------------------------
// Small SearchBox
//-------------------------------------
function setSmallSearchBoxEvents(){
	var $Input = $('#smallSearchBoxInputContainer input');
	
	$Input.focus(function(event) {
		$(this).parent().attr("class","smallSearchBoxInputBg_focus");
	});  
	$Input.blur(function(event) {
		$(this).parent().attr("class","smallSearchBoxInputBg");
	});
	$Input.keypress(function(event) {
		if (event.keyCode == 13) {
			if($(this).val() != ""){
				var searchString = $(this).val();
				searchString = searchString.replace(/ /g, "_");
				newListPage("search", searchString)
			}
			return false;
		} 
	});
	
	var $Button = $("#smallSearchBoxBtn");
	$Button.hover(
		function(event) {
			linkBehavior(this, 'over')
		}, 
		function(event) {
			linkBehavior(this, 'out')	
		}
	);
	$Button.click(smallSubmit);
}
function smallSubmit(){
	var $Input = $('#smallSearchBoxInputContainer input');
	if($Input.val() != ""){
		var searchString = $Input.val();
		searchString = searchString.replace(/ /g, "_");
		newListPage("search", searchString)
	}
}
//-------------------------------------
// denStorDanske
//-------------------------------------
function denStorDanske(){
		$Input = $('input#denStoreDanskeInputField')
		
		$Input.focus(function() {  
		if (this.value == this.defaultValue){  
			this.value = '';  
		}  
		if(this.value != this.defaultValue){  
			this.select();  
		}
	});  
	$Input.blur(function() {  
		if (this.value == ''){  
			this.value = this.defaultValue;  
		}
	});
	$("#denStoreDanskeSearchButtonMapArea").click(function(event) {
		window.open ("http://www.denstoredanske.dk/Special:Opslag?opslag=" + encodeURIComponent($('input#denStoreDanskeInputField').val()),"denStoreDanske");
	})
	$("#denStoreDanskeSearchButtonMapArea").hover(
		function () {
			imgBehavior($('#denStoreDanskeSearchButtonImg')[0], 'over')
		}, 
		function () {
			imgBehavior($('#denStoreDanskeSearchButtonImg')[0], 'out')	
		}
	);
}
//-------------------------------------
// Check and submit form
//-------------------------------------
function checkAndSubmitLoginForm(container, formFunction, span) {
	var errorCount = 0;		
	var $TextInputs = $("input:text, input:password, textarea", container)
	$TextInputs.each( function(index) {
		if(checkForm($(this))){			
			$(this).error()
			$(this).focus();
			displayErrorText(span, "Der er fejl og/eller mangler i de markerede felter")
			errorCount++
		}	
		/*else{			
			if($(this).attr("id")=="password2"){
				if($(this).val() !=  $("input#password", form).val()){
					$(this).error()
					$("input#password", form).error()
					$(this).focus();
					displayErrorText(span, "Gentag password")
					errorCount++
				}	else{
					$(this).blur()
					displayErrorText(span, "")
				}	
			}
		}	
		*/
		if(errorCount>0){
			return false;
		}
	});
	if(errorCount==0){
		eval(formFunction)
	}	
}
function displayErrorText(span, error_str){
	if(typeof(span.defaultText) == "undefined"){
		span.defaultText = $(span).text();
	}	
	if (error_str != "") {
		$(span).text(error_str)
		$(span).css("color","#F00");
	} else {
		$(span).text(span.defaultText)
		$(span).attr("style","");
	}

}
function checkForm($Input) {
	if ($Input.val() == "") {
		return true;
	}
	var RegExEmail= new RegExp("email");
	if (RegExEmail.test($Input.attr("id"))) {
		if (checkEmail($Input.val())) {
			return true;
		}
	}
	return false;
}
function checkEmail(txt, msg) {
	var RegExFilter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (RegExFilter.test(txt)) {
		if (txt.length >= 7) {
			if (txt.indexOf("@") >= 0) {
				if ((txt.indexOf("@") + 2) < txt.lastIndexOf(".")) {
					if (txt.lastIndexOf(".") < (txt.length - 2)) {
						return false;
					}
				}
			}
		}
	}
	return true;
}
//-------------------------------------
// List controller
//-------------------------------------
function ListController(paramObject) {
	$("#sort_books, #results_books, #results_authors, #sort_articles, #results_articles").change(function(){
		var $this = $(this);
		changeListPage($this.attr("id"), $this.val());		
	});
}
function mergeParameters(name, val, query){
	var pObj = {}
	pObj[name] = {"name":name, "value":val}
	
	if(typeof query == "undefined") query = $.getAllQueryStrings();
	return $.extend(true,query, pObj);	
}

function changeListPage(name, val){
	debug("changeListPage");
	var query = mergeParameters(name, val)
	
	var tab = "";
	if($("div.ui-tabs").length > 0){
	 var selected = $("div.ui-tabs").tabs('option', 'selected');
	 tab = $("ul.ui-tabs-nav a:eq("+selected+")").attr("href").replace("#","");
	 query = mergeParameters("tab", tab, query)
	}
	newQueryPage(query)
}

function newListPage(name, val){
	debug("newListPage");
	
	var query = mergeParameters(name, val)
	query = mergeParameters("page", "1", query)
	
	var tab = "";
	if($("div.ui-tabs").length > 0){
	 var selected = $("div.ui-tabs").tabs('option', 'selected');
	 tab = $("ul.ui-tabs-nav a:eq("+selected+")").attr("href").replace("#","");
	 query = mergeParameters("tab", tab, query)
	}
	newQueryPage(query)
}

function newQueryPage(query){
	var query_arr = [];
	for(var param in query){
		query_arr.push(query[param]);
	}
	
	var query_str = $.param(query_arr);

	var location = window.location.href;
	if(location.indexOf("#")>-1) location = location.split("?")[0];
	if(location.indexOf("?")>-1) location = location.split("?")[0];	
	
	window.location.href = URL+location+"?"+query_str
}
//-------------------------------------
// setSearchOnType
//-------------------------------------
function setSearchOnType() {
	$('div#headerSearchInput input').searchOnType({
		inittext: "Søgning: Indtast titel eller forfatter",
		// the default text in the search box
		searchURL: URL + "/layouts/ajax/typeahead.ashx?",
		//URL to search engine
		element: "#headerSearchInput",
		container: "#headerContent",		
		resultClass: "searchOnTypeResults",
		submitQuery: searchSubmit,
		top: 23,
		left: -21
	});
	

	
	
	var $AuthorSearch = $('div#smallSearchBox input#authorSearch')	
	if($AuthorSearch.length > 0){
		var letter = $.getQueryString({ID:"letter"});
		$AuthorSearch.searchOnType({
			inittext: "",
			// the default text in the search box		
			searchURL: URL + "/layouts/ajax/typeahead.ashx?" + ((letter != "undefined") ? "letter=" + letter + "&" : ""),			
			//URL to search engine
			element: "#smallSearchBoxInputContainer",
			container: "#mainContent",		
			format: "small",		
			resultClass: "searchOnTypeSmallResults",
			submitQuery: smallSubmit,
			type: "author",		
			width: 344,
			top: 26,
			left: -11
		});	
	}
	
	var $BookSearch = $('div#smallSearchBox input#bookSearch')
	if($BookSearch.length > 0){	
		var location = window.location.href;
		if(location.indexOf(".aspx")>-1) location = location.split(".aspx")[0];
		var location_arr = location.split("/").splice(2);
		location_arr = location_arr.splice(1, location_arr.length);	
		var category = location_arr.join("/");	
		$BookSearch.searchOnType({
			inittext: "",
			// the default text in the search box
			searchURL: URL + "/layouts/ajax/typeahead.ashx?" + ((category != "") ? "category=" + category + "&" : ""),
			//URL to search engine
			element: "#smallSearchBoxInputContainer",
			container: "#mainContent",		
			format: "small",		
			resultClass: "searchOnTypeSmallResults",
			submitQuery: smallSubmit,
			type: "title",		
			width: 344,
			top: 26,
			left: -11
		});	
	}
}
//-------------------------------------
// Run form scripts
//-------------------------------------
function setTopSelect(){
	var $Select = $("#headerSelectionBox select")
	$Select.change(function(){
		var $ThisOption = $(this)
		var val_arr = $ThisOption.val().split("|");		 
		if(val_arr.length > 1){
			window.open (val_arr[0],"topSelectWindow");
		}else{
			window.location = val_arr[0];
		}
		setTimeout(function(){
			$("#headerSelectionBox option:first").attr("selected","selected");
			$("#headerSelectionBox option:first", $ThisOption.parent()).change();
		},
		10);
	});
}
//-------------------------------------
// setAuthorAdmin
//-------------------------------------
function initColorChooser(){	
	$('#picker').farbtastic(
		"#colorChooser input",
		function(color){
			$("#main").css("background", color)
		}
	);
	$("#main").css("background", $("#colorChooser input").val())
}
function setAdminList() {
	$(".elementList li").hover(function () {
		$(this).css("background-color", "#fffaf2");
	},
	
	function () {
		var orColor = ($(this).hasClass("active")) ? "#f3f3f3" : "#fff";
		$(this).css("background-color", orColor);
	});
	
	
	$(".elementList, .pictureList, .linkList").map(function (index) {
		var $List = $(this);
		var $Hidden = $("input:hidden:first", $List);
		var $Li = $("li", $List);
		var $Ul = $("ul", $List);
		var $SelectionButtons = $(".contentSelectionButtons", $List);
		
		function setBehaviour() {
			var $TheeseLi = $("li", $Ul);
			var $Deletes = $("a.deleteButton", $TheeseLi);
			var $MoveUps = $("a.moveUpButton", $TheeseLi);
			var $MoveDowns = $("a.moveDownButton", $TheeseLi);
	
			if ($TheeseLi.length > 1) {
				$MoveUps.hover(over,out);
				$MoveUps.click(function () {
					var $ThisLi = ($(this).parent().is("li")) ? $(this).parent() : $(this).parent().parent();
					
					var $Text = $("textarea", $ThisLi)
					if ($Text.length > 0){
						toggleEditor($Text.attr("id"))
					}
					
					var $Prev = findPrev($ThisLi); 
					if ($Prev.length > 0) {
						$Prev.before($ThisLi);
					} else {
						$("li:last", $TheeseLi.parent()).after($ThisLi);
					}
					$ThisLi.mouseout();
					$(this).mouseout();
					updateList();
					$SelectionButtons.show();
					
					if ($Text.length > 0){
						toggleEditor($Text.attr("id"))
					}
					fadeOut($ThisLi);					
				});
				
				$MoveDowns.hover(over,out);
				$MoveDowns.click(function () {
					var $ThisLi = ($(this).parent().is("li")) ? $(this).parent() : $(this).parent().parent();				
					
					var $Text = $("textarea", $ThisLi)
					if ($Text.length > 0){
						toggleEditor($Text.attr("id"))
					}
					var $Next = findNext($ThisLi); 
					if ($Next.length > 0) {
						$Next.after($ThisLi);
					} else {
						$("li:first", $TheeseLi.parent()).before($ThisLi);
					}					
					$ThisLi.mouseout();
					$(this).mouseout();
					updateList();
					$SelectionButtons.show();
					
					if ($Text.length > 0){
						toggleEditor($Text.attr("id"))
					}
					fadeOut($ThisLi);					
				});
			} else {		
				$MoveUps.hide();
				$MoveDowns.hide();
			}	
			
			$Deletes.hover(over,out);
			$Deletes.click(function () {
				var $ThisLi = ($(this).parent().is("li")) ? $(this).parent() : $(this).parent().parent();
				if (confirm("Vil du slette dette element fra din side?")) {
					$ThisLi.mouseout();
					$(this).mouseout();
					$ThisLi.hide();
					deleteArray.push($ThisLi.attr("id"));
					updateList();
					$SelectionButtons.show();
				}
			});
		}

		function toggleEditor(id) {
			if (!tinyMCE.get(id))
				tinyMCE.execCommand('mceAddControl', false, id);
			else
				tinyMCE.execCommand('mceRemoveControl', false, id);
		}
		
		function over() {
			$(this).css("background-position","0 15px");
		}
		
		function out() {
			$(this).css("background-position","0 0");
		}
		
		function findNext($ThisLi) {
			if ($ThisLi.next().length == 0){
				return $ThisLi.next();
			} else if(!$ThisLi.next().is(":hidden")){
				return $ThisLi.next();
			} else {
				return findNext($ThisLi.next());
			}		
		}
		
		function findPrev($ThisLi) {
			if ($ThisLi.prev().length == 0){
				return $ThisLi.prev();
			} else if(!$ThisLi.prev().is(":hidden")){
				return $ThisLi.prev();
			} else {
				return findPrev($ThisLi.prev());
			}		
		}
		
		function fadeOut($ThisLi) {
			if($ThisLi[0].ani == true) {				
				return;
			}else{
				$ThisLi[0].ani = true;
			}
			
			var orColor = "#ffffff";
			$ThisLi.css("background-color","#fedeb0");
			if($ThisLi.find("div.listEdit").length > 0){
				orColor  = "#f2f2f2";
			} else if($ThisLi.hasClass("active")){
				orColor = "#f3f3f3";
			}
			setTimeout(function() {
				$ThisLi.animate({
					backgroundColor: orColor
				},
				1000,
				"linear",
				function(){
					this.ani = false;
				});
			}, 100);
		}
		
		function updateList() {
			var list_array = [];
			var li_array = [];
			$("li", $Ul).not(":hidden").each(function (index) {
				list_array.push($(this).attr("id"));
			});
			$Li.each(function (index) {
				li_array.push($(this).attr("id"));
			});
			$Hidden.val(list_array.join(",")+";"+deleteArray.join(",")+";"+li_array.join(","));
			debug($Hidden.val());
		}		
		
		function hideFileInputs(){
			$(".fileInput").map(function(index){
				var $FileInput = $(this);
				if($FileInput.prev().length > 0) {
					if($FileInput.prev().is(".fileName")){		
						$("a", $FileInput.prev()).unbind();
						$("a", $FileInput.prev()).click(function(){
							fileShow($(this).parent().next())
							$(this).parent().hide();
							
							if($(this).parent().parent().is("li")){
								$(this).parent().parent().parent().next().show();
							}
						});
						fileHide($FileInput)
					}
				}
			});
		}
		
		function fileHide($FileInput){
			$FileInput.css({
				"height" : "1px",
				"visibility" : "hidden",
				"overflow" : "hidden"
			});
		}
		function fileShow($FileInput){
			$FileInput.css({
				"height" : "",
				"visibility" : "",
				"overflow" : ""
			});
		}
		
		//
		var deleteArray = [];
		
		var $GrayButton = $("a.grayButton", $List);
		$GrayButton.click(function () {				
			deleteArray = [];
			$Li.show();
			$Ul.append($Li);
			$SelectionButtons.hide();	
			hideFileInputs();
		});
		
		//run		
		updateList();
		setBehaviour($Li);
		hideFileInputs();
		$SelectionButtons.hide();
	});
}

function setAuthorAdmin(){
	if($('.contentSelectionBox').length == 0) return

	function selectContent(){		
		var selected = parseInt($(this).attr("index"))
		$Content.each(function(index){
			var orStyle = {
				"background-image" : "url(/images/graphic/mainBg.jpg)",
				"background-repeat" : "repeat-x",
				"background-color" : "#f9f9f9",
				"background-position" : "center top"
			}
			$("#main").css(orStyle);
			if(selected == index){
				$(this).show();
				if($(this).find("#colorChooser").length > 0){
					initColorChooser();
				}	 
			}else{
				$(this).hide()
			}
		});
	}
	
	$Radios = $(".contentSelectionBox .greyHeader :radio")
	if($Radios.length > 0) {		
		$Content = $(".contentSelectionBox .contentBox")
		var selected = 0;
		$Radios.each(function(index){
			$(this).attr("index", index)
			if(this.checked){
				selected = index;
			}
		});
		$Radios.eq(selected)[0].selected = true;
		$Radios.click(selectContent);
		$Radios.eq(selected).click();
		$Radios.eq(selected).change();		
	}
	
	setAdminList();	
}


//-------------------------------------
// Run form scripts
//-------------------------------------
function runFormScripts(){
	ListController();
	setSearchBoxEvents();	
	styleFormFields();
	denStorDanske();
	setSmallSearchBoxEvents()
	setLigthboxEvents();
	setSearchOnType();
	setTopSelect();
	setAuthorAdmin();
}