/**
 * jQuery RMQLightBox plugin
 * This jQuery plugin to create a Gallery Photo to easy implementation
 * and adapted to me for use like a plugin from jQuery.
 * @name jquery-rmqlightbox-0.1.js
 * @author Ramiro Fdo Gomez RamequeJeque- http://www.ramequejeque.com
 * @version 0.1
 * @date Octiber 11, 2009
 * @category jQuery plugin
 * @copyright (c) 2009 Ramiro Fdo Gomez (ramequejeque.com)
 * @license CC Attribution-No Derivative Works 2.5 Colombia - http://creativecommons.org/licenses/by-sa/2.5/co/
 * @example Visit http://gallery.ramequejeque.com/ for more informations about this jQuery plugin
 */


/*Esta obra está bajo una licencia Reconocimiento-Compartir bajo la misma licencia 2.5 Colombia de Creative Commons. Para ver una copia de esta licencia, visite http://creativecommons.org/licenses/by-sa/2.5/co/ o envie una carta a Creative Commons, 171 Second Street, Suite 300, San Francisco, California 94105, USA.*/

(function($) {
	$.widget("ui.rmqLightBox", {
	
		_init: function() {
			//
			var self = this,
			options = this.options,
			element = this.element;
			/****/
			var defaultTitle = 'RMQLightBox';
			this.lightBoxTitle = (self.options.title) ? self.options.title : (!this.element.attr('title'))? defaultTitle :this.element.attr('title');	
			/****/
			if(self.options.template){
				self._loadTemplate();
			}else{
				this.remove();
				self.debug.error('Falta definir un servicio')	;
			}
			self.createModal();
		},
		createModal:function(){
			var self= this;
			$('body').append('<div class="ui-rmqLightBox-modal" id="ui-modal-rmqLightBox"></div>');
			$('#ui-modal-rmqLightBox').click(function(){
				$('#ui-modal-rmqLightBox').hide();
				$('.ui-rmqLightBox-container').remove();
			})
			self._hideModal();
			
		},
		_hideModal:function(){
			var self= this;
			$('#ui-modal-rmqLightBox').hide();
		},
		_showModal:function(){
			var self= this;
			$('#ui-modal-rmqLightBox').show();
		}
		,
		_loadTemplate: function(){
			var self = this;
			$.ajax({
					url: self.options.template,
					cache: false,
					success: function(html){
						self.setHTML(html);
						self._createLightBox();
					}
			});
		},
		_createLightBox:function(data){
			var self = this;
			rel = 'lightbox:'+self.options.type;
			/**/
			$(self.element).find('a').each(function(arr){
				var dataSouce = new Object();
				dataSouce.link = $(this).attr('href');
				dataSouce.desc = $(this).attr('title');
				/**/
				$(this).attr({href:'javascript:;'})
				$(this).click(function(){self._renderLightBox(dataSouce)});
			});
		},
		_renderLightBox:function(resource){
			var self = this;
			var _html = self.getHTML();
			/**/
			var className = "ui-h";
			$('.ui-rmqLightBox-container').remove();
			self._showModal();
			$('body').append('<div class="ui-rmqLightBox-container"></div>');
			$('.ui-rmqLightBox-container').hide();
			$('.ui-rmqLightBox-container').html('<div class="ui-rmqLightBox-content">'+_html+'</div>');
			$('.ui-rmqLightBox-container').find('.ui-rmqLBox-header').html('Titulo');
			var img = new Image();
			$(img).load(function(){
						$(this).fadeOut();
						$('.ui-rmqLightBox-container').find('.ui-rmqLBox-image').html('').append(this);
			}).error(function () {
						alert('notify the user that the image could not be loaded');
			}).attr('src', resource.link);
			
			imageW = $(img).attr('width');
			imageH = $(img).attr('height');
			/***/
			windowW  = $(window).width();
			windowH = $(window).height();
			/***/
			posWinLeft = (Math.floor(windowW)/2) - (imageW/2);
			posWinTop = ((Math.floor(windowH)/2) - (imageH/2))-80;
			if(posWinTop < 0){
				posWinTop = 10;
			}
			
			/***/
			$('.ui-rmqLightBox-container').css('width',imageW+35+'px');
			$('.ui-rmqLightBox-container').css('left',posWinLeft);
			$('.ui-rmqLightBox-container').css('top',posWinTop);
			$('.ui-rmqLightBox-container').find('.ui-rmqLBox-description').html('').append(resource.desc);
			
			/****/
			$('.ui-rmqLightBox-container').append('<div class="ui-rmqLBox-close"></div>');
			$('.ui-rmqLightBox-container').append('<div class="ui-rmqLBox-next"></div>');
			$('.ui-rmqLightBox-container').append('<div class="ui-rmqLBox-back"></div>');
			$('.ui-rmqLightBox-container').fadeIn().find('.ui-rmqLBox-image > img').fadeIn();
			
		}
		,setHTML:function(_tpl){
			var self = this;
			this.tpl = _tpl;
		},
		getHTML:function(){
			var self = this;
			return self.tpl;
		},debug:{
			error:function(msg){
				if($('.ui-debug-lightBox')){
					$(body).append('<div class="ui-debug-lightBox"></div>');
				}
				$('.ui-debug-gallery').append('<p class="error">'+msg+'</p>');
			},
			info:function(msg){
				if($('.ui-debug-lightBox')){
					$(body).append('<div class="ui-debug-lightBox"></div>');
				}
				$('.ui-debug-gallery').append('<p class="info">'+msg+'</p>');	
			},
		}
	});
	/******/
	$.extend($.ui.rmqGallery, {
		version: "0.1",
		defaults: {
			type:'group'
		}
	});
	/*******/
})(jQuery);

