﻿/*
	func.js
------------------------------------------------------ */

//trim function. used in the FAQ function to clean up the markup.
function trim(str) {
	if(!str) return false;
	str = str.replace(/^\s\s*/, '');
	var ws = /\s/;
	var i = str.length;
	while (ws.test(str.charAt(--i)));
	return str.slice(0, i + 1);
}

function searchRedirect(event, searchBoxId, searchUrl) {
	if(event.keyCode == 13) {
		window.location = searchUrl + "?k=" + $('#' + searchBoxId).val();
		return false;
	}
	return true;
}

// set new default for autocompleter height:
$.Autocompleter.defaults.scrollHeight = 300; //px



$(document).ready(function(){
	//auto-adjust height of boxes on frontpage 
	// <div class="row autoheight">
	var whatHeight = (typeof document.body.style.maxHeight === "undefined") ? 'height' : 'min-height';
	$(".autoheight").each(function(){
		var highest = 0; 
		//var $cols = $(this).find(".box");
		var $cols = $(this).find(".box-body");
		$cols.each(function(){
			var $col = $(this);
			var h = $col.outerHeight();
			if (h > highest) {highest=h;}
		});
		$cols.each(function(){
			$(this).css(whatHeight, highest);
		});
	});

	
	//To change title text to Danish of the search result Next and Previous links
	$("#SRP a[id^=SRP_P]").each(function(i){
	    if($(this).attr("id") != "SRP_Prev"){
	        $(this).attr("title","Side "+$(this).attr("id").slice(5));
	    }
	});
	
    if($("a#SRP_Next").length){
        $("a#SRP_Next").attr("title","Næste");	
	}
	
	if($("a#SRP_Prev").length){
        $("a#SRP_Prev").attr("title","Forrige");	
	}
	
	$('table[id*="falckContentTable"]').each(function(){ 
        $table = $(this);
        $table.addClass('data zebra')
        $table.find("tbody tr:even").addClass("even");
        $table.find("tbody tr:odd").addClass("odd zebra");
    });

	
	// setting up tabs
	var tabs = $("#tabs");
	if(tabs.size() > 0) {
	    var $tabs = tabs.tabs();
	    showSearchTermTab($tabs);
	}
	
	// fixing the list for products
	$('.prod li').wrapInner('<span />');

	// close success message
	$("#message").animate({opacity: 1.0}, 3000).fadeOut("slow");
	
	$(".faq").each(function(){

		$(this).find("h3").parent().contents().filter(function(){
			if (this.nodeType != 1){
				if ( trim(this.nodeValue) == "" ) {
					$(this).remove();
					return false;
				}
				return true;
			}
			return false
		}).wrap("<p/>");

		$(this).find("h3").each(function(){
			$(this)
			.nextUntil( "h3" ).wrapAll('<div class="faq-answer" style="display:none"/>')
			.end()
			.hover(function(){ $(this).addClass("hover"); },function(){ $(this).removeClass("hover"); })
			.toggle(
				function(){
					$(this).addClass("on").next(".faq-answer").show(); //.stop(true,true).slideDown(200);
				},
				function(){
					$(this).removeClass("on").next(".faq-answer").hide(); //.stop(true,true).slideUp(200);
				}
			);
		});
	});


	// accordion styled menu in section_img...  
	$(".accordion").accordion({ header: "h3",event: "mouseover", autoHeight:false });

	
	// annotation 
	$('p.annotation').before('<a class="annotation" href="#">B</a>');
	$('a.annotation').click(function(){
		return false;
	}).focus(function(){
		$(this).next('p.annotation').css($(this).offset()).show();
		return false;
	}).blur(function(){
		$(this).next('p.annotation').hide();
		return false;
	}).hover(function(){
		$('p.annotation:visible').hide();
		$(this).next('p.annotation').css($(this).offset()).show();
	},function(){
		$(this).next('p.annotation').hide();
	});

	// tooltip
	$('p.help').before('<a class="help" href="#">?</a>');
	$('p.help').wrap('<div class="help" />');
	$('a.help').click(function(){
		return false;
	}).focus(function(){
		$(this).next('div.help').css($(this).offset()).show();
		return false;
	}).blur(function(){
		$(this).next('div.help').hide();
		return false;
	}).hover(function(){
		$('div.help:visible').hide();
		$(this).next('div.help').css($(this).offset()).show();
	},function(){
		$(this).next('div.help').hide();
	});

	// sortable tables...
	$("table.sortable").tablesorter({widgets: ['zebra']});
});
/*
	adding round corners
------------------------------------------------------ */
if($.browser.msie && $.browser.version > 6){
	$(document).ready(function() {
		$('div.round').append('<b class="ctl"></b><b class="ctr"></b><b class="cbl"></b><b class="cbr"></b>');
	});
}


/*
    customer service
------------------------------------------------------ */
function manualAutoComplete(searchString, data, messageElement) {
    var hit = false;
    if(searchString != '') {
    $(data).each(function(index, el) {
        if(searchString == el.text) {
            hit = true;
        } else { 
            var parts = (el.text.split(':'));
				if(parts[0].length && parts[1].length){
					if(trim(parts[1]).toLowerCase() == trim(searchString).toLowerCase() || trim(parts[0]).toLowerCase() == trim(searchString).toLowerCase()) {
						hit = true;
					}					
				}			
        }
        if(hit) {
            document.location = el.url;
            return false;
        }
    });
    }
    if(!hit) {
        messageElement.show();
		messageElement.prevAll(".ac_input").keypress(function(e){
			if(e.which == "8"){
	            messageElement.hide();
				$(this).unbind("keypress");
			}				
		});
        $(document).click(function() {
            messageElement.hide();
        }); 
    }
}

/*
    show the tab containing the searched term
---------------------------------------------- */
function showSearchTermTab($tabs) {
    var searchterm = getSearchTerm();
    if(searchterm) {
        $('div#tabs > div').each(function(index, div){
	    if($(div).html().indexOf(searchterm) != -1) {
                $tabs.tabs('select',index);
                return false;
            }
        }
        );
    }
}

function getSearchTerm() {
    var refUri = parseUri(document.referrer);
    var curUri = parseUri(window.location);
    if(refUri.host == curUri.host) {
        return refUri.queryKey.k;
    } else if(refUri.host.indexOf("yahoo") != -1) {
        return refUri.queryKey.q?refUri.queryKey.q:refUri.queryKey.p;
    }
    else {
        return refUri.queryKey.q;
    }
}

function parseUri (str) {
	var	o   = parseUri.options,
		m   = o.parser.exec(str),
		uri = {},
		i   = 14;

	while (i--) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});

	return uri;
};

parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
		name:   "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
};


/*
	from old ie6.js... used on falck.com? 
------------------------------------------------------ */
if($.browser.msie && $.browser.version < 7){
	$(document).ready(function() {
	    // IE gets some :hover action
	    if (document.all) {
	        $("ul.dropdown li").hoverClass("Hover");
	        $("ul.dropdown li ul").hoverClass("Hover");
	        $("#nav>ul>li").hoverClass("Hover");
	    }
	});
	
	// Generic function for adding .hover on any element in IE<7
	$.fn.hoverClass = function(c) {
	    return this.each(function() {
	        $(this).hover(
				function() { $(this).addClass(c); },
				function() { $(this).removeClass(c); }
			);
	    });
	};
}


