/**
* @author    Kyle Hengst <kyle@cyberdesignworks.com.au>
*/
(function($)
	{

		$.fn.jSlideshow = function(settings)
			{

				settings = jQuery.extend({
					delay: 5,
					rand: false,
					indicators: '',
					auto:false,
					onShow:null
				}, settings);

				this.each(function()
				{
					var scope = $(this);
					var interval = null;
					var index = 0;
					var logos = scope.children();
					var total = logos.length;
					var debug = '';
					var indicators = null;
					var hash = location.hash;
//					hash = eval(hash.replace('#',''));
					
					// indicators
					if(settings.indicators!='')
					{
						indicators = $(settings.indicators);
						$('li',indicators).click(function(){

							var p = $(this).parent();
							var i = $('li',p).index(this);
							stop();
							go(i);
							if(settings.auto) loop();
//							return false;
							
						});
					}
					
					// capture indices
					var indices = new Array();
					for(i=0;i<total;i++)
						indices.push(i);
					
					// sort slides randomly
					if(settings.rand)
						indices.sort(function(){ return (Math.round(Math.random())-0.5);});


					// set all logos to transparent
					$("li",scope).hide();

					// loop
					function loop()
					{
						interval = setInterval(
							function(){go()},
							settings.delay*1000
						);
					}
					
					function stop()
					{
						clearInterval(interval);
					}

					// interval function
					function go(n)
					{
						hide(index);
						
						if(n!=undefined)
						{
							index = n;
						}
						else
						{
							index++;
							if(index>=total)
								index = 0;
						}
							
						show(index);
					}

					function start()
					{
						if(!indicators) return;
						var a = $('li a[href="'+hash+'"]',indicators);
						var i = 0;
						if(a)
						{
							var ul = a.parent().parent();
							var li = a.parent();
							var index = $('li',ul).index(li);
							if(index>0) i = index;
						}
						show(i);
					}


					function hide(child_index)
					{
						var child = $("li",scope).eq(indices[child_index]);
						child.fadeOut('slow');
					}

					function show(child_index)
					{
						var child = $("li",scope).eq(indices[child_index]);
						$('.image',child).css({left:'-630px'}).animate({left:'0px'},1000,'easeInOutCubic');
						$('.text',child).css({right:'-280px'}).animate({right:'10px'},1000,'easeInOutCubic');
//						child.fadeIn('slow');
						child.show();
						
						// indicators
						var rel = '';
						if(indicators)
						{
							$('a',indicators).removeClass('selected');
							var indicator = $('a',indicators).eq(indices[child_index]);
							rel = indicator.attr('rel');
							indicator.addClass('selected');
						}

						// onShow
						if ($.isFunction(settings.onShow)) {
							settings.onShow(rel);
						}

					}

					if(settings.auto) loop();
					start();


				})

			} // close slidingPanels

	})(jQuery);
