var jetools = {
	// CREAR SIMPLE POPUP IMAGE
	simplePopupImage: function(obj, opts){
				
		opt = jQuery.extend({
			attrSrcImg: "altSrc",
			cssBgMask: "BgMask",
			cssImagePop: "ImagePop",
			fadeInTime: 500,
			cssBtnClose: "BtnClosePop",
			btnCloseText: "X CERRAR",
			btnClosePaddingX: 0
		}, opts);
		
		// Inicializamos los objetos
		$.each($(obj), initialize);
		// Funcion de inicializacion
		function initialize(index, value){
			var imageSrc =  $(value).attr(opt.attrSrcImg);
			
			var newImage = new Image();
			newImage.src = imageSrc;			
			
			newImage.onload = function(){;
				ApplyClick(index, value, newImage.width, newImage.height);
			};
		}
		
		function ApplyClick(index, value, imgWidth, imgHeight){
			$(value).click(function(me){
				var imageSrc = $(value).attr(opt.attrSrcImg);
				var position = $(value).offset();
				
				$('body').append('<div class="' + opt.cssBgMask + '"></div>');
				$('body').append('<img class="' + opt.cssImagePop + '" src="' + imageSrc + '" />');
				
				$('.'+opt.cssImagePop).css({
					'top': me.pageY - $(window).scrollTop(),
					'left': me.pageX,
					'width': 0,
					'height': 0,
					'opacity': 0
				}).animate({
					'top': ($(window).height() / 2) - (imgHeight / 2),
					'left': ($(window).width() / 2) - (imgWidth / 2),
					'width': imgWidth,
					'height': imgHeight,
					'opacity': 1
				}, opt.fadeInTime * 2, function(){
					$('body').append('<div class="' + opt.cssBtnClose + '">' + opt.btnCloseText + '</div>');
					$('.'+opt.cssBtnClose).css({
						'top': ($(window).height() / 2) - (imgHeight / 2),
						'left': ($(window).width() / 2) + (imgWidth / 2) - $('.'+opt.cssBtnClose).width() - opt.btnClosePaddingX
					});
					$('.'+opt.cssBtnClose).click(function(){
						RemoveThings();
					});
				});
				
				$('.'+opt.cssBgMask).css('opacity', 0).show().stop().animate({
					opacity: "0.8"
				}, opt.fadeInTime)
				.click(function(){
					RemoveThings();
				});
				
				
				$(window).resize(function(){
					$('.'+opt.cssImagePop).css({
						'top': ($(window).height() / 2) - (imgHeight / 2),
						'left': ($(window).width() / 2) - (imgWidth / 2)
					});
				});
				
				function RemoveThings(){
					$('.'+opt.cssBgMask).stop().animate({
						opacity: "0"
					}, opt.fadeInTime, function(){
						$('.'+opt.cssBgMask).remove();
					})
					$('.'+opt.cssImagePop).stop().animate({
						opacity: "0"
					}, opt.fadeInTime, function(){
						$('.'+opt.cssImagePop).remove();
					})
					$('.'+opt.cssBtnClose).stop().animate({
						'opacity': 0
					}, opt.fadeInTime, function(){
						$('.'+opt.cssBtnClose).remove();
					});
				}
			});
		}
	},
	
	simpleBanner: function(obj, opts){
				
		opt = jQuery.extend({
			banners: ".Banner",
			fadeTime: 500,
			thumbs: ".Thumb",
			thumbsrc: 'src',
			thumbactivesrc: 'activesrc',
			activeBanner: 0,
			intervalChangeTime: 10000
		}, opts);
		
		// Inicializamos los objetos
		$.each($(obj), initialize);
		// Funcion de inicializacion
		function initialize(index, value){
			var intervalcontent;
			$.each($(value).find(opt.thumbs), function(i, v){
				var index = i;
				var thumb = new Image();
				if (i == opt.activeBanner){
					thumb.src = $(v).attr(opt.thumbactivesrc);
				} else {
					thumb.src = $(v).attr(opt.thumbsrc);
				}
				$(v).append(thumb);
				
				$(v).click(function(){
					UpdateBanners(index);
				});
			});
			
			SetIntervalAutochange();
			
			$(value).hover(function(){
				clearInterval(intervalcontent);
			}, function(){
				SetIntervalAutochange();
			});
			
			function SetIntervalAutochange(){
				intervalcontent = setInterval(function(){
					var nextIndex = opt.activeBanner;
					nextIndex++;
					if (nextIndex > $(value).find(opt.thumbs).length - 1) nextIndex = 0;
					UpdateBanners(nextIndex);
				}, opt.intervalChangeTime);
			}
			
			function UpdateBanners(index){
				if (index != opt.activeBanner){
					opt.activeBanner = index;
					for (var a = 0; a < $(value).find(opt.banners).length; a++){
						if (a == opt.activeBanner){
							$(value).find(opt.banners).eq(a).fadeIn(opt.fadeTime);
							var thumb = $(value).find(opt.thumbs).eq(a);
							thumb.find('img').attr('src', thumb.attr(opt.thumbactivesrc));
						} else {
							$(value).find(opt.banners).eq(a).fadeOut(opt.fadeTime);
							var thumb = $(value).find(opt.thumbs).eq(a);
							thumb.find('img').attr('src', thumb.attr(opt.thumbsrc));
						}
					}
				}
			}
			
		}
		
		
		
	}
};
