// JavaScript Document
var domain = "/";
$(document).ready(function(){
	
	$("#menu > #mainMenu").mousemove(menu.mouseMove);
	$("#menu > #mainMenu").mouseleave(menu.hideDescription);
	$("#menu > #mainMenu > li > a").hover(menu.setDescription);
	
	
	$("#footer").mouseenter(function() {
						$("#footer .sitemap").fadeTo("slow", 1)
						});	
	$("#footer").mouseleave(function() {
						$("#footer .sitemap").fadeTo("slow", .5)
						});	
	
	$(".search .query").blur(function () {
										$(".search ul").fadeOut("fast");
										$(this).fadeTo("slow", 0.5,
											function() {
												var t = $(this).attr("title");
												$(this).attr("value", t)
											}
										);
										//$(this).attr("value", $(this).attr("title"));
										search.currentWord = "   ";
									});
	
	$(".search .query").focus(function () {
										 if ($(this).attr("title")=="") {
										 	$(this).attr("title", $(this).attr("value"))
										 }
										 $(this).fadeTo("slow", 1);
										 if ($(this).attr("value") == $(this).attr("title")) $(this).attr("value", "");
										 });
	
	$(".search .query").keyup(search.changed);
	$(".search input").keypress(function (e) {
		  if (e.which == 13) {
			search.detailSearch('#searchBox input', settings.catId);
		  }
		});



	$("#sendMailDlg").dialog({ 
		modal: true, 
		buttons: { 
			"Ok": function() { 
				mailArticle.send(); 
			}, 
			"Annullér": function() { 
				$(this).dialog("close"); 
			} 
		}, 
		resizable: false,
		autoOpen: false });

	msg.init();
	

});


// ************* SETTINGS (SET BY UMBRACO) ****************
settings = {
	langExt: "",
	nodeId: 0,
	rootNode: 1084,
	catId: 1084
	} 






// ****************  SEARCH  ****************************


search = {
	oldValue: "",
	minChars: 3,
	nextWord: null,
	currentWord: "   ",
	items: [],
	
	source: null
	};

search.changed = function () {
	search.source = $(this).parent().parent();
	var newValue = $(this).attr("value").toLowerCase();
	search.oldValue = newValue;
	
	if (newValue.length == search.minChars) {
		if (newValue != search.currentWord) {
			//console.log("new search:" + newValue);
			search.newSearch(newValue);
		}
		search.redraw();
	} else if (newValue.length < search.minChars) {
		$(".search ul").fadeOut("fast");
	} else {
		search.redraw();
	}
	
}
search.searchDone = function (_msg, dd) {
	//console.log("loading done: " + _msg);
	_msg = _msg.split(">")[2].split("<")[0];
	var items = _msg.split("\r");
	
	search.items.length = 0;
	var itemA;
	for (var i=0; i<items.length; i++) {
		itemA = items[i].split("\t");
		search.items.push({heading: itemA[0], url: itemA[1]});
	}
	
	search.redraw(); 
}

search.newSearch = function (word) {
	//alert(settings.catId);
	catId = $(search.source).find("form #startNode").attr("value");
	if (!catId>0) catId=0;
//********************************** HARDCODED VALUE BELOW ************************************************
	search.currentWord = word;
	$.ajax({  
		type: "GET",  
		url: domain + "Services/Frontend.asmx/hdrsearch",  
		data: {
			word: word, 
			node: catId,
			language: settings.langExt},  
		contentType: "application/x-www-form-urlencoded",  
		dataType: "text",  
		success: search.searchDone,  
		error: function(error) {    
		 alert("Error: " + error);
		}
	}); 		
}

search.redraw = function () {
	var html = "";
	var heading;
	var headingStart;
	for (var i=0; i<Math.min(search.items.length, 20); i++) {
		headingStart = search.items[i].heading.toLowerCase().indexOf(search.oldValue);
		if (headingStart>-1) {
			heading = search.items[i].heading.substr(0, headingStart) + "<em>" + search.items[i].heading.substr(headingStart, search.oldValue.length) + "</em>";
			heading += search.items[i].heading.substr(headingStart + search.oldValue.length);
			html += "<li><a href=\"" + search.items[i].url + "\">" + heading + "</a></li>";
		}
	}

	$(search.source).find("ul").html(html);
	if (html.length>0) {
		$(search.source).find("ul").fadeIn("fast");
	} else {
		$(search.source).find("ul").fadeOut("fast");
	}
}

search.getItems = function (value) {
	// **************************** HARD CODED VALUE *********************
  $.ajax({  
         type: "GET",  
         url: domain + "Services/Frontend.asmx/hdrsearch",  
         data: {word: value, node: 0},  
         contentType: "application/x-www-form-urlencoded",  
         dataType: "text",  
         success: function(msg,dd) {
			 msg = msg.split(">")[2].split("<")[0];
			 var items = msg.split("\n");
			 var word = items.pop().split("\t")[0];
			 
			 if (word==search.oldValue) {
				 search.items = "";
				 var citem;
				 var output = ""
				 for (var i=0; i<items.length; i++) {
				 	citem = items[i].split("\t");
					citem[0] = citem[0].replace(search.oldValue, "<em>" + search.oldValue + "</em>");
				 	search.items += "<li><a href=\"" + citem[1] + "\">" + citem[0] + "</a></li>";
				 }
				 $(".search ul").html(search.items);
				 $(".search ul").fadeIn("fast");
			 }            
         },  
         error: function(error) {    
             alert("Error: " + error);
			 
         }  
     }); 
	
}

