﻿var menuIsClicked = false;

$(function() {
	$("#mce-EMAIL").bind('focus', function() {
		if($(this).val()=='E-mail') $(this).val('');
	})
	.bind('blur', function() {
		if($(this).val()=='') $(this).val('E-mail');
	});
	$("#mce-FNAME").bind('focus', function() {
		if($(this).val()=='Navn') $(this).val('');
	})
	.bind('blur', function() {
		if($(this).val()=='') $(this).val('Navn');
	});	
	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')!="") {
				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) {
		
		$("#gal_switch_label").html(fLang.m.gal.switchView);
		
		// 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, 
			opacity : 0.8,
			current : "",
			onOpen : function() {
				$(this).parents("div.gal").hide();
			},
			onClosed : function() {
				$(this).parents("div.gal").show();
			}
		
		});		

		// 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_flashHolder").length>0) {
		$("#customImg_flashHolder").css({ 'width':Math.ceil($("body").width())+'px', 'display' : 'block' });
		$(window).resize(function(){
			$("#customImg_flashHolder").css({ 'width':Math.ceil($("body").width())+'px' });
		});
	}	
	
	if($("#flashMag").length>0) {
		// var obj = $("#flashMag");
		// var flashMag = obj.clone();
		// obj.remove();
		var flashMag = $("#flashMag");
		//$("body").append(flashMag);
		flashMag.css({
			'display' : 'block'
		}).hover(function(){},function(){ $(this).css('zIndex','0'); });
		$("#inner_main").hover(function(){
			//$(flashMag).css('zIndex','99999');
		},function(){});
	}
	
	//initGraphicalMenu("nav");
	
	if($("#contactForm").length>0) {
		initCheckboxes($("#contactForm"));
	}
	
	// init bund knapper
	var menuName = $("#menName").val();
	var tmpId = "";
	switch(menuName) {
		case "kampagne":
			tmpId = "bund_" + menuName;
			break;
		case "logo":
			tmpId = "bund_" + menuName;
			break;
		case "web":
			tmpId = "bund_" + menuName;
			break;
	}
	if(tmpId!="") {
		//$("#" + tmpId).attr('src',
		var source = $("#" + tmpId).attr('onmouseover').toString();
		source = source.replace('function onmouseover(event) {','');
		source = source.replace(/\n/ig,'');
		source = source.replace(/\r/ig,'');
		source = source.replace('}','');
		source = source.replace('this.src = ','');
		source = source.replace('\";','');
		source = source.replace('\"','');
		source = source.replace('    ','');
		source = source.replace("function onmouseover(){this.src='","");
		source = source.replace("';","");
		$("#" + tmpId).attr('src',source).bind('mouseout',function() { $(this).attr('src',source); });
	}
	
	// case module
	$("ul#caseMenus li").disableSelection();
	$("ul#caseMenus li.case").click(function() {
		if(!$(this).hasClass('active')) {
			$("ul#caseMenus li.case.active").removeClass('active');
			$(this).addClass('active');
			// find next
			var $newImg = $("div#caseImages img[rel=" + $(this).attr('rel') + "]");
			var $currImg = $("div#caseImages img.active");
			$newImg.css({
				'display' : 'none',
				'z-index' : '2'
			}).fadeIn('slow',function() {
				$currImg.removeClass('active').css('z-index','');
				$newImg.addClass('active').css('z-index','');
			});
		}		
		
	});
	
});

function initCheckboxes(obj) {
	jQuery.each(obj.find('input[type=checkbox]'), function() {
		var cb = $(this);
		var checked = (cb.attr('checked')) ? ' checked' : '';
		var cId = cb.attr('id');
		cb.after("<div class='dyncheck" + checked + "' rel='" + cId + "'></div>");
		cb.css('display','none');
	});
	obj.find('div.dyncheck').bind('click', function() {
		var cb = $(this);
		var cId = cb.attr('rel');
		var checked = cb.hasClass('checked');
		if(checked) {
			$("#" + cId).attr('checked',false);
			cb.removeClass('checked');
		} else {
			$("#" + cId).attr('checked',true);
			cb.addClass('checked');
		}
	});
}

function initGraphicalMenu(obj) {
	jQuery.each($("#" + obj + " li"), function() {
		var menName = $(this).find('a').html();
		menName = menName.toLowerCase();
		$(this).find('a').html("<img src=\"/gfx/template/" + menName + "_0.png\" alt=\"" + menName + "\" class=\"graphMen out\" /><img src=\"/gfx/template/" + menName + "_1.png\" alt=\"" + menName + "\" class=\"graphMen over\" />")	
		if($(this).find('a').hasClass('active')) {
			$(this).find("img[class='graphMen out']").hide();
			$(this).find("img[class='graphMen over']").show();		
		}
	});
	$("#" + obj + " li a").hover(
		function() { 
			if(!$(this).hasClass('active')) {
				$(this).find("img[class='graphMen out']").hide();
				$(this).find("img[class='graphMen over']").show();
			}
		},
		function() {
			if(!$(this).hasClass('active')) {
				$(this).find("img[class='graphMen over']").hide();
				$(this).find("img[class='graphMen out']").show();
			}
		}
	);
	$("#" + obj).show();
}

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!");
		// }
	// });

}
