/*
GroupPhone
02-04-2010
-----------------
BrandTech
Jasper Haggenburg
-----------------
POPUP
Able to show error/info/loading-messages
*/
(function($)
{
	
	$.fn.popup = function(I18n) {

		/* private variables */
		var self 	= this;		
		var I18n	= I18n;

		/* public variables */	
		this.$container = $(this).hide();
		this.$title			= this.$container.find('.type');
		this.$message		= this.$container.find('.message');
		this.$close			= this.$container.find('.close').show();
		this.$callback	= this.$container.find('.callback').disableTextSelect();
		this.$bg				= this.$container.find('.bg');
		this.$icon			= this.$container.find('.icon').hide();
		this.ie6				= ($.browser.msie && $.browser.version.substr(0,1)=='6');
		
    /* public methods */
		this.initialize = function() {
			this.$container.find('.inner').css('opacity',.94);
			this.$close.button(self.hide, false, true);
			this.$bg.click(self.hide).css('opacity',.5);
			
			return this;
		};
		
		this.show = function(type, message, callback, callback_text) {		
			//Set content
			var title 	= (I18n[type].title) ? I18n[type].title : type;
			var message = this.getI18n(type, message);

			this.$title.html(title);
			this.$message.html(message);
			
			//Set style
			if(type) this.$container.attr('class',type);
			
			if(!self.ie6)	{
				//Hide by default, show in error() and download()
				this.$icon.hide();
				//Show popup
				this.$container.stop().fadeTo(200,1);
				//Set callbackbutton
				if($.isFunction(callback)) {
					var callback_text = this.getI18n('callback', callback_text);
					this.$callback.text(callback_text).show().unbind('click').click(function() {
						self.hide(callback);
					});
				} else {
					this.$callback.hide();
				}
			} else {
				if(!callback) { alert(message) } else if(confirm(message)) { callback() }
			}
			
			return this;
		};
		
		this.hide = function(callback) {
			self.$container.stop().fadeTo(200,0, function() { 
				if($.isFunction(callback)) callback();
				$(this).hide().removeClass('loading confirm error info');
			});
			return self;
		};
	
		this.error = function(message, info, callback) {		
			this.show('error', message, callback, 'retry');

			//Additional stuff
			this.$icon.show();
			$('<br/>('+info+')').appendTo(this.$message);
		};
		
		this.info = function(message) {
			this.show('info', message);
		};
		
		this.loading = function(message) {
			if($('#popup.error').length == 0 && $('#popup.info').length == 0)
				this.show('loading', message);
		};

		this.hideLoading = function(message) {		
			if(this.$container.hasClass('loading') && this.$message.html() == self.getI18n('loading', message))
				this.hide()
		};		
		
		this.confirm = function(message, callback) {
			this.show('confirm', message, callback, 'default');		
		};

		this.download = function(message, callback) {
			this.show('download', message, callback, 'download');		
			//this.$icon.show();
		};
		
		this.getI18n = function(type, message) {
			message = message.toLowerCase();		
			if(I18n[type][message]) message = I18n[type][message];
			return message;
		};

		return this.initialize();		
	};

})(jQuery);
