/*
 * 	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>
 *
 */

(function($) {

	$.fn.easySlider = function(options){
	  
		// default configuration properties
		var defaults = {
			prevId: 		'prevBtn',
			prevText: 		'<',
			nextId: 		'nextBtn',	
			nextText: 		'>',
			txtNeidId: 		'txtNeid',
			//orientation:	'vertical', //  'vertical' is optional;
			orientation:	'', //  'vertical' is optional;
			speed: 			800			
		}; 
		
		var options = $.extend(defaults, options);  
		var click = 0;
		
		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');
			var txtHidden = 0;
			var direction = '';
			var clicked = 0;
			var startStoerZeit = 6000; //standard: 6000
			var clickStoerZeit = 6000; //standard: 6000
			var textEinblendZeit = 500; // standard 2000
			
			$("ul", obj).css('width',s*w);			
			if(!vertical) $("li", obj).css('float','left');
			//$(obj).before('<div class="buttons"><div id="'+ options.prevId +'"><a href=\"javascript:void(0);\"><img align="middle" src="images/prev.gif" alt="'+ options.prevText +'"/></a></div></div>');
			//$(obj).after('<div class="buttons"><div id="'+ options.nextId +'"><a href=\"javascript:void(0);\"><img align="middle" src="images/next.gif" alt="'+ options.nextText +'"/></a></div></div>');		
			
			hiding(txtHidden);
			
			/*$("a","#"+options.prevId).hide();
			$("a","#"+options.nextId).hide();
			$("a","#"+options.nextId).click(function(){		
				animate("next");
				hiding(txtHidden);
				if (t>=ts) $(this).fadeOut();
				$("a","#"+options.prevId).fadeIn();
			});
			$("a","#"+options.prevId).click(function(){		
				animate("prev");
				hiding(txtHidden);
				if (t<=0) $(this).fadeOut();
				$("a","#"+options.nextId).fadeIn();
			});	*/

			
			function animate(dir){
				//alert('ani: '+animated);
				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
					);					
				}
			};
			
			function hiding(hide){
				if(hide==0)
				{
					$("div.txt",obj).hide();
					$("div.eig").hide();
					hide = 1;
					if(hide==1){
						//$("h2.h2").click(function(){
							/*$("div.eig").fadeIn('100');
							$("div.txt",obj).fadeIn('100');
							hide = 0;*/
							//if (t>=ts){ //letzte eigenschaft
								//animate("prev");
								//hiding(txtHidden);
							//}else{
								//animate("next");
								//hiding(txtHidden);
							//}
						//});
					}
					if(hide==1)
						window.setTimeout(function(){
							$("div.eig").fadeIn('100');
							$("div.txt",obj).fadeIn('100');
						}, textEinblendZeit);
				}
			};
			
			startStoer = window.setTimeout(function(){
				$("#stoerer").fadeIn('100');
				$("#stoerer").html('<div class="stoercont"><a href="http://www.graco-berlin.de/verlag/artikel.php?id=9" title="Der kleine Türöffner" target="_blank"><img src="images/stoerer.gif" border=0></a><div onclick="$(\'#stoerer\').html(\'\'); $(\'body\').removeClass(\'dunkel\'); $(\'#stoerer\').fadeOut(\'100\');">Zurück zu den Eigenschaften</div></div>');
				$("body").addClass("dunkel");
			}, startStoerZeit+options.speed);
			//if(clicked==1) clearTimeout(startStoer);
			
			function stoerClicki(){
				clickStoer = window.setTimeout(function(){
						$("#stoerer").append('<div class="stoercont"><a href="http://www.graco-berlin.de/verlag/artikel.php?id=9" title="Der kleine Türöffner" target="_blank"><img src="images/stoerer.gif" border=0></a><div onclick="$(\'#stoerer\').html(\'\'); $(\'body\').removeClass(\'dunkel\'); $(\'#stoerer\').fadeOut(\'100\');">Zurück zu den Eigenschaften</div></div>');
						$("#stoerer").fadeIn('100');
				}, clickStoerZeit+options.speed); 
			}
						
			$("h2.h2").click(function(){
				$("#stoerer").fadeIn('100');
				$("#stoerer").html('<div class="stoercont"><a href="http://www.graco-berlin.de/verlag/artikel.php?id=9" title="Der kleine Türöffner" target="_blank"><img src="images/stoerer.gif" border=0></a><div onclick="$(\'#stoerer\').html(\'\'); $(\'body\').removeClass(\'dunkel\'); $(\'#stoerer\').fadeOut(\'100\');">Zurück zu den Eigenschaften</div></div>');
				$("body").addClass("dunkel");
			});
			
			$("#nextBtn").click(function(){
				if(startStoer) clearTimeout(startStoer);
				if(typeof clickStoer != "undefined") clearTimeout(clickStoer);
				//clicked=1;
				if (t<=0) direction = 'next';
				if ((t>=ts) && direction == 'next') {
					direction = 'prev';
					//alert(direction+' t='+t+' ts='+ts);
					t=0;
					ts = s-1; }
					animate(direction);
					hiding(txtHidden);
					stoerClicki();
			});
			
			$("#prevBtn").click(function(){
				if(startStoer) clearTimeout(startStoer);
				if(typeof clickStoer != "undefined") clearTimeout(clickStoer);
				//clicked=1;
				
				
					direction = 'prev';
					//alert(direction+' t='+t+' ts='+ts);
					
					animate(direction);
					hiding(txtHidden);
					stoerClicki();
			});
			
			//if(s>1) $("a","#"+options.nextId).fadeIn();	
		});
	  
	};

})(jQuery);
