/*
 * 	Easy Slider 1.5 - jQuery plugin
 *	written by Alen Grakalic	
 *	http://cssglobe.com/post/4004/easy-slider-15-the-easiest-jquery-plugin-for-sliding
 *
 *	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
 *
 */
(function($) {
	$.fn.easySlider = function(options){
		
		// default configuration properties
		var defaults = {
			prevId: 		'prevBtn',
			prevText: 		'Previous',
			nextId: 		'nextBtn',
			nextText: 		'Next',
			controlsShow:	false,
			controlsBefore:	'',
			controlsAfter:	'',
			controlsFade:	false,
			firstId: 		'firstBtn',
			firstText: 		'First',
			firstShow:		false,
			lastId: 		'lastBtn',
			lastText: 		'Last',
			lastShow:		false,
			vertical:		false,
			speed: 			800,
			auto:			true,
			pause:			10000,
			continuous:		true
		}; 
		
		var options = $.extend(defaults, options);  
		
		$("#loader-bar").stop().css("width", "0%");
		$("#loader-bar").stop().animate({width: "100%"}, options.pause, "linear" );
				
		var obj = $(this);
		var s = $("li .image", obj).length;
		var w = 900;
		var h = 300;
		var timeout;
		obj.width(w);
		obj.height(h);
		obj.css("overflow","hidden");
		var ts = s-1;
		var t = 0;
			
		$("ul", obj).css('width',s*w);
		if(!options.vertical) $("li", obj).css('float','left');
		
		if(options.controlsShow){
			var html = options.controlsBefore;
			if(options.firstShow) html += '<span id="'+ options.firstId +'"><a href=\"javascript:void(0);\">'+ options.firstText +'</a></span>';
			html += ' <span id="'+ options.prevId +'"><a href=\"javascript:void(0);\">'+ options.prevText +'</a></span>';
			html += ' <span id="'+ options.nextId +'"><a href=\"javascript:void(0);\">'+ options.nextText +'</a></span>';
			if(options.lastShow) html += ' <span id="'+ options.lastId +'"><a href=\"javascript:void(0);\">'+ options.lastText +'</a></span>';
			html += options.controlsAfter;
			$(obj).after(html);
		};
		
		$("a","#"+options.nextId).click(function(){
			animate("next",true);
		});
		$("a","#"+options.prevId).click(function(){
			animate("prev",true);				
		});
		$("a","#"+options.firstId).click(function(){
			animate("first",true);
		});
		$("a","#"+options.lastId).click(function(){
			animate("last",true);
		});
		$(".button").click(function(){
			animate(this.id.substring(6),true);
			$(".button").removeClass("selected");
			$(this).addClass('selected');
			$(".menu_how div").removeClass("selected_left");
			
			if (this.id.substring(6)=='0'){
				$('#how_it_works').addClass('selected_left');
			} else {
				//alert(this.id.substring(6));
				$('#button' + (this.id.substring(6)-1)).addClass('selected_left');
			}
		});
		
		function IsNumeric(sText){
		   var ValidChars = "0123456789.";
		   var IsNumber=true;
		   var Char;
		   for (i = 0; i < sText.length && IsNumber == true; i++)
			  {
			  Char = sText.charAt(i);
			  if (ValidChars.indexOf(Char) == -1)
				 {
				 IsNumber = false;
				 }
			  }
		   return IsNumber;
		}
		
		function animate(dir,clicked){
			var ot = t;
			// check if dir is a number and not a command
			if (IsNumeric(dir)) {
				t = dir;
			} else {
				switch(dir){
					case "next":
						t = (ot>=ts) ? (options.continuous ? 0 : ts) : t+1;
						break;
					case "prev":
						t = (t<=0) ? (options.continuous ? ts : 0) : t-1;
						break;
					case "first":
						t = 0;
						break;
					case "last":
						t = ts;
						break;
					default:
						break;
				};
			}
			
			var diff = Math.abs(ot-t);
			var speed = options.speed; // constant time for animation
			//var speed = diff*options.speed; // constant speed for animation
			var pause = options.pause;
			if(!options.vertical) {
				p = (t*w*-1);
				$("ul",obj).animate({ marginLeft: p },speed );
			} else {
				p = (t*h*-1);
				$("ul",obj).animate(
					{ marginTop: p },
					speed
				);
			};
			
			if(!clicked){
				$('.button').removeClass('selected');
				$('#button' + t).addClass('selected');
				$(".menu_how div").removeClass("selected_left");
				if (t==0){
					$('#how_it_works').addClass('selected_left');
				} else {
					$('#button' + (t-1)).addClass('selected_left');
				}
			}
			
			if(clicked){
				clearTimeout(timeout);
				$('#slider').unbind('mouseover').unbind('mouseenter').unbind('mouseleave').unbind('mouseout');
				$("#loader-bar").stop();
				$("#loader").animate({opacity:0}, 1000 );
				$("#loader-bar").animate({opacity:0}, 1000 );
				//$("#loader").animate('slow');
			}
			if(options.auto && dir=="next" && !clicked){;
				timeout = setTimeout(function(){
					animate("next",false);
				},options.speed+options.pause);
				$("#loader-bar").stop().css("width", "0%");
				$("#loader-bar").stop().animate({width: "100%"}, options.speed+options.pause, "linear" );
			};
		};
		// init
		
		if (options.auto){
			var percentLoaded = 0;
			$('#slider').hover(
				function() {
					//enter - pause or reset progress bar - pause or reset auto animation
					$("#loader-bar").stop();
					percentLoaded = parseFloat($("#loader-bar").css("width"))/100;
					clearTimeout(timeout);
				},
				function(){
					//leave - resume progress bar - resume auto animation
					timeout = setTimeout(function(){
						animate("next",false);
					},(1-percentLoaded)*(options.speed+options.pause));
					$("#loader-bar").stop().animate({width: "100%"}, (1-percentLoaded)*(options.speed+options.pause), "linear" );
				}
			)
		}
		
		var timeout;
		if(options.auto){
			timeout = setTimeout(function(){
				animate("next",false);
			},options.pause);
		};		
	
		if(!options.continuous && options.controlsFade){
			$("a","#"+options.prevId).hide();
			$("a","#"+options.firstId).hide();
		};
	};

})(jQuery);

$(document).ready(function(){
	$('.button').each( function(){
	    $(this).mouseover( function(){ $(this).addClass('hover'); } ).mouseout( function(){ $(this).removeClass('hover'); } );
	});
	$('.button').each( function(){
	    $(this).mouseover( function(){
			if(this.id.substring(6)=='0'){
				$("#how_it_works").addClass('hover_left');
			} else {
				$("#button" + (this.id.substring(6)-1)).addClass('hover_left');
			}
		}).mouseout( function(){
			if(this.id.substring(6)=='0'){
				$("#how_it_works").removeClass('hover_left');
			} else {
				$("#button" + (this.id.substring(6)-1)).removeClass('hover_left');
			}
		});
	});
});
