/*
 * 	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
 *
 */
 
/*
 *	markup example for $("#slider").easySlider();
 *	
 * 	<div id="slider">
 *		<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 _anchor() {
		if (Object.isString(arguments[0])) {
			var s = arguments[0];
			var href = window.location.href;
			var _ = href.indexOf("#");
			if (_ > -1) {
				href = href.substr((_ + 1));
				var vars = href.split("&");
				var returnV = '';
				$A(vars).each(function(value, index) {
					var val = value.split("=");										 
					if (s == val[0]) returnV = val[1];	
				});
				return returnV;
				
			} else return '';		
		}
	}
	
	function trace(id) {
			return;
			var href = window.location.href;
			var _ = href.indexOf("#");
			if (_ > -1) {
				var base = href.substr(0, _);
				href = href.substr((_ + 1));	
				var vars = href.split("&");
				$A(vars).each(function(value, index) {
					var val = value.split("=");										 
					if ('dipinto' == val[0]) window.location.href = location.href.replace(value, val[0] + '=' + (id + 1));	
				});
			} else {
				window.location.href += '#dipinto=' + (id+1);
			}
			
			return false;
	}

(function(jQuery) {

	if (jQuery.browser.msie) { 
			document.execCommand("BackgroundImageCache", false, true); 
	}

	jQuery.fn.easySlider = function(options){
			  
		// default configuration properties
		var defaults = {			
			prevId: 		'prevBtn',
			prevText: 		'Previous',
			prevShow: true,
			nextId: 		'nextBtn',	
			nextText: 		'Next',
			nextShow: true,
			controlsShow:	true,
			controlsBefore:	'',
			controlsAfter:	'',	
			controlsFade:	true,
			firstId: 		'firstBtn',
			firstText: 		'First',
			firstShow:		false,
			lastId: 		'lastBtn',	
			lastText: 		'Last',
			lastShow:		false,				
			vertical:		false,
			speed: 			800,
			auto:			true,
			pause:			5000,
			continuous:		true
		}; 
		
		var options = jQuery.extend(defaults, options);  
		
		var ancor //= Math.ceil(($$("#slider li").size() - 1) * Math.random()); //= _anchor('dipinto');
		//alert(ancor);
		switch (ancor) {
			case '' : break;
			default: {		
				var t = ancor - 1;
				var obj = jQuery(this); 				
				var s = jQuery("li", obj).length;
				var w = jQuery("li", obj).width() || 150; 
				var h = jQuery("li", obj).height() || 120; 
				var diff = t;
				var speed = diff*options.speed;						
				p = (t*w*-1);
				}
		}
		
		var tt = t || 0;
		//Preload
		var basePath = 'images/portf/';
		var img = [];
		$A($$('li.image')).each(function(element, index) {
			img[index] = new Image();
			var path = element.className.replace('image ', '').replace('image', '');
			
			if (tt > 0 && path == 'dipinti-' + tt) {
				img[index].onload = function(e) {
					$$('li.image')[img.indexOf(this)].style.backgroundImage = 'url(' + this.src +')';
					jQuery("ul", obj).animate(
						{ marginLeft: p }, 
						0
					);		
				}
			} else {
				img[index].onload = function(e) {
					$$('li.image')[img.indexOf(this)].style.backgroundImage = 'url(' + this.src +')';
				}
			}
					
		})
		
		$A(img).each(function(element, index) {
			li = $$('li.image')[index];
			var path = li.className.replace('image ', '').replace('image', '');
			element.src = basePath + path + '.jpg';
			
		})
			
		this.each(function() {  
			var obj = jQuery(this); 				
			var s = jQuery("li", obj).length;
			var w = jQuery("li", obj).width() || 150; 
			var h = jQuery("li", obj).height() || 120; 
			obj.width(w); 
			obj.height(h); 
			obj.css("overflow","hidden");
			var ts = s-1;
			var t = tt;
			jQuery("ul", obj).css('width',s*w);			
			if(!options.vertical) jQuery("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>';
				if(options.prevShow) html += ' <span id="'+ options.prevId +'"><a href=\"javascript:void(0);\">'+ options.prevText +'</a></span>';
				if(options.nextShow) 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;						
				jQuery(obj).after(html);										
			};
	
			jQuery("a","#select1").click(function(){		
				animate("select1",true);
			});
			jQuery("a","#select2").click(function(){		
				animate("select2",true);
			});
			jQuery("a","#select3").click(function(){		
				animate("select3",true);
			});
			jQuery("a","#select4").click(function(){		
				animate("select4",true);
			});
			jQuery("a","#select5").click(function(){		
				animate("select5",true);
			});
			jQuery("a","#select6").click(function(){		
				animate("select6",true);
			});
			jQuery("#"+options.nextId).click(function(){		
				animate("next",true);
			});
			jQuery("#"+options.prevId).click(function(){		
				animate("prev",true);				
			});	
			jQuery("a","#"+options.firstId).click(function(){		
				animate("first",true);
			});				
			jQuery("a","#"+options.lastId).click(function(){		
				animate("last",true);				
			});		
			
			function animate(dir,clicked){
				var ot = t;				
				switch(dir){
					case "select1":
						t = 0;
						break;
					case "select2":
						t = 1;
						break;
					case "select3":
						t = 2;
						break;
					case "select4":
						t = 3;
						break;
					case "select5":
						t = 4;
						break;
					case "select6":
						t = 5;
						break;	
					case "next":
						t = (ot>=ts) ? (options.continuous ? 0 : ts) : t+1;			
						trace(t);
						break; 
					case "prev":
						t = (t<=0) ? (options.continuous ? ts : 0) : t-1;
						trace(t);
						break; 
					case "first":
						t = 0;
						break; 
					case "last":
						t = ts;
						break; 
					default:
						break; 
				};	
				
				var diff = Math.abs(ot-t);
				var speed = diff*options.speed;						
				if(!options.vertical) {
					p = (t*w*-1);
					jQuery("ul",obj).animate(
						{ marginLeft: p }, 
						speed
					);				
				} else {
					p = (t*h*-1);
					jQuery("ul",obj).animate(
						{ marginTop: p }, 
						speed
					);					
				};
				
				/*if(!options.continuous && options.controlsFade){					
					if(t==ts){
						$(options.nextId).style.visibility = 'hidden';
						//_$("#"+options.lastId).style.visibility = 'hidden';
					} else {
						$(options.nextId).style.visibility = 'visible';
						//_$("#"+options.lastId).style.visibility = '';					
					};
					if(t==0){
						$(options.prevId).style.visibility = 'hidden';
					//	_$("#"+options.firstId).style.visibility = 'hidden';
					} else {
						$(options.prevId).style.visibility = 'visible';
						//_$("#"+options.firstId).style.visibility = '';
					};					
				};			*/	
				
				if(clicked) clearTimeout(timeout);
				if(options.auto && dir=="next" && !clicked){;
					timeout = setTimeout(function(){
						animate("next",false);
					},diff*options.speed+options.pause);
				};
				
			};
			
			// init
			var timeout;
			if(options.auto){;
				timeout = setTimeout(function(){
					animate("next",false);
				},options.pause);
			};		
		
			/*if(!options.continuous && options.controlsFade){
				if (t > 0) 
					$(options.prevId).style.visibility = 'visible';
				if(t<ts)
					$(options.nextId).style.visibility = 'visible';
				//_$(options.firstId).style.visibility = 'hidden';				
			};*/				
			
		});
	  
	};

})(jQuery);





