/*
 * 	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
 *
 */
 
/*
 *	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>
 *
 */


$(document).ready(function(){
    
    $('#slider ul li').hide();
	var count_list = $('#slider ul').children().size();
    count_list = count_list-1;
    
    var slider_content = new Array();
	var child_index = 0;
	
	var i=0;
	
	for (i=0; i<=count_list; i++) {
		child_index = child_index + 1;
		slider_content[i] = $("#slider ul li:nth-child(" + child_index + ")").wrapInner("<li></li>").html();
	}
	
	shuffle(slider_content);
	
	var child_index = 0;
	var i=0;

	for (i=0; i<=count_list; i++) {
		child_index = child_index + 1;
		$("#slider ul li:nth-child(" + child_index + ")").replaceWith(slider_content[i]);
	}
	
	$("#home-feature #slider").css({'min-height' : '240px'});
    $("#home-feature #slider").css({'height' : 'auto'});
	$("#slider").easySlider();
	$("#slider").css('display','block');
	
});


shuffle = function(o){ //v1.0;
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
};




(function($) {
	   
	$.fn.easySlider = function(options){
	  
		// default configuration properties
		var defaults = {
			orientation:	'', //  'vertical' is optional;
			speed: 			800,
			auto:			true,
			pause:			8000		
		}; 
		
		var options = $.extend(defaults, options);  
		
		return this.each(function() {  
			obj = $(this); 				
			var s = $("li", obj).length;
			var w = obj.width(); 
			var h = obj.height(); 
			var ts = s-1;
			var t = 0;
			var vertical = (options.orientation == 'vertical');
			$("ul", obj).css('width',s*w);			
			if(!vertical) $("li", obj).css('float','left');
			$("span#arrow-left").css({'display' : 'block'});
			$("span#arrow-right").css({'display' : 'block'});
			$("span#arrow-left").hide();
			$("span#arrow-right").hide();
			$("span#arrow-right").click(function(){		
				animate("next",true);
				if (t>=ts) $(this).fadeOut();
				$("span#arrow-left").fadeIn();
			});
			$("span#arrow-left").click(function(){		
				animate("prev",true);
				if (t<=0) $(this).fadeOut();
				$("span#arrow-right").fadeIn();
			});	
			function animate(dir,clicked){
				var ot = t;
			    var diff = Math.abs(ot-t);
				var speed = diff*options.speed;
				if(dir == "next"){
					t = (t>=ts) ? ts : t+1;	
				} else {
					t = (t<=0) ? 0 : t-1;
				};								
				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(clicked) clearTimeout(timeout);
				if(options.auto && dir=="next" && !clicked){;
					timeout = setTimeout(function(){
						animate("next",false);
					},diff*options.speed+options.pause);
					if (s>2) $("span#arrow-left").fadeIn();
					if (t>=ts) $("span#arrow-right").fadeOut();
				};
			};
			if(s>1) $("span#arrow-right").fadeIn();
			var timeout;
			if(options.auto){;
				timeout = setTimeout(function(){
					animate("next",false);
				},options.pause);
			};	
		});
	  
	};

})(jQuery);

