﻿/*******************/
/** Dropdown menu **/
/*******************/

var timeOut;
function dropMenu(){
	jQuery('#TopMenu a:first-child').css('z-index','1000');
	jQuery("#Dropdown").slideDown(350);
	clearTimeout(timeOut);
	jQuery('#DropBtn').addClass('active');
	jQuery("#Dropdown").hover(
		function () {
			clearTimeout(timeOut);
		}, 
		function () {
			timeOut = setTimeout(function(){jQuery("#Dropdown").slideUp();jQuery('#DropBtn').removeClass('active')},2000);
			;
		}
	);
}

/********************************/
/** Resize height (for bg img) **/
/********************************/
/*
function resizeHeight(){
	if(jQuery('#MasterWrapper').height() < jQuery('#PageContent').height()){
		jQuery('#MasterWrapper').css('height',jQuery('#PageContent').height());
	}
}

jQuery(window).resize(function(){
	//resizeHeight();			   
});
*/

/*********************/
/** Cookie function **/
/*********************/

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
	  //console.log("Cookie name: " + document.cookie);
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
	{
	c_start=c_start + c_name.length+1;
	c_end=document.cookie.indexOf(";",c_start);
	if (c_end==-1) c_end=document.cookie.length;
	return unescape(document.cookie.substring(c_start,c_end));
	}
  }
return "";
}

/********************/
/** Growl settings **/
/********************/

function growlBasket(productname){
	
	jQuery.blockUI({ 
		message: '<div id="GrowlMsg" class="float"><p>'+productname+' er nu lagt i kurven.</p><span class="arrBtn"><a href="/da/CheckOut/ShoppingCart.aspx?ec_trk=Go+to+Shopping+Cart">Din bestilling</a></span><span class="btn"><a class="closeBlockUI" href="#">Luk</a></span></div>', 
		fadeIn: 300, 
		//fadeOut: 300, 
		//timeout: 5000, 
		showOverlay: true,
		//textAlign:      'center', 
		overlayCSS:  { 
			backgroundColor: '#000', 
			opacity:         0.4
		}, 
		//centerY: true, 
		centerX: true, 
		css: { 
			
			width: 'auto',
			border: 'none',
			background: 'none',
			top: '30%', 
			//left: Math.ceil((jQuery(document).width()-jQuery('#PageContent').width())/2+jQuery('#PageContent').width()-215)+'px', 
			left: Math.ceil(jQuery(document).width()/2-105)+'px'
			//right: '10px'
		} 
	});
	jQuery('.blockOverlay').attr('title','Klik for at lukke').click(jQuery.unblockUI);
	jQuery('.closeBlockUI').attr('title','Klik for at lukke').click(jQuery.unblockUI);
	
	// Gammel popup i højre side:
	/*
	if(jQuery(window).scrollTop() > 120) { 
		var distFromTop = '0px'
	} else { 
		var distFromTop = 120-jQuery(window).scrollTop()+'px';
	} 
	jQuery.blockUI({ 
		message: '<div id="GrowlMsg"><p>'+productname+' er nu lagt i kurven.</p><span class="arrBtn"><a href="/da/CheckOut/ShoppingCart.aspx?ec_trk=Go+to+Shopping+Cart">Din bestilling</a></span></div>', 
		fadeIn: 500, 
		fadeOut: 500, 
		timeout: 5000, 
		showOverlay: false, 
		centerY: false, 
		css: { 
			width: 'auto',
			border: 'none',
			background: 'none',
			top: distFromTop, 
			left: Math.ceil((jQuery(document).width()-jQuery('#PageContent').width())/2+jQuery('#PageContent').width()-215)+'px', 
			right: '10px'
			
		} 
	});
	*/
}

/*
jQuery(window).scroll(function () { //Function throws error i IE6 
	if(jQuery(window).scrollTop() > 120) { 
		jQuery(".blockUI").css("top","0px"); 
	} else { 
		jQuery(".blockUI").css("top",120-jQuery(window).scrollTop()+"px"); 
	} 
});
*/


