﻿var menuIsClicked = false;

$(function() {
	$("ul.dropdown li:first").css('background','none');
	if($("#flashVideo").length>0) {
		if(Math.floor((($(document).height()-576)/2)-115)<0) {
			var logoTop = 0;
			var videoTop = 115;
			var skipTop = 580+115;
		} else {
			var logoTop = Math.floor(($(document).height()-576)/2)-115;
			var videoTop = Math.floor(($(document).height()-576)/2);
			var skipTop = Math.floor((($(document).height()-576)/2)+580);
		}
		$("#flashVideo").css({
			'left' : Math.floor(($(document).width()-1050)/2) + 'px',
			'top' :  videoTop + 'px',
			'display' : 'block'
		});
		$("#videoLogo").css({
			'left' : Math.floor(($(document).width()-169)/2) + 'px',
			'top' : logoTop + 'px',
			'display' : 'block'
		});
		$("#videoSkip").css({
			'left' : Math.floor(($(document).width()-200)/2) + 'px',
			'top' :  skipTop + 'px',
			'display' : 'block'
		});		
	}
	
	jQuery.each($("a.email"), function() {
		var tmpLink = $(this).attr('href');
		if(!tmpLink) tmpLink = $(this).html();
		tmpLink = tmpLink.replace('[snabela]','@');
		tmpLink = tmpLink.replace(/\[\]/ig,".");
		tmpLink = tmpLink.replace('.@.','@');
		$(this).attr('href','mailto:' + tmpLink)
		if(!$(this).hasClass('noTxt')) {
			$(this).html(tmpLink);
		}
		if($(this).hasClass('noLink')) {
			$(this).replaceWith(tmpLink);
		}
	});
	
	$("a.external").live("click", function() {
		
		window.open($(this).attr('href'));
		return false;
		
	});
	
	//traverse active
	jQuery.each($(".dropdown .active"), function() {
	
		$($(this).parent("ul").parents("li:eq(0)")).parent("ul").parents("li:eq(0)").addClass("active");
		$(this).parent("ul").parents("li:eq(0)").addClass("active");

	});
	$("ul.dropdown li").bind("click", function() {
		if(menuIsClicked==false) {
			var linkObj = $(this).find("a");
			if($(linkObj).attr('href')!="") {
				if($(linkObj).hasClass('external')) {
					window.open($(linkObj).attr('href'));
				} else {
					menuIsClicked = true;
					document.location.href = $(linkObj).attr('href');
				}
			}
		}	
	});
	
	if($("#search_keywords").length>0) {
		ajaxSearch("#search_keywords","init");
	}

	// gallery start
	if($("div.gal").length>0) {
		
		$("#gal_switch_label").html(fLang.m.gal.switchView);
		
		// gallery type 1
		$("div.gal img").css("opacity",0.8);
		$("div.gal img").hover(
			function() { $(this).stop().animate({ 'opacity' : 1}, 300); },
			function() { $(this).stop().animate({ 'opacity' : 0.8}, 300); }
		);
		
		// init images

		$.fn.colorbox.settings.transition = "fade";
		$.fn.colorbox.settings.bgOpacity = "0.8";
		$.fn.colorbox.settings.contentCurrent = fLang.m.gal.imageCurrentOutOfTotal;
		$("div.gal a[class='gal']").colorbox();		

		// switch view		
		$("a.switch_thumb").toggle(
			function(){
				$(this).addClass("swap");
				$("ul.display").fadeOut("fast", function() {
					if($("ul.display").hasClass("thumb_view")) {
						$(this).fadeIn("fast").removeClass("thumb_view");
					} else {
						$(this).fadeIn("fast").addClass("thumb_view");
					}
				});
			}, 
			function () {
				$(this).removeClass("swap");
				$("ul.display").fadeOut("fast", function() {
					if($("ul.display").hasClass("thumb_view")) {
						$(this).fadeIn("fast").removeClass("thumb_view");
					} else {
						$(this).fadeIn("fast").addClass("thumb_view");
					}
				});
			}
		); 
	}

	if($("form.dynForm").length>0) {
		init_dynForms();
	}	


	if($("#customImg").length>0) {
		if($("#customImg img").length>1) {
			customImgFolder('init');
		} else {
			$("#customImg img").css('display','block');
		}
	}

	$(".youtube").colorbox({iframe:true, width:850, height:540, open:true});
	


});﻿


var customImgArr = "";
var curr_customImg = 0;
var customImg_time = 2000;
var customImg_delay = 3000;
function customImgFolder(cmd) {
 
	switch(cmd) {
		case "init":
			customImgArr = $("#customImg img").length;
			curr_customImg = customImgArr;
			//$("#customImg img").css('display','block');
			setTimeout("customImgFolder('')",customImg_delay);
		break;
		case "":
			var tmp = curr_customImg;
			curr_customImg = (curr_customImg==0) ? curr_customImg = customImgArr : curr_customImg = curr_customImg-1;
			$("#customImg img").css('zIndex','0');
			$($("#customImg img").get(tmp)).css('zIndex','1');
			$($("#customImg img").get(curr_customImg)).css({ 'opacity' : 0, 'zIndex' : '2', 'display' : 'block' })
			.animate({ 'opacity' : 1 }, {duration : customImg_time, complete: function() {
				setTimeout("customImgFolder('')",customImg_delay);
			}});
			
			
		break;
	}
}

