(function( $ ){
	$.fn.heroGallery = function( options ) {
		//generates a random number
		var hgID = Math.floor(Math.random()*999);
		this.attr('herogalleryID', hgID);
	
	
		//TODO: Implement the different options in the process
		var settings = {
			autoSlide			: true,
			height				: 300,
			width				: 800,
			itemHeight			: 300,
			itemWidth			: 800,
			useThumbnails		: false,
			thumbnailHeight		: 30,
			thumbnailWidth		: 30
		}
		
		setTimeout(nextPhoto,5000);
		//nextPhoto();
		
		if (options){
			$.extend(settings, options);
		}
		
		// Appends the UL itemList if it does not exist
		if ( !this.parent().children('ul.itemList').length > 0 );
			this.parent().append( '<ul class="itemList"></ul>' );
		
		this.children('li').each(function(){
			//Asigns z-index to every LI
			$(this).css('z-index', 100 - $(this).index() );
			var thumbnail = '';
			if(options.useThumbnails == true)
			{
				var thumbURL = $(this).children('img:first').attr('src');
				thumbnail = '<img src="' + thumbURL + '" height="' + options.thumbnailHeight 
								+ '" width="' + options.thumbnailWidth + '" />';
			}
			
			//Appends a new item for each LI
			$(this).parent().siblings('.itemList').append('<li class="item' + ($(this).hasClass('selected') ? ' selected' :'' ) +'">' + thumbnail + '</li>');
		});
		
		this.siblings('.itemList').children('.item').click( function()
		{
			var item = $(this).index();
			var current = $(this).parent().siblings('[heroGalleryID=' + hgID + ']').children('.selected');
			var clicked = $(this).parent().siblings('[heroGalleryID=' + hgID + ']').children('li:eq(' + item + ')');
			
			if( $(current).index() != $(clicked).index() )
			{
				switchPhoto(clicked);
				visibleImagen();
			}
			$(this).parent().children('.selected').removeClass('selected');
			$(this).addClass('selected');
			
		});
		
		function switchPhoto(target)
		{
			$('[heroGalleryID=' + hgID + '] > .selected').fadeTo("slow",0).removeClass("selected");
			$(target).addClass("selected").fadeTo("slow",1);
			$(".itemList > .item.selected").removeClass("selected");
			var query = ".itemList > .item:eq("+$(target).prevAll().length.toString()+")";
			$(query).addClass("selected");
			clearTimeout(heroSlider);
		}

		function visibleImagen()
		{
			document.getElementById('play').style.visibility='visible';
		}
	};
})( jQuery );



function nextPhoto()
{
	var target = '#hotelGallery';
	var current = $(target).children('.item.selected').prevAll().length;
	
	var next;
	if (current != ($(target).children('.item').length-1))
	{
		next = current+1;	
	}
	else
	{
		next = 0;	
	}
	var query = '.item:eq(' + next.toString() + ')';
	heroSlider = setTimeout(nextPhoto,5000);
	
	switchPhotoTemp(current, query);

}

function switchPhotoTemp(current, next)
{
	//alert(current);
	$('#hotelGallery > .item:eq('+current+')').fadeTo("slow",0).removeClass("selected");
	$(next).addClass("selected").fadeTo("slow",1);
	$(".itemList > .item.selected").removeClass("selected");
	var query = ".itemList > .item:eq("+$(next).prevAll().length.toString()+")";
	$(query).addClass("selected");
}
