/**
* @author    Kyle Hengst <kyle@cyberdesignworks.com.au>
*/
(function($)
	{

		$.fn.jSlideshow = function(settings)
			{

				settings = jQuery.extend({
					delay: 5,
					rand: false,
					indicators: '',
					auto:false,
					onShow:null,
					autoHeight:false
				}, 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(total>1 && settings.auto) loop();
							return false;
							
						});
					}

					var height = 0;
					if(settings.autoHeight)
					{
						$("li",scope).each(function(){
							var h = $(this).height();
							if(h>height)
								height = h;
						});
						scope.height(height+30);
					}
					
					// 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){
							show(0);
							return;
						}
						var a = $('li a[href="'+hash+'"]',indicators);
						var i = -1;
						if(a)
						{
							var ul = a.parent().parent();
							var li = a.parent();
							var li_index = $('li',ul).index(li);
							if(li_index>-1){
								i = li_index;
							}
						}
						if(i<0 && settings.randStart)
							i = indices[Math.floor(Math.random()*indices.length)];
						else if(i<0)
							i = 0;
						index = i;
						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]);
						child.fadeIn('slow');
						
						// 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(total>1 && settings.auto) loop();
					start();


				})

			} // close slidingPanels

	})(jQuery);
