(function ($) {

	function createCycle(e, startingSlide) {
	
			var id = $(e).attr('id');
	
		$(e).cycle({ 
			    fx:     'scrollHorz', 
			    speed:  'fast', 
			    timeout: 0,
			    //next:   '#'+id+'-nav .next, #'+id, 
			    next:   '#'+id+'-nav .next', 
			    prev:   '#'+id+'-nav .prev',
			    nowrap: 0,
			    startingSlide: startingSlide,
			    fit: 0,
			    height: 'auto',
			    after: function (cur, next, opt, fwd) {
					/*
					if (opt.currSlide > 0) {
						$('#'+id+'-nav .prev').css({visibility: 'visible'});
					} else {
						$('#'+id+'-nav .prev').css({visibility: 'hidden'});
					}
		
					if (opt.currSlide == opt.elements.length-1) {
						$('#'+id+'-nav .next').css({visibility: 'hidden'});
					} else {
						$('#'+id+'-nav .next').css({visibility: 'visible'});
					}
					
					*/
					//$('#'+id+'-nav').show();
						    	
			    }
			});
	
	}

	$.fn.sjgallery = function () {
		
		function buildGallery(images, startingSlide) {
				
				var gc = $('<div>', { id: 'galleryContent', "class": 'galleryContent' } );
				var gi = $('<div>', { id: 'galleryImages', "class": 'galleryImages' } );
				
				gc.append(gi);

				for (var i=0;i<images.length;i++) {
					
					gi.append( $('<div>', {'class': 'singleimage'}).append( $('<img>', {src: images[i], alt: 'Gallery Image'}) ) );
				
				}
				
				var next_prev = '';
				if (images.length > 1) {
					next_prev = '<a class="prev" href="#"><span class="hidden">Previous</span></a> <a class="next" href="#"><span class="hidden">Next</span></a>';
					
				}
				
				gc.append('<div id="galleryImages-nav" class="nav">'+next_prev+'<a id="close_gallery" href="#">close</a></div>');

				$("body").append(gc);
				
				//Init gallery
				createCycle(gi, startingSlide);
		
		}
		
		function removeGallery(element, options) {
			$('#galleryContent').remove();
		}
		
		function showGallery() {
			
			 $.blockUI.defaults.css = {};
	         $.blockUI({
	         		message: $('#galleryContent'),
	         		overlayCSS: { backgroundColor: '#FFF', opacity: 0.9, cursor: 'default' }
	         });
	         $('.blockOverlay, a#close_gallery').attr('title','Click to close').click(function () {
	         
	         						$.unblockUI({
	         							onUnblock: function (element, options) {
	         								removeGallery();
	         							}
	         						})
         						}
         						);
		
		}

		//Creo il blocco con dentro la gallery		
		return $(this).each(function () {
				
				function IsNumeric(input)
				{
				   return (input - 0) == input && input.length > 0;
				}
				
				$(this).click(function (e) {
					
					var startingSlide = $(this).attr('startingSlide');
					
					var href = $(this).attr('href').replace(/\#/i, "");
					if (IsNumeric(href)) {
						startingSlide = href;
					}
					
					if (startingSlide == null) startingSlide = 0;
					
					var images = $(this).attr('rel').split('|');
					
					//Mostro il block menu
					buildGallery(images, startingSlide);
					showGallery();
					
					return false;
				
				});
				
				
			
		});
		
	}

	$(function () {
		$('.sjgallery').sjgallery();

		$('.wp-featured').cycle({ 
			    fx:     'fade', 
			    speed: 10000,
			    delay: 2000,
			    //timeout: 100,
			    continuous: true,
			    //easing: 'linear',
			    nowrap: 0,
			    fit: 1,
			    height: 'auto'   	
			});
			
		$('.wp-featured').cycle('resume');
		
		$('.sjgallery_inline .galleryImages').each(function () {
			createCycle(this);
		});
		
		
	});

}(jQuery));
