// 	jSlide
//	Author:	Sharif Corbic
//	Mapmy.com.au

(function($){
	$.fn.jslide 	= function(options){
		var outer 	= null;
		var inner 	= null;
		var isMaxHeight	= 0;
		var osMaxHeight = 0;
		var outerHeight	= 0;
		var innerHeight = 0;
		var ratio		= 1;
		var opts	= $.extend({},$.fn.jslide.defaults,options);
		return this.each(function(){
			outer 	= $(this);
			inner = $(this).children(':eq(0)');
			if(outer.css('position')=='static'){
				outer.css({position: 'relative'});	
				inner.css({position: 'relative'});
			}else inner.css({position: outer.css('position')});
			outer.css({overflow: 'hidden',float: 'left'});
			if(!opts.height){
				opts.height = outer.height();
			}
			outer.after('<div id="jquery_slide_outer" style="position:relative;border:'+opts.borderWidth+'px solid '+opts.borderColor+';background-color:'+opts.backgroundColor+';height:'+(opts.height-(opts.borderWidth*2))+'px;width:'+(opts.width-(opts.borderWidth*2))+'px;margin-left:'+opts.marginLeft+'px;float:left;padding:1px;">'
							+'<div id="jquery_slide_inner" style="cursor:pointer;border:1px solid '+opts.innerBorderColor+';width:'+(opts.width-(opts.borderWidth*2)-2)+'px;height:'+(opts.height-(opts.borderWidth*2)-2)+'px;background-color:'+opts.innerBackgroundColor+';position:relative;"></div>'
					  +'</div>');
			isMaxHeight 	= outer.height()-(opts.borderWidth*2)-2;
			osMaxHeight 	= outer.height()-(opts.borderWidth*2);
			outerHeight		= outer.height();
			innerHeight		= inner.height();
			if(innerHeight>outerHeight)ratio = outerHeight/innerHeight;
			outer.next('#jquery_slide_outer').children('#jquery_slide_inner').css({height: isMaxHeight * ratio}).draggable({containment: 'parent',
																														    axis:	'y',
																														    drag: function(e,ui){
																																if(inner.height()>outer.height()){
																																	isMaxHeight 	= outer.height()-(opts.borderWidth*2)-2;
																																	osMaxHeight 	= outer.height()-(opts.borderWidth*2);
																																	da	= inner.height()-outer.height()+20;
																																	rto	= ui.position.top/(osMaxHeight-outer.next('#jquery_slide_outer').children('#jquery_slide_inner').height()-2);
																																	da	= -(da * rto);
																																	inner.css({top: da});
																																}
																																
																															}});
			$(this).data('jslideopts',opts);
			//outer.jslideopts = opts;
		});
	};
	$.fn.jslideupdate	= function(){
		return this.each(function(){
			var opts = $(this).data('jslideopts');
			var inner = $(this).children(':eq(0)');
			var outer = $(this);
			inner.css({top: 0});
			outer.next('#jquery_slide_outer').children('#jquery_slide_inner').css({top: 0});
			isMaxHeight 	= outer.height()-(opts.borderWidth*2)-2;
			osMaxHeight 	= outer.height()-(opts.borderWidth*2);
			outerHeight	= outer.height();
			innerHeight	= inner.height();
			if(innerHeight>outerHeight)ratio = outerHeight/innerHeight;
			else ratio = 1;
			outer.next('#jquery_slide_outer').children('#jquery_slide_inner').css({height: isMaxHeight * ratio});
		});
	}
	$.fn.jslideslideto	= function(p){
		return this.each(function(){
			var opts = $(this).data('jslideopts');
			var inner = $(this).children(':eq(0)');
			var outer = $(this);
			if(inner.height()>outer.height()){
				if(p<0)p=0;
				isMaxHeight 	= outer.height()-(opts.borderWidth*2)-2;
				osMaxHeight 	= outer.height()-(opts.borderWidth*2);
				souter			= outer.next();
				sinner			= souter.children(':eq(0)');
				maxslide		= souter.height()-sinner.height()-2;
				da	= inner.height()-outer.height()+20;
				rto	= p/(osMaxHeight-outer.next('#jquery_slide_outer').children('#jquery_slide_inner').height()-2);
				if(rto>1)rto=1;
				da	= -(da * rto);
				inner.css({top: da});
				sinner.css({top: maxslide*rto});
			}
																																
		});
	}
	// plugin defaults
	$.fn.jslide.defaults	= {
		width:					12,
		borderColor:			'#9a9a9a',
		backgroundColor:		'#FFFFFF',
		borderWidth:			1,
		innerBorderColor:		'#9a9a9a',
		innerBackgroundColor:	'#cacaca',
		marginLeft:				4,
		height:					0
	}
})(jQuery);