(function() {
 
    jQuery.fn.slider = function() 
    {
        jQuery.slider.init();
    }
 
    $.slider = 
    {
    	num: -1,
		cur: 0,
		cr: [],
		al: null,
		at: 5*1000,
		ar: true,
		
		init:function() 
		{
			var size = $('#slide-runner a img').size();
			if(size == 0) return;
			
			var pos = Math.floor(Math.random()*1);
			var i = 0;
			
			$.slider.num = size;
			
			$('#slide-runner a img').each(function() 
			{
				$(this).css({left:((i-pos)*653)});
				$('#slide-nav').append('<a id="slide-link-'+i+'" href="#" onclick="$.slider.slide('+i+');return false;" onfocus="this.blur();">'+(i+1)+'</a>');
				$('div#slide-runner a img, div#slide-controls, div#slide-holder').show();
				
				$.slider.text(this);
				$.slider.on(pos);
				$.slider.cur = pos;
				window.setTimeout('$.slider.auto();', this.at);
				
				i++;
			});
		},
		
		auto:function()
		{
			if(!this.ar)
				return false;
				
			var next = this.cur+1;
			
			if(next >= this.num)
			{
				next = 0;
			}
			
			this.slide(next);
		},
 
        slide:function(pos)
		{
			if(pos<0 || pos>=this.num || pos==this.cur)
				return;
			
			window.clearTimeout(this.al);
			this.al = window.setTimeout('$.slider.auto();',this.at);
			
			var i = 0;
			
			$('#slide-runner a img').each(function() 
			{
				$(this).stop().animate({left:((i-pos)*653)},1000,'swing');
				i++;
			});
			
			this.on(pos);
			this.text($('#slide-runner a img')[pos]);
			this.cur = pos;
		},
		
		on:function(pos)
		{
			$('#slide-nav a').removeClass('on');
			$('#slide-nav a#slide-link-'+pos).addClass('on');
		},
		
		text:function(img)
		{
			this.cr['a'] = $(img).attr('alt');
			this.cr['b'] = $(img).attr('longdesc');
			this.ticker('#slide-client span', $(img).attr('alt'), 0, 'a');
			this.ticker('#slide-desc', $(img).attr('longdesc'), 0, 'b');
		},
		
		ticker:function(el, text, pos, unique)
		{
			if(this.cr[unique] != text)
				return false;
				
			ctext = text.substring(0,pos)+(pos%2?'-':'_');
			$(el).html(ctext);
			
			if(pos==text.length)
				$(el).html(text);
			else
				window.setTimeout('$.slider.ticker("'+el+'","'+text+'",'+(pos+1)+',"'+unique+'");',30);
		}
    }
 
})(jQuery);

