/*
	HUMANIZED MESSAGES 1.0
	idea - http://www.humanized.com/weblog/2006/09/11/monolog_boxes_and_transparent_messages
	home - http://humanmsg.googlecode.com
	adapted by schmiroh 2008
*/

var humanMsg = {
    msgID: "humanMsg",
    processingID: "humanProcessing",
    overlayOpacity: 0.1,
    msgOpacity: 1.0, //for message and processing
	setup: function(appendTo, logName, msgOpacity) {
	    humanMsg.destroyElement("overlay");
	    humanMsg.destroyElement(humanMsg.msgID);
	    humanMsg.destroyElement(humanMsg.processingID);

		// appendTo is the element the msg is appended to
		if (appendTo == undefined)
			appendTo = "body";

		if (msgOpacity != undefined) 
			humanMsg.msgOpacity = parseFloat(msgOpacity);

        // Inject the overlay (body always) and hide
        jQuery("body").append('<div id="overlay"></div>');
        jQuery("#overlay").hide();
        
		// Inject the message structure
		//jQuery(appendTo).append('<div id="'+humanMsg.msgID+'" class="humanMsg"><div id="humanClass"><div class="round"></div><p></p><div class="round"></div></div></div>');
		jQuery(appendTo).append('<div id="'+humanMsg.msgID+'" class="humanMsg"><div style="position:relative;left:15px;top:15px;"><p></p></div></div>');
		
		// Inject the progressing message structure
		jQuery(appendTo).append('<div id="'+humanMsg.processingID+'" class="humanMsg progress" style="position:relative;"><div style="position:absolute;top:40px;left:40px;display:table-cell;vertical-align:middle;"><p style="position:relative;top:-15px;">Bitte warten...&nbsp;<img src="Images/loading.gif" alt="loading" /></p></div></div>');
	},

	displayMsg: function(msg, className) {
		if (msg == "")
			return;

		clearTimeout(humanMsg.t2);

        // Stretch overlay to fill page and fade in
        var arrayPageSize = humanMsg.getPageSize();
        jQuery("#overlay").hide().css({width: "100%", height: arrayPageSize[1]+"px", opacity: humanMsg.overlayOpacity}).fadeIn();

		// Set class
		jQuery("#"+humanMsg.msgID).removeClass("info warning error");
		jQuery("#"+humanMsg.msgID).addClass(className);

		// Inject message
		jQuery("#"+humanMsg.msgID+" p").html(msg);
	
	    // Vertically center message
	    jQuery("#"+humanMsg.msgID+"").vCenter();

		// Adapt display attribute of div-container
        jQuery("#"+humanMsg.msgID).css({display: "inline", opacity: humanMsg.msgOpacity});

		// Show message
		//jQuery("#"+humanMsg.msgID+"").show().animate({ opacity: humanMsg.msgOpacity}, 200);
		jQuery("#"+humanMsg.msgID+"").show();

		// Watch for mouse & keyboard in 3s
		humanMsg.t1 = setTimeout("humanMsg.bindEventsRemoveMsg()", 3000);
		// Remove message after 10s
		humanMsg.t2 = setTimeout("humanMsg.removeMsg()", 10000);
	},

	removeMsg: function() {
		// Unbind mouse & keyboard
		jQuery(window)
			.unbind("mousemove", humanMsg.removeMsg)
			.unbind("click", humanMsg.removeMsg)
			.unbind("keypress", humanMsg.removeMsg)

		// If message is fully transparent, fade it out
		if (jQuery("#"+humanMsg.msgID).css("opacity") == humanMsg.msgOpacity)
			jQuery("#"+humanMsg.msgID).animate({ opacity: 0 }, 500, function() { jQuery(this).hide() })
		//  Fade out overlay too
		if (jQuery("#overlay").css("opacity") == humanMsg.overlayOpacity) {
		    jQuery("#overlay").animate({ opacity: 0 }, 500, function() { jQuery(this).hide() })
		}
		
		// Adapt display attribute of div-container
        jQuery("#"+humanMsg.msgID).css({display: "none"});
	},

	bindEventsRemoveMsg: function() {
	    // Remove message if mouse is moved or key is pressed
		jQuery(window)
			.mousemove(humanMsg.removeMsg)
			.click(humanMsg.removeMsg)
			.keypress(humanMsg.removeMsg)
	},

	displayProcessingMsg: function() {
	    // Vertically center message
	    jQuery("#"+humanMsg.processingID+"").vCenter();

		// Adapt display attribute of div-container
        jQuery("#"+humanMsg.processingID).css({display: "inline", opacity: humanMsg.msgOpacity});

		// Show message
		jQuery("#"+humanMsg.processingID+"").show();
	},

	removeProcessingMsg: function() {
		// If message is fully transparent, fade it out
		if (jQuery("#"+humanMsg.processingID).css("opacity") == humanMsg.msgOpacity)
			jQuery("#"+humanMsg.processingID).animate({ opacity: 0 }, 500, function() { jQuery(this).hide() })

		// Adapt display attribute of div-container
        jQuery("#"+humanMsg.processingID).css({display: "none"});
	},

    getPageSize : function(){
        var xScroll, yScroll;

        if (window.innerHeight && window.scrollMaxY) {  
                xScroll = window.innerWidth + window.scrollMaxX;
                yScroll = window.innerHeight + window.scrollMaxY;
        } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
                xScroll = document.body.scrollWidth;
                yScroll = document.body.scrollHeight;
        } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
                xScroll = document.body.offsetWidth;
                yScroll = document.body.offsetHeight;
        }

        var windowWidth, windowHeight;

        if (self.innerHeight) { // all except Explorer
                if(document.documentElement.clientWidth){
                        windowWidth = document.documentElement.clientWidth; 
                } else {
                        windowWidth = self.innerWidth;
                }
                windowHeight = self.innerHeight;
        } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
                windowWidth = document.documentElement.clientWidth;
                windowHeight = document.documentElement.clientHeight;
        } else if (document.body) { // other Explorers
                windowWidth = document.body.clientWidth;
                windowHeight = document.body.clientHeight;
        }       

        // for small pages with total height less then height of the viewport
        if(yScroll < windowHeight){
                pageHeight = windowHeight;
        } else { 
                pageHeight = yScroll;
        }

        // for small pages with total width less then width of the viewport
        if(xScroll < windowWidth){      
                pageWidth = xScroll;            
        } else {
                pageWidth = windowWidth;
        }

        arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
        return arrayPageSize;
    },

    destroyElement: function(id){
        if(el = document.getElementById(id)) {
            el.parentNode.removeChild(el);
        }
    }
};

// vertically center something in the viewport
(function($){
   $.fn.vCenter = function(options) {
     var pos = {
       sTop : function() {
         return window.pageYOffset || $.boxModel &&
            document.documentElement.scrollTop ||   document.body.scrollTop;
       },
       wHeight : function() {
            if ( $.browser.opera || ($.browser.safari && parseInt($.browser.version) > 520) ) {
                            return window.innerHeight - (($(document).height() >window.innerHeight) ? getScrollbarWidth() : 0);
            } else if ( $.browser.safari ) {
                    return window.innerHeight;
            } else {
            return $.boxModel && document.documentElement.clientHeight || document.body.clientHeight;
         }
       }
     };
     return this.each(function(index) {
       if (index == 0) {
         var $this = $(this);
         var elHeight = $this.height();
         $this.css({
           position: "absolute",
           marginTop: "0",
           top: pos.sTop() + (pos.wHeight() / 2) - (elHeight / 2)
        });
       }
     });
   };
})(jQuery);

jQuery(document).ready(function(){
	humanMsg.setup();
})