/*
*This page adds site behavior to any page. It is only based on classes. 
*For any function simply add appropriate class to element.
*/

var HOVER = '_hover';

function getPath(obj) {
	return ($(obj).attr('src')).substring(0, ($(obj).attr('src')).lastIndexOf('/'));
}

function getOriginalImage(obj) {
	var pathAndFileName = $(obj).attr('src');
	var withoutExtension = pathAndFileName.substring(0, pathAndFileName.lastIndexOf('.'));
	var extension = pathAndFileName.substring(pathAndFileName.lastIndexOf('.'),pathAndFileName.length);	
	var fileName = pathAndFileName.substring(withoutExtension.lastIndexOf('/') + 1, withoutExtension.length);
	var without = fileName.substring(0, fileName.indexOf(HOVER));
	return without + extension;
}

function getHoverImage(obj) {
	var pathAndFileName = $(obj).attr('src');
	var withoutExtension = pathAndFileName.substring(0, pathAndFileName.lastIndexOf('.'));
	var extension = pathAndFileName.substring(pathAndFileName.lastIndexOf('.'),pathAndFileName.length);
	var fileName = withoutExtension.substring(withoutExtension.lastIndexOf('/') + 1, withoutExtension.length);
	return fileName + HOVER + extension;
}

function clearLoginForm() {
	$("input[name='email']").attr("value", "");
	$("input[name='password']").attr("value", "");
}

/*  
 * Customer Web
 */
 function setExitWithPopup(value){
 	exitWithPopup = value;
 }

 var exitWithPopup = 1;
 function openB2CPublicPopup() {
 	if(exitWithPopup == 1){
		window.open('http://tuotanto.heyday.fi/fortum/ruotsipopup/','Fortum','toolbar=no,directories=no,menubar=no,scrollbars=no,resizable=no,width=395,height=610',false);
	}
 }
var indicatorId = "offering-loading-indicator-" + new Date().getTime().toString(36);
function forceRef(selector) {
	var ref = $(selector);
	if (ref.length == 0) 
		throw new Error("Bad selector: " + selector);
	else return ref;
};

function overlay() {
	return forceRef("body");
}

function indicator(){ 
	return forceRef("#" + indicatorId); 
}

function showOverlay(){ 
	overlay().addClass("overlay"); 
}

function centerAbove(bg, fg){
	var offset = bg.offset();
	fg.css({
		top: (300) + "px",
		left: (offset.left + ((bg.width() - fg.width()) / 2)) + "px"
	});
}

function showLoadingIndicator() {
	showOverlay();
	/*var LINK = "link[rel=offering-loading-indicator-img]";*/
	var link = "/mittkonto/resources/org.apache.wicket.ajax.AbstractDefaultAjaxBehavior/indicator.gif";
	if (link.length == 0) { 
		throw new Error("Could not find Offering loading indicator image reference: " + LINK);
	}
	$(document.body).append($("<img alt='' style='position: absolute; top: 0; left: 0;'/>").attr({
		src: link, id: indicatorId
	}));
	centerAbove(overlay(), indicator());
}

$(document).ready(function(){
	// Scroll to error message if it's available
	var padding = 30;
	if ($("span.feedbackPanel li.feedbackPanelERROR").length)
	{
		var offset = $(".feedbackPanelERROR").offset().top;
		if (window.self != window.top)
		{
			offset += $(window.self.frameElement).offset().top;
			var par = parent;
			while (par != window.top)
			{
				if (par.frameElement)
					offset += $(par.frameElement).offset().top;
				par = par.parent;
			}
		}
		$('html,body', top.document).animate({scrollTop: (offset - padding)}, 500);
	}
	
	$(".showIndicator").click(function () {  
		// IE fix, otherwise the indicator will freeze when page starts to load
		setTimeout("showLoadingIndicator()",100);		
	});

	// Links with class="external" will be opened in a new window
    $("a.external").click(function() { 
    	window.open(this.href, "wwwfortumsewin"); return false; 
    });
    
	// Links with class="external" will be opened in a new window
    $("a.newwindow").click(function() { 
    	window.open(this.href, "wwwfortumsewin"); return false; 
    });
	
	// Add hover effect to all images with class="button"
	$('img.button').hover(function(){
		var path = getPath(this);
		$(this).attr('src', path + '/' + getHoverImage(this));
	}, function(){
		var path = getPath(this);
	    $(this).attr('src', path + '/' + getOriginalImage(this));
	});
	
	// Add hover effect to all input type image
	$(':image').hover(function(){
		var path = getPath(this);
		$(this).attr('src', path + '/' + getHoverImage(this));
	}, function(){
		var path = getPath(this);
	    $(this).attr('src', path + '/' + getOriginalImage(this));
	});
	
	//Add a divider below errors
	$("span.feedbackPanel ul").after('<img src="/mittkonto/img/nipto/gray_line_long.gif" class="divider" />');
	
	// Red label for all fields that don't validate
	$(".errorHighlight").each(function () {
		$(this).siblings('label').attr('class','errorLabel');
	});
	
	$("form[name='loginForm']").submit(function () {
		if (window.validate!='failure')  {
			window.setTimeout("clearLoginForm()",500);
		}
		return true;
	});
});
