// jQuery Content Slider
// Scripting for Web Applications: 2

(function($){
		  
	$.fn.jSlideView = function(options){
		
		var that = this.eq(0);
		var orig = this;
		
		options = $.extend({
			direction: "vertical",
			next: "."+that[0].id+"-next",
			prev: "."+that[0].id+"-prev",
			speed: 800,
			easing: "swing",
			loop: false,
			page: 0,
			stop: function(){},
			change: function(){}
		}, options || {});
		$('<a class="'+options.prev.substr(1,options.prev.length)+'">prev</a><a class="'+options.next.substr(1,options.next.length)+'">next</a>').appendTo(document.body);
		
		this.goTo = function(page){
			if(curpos>page){
				curpos=page+1;
				$(options.prev).eq($(options.prev).length-1).trigger("click");
			} else if(curpos<page){
				curpos=page-1;
				$(options.next).eq($(options.next).length-1).trigger("click");
			}
		}
		
		this.getMax = function(){
			return blocks;
		}
		
		$(this).children("li").css({
			float: "left",
			display: "block"
		});
		
		itemHeight = that.contents("li").eq(0).height();
		itemWidth = that.contents("li").eq(0).width();
		parentHeight = that.height();
		parentWidth = that.width();
		options = $.extend({
			view: (parentHeight*parentWidth)/(itemHeight*itemWidth),
			width: parentWidth,
			height: parentHeight
		}, options);
			
		var blocks = Math.ceil(that.children("li").length / options.view)-1;
		if(options.page>blocks){
			options.page=blocks;
		}else if(options.page<0){
			options.page=0;
		}
		
		var swidth = (options.direction=="vertical")?options.width:options.width*(blocks+1);
		var sheight = (options.direction=="vertical")?options.height*(blocks+1):options.height;;
		
		that.wrapInner('<div></div>');
		var slider = that.children("div:first");
		
		$(slider).css({
			position: "absolute",
			margin: 0,
			padding: 0,
			top: 0,
			left: 0,
			width: swidth,
			height: sheight
		});
		
		that.css({
			listStyle: "none",
			padding: 0,
			overflow: "hidden",
			position: "relative",
			width: options.width,
			height: options.height
		});
	
		var curpos = options.page;
		
		var ui = {
			
		};
		
		
		$(options.next).click(function(){
			if(curpos<blocks){
				curpos++;
				if(curpos>=blocks){
					curpos=blocks;
					if(!options.loop){
						$(options.next+":not(.nohide)").animate(
							{opacity:0},
							function(){
								$(this).css({visibility:"hidden"})
							}
						);
					}
				}
			}else{
				if(options.loop){
					curpos=0;
				}else{
					$(options.next+":not(.nohide)").animate(
						{opacity:0},
						function(){
							$(this).css({visibility:"hidden"});
						}
					);
					return false;
				}
			}
			if(orig.uislider){orig.uislider.slider('value', curpos);}
			if(blocks>0){ 
				$(options.prev+":not(.nohide)")
					.css({visibility:"visible"})
					.animate({opacity:1});
			}
			if(options.direction=="vertical"){
				$(slider).stop(true).animate({
					top: -(curpos*options.height)
				}, {duration: options.speed, easing: options.easing,
					complete: function(){
						options.stop(ui, curpos);	
					}
				});
			}else{
				$(slider).stop(true).animate({
					left:-(curpos*options.width)
				}, {duration: options.speed, easing: options.easing,
					complete: function(){
						options.stop(ui, curpos);	
					}
				});
			}
			options.change(ui, curpos);
			return false;
		});
		
		
		$(options.prev).click(function(){
			if(curpos>0){
				curpos--;
				if(curpos<=0){
					curpos=0;
					if(!options.loop){
						$(options.prev+":not(.nohide)").animate(
							{opacity:0},
							function(){
								$(this).css({visibility:"hidden"})
							}
						);
					}
				}
			}else{
				if(options.loop){
					curpos=blocks;
				}else{
					$(options.prev+":not(.nohide)").animate(
						{opacity:0},
						function(){
							$(this).css({visibility:"hidden"});
						}
					);
					return false;
				}
			}
			if(orig.uislider){orig.uislider.slider('value', curpos);}
			
			if(blocks>0){ $(options.next+":not(.nohide)").css({visibility:"visible"}).animate({opacity:1}); }
			if(options.direction=="vertical"){
				$(slider).stop(true).animate({
					top: -(curpos*options.height)
				}, {duration: options.speed, easing: options.easing,
					complete: function(){
						options.stop(ui, curpos);	
					}
				});
			}else{
				$(slider).stop(true).animate({
					left:-(curpos*options.width)
				}, {duration: options.speed, easing: options.easing,
					complete: function(){
						options.stop(ui, curpos);	
					}
				});
			}
			options.change(ui, curpos);
			return false;
		});
		
		if(!options.loop){
			if(options.page==0){
				$(options.prev+":not(.nohide)").css({visibility:"hidden", opacity:0})
			}else if(options.page==blocks){
				$(options.next+":not(.nohide)").css({visibility:"hidden", opacity:0})
			}
			
		}
			
		if(options.direction=="vertical"){
			$(slider).stop(true).css({
				top: -(options.page*options.height)
			});
		}else{
			$(slider).stop(true).css({
				left:-(options.page*options.width)
			});
		}
		
		
		//$(options.prev).eq(0).trigger("click");
		//this.slideshow={};
		var slideint;
		var slideend;
		var slidestate=false;
		this.slideshow = function(newoptions){
			clearInterval(slideint);
			clearTimeout(slideend);
			if(newoptions=="off"){slidestate=false; return false;}
			if(newoptions=="toggle"){
				if(slidestate){
					slidestate=false;
					return false;
				}
			}
			slidestate=true;
			options.slideshow = (typeof(newoptions)=="object")?newoptions:options.slideshow;
			if(options.slideshow.direction!="back" && options.slideshow.direction!="forward"){
				options.slideshow.direction="forward";
			}
			
			slideint = setInterval(
				function(){
					if(options.slideshow.direction == "back"){
						$(options.prev).eq($(options.prev).length-1).trigger("click");
					} else if(options.slideshow.direction == "forward"){
						$(options.next).eq($(options.next).length-1).trigger("click");
					}
				},
				options.slideshow.speed
			);
			if(options.slideshow.timeout){
				slideend = setTimeout(
					function(){
						clearInterval(slideint);
						slidestate=false;
					},
					options.slideshow.timeout
				);
			}
		}
		
		if(options.slideshow){
			orig.slideshow();
		}
		
		this.setPagination = function(pOptions){
			
			var pBlocks = Math.ceil(pOptions.max/pOptions.view);
			var curB = 0;
			for(var i=0; i<pBlocks; i++){
				var tagbuilder='<span class="ui-builderblock-'+i+'">';
				for(var j=(i*pOptions.view);j<Math.min((i+1)*pOptions.view, pOptions.max); j++){
					tagbuilder+='<a href="#">'+(j+1)+'</a> ';
				}
				tagbuilder+='</span>';
				$(tagbuilder).appendTo($(pOptions.target)).css({opacity:(i==0)?1:0, display:(i==0)?"inline":"none"});
			}
			pBlocks--;
			$('<a href="#" class="pagin-prev">'+((options.paginate.text)?'&laquo;':'')+'</a>').prependTo(pOptions.target)
				.click(function(){
					if(curB>0){
						curB--;
					}else{
						curB=pBlocks;
					}
					$(pOptions.target+" .ui-builderblock-"+((curB==pBlocks)?0:curB+1)).animate({opacity:0},300, function(){
						$(this).css({display:"none"});
						$(pOptions.target+" .ui-builderblock-"+(curB)).css({display:"inline"}).animate({opacity:1}, 300, function(){
							//$(this).css({visibility:"visible"});
						});
					});
					return false;
				})
				.after("&nbsp;")
			;
			$('<a href="#" class="pagin-next">'+((options.paginate.text)?'&raquo;':'')+'</a>').appendTo(pOptions.target)
				.click(function(){
					if(curB<pBlocks){
						curB++;
					}else{
						curB=0;
					}
					$(pOptions.target+" .ui-builderblock-"+((curB==0)?pBlocks:curB-1)).animate({opacity:0},300, function(){
						$(this).css({display:"none"});
						$(pOptions.target+" .ui-builderblock-"+(curB)).css({display:"inline"}).animate({opacity:1}, 300, function(){
							//$(this).css({visibility:"visible"});
						});
					});
					return false;
				})
			;
			$(pOptions.target+" a").click(function(){
				orig.goTo($(this).html()-1);
				return false;
			});
		}
		
		if(options.paginate && options.paginate.id){
			
			$.extend({max:10},options.paginate);
			this.setPagination({
				target: options.paginate.id,
				direction: "horizontal",
				view: options.paginate.max,
				speed: 800,
				max: blocks+1
			});
		}
		
		$.fn.jSlideView.collect.push({elem: that[0]});
		
		//console.log($.fn.jSlideView.collect[$.fn.jSlideView.collect.length-1]);
		//end old code block
		return this;
	}
	$.fn.jSlideView.collect = [];
})(jQuery);