var searchTimeout = "";
function ajaxSearch(inputKW,cmd) {
	switch(cmd) {
		case "init":
			$(inputKW)
			.attr('autocomplete','off')
			.bind('keyup', function() {
				clearTimeout(searchTimeout);
				if($(this).val().length > 0) {
					ajaxSearch(inputKW,"do");
				} else {
					ajaxSearch(inputKW,"clear");
				}
			});
		break;

		case "clear":
			$("#container_search_results").slideUp('normal',function() {
				$(this).find('p.container').html('');
			});
		break;
		case "do":
			searchTimeout = setTimeout("ajaxSearch('" + inputKW + "','doIt');",500);
		break;
		case "doIt":
			$.ajax({
			   type: "POST",
			   url: "/inc/modules/ajax/ajaxSearch.asp",
			   data: "t=ajax&q=" + htmlSecure($(inputKW).val()),
			   success: function(msg){			
					$("#container_search_results").find('p.container').html(msg);
					$("#container_search_results").slideDown('normal');
				}
			});
		break;
	}
}

function htmlSecure(str_tmp) {
	var tmp = str_tmp;
	if(tmp!=""&&tmp.length>0) {
		tmp = tmp.replace(/>/g,"&gt;");
		tmp = tmp.replace(/</g,"&lt;");
		tmp = tmp.replace(/[\r\n]+/g, " ");
		tmp = encodeURIComponent(tmp);
	}
	return tmp;
}

function validEmail(str) {
	var re = new RegExp(/[a-z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-z]{2,12}/ig);
	return (str.match(re)) ? true : false;
}


function init_dynForms() {
	jQuery.each($("form.dynForm div.dyn"), function() {
		var parentForm = $(this).parent('form');
		
		var this_id = $(this).attr('id');
		var this_name = $(this).html();
		var this_type = $(this).attr('class').replace(/dyn /ig,"");
		
		var new_input = $("<input type=\"" + this_type + "\" name=\"" + this_name + "\" id=\"" + this_id + "\" />");

		$(this).replaceWith(new_input);
	});
	// $("#fSend").bind('click', function() {
		// if($("#fName").val()!=""&&$("#fPhone").val()!="") {
			// $(this).parent('form').submit();
		// } else {
			// alert("Udfyld venligst navn og telefonnummer!");
		// }
	// });

}


function signUpForCMNewsletter() {

	var err = 0;
	var errStr = "";

	// // var arr = $.map($('#signUpBox input[type=checkbox]:checked'), function(e) { return $(e).val(); })
	// // if(arr.length==0) {
		// // errStr += "\n\n- Vælg en eller flere grupper";
		// // err++;
	// // }
	// // var susName = $("#susName").val();
	// // if(susName.length==0) {
		// // errStr += "\n\n- Udfyld venligst firmanavn";
		// // err++;
	// // }

	// var firmanavn = $("#firmanavn").val();
	// if(firmanavn.length==0) {
		// errStr += "\n\n- Skriv venligst et firmanavn";
		// err++;
	// }	
	
	// var nreName = $("#navn").val();
	// if(nreName.length==0) {
		// errStr += "\n\n- Skriv venligst dit navn";
		// err++;
	// }

	// var titel = $("#titel").val();
	// if(titel.length==0) {
		// errStr += "\n\n- Skriv venligst din jobtitel";
		// err++;
	// }	

	// var adresse = $("#adresse").val();
	// if(adresse.length==0) {
		// errStr += "\n\n- Skriv venligst en adresse";
		// err++;
	// }	

	// var postNr = $("#postNr").val();
	// if(postNr.length==0) {
		// errStr += "\n\n- Skriv venligst et postnummer";
		// err++;
	// }	

	// var byen = $("#byen").val();
	// if(byen.length==0) {
		// errStr += "\n\n- Skriv venligst et bynavn";
		// err++;
	// }	
	

	var nreEmail = $("#email").val();
	if(nreEmail.length==0) {
		errStr += "\n\n- Skriv venligst din e-mailadresse";
		err++;
	} else {
		if(validEmail(nreEmail)==false) {
			errStr += "\n\n- Skriv venligst en gyldig e-mailadresse!";
			err++;		
		}
	}

	// var telefon = $("#telefon").val();
	// if(telefon.length==0) {
		// errStr += "\n\n- Skriv venligst et telefonnummer";
		// err++;
	// }	
	
	
	if(err>0) {
		alert(
			"Følgende fejl opstod:" +
			errStr
		);
		return false;
	} else {
		$("#signUpBox").attr('action',$("#theUrl").val() + '?tak-for-tilmelding');
		$("#cmd").val('signUp');
		return true;
	}

}