/**
 * Lightbox plugin
 * 
 * Dependencies :
 *  UI Core
 *  UI Position
 *  UI Widget
 *  UI Dialog
 */
(function($){
	
	/**
	 * @param string id
	 * @return Lightbox
	 */
	$.lightbox = function(id){
		return new LightBox(id);
	};
	
	/**
	 * @param string id
	 */
	var LightBox = function(id){
		/**
		 * @private string
		 */
		var _id = id ? id : 'lightbox';
		/**
		 * @private string
		 */
		var _title = '';
		/**
		 * @private string
		 */
		var _content ='';
		/**
		 * @private bool
		 */
		var _showCloseButton = false;
		/**
		 * @private bool
		 */
		var _showWrapper = false;
		/**
		 * @private bool
		 */
		var _created = false;
		/**
		 * @private div
		 */
		var _lightbox;
		
		/**
		 * Init
		 */
		$.includeCssFile('/lib/jquery/plugin/jquery.lightbox.css');
		_lightbox = $('<div id="'+_id+'" class="lightbox"></div>');
		$(document).append(_lightbox);
		
		/**
		 * @return void
		 */
		var _create = function(){
			if(!_created){
				_lightbox.dialog({
					autoOpen: false,
					modal: true,
					resizable: false,
					draggable: false,
					width: 'auto',
					open: function(){
						if(_showCloseButton){
							$('.ui-dialog-titlebar-close').show();
						}
						else{
							$('.ui-dialog-titlebar-close').hide();
						}
						if(_title == ''){
							$('.ui-dialog-titlebar').hide();
						}
						else{
							$('.ui-dialog-titlebar').show();
						}
						if(!_showWrapper){
							_hideWrapper();
						}
					},
					close: function(){
						if((_id == 'lightbox-video') || (_id == 'lightbox-footer')){
							_lightbox.html('');
						}
					}
				});
				_created = true;
			}
		};
		
		/**
		 * @return void
		 */
		this.open = function(){
			_create();
			if(!_lightbox.dialog('isOpen')){
				_lightbox.html(_content);
				_lightbox.dialog('option', 'title', _title);
				_lightbox.dialog('open');
			}
		};
		
		/**
		 * @return void
		 */
		this.clean = function(){
			_lightbox.dialog('option', 'title', '');
			_title = '';
			_lightbox.html('');
			_content = '';
		}
		
		/**
		 * @return void
		 */
		this.refresh = function(){
			if(_lightbox.dialog('isOpen')){
				_lightbox.html(_content);
				_lightbox.dialog('option', 'title', _title);
			}
		};
		
		/**
		 * @return void
		 */
		this.close = function(){
			if(_lightbox.dialog('isOpen')){
				_lightbox.dialog('close');
				this.clean();
			}
		};
		
		/**
		 * @return void
		 */
		this.destroy = function(){
			_lightbox.dialog('destroy');
			$('#'+_id).remove();
		};
		
		/**
		 * @param string title
		 * @return void
		 */
		this.setTitle = function(title){
			_title = title;
		};
		
		/**
		 * @param string content
		 * @return void
		 */
		this.setContent = function(content){
			_content = content;
		};
		
		/**
		 * @return void
		 */
		this.showCloseButton = function(){
			_showCloseButton = true;
		};
		
		/**
		 * @return void
		 */
		this.hideCloseButton = function(){
			_showCloseButton = false;
		};
		
		/**
		 * @return void
		 */
		this.showWrapper = function(){
			_showWrapper = true;
		};
		
		/**
		 * @return void
		 */
		this.hideWrapper = function(){
			_showWrapper = false;
		};
		
		/**
		 * @return void
		 */
		var _hideWrapper = function(){
			$('#'+_id).css('padding', 0);
			$('#'+_id).parent().css('padding', 0);
			$('#'+_id).parent().css('border', 0);
			//$('#'+_id).parent().css('background', 'none');
			$('#'+_id).parent().css('border-radius', 0);
		}
	};
})(jQuery);
