function findParent(jQueryObject, hasClass, hasAttr, steps){
	var stepsDone = 0;
	
	while(stepsDone <= steps){
		
		if(hasClass){
			if(jQueryObject.parent().hasClass(hasClass)){
				return jQueryObject.parent();
			} else {
				jQueryObject = jQueryObject.parent();
			}
		} else {
			if(typeof(jQueryObject.parent().attr(hasAttr)) !== 'undefined'){
				return jQueryObject.parent();
			} else {
				jQueryObject = jQueryObject.parent();
			}
		}
		stepsDone++;
	}
	
	return false;
}

function filterString(str, removeBefore, removeAfter){
	if(removeBefore){
		str = str.replace(removeBefore, '');
	}
	
	if(removeAfter){
		str = str.replace(removeAfter, '');
	}
	
	return str;
}

/*
	This function takes the black div and "paints" the whole screen black. 
	The layer has an opacity which makes it a bit transparent.
	
	type	1|0, 1 == Make black, 0 == Remove black layer
*/
function black(type){
	var duration = 500; //How fast the layer fades in or out
	
	if(type == 1){
		$('#messageBackground').css({
			positon : 'absolute',
			top : '0px',
			left : '0px',
			width : $(document).width() + 'px',
			height : $(document).height() + 'px'
		});
		
		$("#messageBackground").css({display : "block"});
		$("#messageBackground").animate({ 
	        opacity: 0.92
	      }, duration );
	} else {
		$("#messageBackground").animate({ 
	        opacity: 0.0
	      }, duration );
	    var t = setTimeout('$("#messageBackground").css({display : "none"})', duration);
	}
}

/*
	Shows a message on the screen (center-positioned), possible with a question
*/
function showMessage(params){
	
	var duration = 500;
	if(params.duration){
		duration = params.duration;
	}
	
	black(1);
	
	$("#messageGraphic").attr('src', 'images/message/graphic.' + params.graphic + '.png');
	$("#messageText").text(params.message);
	
	if(params.altTwo){
		$("#messageBoxLeft").show();
		$("#messageBoxRight").show();
		$("#messageBoxCenter").hide();
		
		//Box one
		$("#messageBoxLeft").click(function(){
    	eval(params.altOne.action);
    	$("#messageBoxLeft").unbind('click');
    });
    
    $("#messageBoxLeft").attr('class', params.altOne.color);
    $("#messageBoxLeft img").attr('src', 'images/message/icon.' + params.altOne.icon + '.png');
    $("#messageBoxLeft span.messageBoxHeadline").text(params.altOne.headline);
    $("#messageBoxLeft span.messageBoxExplain").text(params.altOne.explain);
		
		//Box two
		$("#messageBoxRight").click(function(){
    	eval(params.altTwo.action);
    	$("#messageBoxRight").unbind('click');
    });
    
    $("#messageBoxRight").attr('class', params.altTwo.color);
    $("#messageBoxRight img").attr('src', 'images/message/icon.' + params.altTwo.icon + '.png');
    $("#messageBoxRight span.messageBoxHeadline").text(params.altTwo.headline);
    $("#messageBoxRight span.messageBoxExplain").text(params.altTwo.explain);
	    
	} else {
		//Only one alternative, make this center
		$("#messageBoxLeft").hide();
		$("#messageBoxRight").hide();
		$("#messageBoxCenter").show();
		
		$("#messageBoxCenter").click(function(){
    	eval(params.altOne.action);
    	$("#messageBoxCenter").unbind('click');
    });
	  
    $("#messageBoxCenter").attr('class', params.altOne.color);
    $("#messageBoxCenter img").attr('src', 'images/message/icon.' + params.altOne.icon + '.png');
    $("#messageBoxCenter span.messageBoxHeadline").text(params.altOne.headline);
    $("#messageBoxCenter span.messageBoxExplain").text(params.altOne.explain);
      
    try {
    	//Make sure the "return"-key isn't already bound
    	$(document).unbind('keydown', 'return');
    } catch(errno) {
    	
    }
	  $(document).bind('keydown', 'return', function(){
	  	eval(params.altOne.action);
	  	$(document).unbind('keydown', 'return');
	  });
	}
	
	var xPos = ($(document).width()/2)-($("#message").width()/2);
	//var yPos = (document.documentElement.scrollTop+($(window).height()/2)) - ($("#message").height()/2);
	//var yPos = (screen.height/2) - ($("#message").height()/2) - 100;
	var yPos = ($(window).height()/2) - ($("#message").height()/2);
	
	$('#message').css({
		positon : 'fixed',
		top : yPos + 'px',
		left : xPos + 'px'
	});
	
	$("#message").css({display : "block"});
	$("#message").animate({ 
        opacity: 1.0
      }, duration );
}