/**********************/
/** Basket functions **/
/**********************/

//console.log("Cookie name: " + document.cookie);
function updateIdField(productCode, primFormId, idFieldId){
	var pageId = jQuery('#PageId').attr('value');
	//replaceSubstring(jQuery(this).text(), ',-', '');
	var sessionId = replaceSubstring(getCookie('SC_ANALYTICS_SESSION_COOKIE'), ',', '');
	//alert(sessionId);
	var d = new Date();
	var timeStamp = d.getTime();
	var productCheckoutId = 'product_'+productCode+'__session_'+sessionId+'__time_'+timeStamp;
	jQuery('#main_0_form_'+primFormId+'_field_'+pageId+'_'+idFieldId).attr('value',productCheckoutId);
}
function submitForm(submitBtnId, productCode){
	var formId = jQuery('#form_'+submitBtnId);
	var isValid = ValidateForm(formId);
	//alert(ValidateForm(formId));
	if(isValid){
		AddToShoppingCart(productCode, 1);
		//alert('#main_0_form_'+submitBtnId+'_form_'+submitBtnId+'_submit')
		jQuery('#main_0_form_'+submitBtnId+'_form_'+submitBtnId+'_submit').click();
	}
}

function submitenter(myfield,e)
{
	var keycode;
	if (window.event){
		keycode = window.event.keyCode;
	}
	else if (e){
		keycode = e.which;
	}
	else {
		return false;
	}
	
	if (keycode == 13)
	{
		var isValid = ValidateForm(myfield.closest('.scfForm'));
		//myfield.form.submit();
		return false;
	}
	else
	{
		return false;
	}
}

function injectToForm(){
	/*console.log(jQuery(".scfSingleLineTextLabel").length);
	jQuery(".scfSingleLineTextLabel").keyup(function(event){
		console.log(event.keyCode);
		if (event.keyCode == '13') {
     event.preventDefault();
		}
		return false;
	});*/
	//strOnSubmit = jQuery("#mainform").attr('onsubmit')+' return false;';
	//console.log(strOnSubmit);
	//jQuery("body").keydown(function(event){
	//	alert(event.keyCode);
	//	return false;
	//});
	/*Query("#mainform").bind('submit',function(){
		return WebForm_OnSubmit(); 
		return false;
	});*/
	//Query("#mainform").attr('onsubmit',jQuery("#mainform").attr('onsubmit')+' return false;');
	
}



/*-------------------------------------------------------------------*/

