// JavaScript Document

$.fn.slideScroll = function(options) {
	
	var defaults = {
		navClass: 'slideNav'
		,navVAlign: 'middle'
		,navPadding: 5
		,currentSlide: 1
	};
	
	var o = $.extend(defaults, options);
	var currentSlide = o.currentSlide;
	var windowID = $(this).attr('id');
	
	var slideCount = $('>ul>li' ,this).length;
	var x = 0;
	
	/* adjust width of the list and the items to fit the dimensions of the wrapper (set in css) */
	$('ul', this).width($(this).width() * slideCount);
	$('ul>li', this).width($(this).width());
	
	var slideWindow = $(this);
	var slideWrap = $('ul', this);
	
	/* create simple nav buttons */
	var newNav = $('<a />')
		.attr("id", windowID + '_next')
		.attr("title", "next slide")
		.addClass(o.navClass + '_next')
		.css({top: o.navPadding, right: o.navPadding})
		.appendTo(this);
	var navNext = $('#' + windowID + '_next');
	
	var newNav = $('<a />')
		.attr("id", windowID + '_prev')
		.attr("title", "previous slide")
		.addClass(o.navClass + '_prev')
		.css({top: o.navPadding, left: o.navPadding})
		.appendTo(this);
	var navPrev = $('#' + windowID + '_prev');
		
	
	/* if the vertical align for the nav is set, then adjust accordingly */
	if (o.navVAlign == 'middle') {
		navPrev.css({
			top: (slideWindow.height() - navPrev.height()) / 2
		});
		navNext.css({
			top: (slideWindow.height() - navNext.height()) / 2
		});
	} else if (o.navVAlign == 'bottom') {
		navPrev.css({
			top: slideWindow.height() - navPrev.height() - o.navPadding
		});
		navNext.css({
			top: slideWindow.height() - navNext.height() - o.navPadding
		});
	}
	
	$('#' + windowID + '_next, #' + windowID + '_prev, .' + o.navClass).hover(function() {
		$(this).css({"background-position":"center"});
	},function() {
		$(this).css({"background-position":"top"});
	});
	
	// make the buttons disappear if the current slide is the first or the last
	if (currentSlide == 1) navPrev.css({display: "none"});
	else if (currentSlide == slideCount) navNext.css({display: "none"});
	
	$('#' + windowID + '_next, #' + windowID + '_prev, .' + o.navClass).click(function () {
		
		$(this).css({"background-position":"bottom"});
		parts = $(this).attr('id').split("_");
		show = parts[0];
		id = parts[1];
		
		if (id.match(/^[0-9]+$/) != null) currentSlide = id;
		else if (id == 'prev' && currentSlide > 1) currentSlide--;
		else if (id == 'next' && currentSlide != slideCount) currentSlide++;
		else if (id == 'first' && currentSlide > 1) currentSlide = 1;
		else if (id == 'last' && currentSlide != slideCount) currentSlide = slideCount;
		
		
		// make the buttons disappear if the current slide is the first
		if (navPrev.length && currentSlide == 1) $('#' + show + '_prev').css({display: "none"}); //"opacity":0.50
		else $('#' + show + '_prev').css({display: "block"}); //"opacity":1
		
		// make the buttons disappear if the current slide is the last
		if (navNext.length && currentSlide == slideCount) $('#' + show + '_next').css({display: "none"}); //"opacity":0.50
		else $('#' + show + '_next').css({display: "block"}); //"opacity":1
		
		x = $('#' + show).width() * - (currentSlide-1) + "px";
		$('#' + show + ' .slideWrap').stop().animate({left: x}, {duration:350});
	});
	
	// Start of by moving the slide along to the requested first slide
	x = slideWindow.width() * - (currentSlide-1) + "px";
	slideWrap.stop().animate({left: x}, {duration:350});
	
	//console.log($('ul>li', this).css('clear'));
};
