﻿$(function() {
	
	if($("#customImgTD img.cs").length>0) {
		$("#customImgTD").css('height',$("#customImgTD img.cs").height() + 'px');
	}
	$("#menu li:last").css('background','transparent');
	
	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, #submenu .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() {
		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','#c0c0c0');
				$(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.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();
	}	
	m_news_newsTicker();
	

	$("#social").find("a").hover(
		function() {
			$("#social p.text").html($(this).find('img').attr('alt'));
		},
		function() {
			$("#social p.text").html('');
		}
	);
	if($("#relatedPages").length>0) {
		$("#relatedPages div").hover(
			function() { $(this).css('backgroundColor','#f2f2f2'); },
			function() { $(this).css('backgroundColor',''); }
		);
	}
});﻿


var currNewsItem = 0;
// news
function m_news_newsTicker() {
	var animateSpeed = 1000;
	var shiftSpeed = 5000;
	if(currNewsItem>0) {
		var lastItem = currNewsItem-1;
		if(lastItem<0) lastItem = 0;
		if(currNewsItem>$("#newsTicker .moreNews").length-1) currNewsItem = 0;
		// remove current first
		$("#newsTicker .moreNews:eq(" + lastItem + ")").css({'opacity':1,'top':'0px','display': 'block'});
		$("#newsTicker .moreNews:eq(" + lastItem + ")").animate({
			'top': '0px', 'opacity' : 0
		},{
			'duration': animateSpeed, 'complete': function() {
				$(this).css('display','none');
			if($("#newsTicker .moreNews:eq(" + currNewsItem + ")").length==1){
				$("#newsTicker .moreNews:eq(" + currNewsItem + ")").css({'opacity':0,'top':'0px','display': 'block'});
				$("#newsTicker .moreNews:eq(" + currNewsItem + ")").animate({
					'top': '0px', 'opacity' : 1
				},{
					'duration': animateSpeed, 'complete': function() {
					currNewsItem++;
					setTimeout("m_news_newsTicker()",shiftSpeed);
					}
				});					
			}
			}
		});			
	} else {
		if($("#newsTicker .moreNews:eq(" + currNewsItem + ")").length==1){
			$("#newsTicker .moreNews:eq(" + currNewsItem + ")").css({'opacity':0,'top':'0px','display': 'block'});
			$("#newsTicker .moreNews:eq(" + currNewsItem + ")").animate({
				'top': '0px', 'opacity' : 1
			},{
				'duration': animateSpeed, 'complete': function() {
				currNewsItem++;
				setTimeout("m_news_newsTicker()",shiftSpeed);
				}
			});			
		}
	}
	
}

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 orderInfo() {
	var err = false;
	
	var itemsArr = ["info_website","info_seo","info_sms","info_tryk","info_webshop","info_flash3d","info_eMagasin"];
	var tmp_post_url = "";
	for(var i=0;i<itemsArr.length;i++) {
		var tmpVal = ($("#" + itemsArr[i]).attr('checked')) ? "X" : "-";
		tmp_post_url = tmp_post_url +
		 "&" + itemsArr[i] + "=" + htmlSecure(tmpVal);
	}
	
	var name = $("#infoName").val();
	var email = $("#infoEmail").val();	
	var phone = $("#infoPhone").val();	
	
	if(name.length==0) { 
		$("#infoName").animate({ 'backgroundColor' : '#f1612d', 'color' : '#fff' },200); 
		err = true; 
	} else { 
		$("#infoName").css({ 'backgroundColor' : '', 'color' : '#000' }); 
	}
	
	if(!validEmail(email)) { 
		$("#infoEmail").animate({ 'backgroundColor' : '#f1612d', 'color' : '#fff' },200); 
		err = true; 
	} else { 
		$("#infoEmail").css({ 'backgroundColor' : '', 'color' : '#000' }); 
	}

	if(phone.length!=8||isNaN(phone)==true) { 
		$("#infoPhone").animate({ 'backgroundColor' : '#f1612d', 'color' : '#fff' },200); 
		err = true; 
	} else { 
		$("#infoPhone").css({ 'backgroundColor' : '', 'color' : '#000' }); 
	}	
	//window.open("/inc/modules/forms/ajax.asp?cmd=orderInfo&name=" + htmlSecure(name) + "&email=" + htmlSecure(email) + "&phone=" + htmlSecure(phone) + tmp_post_url);
	//return false;
	if(!err) {
		$.ajax({
		   type: "POST",
		   url: "/inc/modules/forms/ajax.asp",
		   data: "cmd=orderInfo&name=" + htmlSecure(name) + "&email=" + htmlSecure(email) + "&phone=" + htmlSecure(phone) + tmp_post_url,
		   success: function(msg){
				var boxHtml = "";
				switch(parseInt(msg)) {
					case 0:
						boxHtml = fLang.m.newsletter.signUp_code_0_somethingWentWront;
					break;
					
					case 1:
						if(name.indexOf(" ")>0) {
							var tmp = name.split(" ");
							name = tmp[0];
						}
						boxHtml = fLang.m.newsletter.signUp_code_1_thankYouEmailVerificationSent.replace('%%name%%',name);
					break;
					
					case 2: 
						if(name.indexOf(" ")>0) {
							var tmp = name.split(" ");
							name = tmp[0];
						}					
						boxHtml = fLang.m.newsletter.signUp_code_2_alreadySignedUp.replace('%%name%%',name);
					break;

					case 3:
						if(name.indexOf(" ")>0) {
							var tmp = name.split(" ");
							name = tmp[0];
						}
						boxHtml = fLang.m.newsletter.signUp_code_3_alreadySignedUpButNewSubscription.replace('%%name%%',name);
					break;
					
				}
				$(".shoutBox.infoMail div.c").animate({ 'height' : '159px' },{ duration: 500, easing : 'easeOutQuad' });
				$(".shoutBox.infoMail div.c div:first").animate({ 'opacity' : 0 },{ duration: 500, easing : 'easeOutQuad', complete : function() {
					$(this)
					.html("<div style=\"padding: 20px 0 0 20px; width: 262px\"><h1>Tak!</h1><h2>Vi kontakter dig snarest.</h2></div>")
					.animate({ 'opacity' : 1 },{ duration: 1500, easing : 'easeOutQuad' });
				} });
		   }
		 });		
	}
}