/*
 * 	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: 		'Previous',
			nextId: 		'nextBtn',	
			nextText: 		'Next',
			orientation:	'', //  'vertical' is optional;
			speed: 			800			
		}; 
		
		var options = $.extend(defaults, options);  
		
		return this.each(function() {  
			obj = $(this); 				
			var s = $(".ce_text", obj).length;
			var w = obj.width(); 
			var h = obj.height(); 
			var ts = s-1;
			var t = 0;
			var vertical = (options.orientation == 'vertical');

			
			$(".slide_box", obj).css('width',s*w);
			if(!vertical) $(".ce_text", obj).css('float','left');
			$(obj).after('<span id="'+ options.prevId +'"><em class="inactive"><a href=\"javascript:void(0);\">'+ options.prevText +'</a></em></span> <span id="'+ options.nextId +'"><em><a href=\"javascript:void(0);\">'+ options.nextText +'</a></em></span>');		
			$("a","#"+options.prevId).hide();
			$("a","#"+options.nextId).hide();
			//$("#"+options.prevId).toggleClass("inactive");
			$("a","#"+options.nextId).click(function(){		
				animate("next");
				if (t>=ts){ $(this).fadeOut();$(this).parent().addClass("inactive_next");}
				$("a","#"+options.prevId).fadeIn();$("a","#"+options.prevId).parent().removeClass("inactive");
			});
			$("a","#"+options.prevId).click(function(){		
				animate("prev");
				if (t<=0) {$(this).fadeOut();$(this).parent().addClass("inactive");}
				$("a","#"+options.nextId).fadeIn();	$("a","#"+options.nextId).parent().removeClass("inactive_next");
			});	
			function animate(dir){
				if(dir == "next"){
					t = (t>=ts) ? ts : t+1;	
				} else {
					t = (t<=0) ? 0 : t-1;
				};
				if(!vertical) {
					p = (t*w*-1);
					$(".slide_box",obj).animate(
						{ marginLeft: p }, 
						options.speed
					);				
				} else {
					p = (t*h*-1);
					$(".slide_box",obj).animate(
						{ marginTop: p }, 
						options.speed
					);					
				}
			};
			if(s>1) $("a","#"+options.nextId).fadeIn();	
			
			//Albert Add goto and loop function
			//goto ( idx from 0 )
			var goto = function( idx ){
				//
				if ( idx >= ts ){
					$("a","#"+options.nextId).hide().parent().addClass("inactive_next");			
				}else{
					$("a","#"+options.nextId).show().parent().removeClass("inactive_next");		
				}
				//
				if ( idx <= 0 ){
					$("a","#"+options.prevId).hide().parent().addClass("inactive");			
				}else{
					$("a","#"+options.prevId).show().parent().removeClass("inactive");		
				}
				//
				t = idx;
				if(!vertical) {
					p = (t*w*-1);
					$(".slide_box",obj).animate(
						{ marginLeft: p }, 
						options.speed
					);				
				} else {
					p = (t*h*-1);
					$(".slide_box",obj).animate(
						{ marginTop: p }, 
						options.speed
					);					
				}
			}
			//loop
			var stop_loop = false;
			
			jQuery('#new_product').hover(function(){
				stop_loop = true;
			},function(){
				stop_loop = false;
			});
			
			window.setInterval(function(){
				if ( stop_loop ){ return }
				
				var next_t = t+1;
				if ( next_t > ts ){
					next_t = 0;
				}
				goto( next_t );
			},5000);
			
			
		});
	  
	};

})(jQuery);
*/


jQuery(function(){
	
	var $wrap = jQuery('#new_product');
	var $window = $wrap.find('.cons');
	var $list = $wrap.find('.slide_box');
	var $items = $list.children();
	
	//var block_width = 147;
	var block_width = $items.eq(0).width();
	var items_count = $items.size();
	var cur_index = 0;
	var stop_flag = false;
	//console.log(block_width);
	
	$list.css({
		'width':'9999px',
		'position':'relative'
	});
	
	$items.css({
		'float':'left',
		'display':'inline'
	});
	
	$items.clone(true).insertAfter($items);
	
	$wrap.hover(function(){
		stop_flag = true;
	},function(){
		stop_flag = false;
	});
	
	
	var goto_next = function(){
		cur_index++;
		var jump_first = 0;
		if ( cur_index >= items_count ){
			cur_index = 0;
			jump_first = 1;
		}
		
		var taeget_offset = - ( jump_first * items_count + cur_index ) * block_width;
		
		
		$list.animate({
			'left':taeget_offset	
		},'slow',function(){
			if( jump_first ){
				$list.css({
					'left':0
				});
			}
		});
		
	}
	
	
	
	if ( items_count > 1 ){
		window.setInterval( function(){
			if ( stop_flag ){
				return false;
			}
			goto_next();
		},3000 );
	}
	
	
});

