var letour = window.letour || {};

letour.Classes = {
	
	init: function()
	{
		var Y = letour.Classes;
		Y.slideshow();
		Y.clearOnClick();
	},
	
	slideshow: function()
	{
		$('.slideshow').each(function()
		{
			var elements = $(this).children();
			
			var i = 0;
			elements.each(function()
			{
				$(this).click(function()
				{
					var id = $(this).attr('id');
					var length = elements.length;
					
					if(length > 1) 
					{
						for(var i = 0; i < length; i++)
						{
							var el = elements[i];
							
							$(el).animate({opacity: 0}, 1, function(){$(this).css("z-index", 0)});
						}
						
						for(i = 0; i < length; i++)
						{
							var el = elements[i];
							
							if($(el).attr('id') == id) 
							{
								if(i + 1 < length)
								{
									$(elements[i+1]).animate({opacity: 100}, 1, function(){$(this).css("z-index", 99)});
								}
								else
								{
									$(elements[0]).animate({opacity: 100}, 1, function(){$(this).css("z-index", 99)});
								}
							}
						}
					}
					
					return false;
				});
			});
		});
	},
	
	/* Clears text fields of default values when clicked */
	clearOnClick: function()
	{
		var saved = new Array();
		
		$('.clear-click').each(function(i)
		{
			//add a unique class to each textfield which needs to be cleared
			$(this).addClass('saved-' + i);
			//save the current (default) value of the field under the index
			//we assigned as part of the class
			saved[i] = $(this).attr('value');
			
			$(this).click(function()
			{
				//non-existent fallback index
				var num = -1;
				
				//get all classes assigned to object (space-separated string)				
				var classes = $(this).attr('class');
					//explode the classes into an array
					classes = classes.split(' ');
				
				//loop through the classes
				$(classes).each(function(j)
				{
					//explode the class name by dash
					var class_parts = classes[j].split('-');
					
					//the unique class we assigned above will match this condition
					if(class_parts.length == 2 && class_parts[0] == 'saved')
						//save the number assigned to this textfield
						num = class_parts[1];
				});
				
				//if the textfield's current value is equal to original (default), clear it
				if($(this).attr('value') == saved[num])
					$(this).attr('value', '');
			});			
		});
	}
}

if(Drupal.jsEnabled)
{
	$(document).ready(letour.Classes.init);
}
