/* =Global stuff
===============================================*/
if (typeof com === 'undefined' || !com) {
    com = {};
}
if (typeof com.herma === 'undefined' || !com.herma) {
    com.herma = {};
}

if (typeof com.herma.dialog === 'undefined' || !com.herma.dialog) {
    com.herma.dialog = {};
}

/* =Notice Box
===============================================*/
com.herma.dialog.notice = {
	init : function() {
		var self = this;

		this.dialog = $('#noticeBox').clone().appendTo($('body'));
		
        $('#noticeBox .popUpClose').click(
            function() {                                
                self.hide();
                return false;
            }
        );

        $('.grid-popUpNoticeBoxRight').click(
            function() {
                self.hide();
                return false;
            }
        );
	},

    show: function(noticeType, content)
    {
    	$('object:visible').each(function(){
    		$(this).wrap('<div class="hiddenDialog" />');
    		$(this).parent().css({
    			height: $(this).height() + "px",
    			width: $(this).width() + "px"
    		});
    		$(this).hide();
    	});
    	
		switch(noticeType) {
		  case 'success':
		    this.dialog.find('div.noticeBoxType').html('Erfolgreich');
			this.dialog.find('span.grid-popUpNoticeBoxLeft').removeClass('grid-popUpNoticeBoxAlert grid-popUpNoticeBoxSuccess').addClass('grid-popUpNoticeBoxSuccess');
            break;   
          case 'notice': 
		    this.dialog.find('div.noticeBoxType').html('Hinweis');
			this.dialog.find('span.grid-popUpNoticeBoxLeft').removeClass('grid-popUpNoticeBoxAlert grid-popUpNoticeBoxSuccess').addClass('grid-popUpNoticeBoxSuccess');
            break;
          case 'alert':
			this.dialog.find('div.noticeBoxType').html('Achtung');
			this.dialog.find('span.grid-popUpNoticeBoxLeft').removeClass('grid-popUpNoticeBoxAlert grid-popUpNoticeBoxSuccess').addClass('grid-popUpNoticeBoxAlert');
		    break; 
          default:
            this.dialog.find('div.noticeBoxType').html('Achtung');
			this.dialog.find('span.grid-popUpNoticeBoxLeft').removeClass('grid-popUpNoticeBoxAlert grid-popUpNoticeBoxSuccess').addClass('grid-popUpNoticeBoxAlert');
            break; 
        }
		
		
		

    	this.dialog.find('div.noticeBoxContent').html(content);
		com.herma.dialog.notice.centerDialog(this.dialog);
    	this.dialog.show();
	},

	hide: function()
    {
    	this.dialog.hide();
    	$('.hiddenDialog').each(function(){
    		$(this).children().show();
    		$(this).children().unwrap();
    	});
	},

	centerDialog : function(dlg) {
		var width = window.innerWidth || document.documentElement && document.documentElement.clientWidth || document.body.clientWidth;
        var height = window.innerHeight || document.documentElement && document.documentElement.clientHeight || document.body.clientHeight;
        var dlgWidth = $(dlg).outerWidth();
        var dlgHeight = $(dlg).outerHeight();
        var left = (width - dlgWidth) / 2;
        var top = (height - dlgHeight) / 2;
        top = (top > 20) ? top : 20;
		$(dlg).css('top', top+'px');
        $(dlg).css('left', left+'px');
	}
}

/* =Confirmation Dialog
===============================================*/
com.herma.dialog.confirmation = {
	init : function() {
		var self = com.herma.dialog.confirmation;

        this.dialog = $('#confirmationDialog').clone().appendTo($('body'));

        $('#confirmationDialog .popUpClose').click(
            function() {
                self.hide();
                return false;
            }
        );
		
		$('.grid-popUpConfirmationDialogConfirm').live('click', self.confirm);
		$('.grid-popUpConfirmationDialogCancel').live('click', self.cancel);
	},

	confirm: function() {
		var self = com.herma.dialog.confirmation;

		self.hide();

		if(typeof self.confirmCB == 'function') {
			self.confirmCB();
		}
		return false;
	},

	cancel: function() {
		var self = com.herma.dialog.confirmation;

		self.hide();
		
		if(typeof this.cancelCB == 'function') {
			this.confirmCB();
		}
		return false;
	},
	
    show: function(title, content, confirmCB, cancelCB)
    {
    	$('object:visible').each(function(){
    		$(this).wrap('<div class="hiddenDialog" />');
    		$(this).parent().css({
    			height: $(this).height() + "px",
    			width: $(this).width() + "px"
    		});
    		$(this).hide();
    	});
    	
		this.confirmCB = confirmCB;
        this.cancelCB  = cancelCB;

		this.dialog.find('div.confirmationDialogTitle').html(title);
    	this.dialog.find('div.confirmationDialogContent').html(content);

		com.herma.dialog.confirmation.centerDialog(this.dialog);
    	this.dialog.show();
	},

	hide: function()
    {
    	com.herma.dialog.confirmation.dialog.hide();
    	$('.hiddenDialog').each(function(){
    		$(this).children().show();
    		$(this).children().unwrap();
    	});
	},

	centerDialog : function(dlg) {
		var width = window.innerWidth || document.documentElement && document.documentElement.clientWidth || document.body.clientWidth;
        var height = window.innerHeight || document.documentElement && document.documentElement.clientHeight || document.body.clientHeight;
        var dlgWidth = $(dlg).outerWidth();
        var dlgHeight = $(dlg).outerHeight();
        var left = (width - dlgWidth) / 2;
        var top = (height - dlgHeight) / 2;
        
        top = (top > 20) ? top : 20;
		$(dlg).css('top', top+'px');
        $(dlg).css('left', left+'px');
	}
}

$(function() {
	com.herma.dialog.notice.init();
	com.herma.dialog.confirmation.init();
});