// Document ready start *******//
jQuery(document).ready(function(){
// ****************************//

jQuery('#DropBtn').click(function(){
	dropMenu();				 
});

/************************/
/** Frontpage redirect **/
/************************/
//alert(document.location.pathname.slice(3));
//alert(document.cookie);
var Domain = location.pathname.slice(4).toLowerCase();
if(Domain.substring(0,6)=='privat'){
	document.body.id = 'Private';
	document.cookie='DAHDomain=private;expires=Mon, 31 Dec 2012 23:59:59 UTC; path=/';
}
else if(Domain.substring(0,7)=='erhverv'){
	document.body.id = 'Business';
	document.cookie='DAHDomain=business;expires=Mon, 31 Dec 2012 23:59:59 UTC; path=/';
}



/**************************/
/** Live search settings **/
/**************************/

jQuery('#liveSearchInput').keyup(function(){
	jQuery('.DealerList').removeHighlight();
	jQuery('.dealerAdditional').removeClass('viewable');
	var searchField = jQuery(this);
	var searchInput = searchField.attr('value');
	//alert(searchInput);
	if(searchInput=='' || searchInput.length<=2){
		jQuery('.dealerInfo').closest('tr').show();
	}else if(searchInput.length>2){
		jQuery('.dealerInfo').closest('tr').hide();
		jQuery('.dealerInfo:contains('+searchInput+')').highlight(searchInput).closest('tr').show();
		jQuery('.additionalInfo:contains('+searchInput+')').each(function(){
			jQuery(this).highlight(searchInput).closest('.dealerAdditional').addClass('viewable');
			jQuery(this).closest('tr').show();
		});
		//$('#output').text(searchInput);
	}
});
/*
jQuery('#stationTable tr').hover(function(){
	jQuery('.viewable').show();
}, function(){
	jQuery('.viewable').hide();
});
*/
/**************************/
/** tableSorter settings **/
/**************************/

jQuery('#stationTable').tablesorter();

/***********************/
/** Fancybox settings **/
/***********************/

jQuery('.openFancyBox').fancybox({
	'margin': 0,
	'padding': 0,
	'overlayColor': '#000',
	'overlayOpacity': 0.4,
	'centerOnScroll': true
});


jQuery("#Offentlig, #kobsbetingelser").fancybox({
	'width': 956,
	'height': 470,
	'margin': 0,
	'padding': 0,
	'overlayColor': '#000',
	'overlayOpacity': 0.4,
	'centerOnScroll': true
});

/************************/
/** Form replace input **/
/************************/

jQuery('.replaceInput').each(function(){
	var thisVal = jQuery(this).attr('value');							
	jQuery(this).focus(function(){
		if(jQuery(this).attr('value')==thisVal){
			jQuery(this).attr('value','');
		}
	}).blur(function(){
		if(jQuery(this).attr('value')==''){
			jQuery(this).attr('value',thisVal);
		}
	});														
});

/****************************/
/** Formular indstillinger **/
/****************************/

jQuery('#paySelect').change(function(){
	var curId = jQuery(this).attr('value');
	jQuery('.paymethod').hide();
	jQuery('#'+curId).show();
});

/**********************/
/** Tabmenu settings **/
/**********************/

jQuery(".TabMenu li:first").addClass('active');
jQuery(".TabMenuWrapper div:first").show();
jQuery(".TabMenu > li").click(function(e){
	jQuery('.TabMenu > li.active').removeClass('active');
	jQuery(this).addClass('active');
	jQuery('.TabContent:visible').hide();
	jQuery('.TabContent').eq(jQuery(this).index()).show();
	return false;
});

/************************/
/** Accordion settings **/
/************************/

jQuery(".Accordion li:first").addClass('AccActive').find('.AccContent').show();
jQuery(".Accordion li h4").click(function(){
	jQuery(".Accordion li .AccContent:visible").slideUp("fast").removeClass('AccActive');
	jQuery('.Accordion').find('.AccActive').removeClass('AccActive');
	jQuery(this).parent().addClass('AccActive').find('.AccContent').slideDown("fast");
	return false;
});

/**********************/
/** Tooltip settings **/
/**********************/

jQuery(".toolTipList li").hover(function(){ 
    jQuery(this).find(".toolTip").show(); 
}, function(){
	jQuery(this).find(".toolTip").hide(); 
});

/***************/
/** Accordion **/
/***************/

jQuery(".accordionLink").click(function(){
	//alert('test')
	if(jQuery(this).hasClass('On')){
		jQuery(this).removeClass('On').next('.accordionContent').slideUp(300);
	}else{
		jQuery(this).addClass('On').next('.accordionContent').slideDown(250);
	}	   
});


jQuery(".toolTipList li").hover(function(){ 
    jQuery(this).find(".toolTip").show(); 
}, function(){
	jQuery(this).find(".toolTip").hide(); 
});

/********************************/
/** Subscription form settings **/
/********************************/
/*
jQuery('.Text').find('input.scfSingleLineTextBox').addClass('Req Text').keypress(function(e){
	//alert(e.keyCode);
	if(e.which==13){
		e.preventDefault();
		return false;
	} else {
		return true;
	}
});
*/
/********************/
/** IE z-index fix **/
/********************/

if(jQuery.browser.msie == true && parseInt(jQuery.browser.version) < 8)
{ 
	var zIndexNumber = 1000; 
	jQuery('div').each(function() { 
		jQuery(this).css('zIndex', zIndexNumber); 
		zIndexNumber -= 10;
	});
}

/**********************/
/** Formatting price **/
/**********************/

jQuery(document).ready(function(){
	jQuery('.price').each(function(){
		var newString = replaceSubstring(jQuery(this).text(), ',-', '');	
		var newString = replaceSubstring(newString, '.', ',');	
		jQuery(this).text(newString);
	});							
});

// End document-ready
});