search.detailSearch = function (sourceControl) {
	var form = $(sourceControl).submit();
	
	
	//window.location.href = "/soeg?query=" + escape($(sourceControl).attr("value")) + "&startNode=" + startNode;
}



// **************** MENU **********************

menu = {
	timerId: null
	};

menu.showDescription = function () {
	$("#subMenu").fadeOut("fast");
	$("#menuDescription").fadeIn("slow");
}

menu.hideDescription = function () {
	if (menu.timerId) clearTimeout(menu.timerId);
	$("#subMenu").fadeIn("fast");
	$("#menuDescription").fadeOut("slow");

}

menu.setDescription = function () {
	$("#menuDescription").html($(this).children(".hidden").html());
}

menu.mouseMove = function () {
	if (menu.timerId) clearTimeout(menu.timerId);
	menu.timerId = setTimeout('menu.showDescription()', 500);
}

zoomArticle = function () {
	$(".mainArticle").toggleClass("articleZoom");
}




/*    *********** MAIL ARTICLE *******************/

mailArticle = {};
mailArticle.open = function () {
	$("#sendMailDlg").dialog('open');
}
mailArticle.send = function (nodeId) {
	$("#sendMailDlg").dialog('close');
	msg.message("Vent venligst!");
	msg.open({title: "Vent venligst!"});
	// **************************** HARD CODED VALUE *********************
	$.ajax({  
		 type: "GET",  
		 url: domain + "Services/Frontend.asmx/mailarticle",  
		 data: {name: $("#sendMailDlg #yourName").attr("value"),
		 		email: $("#sendMailDlg #email").attr("value"),
				node: settings.nodeId,
				language: settings.langExt},  
		 contentType: "application/x-www-form-urlencoded",  
		 dataType: "text",  
		 success: function(_msg,dd) {
			 msg.close();			 
		 },  
		 error: function(error) {    
			 alert("Error: " + error);
			 msg.close();
		 }  
	 });
}

//******************* MESSAGE DLG ********************//

msg = {};
msg.id = "#msg";
msg.open = function (args) {
	msg.setArgs(args);
	$(msg.id).dialog('destroy');
	$(msg.id).dialog(msg.args);
}
msg.args = {};
msg.setArgs = function (args) {
	msg.args = {
		title:"",
		modal:true,
		resizable: false
	};
	for (a in args) {
		msg.args[a] = args[a];
	}
}
msg.init = function () {
	$(msg.id).dialog({autoOpen: false});
}
msg.message = function (message) {
	if (message) {
		$(msg.id).html(message);
	} else {
		return $(msg.id).html();
	}
}

msg.close = function () {
	$(msg.id).dialog('destroy');
}

//******************* Forside billeder ********************//

imageFlow = {
	images: ["/gfx/headerBack01.jpg",
			"/gfx/headerBack02.jpg",
			"/gfx/headerBack03.jpg",
			"/gfx/headerBack04.jpg",
			"/gfx/headerBack05.jpg",
			"/gfx/headerBack06.jpg",
			"/gfx/headerBack07.jpg",
			"/gfx/headerBack08.jpg",
			"/gfx/headerBack09.jpg",
			"/gfx/headerBack10.jpg"],
	timespan: 5000,
	current: 1,
	timerId: -1,
	currentTarget: null
}

imageFlow.init = function () {
	//var t =  Math.floor(Date.now().valueOf() / imageFlow.timespan);
	imageFlow.current = Math.floor(Math.random()*imageFlow.images.length);
	imageFlow.timerId = setInterval('imageFlow.nextImage()', imageFlow.timespan);
	imageFlow.currentTarget = $("#header #headerBack .front");
	imageFlow.nextImage();
}

imageFlow.nextImage = function () {
	imageFlow.current = (imageFlow.current+1) % imageFlow.images.length;
	if (imageFlow.currentTarget == $("#header #headerBack .front")) {
	
	
	}

	
	$(imageFlow.currentTarget).attr("src", imageFlow.images[imageFlow.current]);
	$(imageFlow.currentTarget).load(imageFlow.loaded);
}

imageFlow.loaded = function () {

	if ($("#header #headerBack .front").css("z-index")=="1") {
		$("#header #headerBack .back").fadeOut("slow", imageFlow.fadeOutHandler);
	} else {
		$("#header #headerBack .front").fadeOut("slow", imageFlow.fadeOutHandler);
	}
	
}

imageFlow.fadeOutHandler = function () {

	if ( $("#header #headerBack .back").css("z-index")=="1") {
		$(imageFlow.currentTarget).css("z-index", "2");
		imageFlow.currentTarget = $("#header #headerBack .front");
	} else {
		$(imageFlow.currentTarget).css("z-index", "2");
		imageFlow.currentTarget = $("#header #headerBack .back");	
	}
	$(imageFlow.currentTarget).css("z-index", "1");
	$(imageFlow.currentTarget).css("display", "inline");
	
}

				 