/*
	Hide the message from 'showAlertQuestion'
*/
function closeMessage(){
	var duration = 500;
	black(0);
	$("#messsage").animate({ 
        opacity: 0.0
      }, duration );
    var t = setTimeout('$("#message").css({display : "none"})', duration);
    var t2 = setTimeout('resetMessage();', duration);
}

/*
	Resets the message-information
*/
function resetMessage(){
	$("#messageGraphic").attr('src', 'images/message/graphic.default.png');
	$("#messageText").text('Engstream WorkRoom, laddar meddelande...');
	//Left
	$("#messageBoxLeft").attr('class', 'grey');
  $("#messageBoxLeft img").attr('src', 'images/message/icon.white.default.png');
  $("#messageBoxLeft span.messageBoxHeadline").text('Vänta');
  $("#messageBoxLeft span.messageBoxExplain").text('Information laddas');
	//Right
  $("#messageBoxRight").attr('class', 'grey');
  $("#messageBoxRight img").attr('src', 'images/message/icon.white.default.png');
  $("#messageBoxRight span.messageBoxHeadline").text('Vänta');
  $("#messageBoxRight span.messageBoxExplain").text('Information laddas');
	//Center
  $("#messageBoxCenter").attr('class', 'grey');
  $("#messageBoxCenter img").attr('src', 'images/message/icon.white.default.png');
  $("#messageBoxCenter span.messageBoxHeadline").text('Vänta');
  $("#messageBoxCenter span.messageBoxExplain").text('Information laddas');
}

function toggleValue(presentValue, value1, value2){
	if(presentValue == value1){
		return value2;
	} else {
		return value1;
	}
}

function addAlert(id, headline, message, adjustX, adjustY){
	var posX = $("#" + id).position().left - 34 + adjustX; //where 34 is the width of the marker
	var posY = $("#" + id).position().top - 40 + adjustY; //where 40 is the height of the marker
	
	if(posX < 0){
		posX = 0;
	}
	
	var html = '<div class="popupAlert" id="p_alert_' + id + '"><div class="popupStart"></div><div class="popupMiddle"><a href="" class="roundCross"></a><h1 class="dragMe">' + headline + '</h1><p>' + message + '</p></div><div class="popupEnd"></div></div>';
	$("body").append(html);
	$("#p_alert_" + id).css({
		'left' : posX + 'px',
		'top' : posY + 'px'
	});
	
	var marker = '<a href="" class="popupAlertMarker" id="p_alert_marker_' + id + '"></a>';
	$("body").append(marker);
	$("#p_alert_marker_" + id).css({
		'left' : posX + 'px',
		'top' : posY + 'px',
		'zIndex' : 100
	});
	
	$("#p_alert_" + id).draggable({ handle: 'h1.dragMe', zIndex: 1000, start: function(event, ui){ $(".popupAlert").css({zIndex:100});$(this).css({zIndex:200});}, stop: function(event, ui){ $(this).css({zIndex:200});} });
	
	/************** Popup Alert **************/
	$("#p_alert_marker_" + id).click(function(ev){
		ev.preventDefault();
		
		$("#p_alert_" + id).css({top: posY + 'px', left: posX + 50 + 'px', zIndex: 201 });
		
		$("#p_alert_" + id).show();
	});
	
	$("#p_alert_" + id + " a.roundCross").click(function(ev){
		ev.preventDefault();
		$("#p_alert_" + id).hide();
	});
	
	$("#p_alert_" + id).click(function(ev){
		$(".popupAlert").css({ zIndex: 100 });
		$("#p_alert_" + id).css({ zIndex: 200 });
	});
}

function removeAllAlerts(){
	$("a.popupAlertMarker").remove();
	$("div.popupAlert").remove();
}

function validateDate(dateStr) {
	if(dateStr==""){
		return false;
	} else {
		var r = /^\d{4}-{1}\d{2}-{1}\d{2}$/
		return r.test(dateStr); // Returns true or false
	}
	return false;
}