﻿$(function() {
	
	//onblur=""if($(this).val()=='') $(this).val('" & ArrAttributter(2) & "');"" onfocus=""if($(this).val()=='" & ArrAttributter(2) & "') $(this).val('');
	
	if($('#fName').val()=='') {
		$("#fName").live("blur", function () {
			 //$(this).val('Name');
		});
	};
	
	if($('#fName').val()=='Name') {
		$("#fName").live("focus", function () {
			 //$(this).val('');
		});
	}
	
	$('#fName').live("click", function() {
		
	});

	$('#Bread_Con').click(function() { 
		$(this).toggleClass('expanded'); 
	});
		
	setBackground();

	$(window).resize(function() {
	  setBackground();
	});
	
	$("#slider").easySlider({
		auto: false,
		continuous: true,
		prevText: '',
		nextText: ''
	});
	
	$(".Forside_Table td#kasse").hover(
	  function () {
	  
		$('#forside_kasse_bg').remove();

				
		var newImg = $("<img />");
	
		// sæt attributter, src
		newImg
		.attr('src', '/gfx/template/forside_kasse_bg.gif')
		.attr('id', 'forside_kasse_bg')
		.addClass('forside_kasse_bg')
		.css({
			'opacity' : '0',
			'zindex' : '1000'
		})
		.appendTo($(this))
		//.show('fast')
		
		.animate({
			opacity: 1.0
		}, 
		{
			duration: 750
		});
		

		
	  }, 
	  function () {
		$('#forside_kasse_bg')
		//.hide('fast')
		.animate({
			opacity: 0
		}, 750, function() {
			$('#forside_kasse_bg').remove();
		});

	}
	);
	
	
	
	$('#gallery a, .gallery a').lightBox({fixedNavigation:true});

	$("a.external").bind("click", function() {
		
		window.open($(this).attr('href'));
		return false;
	});
	
	//traverse active
	jQuery.each($(".menu .active"), function() {
	
		$($(this).parent("ol").parents("li:eq(0)")).parent("ol").parents("li:eq(0)").addClass("active");
		$(this).parent("ol").parents("li:eq(0)").addClass("active");

	});
	$("ol.dropdown li,ol.leftMenu li").bind("click", function(e) {
		e.stopPropagation();
		var linkObj = $(this).find("a:first");
		if($(linkObj).attr('href')) {
			if($(linkObj).attr('href').length>0) {
				if($(linkObj).hasClass('external')) {
					window.open($(linkObj).attr('href'));
				} else {
					document.location.href = $(linkObj).attr('href');
				}
			}
		}
	});
	$("ol.dropdown li").hover(function(){
	   $(this).addClass("hover").find('a:first').addClass('over');
	   $('> .dir',this).addClass("open");
	   $('ol:first',this).css('display', 'block');
	 },function(){
	   $(this).removeClass("hover").find('a:first').removeClass('over');
	   $('.open',this).removeClass("open");
	   $('ol:first',this).css('display', 'none');
	 });
	
	if($("#search_keywords").length>0) {
		$("#search_keywords")
		.bind('blur', function() {
			if($(this).val().length==0) {
				$(this).val($(this).attr('rel'));		
			}
		})
		.bind('focus', function() {
			if($(this).val()==$(this).attr('rel')) {
				$(this).addClass('focus');
				$(this).val('');			
			}
		});
	
		if($("#search_keywords").val().length==0) $("#search_keywords").val($("#search_keywords").attr('rel')); 
		
		//ajaxSearch("#search_keywords","init");
	}

	// gallery start
	if($("div.gal").length>0) {
		
		// gallery type 1
		$("div.gal img").hover(
			function() { $(this).stop().animate({ 'opacity' : 0.8}, 300); },
			function() { $(this).stop().animate({ 'opacity' : 1}, 300); }
		);
		
		// init images

		// $("div.gal a[class='gal']").colorbox({ transition: 'elastic', speed: 300});		

		// 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');
		}
	}	
	if($("#topform")) {
		$("#topform_pwCheat").click(function() {
			$(this).hide();
			$("#topform_password").focus();
		});
		$("#topform_password").focus(function() {
			$("#topform_pwCheat").hide();
		}).blur(function() {
			if($(this).val()=='') $("#topform_pwCheat").show();
		});
		$("#topform_username").focus(function() {
			if($(this).val()==$(this).attr('rel')) $(this).val('');
		}).blur(function() {
			if($(this).val()=='') $(this).val($(this).attr('rel'));
		});
	}
	
});

function setBackground() {

	var fullscreenWidth = $(window).width();
	var origPicwidth = $('#background1').width();
	var origPicheight = $('#background1').height();
		
	NewPicheight = fullscreenWidth/origPicwidth*origPicheight;
		
	$('#background1').css({
		'width' : $(window).width() + 'px',
		'height' : NewPicheight + 'px'
	});
}

var customImgArr = "";
var curr_customImg = 0;
var customImg_time = 2500;
var customImg_delay = 2000;
function customImgFolder(cmd) {
	switch(cmd) {
		case "init":
			customImgArr = $("#customImg img").length-1;
			curr_customImg = customImgArr;
			$("#customImg img:last").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 + "\" value=\"" + this_name + "\" />");

		$(this).replaceWith(new_input);
	});

}
