/*!
	jSlide v1.0 by (c) Jacob Hallsten	
*/
(function( $ ){
	var methods =
	{
		init: function(settings)
		{
			var carousel = $(this);
			
			carousel.wrap('<div class="jslide" />');
			
			var wrapper = carousel.parent('div.jslide');
			var list = carousel.children('li');
			var listCount = list.size();
			var	carouselId = carousel.attr('id');
			
			var width = (isNaN(parseInt(settings.width)) ? 500 : parseInt(settings.width));
			var height = (isNaN(parseInt(settings.height)) ? 400 : parseInt(settings.height));
			var previousButton = (!($(settings.previousButton)) ? null : $(settings.previousButton));
			var nextButton = (!($(settings.nextButton)) ? null : $(settings.nextButton));
			var align = (!(settings.align) ? 'center' : settings.align);
			var interval = (isNaN(parseInt(settings.interval)) ? null : parseInt(settings.interval));
			var duration = (isNaN(parseInt(settings.duration)) ? 600 : parseInt(settings.duration));
			var callback = (!(jQuery.isFunction(settings.callback)) ? function(){ return true } : settings.callback);
			
			// Save data about the carousel
			methods.carouselData[carouselId] = [0, listCount];
			methods.savedSettings.duration = duration;
			methods.savedSettings.callback = callback
			
			// Set style to the carousel
			wrapper.css({
				'width': width + 'px', 
				'height': height + 'px', 
				'position': 'relative', 
				'overflow': 'hidden' 
			});
			list.css({
				'float': 'left',
				'position': 'relative',
				'width': width,
				'height': height,
				'overflow': 'hidden',
				'text-align': align,
				'margin': '0',
				'padding': '0',
				'list-style-type': 'none'
			});
			carousel.css({
				'position': 'absolute', 
				'width': (width * listCount) + 'px', 
				'top': '0px', 
				'left': '0px'
			});
			
			// Set previous button event
			if(typeof previousButton === 'object') {
				previousButton.click(function() {
					$('#' + carouselId).jslide('previous');
				});
			}
			
			// Set next button event
			if(typeof nextButton === 'object') {
				nextButton.click(function() {
					$('#' + carouselId).jslide('next');
				});
			}
			
			// Set next button event
			if(interval !== null) {
				methods.interval(carousel, interval);
			}
			
			if(callback !== null) {
				callback(methods.carouselData[carouselId]);
			}
		},
		rotate: function(carouselId, nextPicture) {
			var carousel = $('#' + carouselId);
			var wrapper = carousel.parent('div.jslide');
			var width = carousel.children('li').width();
			var carouselData = methods.carouselData[carouselId];
			var left = width * nextPicture;
			var duration = methods.savedSettings.duration;
			var callback = methods.savedSettings.callback;
			carouselData[0] = nextPicture;
			
			carousel.animate({'left': '-' + left + 'px'}, duration, function() {
				callback(carouselData);
			});
			
			return nextPicture;
		},
		next: function(element)
		{
			if(typeof element !== 'object') element = $(this);
			var carouselId = $(element).attr('id');
			var carouselData = methods.carouselData[carouselId];
			var activePicture = carouselData[0];
			var counted = carouselData[1];
			
			if(activePicture == counted-1) nextPicture = 0;
			else nextPicture = activePicture+1;
			
			methods.rotate(carouselId, nextPicture);
			return nextPicture;
		},
		previous: function(element)
		{
			if(typeof element !== 'object') element = $(this);
			var carouselId = $(element).attr('id');
			var carouselData = methods.carouselData[carouselId];
			var activePicture = carouselData[0];
			var counted = carouselData[1];
			
			if(activePicture == 0) nextPicture = counted-1;
			else nextPicture = activePicture-1;
			
			methods.rotate(carouselId, nextPicture);
			return nextPicture;
		},
		goto: function(elm, nextPicture) {
			if(nextPicture == null)
				nextPicture = 0;
			var carouselId = $(this).attr('id');
			return methods.rotate(carouselId, nextPicture);
		},
		interval: function(elm, time) {
			var self = typeof elm === 'object' ? elm : this;
			var f = setInterval(function(element) {
				methods.next(self);
			}, time*1000);
		},
		raise: function() {
			carouselData = methods.carouselData.jslide;
			carouselData[1] = carouselData[1]+1;
		},
		carouselData: {},
		savedSettings: {}
	};
	
	$.fn.jslide = function(method) {
		if( methods[method] ) {
			return methods[method].apply(this, arguments);
		}
		else if(typeof method === 'object' || ! method) {
			return methods.init.apply(this, arguments);
		}
		else {
			$.error( 'Method ' +  method + ' does not exist on jQuery.jslide' );
		}
	};
})( jQuery );

function callbackfunc(data)
{
	var current = parseInt(data[0]);
	var total = parseInt(data[1]);
	var text = $($('#jslide > li > img')[data[0]]).attr('alt');
	
	if(text != '')
		text = '(' + text + ')';
		
	$('#slide-nav > ul > li.center').text(text);
	
	var nextBtn = $('#nextBtn');
	var nextBtnDisabled = $('#nextBtnDisabled');
	var prevBtn = $('#prevBtn');
	var prevBtnDisabled = $('#prevBtnDisabled');
	
	if(current <= 0) {
		prevBtn.attr('class', 'disable');
		prevBtnDisabled.attr('class', 'enable');
	}
	else {
		prevBtn.attr('class', 'enable');
		prevBtnDisabled.attr('class', 'disable');
	}
	if(current >= total-1) {
		nextBtn.attr('class', 'disable');
		nextBtnDisabled.attr('class', 'enable');
	}
	else {
		nextBtn.attr('class', 'enable');
		nextBtnDisabled.attr('class', 'disable');
	}
}
