/*
 * 	Easy Slider - jQuery plugin
 *	written by Alen Grakalic	
 *	http://cssglobe.com/post/3783/jquery-plugin-easy-image-or-content-slider
 *
 *	Copyright (c) 2009 Alen Grakalic (http://cssglobe.com)
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 *	@version 1.1
 *
 * Changelog:
 *	1.1	03.07.10/goshi	add callback, titleId, descrid, counterId, autoslide
 */
 
/*
 *	markup example for $("#images").easySlider();
 *	
 * 	<div id="images">
 *		<ul>
 *			<li><img src="images/01.jpg" alt="" /></li>
 *			<li><img src="images/02.jpg" alt="" /></li>
 *			<li><img src="images/03.jpg" alt="" /></li>
 *			<li><img src="images/04.jpg" alt="" /></li>
 *			<li><img src="images/05.jpg" alt="" /></li>
 *		</ul>
 *	</div>
 *
 */

(function($) {

	$.fn.easySlider = function(options){
	  
		// default configuration properties
		var defaults = {
			prevId: 		'prevBtn',
			prevText: 		'Previous',
			nextId: 		'nextBtn',	
			nextText: 		'Next',
			orientation:	'', //  'vertical' is optional;
			speed: 			800,
			// properties added by goshi
			callback:		null,
			titleId:		null,
			descrId:		null,			
			counterId:		null,
			autoslide:		false
		}; 
		
		var options = $.extend(defaults, options);  
		var interval = null;
		
		function addSlideTimer(num){
			if (!window.autoslide)
				window.autoslide = {};
			window.autoslide[num] = function(){
				$("a","#"+options.nextId).trigger('click');
			}
			clearInterval(interval);
			interval = setInterval('autoslide['+num+']()', options.autoslide);
		}
		
		return this.each(function(num) {  
			obj = $(this); 				
			var s = $("li", obj).length;
			var w = obj.width(); 
			var h = obj.height(); 
			var ts = s-1;
			var t = 0;
			
			var texts = {};
			
			// apply counter with items
			if (options.counterId && $('#'+options.counterId).length > 0){
			
				function addEvent(a){
					$('#c'+a).click(function(){
						animate(a);
						addSlideTimer(num);
						changeCounter();
						updateTexts();
					});
				}
			
				for (var a=0;a<s;a++){
					$('#'+options.counterId).append('<li id="c'+a+'"><a href="javascript:void(0);">'+(a+1)+'</a></li>');
					addEvent(a);
				}
			} 
			
			if (options.titleId && $('#'+options.titleId).length > 0 && $('p', obj).length > 0){
			
				$('p', obj).each(function(a, b){
					texts[a] = {};
					texts[a].descr = b.innerHTML;
					texts[a].title = obj.find('img')[a].getAttribute('alt');
					texts[a].href = obj.find('a')[a].getAttribute('href');					
					});
			}
			
			var vertical = (options.orientation == 'vertical');
			$("ul", obj).css('width',s*w);			
			if(!vertical) $("li", obj).css('float','left');
			$(obj).after('<span id="'+ options.prevId +'"><a href=\"javascript:void(0);\">'+ options.prevText +'</a></span> <span id="'+ options.nextId +'"><a href=\"javascript:void(0);\">'+ options.nextText +'</a></span>');		
			$("a","#"+options.prevId).hide();
			$("a","#"+options.nextId).hide();
			$("a","#"+options.nextId).click(function(){		
				animate('dir', {'dir' : "next"});
				if (t>=ts) $(this).fadeOut();
				$("a","#"+options.prevId).fadeIn();
				changeCounter();
				updateTexts();
			});
			$("a","#"+options.prevId).click(function(){		
				animate('dir', {'dir' : "prev"});
				if (t<=0) $(this).fadeOut();
				$("a","#"+options.nextId).fadeIn();
				changeCounter();
				updateTexts();
			});	
			
			function changeCounter(){
				if (options.counterId && $('#'+options.counterId).length > 0 && $('#c'+t, $('#'+options.counterId)[0]).length > 0){
					$('#'+options.counterId+' li').removeClass('active');
					$('#c'+t, $('#'+options.counterId)).addClass('active');
				}
			}
			
			function updateTexts(){
				
				if (texts[t]){
					if (options.titleId && $('#'+options.titleId).length > 0){
						$('#'+options.titleId).html(texts[t].title);
					}
					if (options.descrId && $('#'+options.descrId).length > 0){
						$('#'+options.descrId).html(texts[t].descr);
					}

				} else {
					if (options.titleId && $('#'+options.titleId).length > 0){
						$('#'+options.titleId).html('');
					}
				
					if (options.descrId && $('#'+options.descrId).length > 0){
						$('#'+options.descrId).html('');
					}
				
				}
			}
			
			function animate(mode, opts){
				if (mode == 'dir'){
					if(opts.dir && opts.dir == "next"){
						t = (t>=ts) ? 0 : t+1;	
					} else {
						t = (t<=0) ? 0 : t-1;
					};								
				} else if (parseInt(mode) >= 0 && parseInt(mode) < s){
					// in another way - we go to the mode item number
					t = mode;
				}
				
				if(!vertical) {
					p = (t*w*-1);
					$("ul",obj).animate(
						{ marginLeft: p }, 
						options.speed
					);				
				} else {
					p = (t*h*-1);
					$("ul",obj).animate(
						{ marginTop: p }, 
						options.speed
					);					
				} 
			};
			if(s>1) $("a","#"+options.nextId).fadeIn();	
			
			// turn on autosliding
			if (options.autoslide)
				addSlideTimer(num);
			
			changeCounter();
			updateTexts();
		});
	  
	};

})(jQuery);
