﻿var menuIsClicked = false;

$(function() {
	//$("body").rightMouseDown( function(e) {
		//alert("(c) Vicky Bjerring");
		//return false;
	//});

	if($("#form_login").length>0) {
		build_login_form();	
	}	


	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").bind("click", function() {
		
		window.open($(this).attr('href'));
		return false;
		
	});
	
	//traverse active
	jQuery.each($(".menu .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) {
			menuIsClicked = true;
			var linkObj = $(this).find("a");
			if($(linkObj).attr('href').length>0) {
				if($(linkObj).hasClass('external')) {
					window.open($(linkObj).attr('href'));
				} else {
					document.location.href = $(linkObj).attr('href');
				}
			}
		}
	
	});
	
	if($("#search_keywords").length>0) {
		$("#search_keywords")
		.bind('blur', function() {
			if($(this).val().length==0) {
				$(this).css('color','#606060');
				$(this).val('Find hvad du søger');		
			}
		})
		.bind('focus', function() {
			if($(this).val()=='Find hvad du søger') {
				$(this).css('color','#000');
				$(this).val('');			
			}
		});
	
		if($("#search_keywords").val().length==0) $("#search_keywords").val('Find hvad du søger'); 
		
		ajaxSearch("#search_keywords","init");
	}

	// gallery start
	if($("div.gal").length>0) {
		
		
		// gallery type 1
		// $("div.gal img").css("opacity",0.8);
		$("div.gal img").hover(
			function() { $(this).stop().animate({ 'opacity' : 0.75}, 300); },
			function() { $(this).stop().animate({ 'opacity' : 1}, 300); }
		);
		
		// init images
		$("div.gal a[class='gal']").lightBox({ containerResizeSpeed: 800, txtImage : "", txtOf : "/", overlayOpacity: 1 });
		
	}

	if($("form.dynForm").length>0) {
		init_dynForms();
	}	
	
	if($("#customImgFolder").length>0) {
		if($("#customImgFolder img").length>1) {
			customImgFolder('init');
		} else {
			$("#customImgFolder img").css('display','block');
		}
	}
	
	$("#contactForm").orangoForm();
	$("a[href='/login/']").bind('click', function() {
		$("#modal")
		.css({
			'opacity' : 0.8,
			'display' : 'block'
		});		
		$("#loginForm")
		.css({
			'left' : Math.floor(($("body").width()-302)/2) + 'px',
			'top' : Math.floor(($("body").height()-228)/2) + 'px',
			'display' : 'block'
		});
		
		$("#login_username").focus();
			
	
		return false;
	});	
	
});


var customImgArr = "";
var curr_customImg = 0;
var customImg_time = 2500;
var customImg_delay = 2000;
function customImgFolder(cmd) {
	switch(cmd) {
		case "init":
			customImgArr = $("#customImgFolder img").length-1;
			curr_customImg = customImgArr;
			$("#customImgFolder 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;
			$("#customImgFolder img").css('zIndex','0');
			$($("#customImgFolder img").get(tmp)).css('zIndex','1');
			$($("#customImgFolder 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 build_login_form() {
	
	$("#form_login").append(
		"<input type=\"text\" id=\"login_username\" name=\"ocms_usr\" />" + 
		"<input type=\"text\" id=\"login_passwordTxt\" />" +
		"<input type=\"password\" id=\"login_password\" name=\"ocms_pwd\" />" +
		"<input type=\"submit\" id=\"login_submit\" value=\"\" />"
	);
	// $("#login_submit").hover(
		// function() {
			// $(this).css("background","url(/ocms/gfx/login_btn_submit_over.png) top left no-repeat");
		// },
		// function() {
			// $(this).css("background","transparent");
		// }
	// )
	$("#form_login_container").css("display","block");
	$("#login_username")
	.val('Brugernavn')
	.bind('focus', function() {
		if($(this).val()=="Brugernavn") $(this).val('');
	})
	.bind('blur', function() {
		if($(this).val()=="") $(this).val('Brugernavn');
	});
	
	$("#login_passwordTxt")
	.val('Password')
	.bind('focus', function() {
		$(this).hide();
		$("#login_password").focus();
	});
	
	$("#login_password")
	.bind('blur', function() {
		if($(this).val().length==0) {
			$("#login_passwordTxt").show(); 
		}
	});	
	$("#form_login").attr('action','/inc/modules/login/login.asp');
	
}


function clean_string(str) {
	if(str!="") {
		var tmp = str;
    var regExp = new RegExp("[^0-9-_a-zA-Z@\.]","gi");
    tmp = tmp.replace(regExp,"");		
		return tmp;
	}
}


function validate_login() {
	var usr = document.getElementById("login_username");
	var pwd = document.getElementById("login_password");
	if(usr.value != "" && pwd.value.length > 0) {
		usr.value = clean_string(usr.value);
		return true;
	} else { 
		return false;
	}
}
