// JavaScript Document
(function($) { 
/*
 * Feature Slideshow
 * Copyright (c) 2010 Ron Heywood.
 * Licensed under the MIT license.
 *
 * @version 1.0
 */
 	$.fn.slideshow = function (options)
	{
		var defaults = {
			prev: '#prev',
			next: '#next',
			slideWidth: 630,
			onMove: function(){},
			onInit: function(){},
			navBox : '#featureNav',
			timeScroll : 0
		};
		
		options = $.extend(defaults, options); 
		var prev = options.prev;
		var next = options.next;
		var container = this;
		var navBox = options.navBox;
		var timeScroll = options.timeScroll;
		var theTimer = null;
		
		$('img',$(prev)).fadeTo('fast', 0.5);
		
		$(container).attr('currentSlideshowIndex',0);
		$(navBox).find('.position1').addClass('active');

		var count = ($(container).children().length) -1;

		$(next).bind('click',
			function(){
				clearTimeout(theTimer); 
				theTimer = setTimeout(timer,timeScroll);
				currentIndex = parseInt($(container).attr('currentSlideshowIndex'));
				if(currentIndex < (count) ){
					container.animate({left: '-='+options.slideWidth});
					currentIndex++;
					$(container).attr('currentSlideshowIndex',currentIndex);
					$(navBox).find('.pager').removeClass('active');
					$(navBox).find('.position'+(currentIndex+1)).addClass('active');
				}
				
				if(currentIndex==count)
				{
					$('img',$(next)).fadeTo('fast', 0.5);
				}
				
				$('img',$(prev)).fadeTo('fast', 1);
				
				return false;
			}
		);
		
		$(prev).bind('click',function(){
			clearTimeout(theTimer); 
			theTimer = setTimeout(timer,timeScroll);
			currentIndex = $(container).attr('currentSlideshowIndex');
			
			if(currentIndex > 0){
				container.animate({left: '+='+options.slideWidth});
				currentIndex--;
				$(container).attr('currentSlideshowIndex',currentIndex);
				$(navBox).find('.pager').removeClass('active');
				$(navBox).find('.position'+(currentIndex+1)).addClass('active');			
			}
			if(currentIndex==0)
			{
				$('img',$(prev)).fadeTo('fast', 0.5)
			}
			if(currentIndex==count)
			{
				$('img',$(next)).fadeTo('fast', 0.5)
			}
			else
			{
				$('img',$(next)).fadeTo('fast', 1)
			}

			return false;
		})
		
		var timer = function() {
			
			currentIndex = parseInt($(container).attr('currentSlideshowIndex'));
			if(currentIndex < (count) ){
				$(next).trigger('click');
			}
			else {
				$('.position1 A',$(container)).trigger('click');
			}
			if(timeScroll!=0){
				clearTimeout(theTimer); 
				theTimer = setTimeout(timer,timeScroll);
			}
		}
		
		var buildNav = function() {
			/*count = $('.slide').length;
			$navString = '';
			$(navBox).html = '';
			for($i=0;$i<count;$i++) {
				$navString += ('<li><a href="#" rel="'+$i+'" class="featureScrollLink"><img src="images/featureBulletOff.png" alt="<" /></a></li>'+"\n");
			}
			$(navBox).html($navString);
			$(navBox).find('A:first-child').html('<img src="images/featureBulletActive.png" alt="<" />');*/
			bindActions();
			if(timeScroll!=0){
				clearTimeout(theTimer); 
				theTimer = setTimeout(timer,timeScroll);
			}
		}
		
		var bindActions = function() {
			$('.featureScrollLink').bind('click',function(){
				clearTimeout(theTimer); 
				theTimer= setTimeout(timer,timeScroll);
				currentIndex = parseInt($(this).attr('rel'));
				$(navBox).find('.pager').removeClass('active');
				$(navBox).find('.position'+(currentIndex+1)).addClass('active');

				$(container).attr('currentSlideshowIndex',currentIndex);
				$offset = options.slideWidth * currentIndex;
				container.animate({left: '-'+$offset+'px'});
				//$('.featureScrollLink').html('<img src="images/featureBulletOff.png" alt="<" />');				
				//$(this).html('<img src="images/featureBulletActive.png" alt="<" />');
		  });
		}
		
		buildNav();

				
		return this.each(function() {
			
			
			
		});
		

		
	}
	
	$.fn.carousel = function (options)
	{
		var defaults = {
			prev: '#prev',
			next: '#next',
			slideHeight: 207,
			onMove: function(){},
			onInit: function(){} 
		};
		
		options = $.extend(defaults, options); 
		var prev = options.prev;
		var next = options.next;
		var container = this;
		
		$('img',$(prev)).fadeTo('fast', 0.5);
		
		$(container).attr('currentSlideshowIndex',0);
		
		var count = ($(container).children().length) -1;
		//console.debug(count);
		$(next).bind('click',
			function(){
				
				currentIndex = $(container).attr('currentSlideshowIndex');
				if(currentIndex < count ){
					container.animate({top: '-='+options.slideHeight});
					currentIndex++;
					$(container).attr('currentSlideshowIndex',currentIndex);
				}
				
				if(currentIndex==count)
				{
					$('img',$(next)).fadeTo('fast', 0.5);
				}
				
				$('img',$(prev)).fadeTo('fast', 1);
				
	
					
					return false;
				}
		);
		
		$(prev).bind('click',function(){
			currentIndex = $(container).attr('currentSlideshowIndex');
			if(currentIndex > 0){
				container.animate({top: '+='+options.slideHeight});
				currentIndex--;
				$(container).attr('currentSlideshowIndex',currentIndex);
			}
			if(currentIndex==0)
			{
				$('img',$(prev)).fadeTo('fast', 0.5)
			}
			if(currentIndex==count)
			{
				$('img',$(next)).fadeTo('fast', 0.5)
			}
			else
			{
				$('img',$(next)).fadeTo('fast', 1)
			}
			return false;
		})
		
		return this.each(function() {
			
			
			
		});
		
	}
})(jQuery);
