/*
 * 	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>
 *
 */
 
 
var left_light = new Image();
left_light.src = "/img/arrow_left_light.gif";
var left_dark = new Image();
left_dark.src = "/img/arrow_left_dark.gif"; 

var right_light = new Image();
right_light.src = "/img/arrow_right_light.gif";
var right_dark = new Image();
right_dark.src = "/img/arrow_right_dark.gif";  


function DoImageChange(prevId, nextId, t, numPanels, ts)
{

    if(t<=0)
	{
			    $("#" + prevId + " a img").attr("src", left_light.src);
			    $("#" + nextId + " a img").attr("src", right_dark.src);
		 
               
	}
	if(t > 0)
	{  
			     var src = $("#" + prevId + " a img").attr("src");
			     if(src != "/img/arrow_left_dark.gif")
			     {
                     $("#" + prevId + " a img").attr("src", left_dark.src);
                 }
                    
	}
	
	if ((t+numPanels)>=(ts+1)) 
	{	   
	            var src = $("#" + nextId + " a img").attr("src");
			    if(src != "/img/arrow_right_light.gif")
			     {
                     $("#" + nextId + " a img").attr("src", right_light.src);
                 }
	}
	
	if((t+numPanels)<(ts+1))
	{
	                 var src = $("#" + nextId + " a img").attr("src");
			     if(src != "/img/arrow_right_dark.gif")
			     {
                      $("#" + nextId + " a img").attr("src", right_dark.src);
                 }
	}
}



(function($) {

	$.fn.easySlider = function(options){
	  
		// default configuration properties
		var defaults = {
			prevId: 		'prevBtn',
			prevText: 		'Previous',
			nextId: 		'nextBtn',	
			nextText: 		'Next',
			orientation:	'', //  'vertical' is optional;
			speed: 			'fast',
			scrollAmount:	-1,
			numberOfPanels: -1
		}; 
		
		
		
		var options = $.extend(defaults, options);  

		return this.each(function() {  

			var obj = $(this); 
						
			var s = $("li", obj).length;
			var w = obj.width();
			var scroll = (options.scrollAmount == -1) ? obj.width() : options.scrollAmount; 
			var numPanels = (options.numberOfPanels == -1) ? s : options.numberOfPanels; 
			var h = obj.height(); 
			var ts = s-1;
			var t = 0;
			var vertical = (options.orientation == 'vertical');
			$("ul", obj).css('width',s*w*10);		
			//alert(s);
			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();
			if(t<=0)
			{
			    //alert('test');
			   // $("#" + options.prevId + " a img").attr("src", "/img/arrow_left_light.gif");
			    //$("#" + options.nextId + " a img").attr("src", "/img/arrow_right_dark.gif");
			    //$("#" + options.prevId + " a img").css("opacity", "0.3");
			    //$("#" + options.prevId + " a img").css("filter", "alpha(opacity=30)");
                //$("#" + options.prevId + " a img").css("-moz-opacity", "0.3");
                //$("#" + options.prevId + " a img").css("-khtml-opacity", "0.3");
               
			}
			
			$("a","#"+options.nextId).hide();
			
			$("a","#"+options.nextId).click(function(){		
			    if(t+numPanels < ts+1)
			    {
				animate("next");
				}
				//alert(t+numPanels);
				//alert(ts);
				if(t > 0)
				{  
			         /*$("#" + options.prevId + " a img").css("opacity", "1");
			         $("#" + options.prevId + " a img").css("filter", "alpha(opacity=100)");
                     $("#" + options.prevId + " a img").css("-moz-opacity", "1");
                     $("#" + options.prevId + " a img").css("-khtml-opacity", "1");
                     */
                     $("#" + options.nextId + " a").attr("href", "javascript:DoImageChange('" + options.prevId + "', '" + options.nextId + "', " + t + ", " + numPanels + ", "  + ts + ");");
                     
                     //$("#" + options.prevId + " a img").attr("src", "/img/arrow_left_dark.gif");
                     
                    //show prev
                    
				}
				if (t+numPanels>=ts+1) 
				{
				     /*$("#" + options.nextId + " a img").css("opacity", "0.3");
			         $("#" + options.nextId + " a img").css("filter", "alpha(opacity=30)");
                     $("#" + options.nextId + " a img").css("-moz-opacity", "0.3");
                     $("#" + options.nextId + " a img").css("-khtml-opacity", "0.3");
                     */
                      $("#" + options.nextId + " a").attr("href", "javascript:DoImageChange('" + options.prevId + "', '" + options.nextId + "', " + t + ", " + numPanels + ", "  + ts + ");");
                     //$("#" + options.nextId + " a img").attr("src", "/img/arrow_right_light.gif");
                     
                    //hide next
                    
				}
				$("a","#"+options.prevId).fadeIn();
			
			});
			
			$("a","#"+options.prevId).click(function(){	
			    if(t > 0)
			    {	
				    animate("prev");
				}
				if(t+numPanels<ts+1)
				{
				     /*$("#" + options.nextId + " a img").css("opacity", "1");
			         $("#" + options.nextId + " a img").css("filter", "alpha(opacity=100)");
                     $("#" + options.nextId + " a img").css("-moz-opacity", "1.0");
                     $("#" + options.nextId + " a img").css("-khtml-opacity", "1.0");
                     */
                      
                       $("#" + options.prevId + " a").attr("href", "javascript:DoImageChange('" + options.prevId + "', '" + options.nextId + "', " + t + ", " + numPanels + ", "  + ts + ");");
                     
                      //$("#" + options.nextId + " a img").attr("src", "/img/arrow_right_dark.gif");
				}
				if (t<=0)
				{
				
				 $("#" + options.prevId + " a").attr("href", "javascript:DoImageChange('" + options.prevId + "', '" + options.nextId + "', " + t + ", " + numPanels + ", "  + ts + ");");
                     
				    /* $("#" + options.prevId + " a img").css("opacity", "0.3");
			         $("#" + options.prevId + " a img").css("filter", "alpha(opacity=30)");
                     $("#" + options.prevId + " a img").css("-moz-opacity", "0.3");
                     $("#" + options.prevId + " a img").css("-khtml-opacity", "0.3");
                     */
                    // $("#" + options.prevId + " a img").attr("src", "/img/arrow_left_light.gif");
                     /*$("#" + options.nextId + " a img").css("opacity", "1");
			         $("#" + options.nextId + " a img").css("filter", "alpha(opacity=100)");
                     $("#" + options.nextId + " a img").css("-moz-opacity", "1.0");
                     $("#" + options.nextId + " a img
                     ").css("-khtml-opacity", "1.0");
                     */
                     //$("#" + options.nextId + " a img").attr("src", "/img/arrow_right_dark.gif");
                     
                     
				}
				
				$("a","#"+options.nextId).fadeIn();
			});	
			
			function animate(dir){
				if(dir == "next"){
					t = (t>=ts) ? ts : t+1;	
				} else {
					t = (t<=0) ? 0 : t-1;
				};								


				if(!vertical) {
					p = (t*scroll*-1);
					$("ul",obj).animate({ marginLeft: p }, options.speed);				
				} else {
					p = (t*h*-1);
					$("ul",obj).animate(
						{ marginTop: p }, 
						options.speed
					);					
				}
			};
			if (s>numPanels) $("a","#"+options.nextId).fadeIn();	
		});
	  
	};

})(jQuery);
