(function($) {

	var opt 		= new Array;
	var played		= new Array;
	var oimg		= new Array;
	var currentpos	= new Array;
	var autoInterval = new Array;
	
	$.fn.eQslider= $.fn.eQslider = function(options){
		
		init = function(tg){
			opt[tg.id] = $.extend({}, $.fn.eQslider.defaults, options);
			played[tg.id]		= new Array();
			oimg[tg.id]		= new Array();
			currentpos[tg.id]	= 0;
		
			$.each($('#'+tg.id+' .slider_item'), function(i,item){
				played[tg.id][i]	= $(item);
				oimg[tg.id][i] 		= $(item).find('img');
			});
					
			$.setBulletNavi(tg);//create the bullet navigation
			

			$('#'+tg.id+'').css('background-image', 'none');
				
				if ($.browser.msie) {
				//litle different in IE, fail to animate the any transparent bg or image!
					var square 	= $('#'+tg.id+' .slider_item:eq(0)').find('.slider_link_out');

					$('.slider_link_out').css("display", "none");
					$('#'+tg.id+' .slider_item:eq(0)')
						.fadeIn(opt[tg.id].animateSpeed, function() {
							square.css("display", "block");
						})
						.addClass('played');
					$("#eqsbutton-0").addClass('press');
					
				} else {
				
					$('#'+tg.id+' .slider_item:eq(0)')
						.fadeIn(opt[tg.id].animateSpeed)
						.addClass('played');
					$("#eqsbutton-0").addClass('press');
					
				};
				
				if( opt[tg.id].autoPlay ) //if autoplay == true, then do the animation.
					$.transition(tg);
		};
		
		// transitions
		$.transition = function(tg){

			autoInterval[tg.id] = setInterval(function() { $.tp(tg) }, opt[tg.id].delay);		
		};
		
		$.tp = function(tg, direction){

				if(typeof(direction) == "undefined")
					currentpos[tg.id]++;
				else
					currentpos[tg.id] = direction;

				if  (currentpos[tg.id] == played[tg.id].length ) {
					currentpos[tg.id] = 0;
				}	

				if (currentpos[tg.id] == -1){
					currentpos[tg.id] = played[tg.id].length-1;
				}
				
			if ($.browser.msie) {
			//litle different in IE, fail to animate the any transparent bg or image!
				var c_img 	= $('.played').find('img');
				
				$('.left_texts, .right_texts').css("display", "none");
				oimg[tg.id][currentpos[tg.id]].css("display", "none");
				c_img.fadeOut( opt[tg.id].animateSpeed, function(){
					$('.played').css("display", "none").removeClass('played');
					$(played[tg.id][currentpos[tg.id]]).css("display", "block").addClass('played');
					oimg[tg.id][currentpos[tg.id]].fadeIn( opt[tg.id].animateSpeed, function(){
						$('.left_texts, .right_texts').css("display", "block");
					});
				});
				
				$('.press').removeClass('press');
				$("#eqsbutton-"+currentpos[tg.id]+"").addClass('press');
				
			} else {
			
				$('.played').fadeOut(opt[tg.id].animateSpeed).removeClass('played');
				$('.press').removeClass('press');
				$("#eqsbutton-"+currentpos[tg.id]+"").addClass('press');
				$(played[tg.id][currentpos[tg.id]]).fadeIn(opt[tg.id].animateSpeed).addClass('played');
				
			};
				
		};

		
		$.setBulletNavi = function(tg){
			//append ul after the main div
			$('<ul id="eqslide_bullet"></ul>').insertAfter($('#'+tg.id+''));
			
			for(k=0;k<played[tg.id].length;k++){
				$('#eqslide_bullet').append("<li><a href='#' class='eqsbutton' id='eqsbutton-"+k+"'></a></li>");
			}
			
			$.each($('.eqsbutton'), function(i,item){
				$(item).click( function(e){
					$('.press').removeClass('press');
					$(this).addClass('press');
					e.preventDefault();
					
					$.tp(tg,i);

					if( opt[tg.id].autoPlay ){
						clearInterval(autoInterval[tg.id]);
						$.transition(tg);
					}
				})
			});
			
			if( opt[tg.id].autoPlay )
				if(opt[tg.id].hoverPause)$.pause(tg);
		};
		
		$.pause = function(tg){
			$('#'+tg.id+'').hover(function(){
				clearInterval(autoInterval[tg.id]);
			}, function(){
				clearInterval(autoInterval[tg.id]);
				$.transition(tg);
			});
		}

		this.each (
			function(){ init(this); }
		);


	};
	
	// default values
	$.fn.eQslider.defaults = {	
		delay : 6000, // delay between div/object
		animateSpeed : 600, //speed on amimation per div/object
		hoverPause : true,
		autoPlay : true
	};	
	
})(jQuery);
