/**
 * jQuery RMQGalley 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-rmqgallery-0.1.js
 * @author Ramiro Fdo Gomez RamequeJeque- http://www.ramequejeque.com
 * @version 0.1
 * @date Octiber 10, 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.rmqGallery", {
	
		_init: function() {
			console.log('init'+ this.element.attr('id'))
			
			//
			var self = this,
			
			
			options = this.options;
			console.log('init RmqGallery :'+self.options.template)

			var defaultTitle = 'RMQGallery';
			this.galleryTitle = (self.options.title) ? self.options.title : (!this.element.attr('title'))? defaultTitle :this.element.attr('title');	
			
			if(self.options.template){
				self._loadTemplate();
			}else{
				self.destroy();
			}
			
			var idGallery = self.element.attr('id');
			self.createModal();
			
		},
		createModal:function(){
			var self= this;
			$('#'+self.idGallery).append('<div class="ui-rmqGallery-modal" id="ui-modal-'+self.idGallery+'"></div>');
			self.hideModal();
			
		},
		hideModal:function(){
			var self= this;
			$('#ui-modal-'+self.idGallery).hide();
		},
		showModal:function(){
			var self= this;
			$('#ui-rmqGallery-modal'+self.idGallery).show();
		},
		destroy: function() {
			var self= this;
			$('#'+self.idGallery).remove();
		}
		,
		_loadTemplate: function(){
			console.log('_loadTemplate')
			var self = this;
			$.ajax({
					url: self.options.template,
					cache: false,
					success: function(html){
						console.log(html);
						self.setHTML(html);
						self._loadGalleryData();
					}
			});
		},
		_loadGalleryData: function(){
			var self = this;
			var typeService = self.options.type;
			console.log('_loadGalleryData : '+self.options.type);
			/**/
			switch(typeService){
				case "jSon":
				case "json":
					$.getJSON(self.options.service,
						function(data){
							self._createGallery(data)
							console.log(data);
					});

				break;
				default:
				break;
			}
		}
		,
		_createGallery:function(data){
			var self = this;
			var _html = self.getHTML();
			$('#'+self.idGallery).append('<h1>'+self.galleryTitle+'</h1>');
			$('#'+self.idGallery).append('<div class="ui-rmqGallery ui-rmwG-container"></div>');
			var className = "ui-h";
			var rel = '';
			if(!self.options.horizontal){className ='ui-v';};
			if(self.options.lightBox){rel = 'lightbox-enabled';};
			$.each(data.gallery.projects, function(ind,item){			
				$('#'+self.idGallery+' > .ui-rmqGallery').append('<div id="'+'ui-rmqGallery-item-'+ind+'" class="'+className+'">'+_html+'</div>');							
			});
			$.each(data.gallery.projects, function(ind,item){
				var imageTmp = '<a href="'+item.imageFull+'" rel="'+rel+'" title="'+item.description+'"><img src="'+item.imageTmb+'" /></a>';
				var dateTmp = item.iniDate+' - '+item.endDate;
				/**/
				$('#ui-rmqGallery-item-'+ind).find('.ui-gallery-header').html(item.name);	
				$('#ui-rmqGallery-item-'+ind).find('.ui-gallery-image').html(imageTmp);
				$('#ui-rmqGallery-item-'+ind).find('.ui-gallery-company').html(item.company);							
				$('#ui-rmqGallery-item-'+ind).find('.ui-gallery-details').html(item.description);	
				$('#ui-rmqGallery-item-'+ind).find('.ui-gallery-date').html(dateTmp);	
				});
			$('.ui-rmqGallery').find('a').each(function(arr){
					$(this).attr({rel:'lightbox:unique'})														
			})
			$('.ui-rmqGallery').rmqLightBox({type:'unique',
											template: 'pages/portfolio/views/tpl.lightBox.tpl'
			});
		}
		,setHTML:function(_tpl){
			var self = this;
			this.tpl = _tpl;
		},
		getHTML:function(){
			var self = this;
			return self.tpl;
		}
		,debug:{
			error:function(msg){
					$(body).append('<div class="ui-debug-gallery error"></div>');
					$('.ui-debug-gallery').append('<p>'+msg+'</p>');
			},
			info:function(msg){
				$(body).append('<div class="ui-debug-gallery info"></div>');
				$('.ui-debug-gallery').append('<p>'+msg+'</p>');	
			},
		}
	});
	/******/
	$.extend($.ui.rmqGallery, {
		version: "0.1",
		defaults: {
			horizontal:true,
			lightBox:true
		}
	});
	/*******/
})(jQuery);

