var textInputValue;
//$(document).pngFix();
//var textInputValues = new Array();
var ie7 = (document.all && !window.opera && window.XMLHttpRequest) ? true : false;

$(document).ready(function(){
	var contentHeight = $(".colrow").height();
	$(".verticalDivider.right, .verticalDivider.left").height(contentHeight);
	$(".mainBody hr").wrap('<div class="horizontalDividerTightMiddle"></div>')
	
	$(".puffStart.smallWrapper").each(function(){
		var wrapperHeight = $(this).height();
		var verticalDivider = $(this).find(".verticalDivider");

		$(verticalDivider).height(wrapperHeight);
	});
		
	//$(".verticalDivider")
	
	$("div#pagemain .list-items li").each(function(){
		image = $(this).find("img");
		imageHeight = $(image).height();
		listItemHeight = $(this).height();
		difference = listItemHeight - imageHeight;
		if(difference > 20){
			$(image).css({"marginBottom" : difference + "px"});
		}
	});
	
	var ver = getInternetExplorerVersion();

	calculateConsumption();
		
		
	var zIndexNumber = 1000;
	$('div').each(function() { 
		if(zIndexNumber >= 0){
			$(this).css('zIndex', zIndexNumber); zIndexNumber -= 10; 
		}
	});
	
	Cufon.replace('.cufon', { hover: true, fontFamily: 'Telge Energi'});
	//Cufon.replace('h2', { hover: true, fontFamily: 'calvert' });
	//Cufon.replace('h3', { hover: true, fontFamily: 'calvert' });
	//Cufon.replace('h4', { hover: true, fontFamily: 'calvert' });
	//Cufon.replace('ol', { hover: true, fontFamily: 'calvert' });

	if(ver == 7){
		/*$(".image-wrap p img").each(function(){
			thisHeight = $(this).height();
			difference = 97 - thisHeight;
			newMarginTop = 49 + (difference / 2);
			$(this).css("marginTop", newMarginTop + "px");
		})*/
		
		/*$("p.cufon cufon").each(function(){
			
		})*/
		
	}
	$("div.mainBody tr:odd").addClass("even"); // Switched to match FF styling by CSS
        $("div.mainBody tr:even").addClass("odd");

	$(".searchOptionsButton").click(function(){
		$(".searchOptions").toggle();
		//$(this).css("backgroundImage", "url('graphics/arrow_up.gif')");
		$(this).toggleClass("open");
	})
	
	var i = 0;
	$("input[type='text']").each(function(){
	})
	
	$("input[type='text']").focus(function(){
		textInputValue = $(this).val();
		//console.debug(textInputValue);
		$(this).val("");
	});
	$("input[type='text']").blur(function(){
		if($(this).val() == "" || $(this).val() == " "){
			$(this).val(textInputValue);
		}
	})
	
	$(".loginPassword.plain").focus(function(){
		thisHiddenPassword = $(this).next(".loginPassword.hidden");
		$(this).hide();
		$(thisHiddenPassword).show().focus();
	});
	$(".loginPassword.hidden").blur(function(){
		thisHiddenPassword = $(this).prev(".loginPassword.plain");
		$(this).hide().val("");
		$(thisHiddenPassword).show().val("Lösenord");
	});
	
	$("#topMenu a").click(function(){
		$("#topMenu a").removeClass("active");
		$(this).addClass("active");
		//return false;
	});

	$("#topSelection a").click(function(){
		$("#topSelection a").removeClass("active");
		$(this).addClass("active");
		//return false;
	});
	
	$("#leftmenu ul a").click(function(){
		$("#leftmenu ul a").removeClass("active");
		$(this).addClass("active");
		//return false;
	});
	
	$("#headerTopItems .login").click(function(){
		$("#loginBoxWrapper").toggle();
		return false;
	})

	$("input[type='submit'], button").mouseover(function(){
		$(this).css("backgroundPosition", "0 -22px");
	});
	$("input[type='submit'], button").mouseout(function(){
		$(this).css("backgroundPosition", "0 0");
	});
	$("input[type='submit'], button").mousedown(function(){
		$(this).css("backgroundPosition", "0 -44px");
	});
	$("input[type='submit'], button").mouseup(function(){
		$(this).css("backgroundPosition", "0 -22px");
	});

	$("input.searchButtonBig").mouseover(function(){
		$(this).css("backgroundPosition", "0 -28px");
	})
	$("input.searchButtonBig").mouseout(function(){
		$(this).css("backgroundPosition", "0 0");
	})
	$("input.searchButtonBig").mousedown(function(){
		$(this).css("backgroundPosition", "0 -56px");
	})
	$("input.searchButtonBig").mouseup(function(){
		$(this).css("backgroundPosition", "0 -28px");
	})

	$("button.delete").mouseover(function(){
		$(this).css("backgroundPosition", "0 -15px");
	})
	$("button.delete").mouseout(function(){
		$(this).css("backgroundPosition", "0 0");
	})
	$("button.delete").mousedown(function(){
		$(this).css("backgroundPosition", "0 -30px");
	})
	$("button.delete").mouseup(function(){
		$(this).css("backgroundPosition", "0 -15px");
	})
})