/****************************/
/** Tip a Friend Functions **/
/****************************/
function openTipAFriend(obj,adjustPos){
	jQuery(".TAFUrlHidden input[type='text']").attr('value',location.href);
	jQuery(".TAFUrlHidden").hide();
	
	var posLeft = (jQuery(obj).position().left+adjustPos)-425 +"px";
	var posTop = jQuery(obj).offset().top+8 +"px";
	jQuery("#TipaFriend").css("left",posLeft).css("top",posTop);
	jQuery("#TipaFriend").show();
}
function closeTipaFriend(){
	jQuery("#TipaFriend").hide();
}

/*-------------------------------------------------------------------*/

function replaceSubstring(inputString, fromString, toString) {
	// Goes through the inputString and replaces every occurrence of fromString with toString
	var temp = inputString;
	if (fromString == "") {
		return inputString;
	}
	if (toString.indexOf(fromString) == -1) {     // If the string being replaced is not a part of the replacement string (normal situation)
		while (temp.indexOf(fromString) != -1) {
			var toTheLeft = temp.substring(0, temp.indexOf(fromString));
			var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
			temp = toTheLeft + toString + toTheRight;
		}
	} else {    // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
		var midStrings = new Array("~", "`", "_", "^", "#");
		var midStringLen = 1;
		var midString = "";
		while (midString == "") {
			for (var i=0; i<midStrings.length; i++) {
				var tempMidString = "";
				for (var j=0; j<midStringLen; j++) { tempMidString += midStrings[i]; }
				if (fromString.indexOf(tempMidString) == -1) {
					midString = tempMidString;
					i = midStrings.length + 1;
				}
			}
		}
		while (temp.indexOf(fromString) != -1) {
			var toTheLeft = temp.substring(0, temp.indexOf(fromString));
			var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
			temp = toTheLeft + midString + toTheRight;
		}
		while (temp.indexOf(midString) != -1) {
			var toTheLeft = temp.substring(0, temp.indexOf(midString));
			var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
			temp = toTheLeft + toString + toTheRight;
		}
	}
	return temp;
}   // Ends the "replaceSubstring" function


/*-------------------------------------------------------------------*/

/*

highlight v3

Highlights arbitrary terms.

<http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html>

MIT license.

Johann Burkard
<http://johannburkard.de>
<mailto:jb@eaio.com>

*/

jQuery.fn.highlight = function(pat) {
 function innerHighlight(node, pat) {
  var skip = 0;
  if (node.nodeType == 3) {
   var pos = node.data.toUpperCase().indexOf(pat);
   if (pos >= 0) {
    var spannode = document.createElement('span');
    spannode.className = 'highlight';
    var middlebit = node.splitText(pos);
    var endbit = middlebit.splitText(pat.length);
    var middleclone = middlebit.cloneNode(true);
    spannode.appendChild(middleclone);
    middlebit.parentNode.replaceChild(spannode, middlebit);
    skip = 1;
   }
  }
  else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) {
   for (var i = 0; i < node.childNodes.length; ++i) {
    i += innerHighlight(node.childNodes[i], pat);
   }
  }
  return skip;
 }
 return this.each(function() {
  innerHighlight(this, pat.toUpperCase());
 });
};

jQuery.fn.removeHighlight = function() {
 return this.find("span.highlight").each(function() {
  this.parentNode.firstChild.nodeName;
  with (this.parentNode) {
   replaceChild(this.firstChild, this);
   normalize();
  }
 }).end();
};

