﻿var menuIsClicked = false;

$(function() {

	$( "#slider1" ).slider({
		value: 1,
		min: 1,
		max: 10,
		slide: function( event, ui ) {
			$( "#amount1" ).html( ui.value );
			$( "#Flid" ).val( ui.value );
		}
	});

	$( "#slider2" ).slider({
		value: 1,
		min: 1,
		max: 10,
		slide: function( event, ui ) {
			$( "#amount2" ).html( ui.value );
			$( "#Skils" ).val( ui.value );
		}
	});
	
	$( "#slider3" ).slider({
		value: 1,
		min: 1,
		max: 10,
		slide: function( event, ui ) {
			$( "#amount3" ).html( ui.value );
			$( "#Fokus" ).val( ui.value );
		}
	});

	
	$(".Knap").mouseover( function() {
			var strRel = $(this).attr('rel');
		
			$(this).attr({
				'src': '/gfx/template/'+strRel+'_hover.png',
			});
		}).mouseout(function(){
		$(this).attr('src', '/gfx/template/'+$(this).attr('rel') +'.png');
	});
	
	$(".Knap").mousedown( function() {
		var strRel = $(this).attr('rel');
		
		$(this).attr({
			'src': '/gfx/template/'+strRel+'_click.png',
		});
	}).mouseup ( function() {
		$(this).attr('src', '/gfx/template/'+$(this).attr('rel') +'_hover.png');
	});
	
	$("#Beregn").click( function() {
		//alert("Beregn");
		$("#sldContainer1, #sldContainer2, #sldContainer3, #Beregn, #Annuller").fadeOut("Fast")
		
		var FullAmount = 10
	
		var Flid = FullAmount-$( "#amount1" ).html();
		var Skils = FullAmount-$( "#amount2" ).html();
		var Fokus = FullAmount-$( "#amount3" ).html();
		
		var TotalAmount = Math.round(((Flid + Skils + Fokus)*10)/3);	
		
		var strHTML = "";
		strHTML = strHTML + "<b>Resultat af din beregning:</b><br />";
		strHTML = strHTML + "Flid: " + $( "#amount1" ).html() + "<br />Færdigheder: " + $( "#amount2" ).html() + "<br />Fokus: " + $( "#amount3" ).html() + "<br /><br />";
		strHTML = strHTML + "Der er en god sandsynlighed for at dit salgsteam kan forøge deres resultater med op til <span style=\"font-size:18px; font-weight:bold;\">" + TotalAmount + "%</span><br /><br />";
		 
		$("#Beregner_Container").html(strHTML);
		
		$("#inner_col3_container").attr('id' , '');
		
		var MoveUp = $("#kontaktFormular").height()+$("#Beregner_Container").height()-25;
		 
		strHTML = strHTML.replace(/<b>/ig, "")
		strHTML = strHTML.replace(/<\/b>/ig, "")
		strHTML = strHTML.replace(/<br \/>/ig,"\n");
		
		$("#Besked").css('color' , '#000');
		$("#Besked").val(strHTML);
		
	});
	
	$("#Annuller, #Beregner_Close").click( function(e) {
		e.stopPropagation();
		$("#Beregner_Close").fadeOut("fast");
		$("#Beregner_Content").slideUp("fast", function() {
			$( "#slider1" ).slider({ value: 1 });
			$( "#amount1" ).html( 1 );
			$( "#Flid" ).val( 1 );
			
			$( "#slider2" ).slider({ value: 1 });
			$( "#amount2" ).html( 1 );
			$( "#Skils" ).val( 1 );		
			
			$( "#slider3" ).slider({ value: 1 });
			$( "#amount3" ).html( 1 );
			$( "#Fokus" ).val( 1 );
		});
		
	});



	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) {
		
		$("#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.contentCurrent = fLang.m.gal.imageCurrentOutOfTotal;
		$("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_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' });
		});
	}	


	


});

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);
	});
	
	$("#fName").bind('focus', function() {
		if($(this).val()=='Dit navn') $(this).val('');		
	}).bind('blur', function() {
		if($(this).val()=='') $(this).val('Dit navn');		
	}).val('Dit navn');
	$("#fEmail").bind('focus', function() {
		if($(this).val()=='Din email') $(this).val('');		
	}).bind('blur', function() {
		if($(this).val()=='') $(this).val('Din email');		
	}).val('Din email');
	
	// $("#fSend").bind('click', function() {
		// if($("#fName").val()!=""&&$("#fPhone").val()!="") {
			// $(this).parent('form').submit();
		// } else {
			// alert("Udfyld venligst navn og telefonnummer!");
		// }
	// });

}
