/*
 * 	custom Easy Paginate 1.0 - jQuery plugin
 *	written by Alen Grakalic	
 *	http://cssglobe.com/
 *
 *	Copyright (c) 2011 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.cookiesEasyPaginate = function(options){

		var defaults = {				
			step: 4,
			delay: 100,
			numeric: true,
			nextprev: true,
			auto:false,
			pause:4000,
			clickstop:true,
			controls: 'pagination',
			current: 'current' 
		}; 
		
		var options = $.extend(defaults, options); 
		var step = options.step;
		var lower, upper;
		var children = $(this).children();
		var count = children.length;
		var obj, next, prev;		
		var page = 1;
		var timeout;
		var clicked = false;
		
		init();
		
		function show(){
			clearTimeout(timeout);
			lower = ((page-1) * step);
			upper = lower+step;
			$(children).each(function(i){
				var child = $(this);
				child.hide();
			
				if(i>=lower && i<upper){ setTimeout(function(){ child.show(); }, ( i-( Math.floor(i/step) * step) )*options.delay ); }
				
			});	
			
			
			$('li','#'+ options.controls).removeClass(options.current);
			$('li[data-index="'+page+'"]','#'+ options.controls).addClass(options.current);
			
			if(options.auto){
				if(options.clickstop && clicked){}else{ timeout = setTimeout(auto,options.pause); };
			};
			
		
		};
		
		function auto(){
			if(upper <= count){ page++; show(); }			
		};
		
		function init(){
			var ol = $('<ol id="'+ options.controls +'"></ol>').insertAfter(obj);
			prev = $('<span class="prev"><img src="/images/common/list_prev.gif" alt="" /></span>')
			.show()
			.appendTo(ol)
			.click(function(){
				
				if( count == 0) return false;
				if( page == 1){ 
					//alert( '첫 페이지입니다.');
					return false;
				}
				
				clicked = true;
				page--;
				show();
			});
			next = $('<span class="next"><img src="/images/common/list_next.gif" alt="" /></span>')
			.show()
			.appendTo(ol)
			.click(function(){
				
				if( count == 0) return false;
				if( ((page-1) * step) + step >= count){
					//alert('마지막 페이지입니다.');
					return false;
				}
				
				clicked = true;			
				page++;
				show();
			});
			
			next.show();
			prev.show();
		};
		
		this.each(function(){ 
			obj = this;
			if(count>step){
				var pages = Math.floor(count/step);
				if((count/step) > pages) pages++;
				
				var ol = $('<ol id="'+ options.controls +'"></ol>').insertAfter(obj);
				prev = $('<span class="prev"><img src="/images/common/list_prev.gif" alt="" /></span>')
				.show()
				.appendTo(ol)
				.click(function(){
					
					if( count == 0) return false;
					if( page == 1){ 
						//alert( '첫 페이지입니다.');
						return false;
					}
					
					clicked = true;
					page--;
					show();
				});
				next = $('<span class="next"><img src="/images/common/list_next.gif" alt="" /></span>')
				.show()
				.appendTo(ol)
				.click(function(){
					
					if( count == 0) return false;
					if( ((page-1) * step) + step >= count){
						//alert('마지막 페이지입니다.');
						return false;
					}
					
					clicked = true;			
					page++;
					show();
				});
				show();
			}else{
				init();
			}
		});	
	
	};	
})(jQuery);