function getInternetExplorerVersion() {    
	var rv = -1; // Return value assumes failure.    
	if (navigator.appName == 'Microsoft Internet Explorer'){        
		var ua = navigator.userAgent;        
		var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");        
		if (re.exec(ua) != null){
			rv = parseFloat(RegExp.$1);    
		}
	}    
	return rv;
}
var pelletsprisperton = 2300;
var pelletsprispersack = 2600;
var eloreperkvh = 110;
var pelletspris = 0;
var validateMsg = 'Fälten "Elförbrukning i KWh" och "Avgår hushållsel" måste fyllas i.';
var chosen = '';

function onBodyLoad()
{
document.getElementById("eloreperkwh").innerHTML = eloreperkvh;
}

function calculateConsumption(){
	//console.debug(pelletsprisperton);
}

// ***********************************************
// SHARED FUNCTIONS
// ***********************************************

var thl="+"
function fix(num,places){
	shift = Math.pow(10,places);
	totalDecimal = "" + Math.round(num * shift);
	integer = totalDecimal.substring(0,totalDecimal.length - places );
	decimal = totalDecimal.substring(totalDecimal.length - places,totalDecimal.length - places + 2);
	return(integer + "." + decimal);
}

function CheckNumericKeyInfo(event){
	var Key = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if( Key == 13 || Key == 44 || Key == 46 || Key == 8 || Key == 116 || Key == 9 || ( Key >= 48 && Key <=57 ) )
	return true;
	else
	return false;
}


function Calculate(){
	getPellets();

	c = $("#A").val() - $("#B").val();
	d = c * (eloreperkvh/100);
	e = c / 4.8 / 0.8 / 1000;
	g = e * pelletspris;
	h = d - g;
	
	e = e.toFixed(2);
	d = Math.round(d);
	g = Math.round(g);
	h = Math.round(h);

	if(ValidateForm()){
		$(".contactform.results").show();
		document.getElementById("C").innerHTML = c;
		document.getElementById("D").innerHTML = d;
		document.getElementById("E").innerHTML = e;
		document.getElementById("G").innerHTML = g;
		document.getElementById("H").innerHTML = h;
	}
}

function ValidateForm(){
	if(document.getElementById("A").value == '' || document.getElementById("B").value == ''){
		alert(validateMsg);
		document.getElementById("A").focus();
		return false;
	}

	return true;
}
function getPellets(){
	$(".radio").each(function(){
		if($(this).is(":checked")){
			chosen = $(this).val();
		}
	})

	if(chosen == ""){
		alert("Leveransform av pellets måste anges, innan beräkningar kan göras");
		return false;
	}

	if(chosen == "sack"){
		document.getElementById("pelletspris").innerHTML = pelletsprispersack;
		pelletspris = pelletsprispersack;
	}
	// Bulk
	else if(chosen == "bulk"){
		document.getElementById("pelletspris").innerHTML = pelletsprisperton;
		pelletspris = pelletsprisperton;
	}
}