﻿/*
 * 
 */

/**
 * 
 */

(function($){

$.extend({

docker:{
	defaults:{
		background:'#000',
		size:250,
		color:'white',
		position:'left',
		opacityHidden:'1',
		opacityShown:'1',
		imgPath:''
	}
	
}

});// $.extend

$.fn.extend({

offsetCore:$.fn.offset,

docker:function(s){
	if (s==undefined) s={};
	var def = $.docker.defaults;
	var bck = s.background || def.background;
	var clr = s.color || def.color;
	var siz = s.size || def.size;
	var pos = s.position || def.position;
	var opos = pos=='top'?'bottom':pos=='bottom'?'top':pos=='right'?'left':'right';
	var ortpos = pos=='top'||pos=='bottom'?'left':'top';
	var dir = pos=='top'||pos=='bottom'?'h':'v';
	var oph = s.opacityHidden || def.opacityHidden;
	var ops = s.opacityShown || def.opacityShown;
	var imgPath = s.imgPath || def.imgPath;

	return this.each(function(){

		var imgtog = $('<img/>')
		.attr('src',imgPath+'img/'+pos+'.gif')
		.css({
			position:'absolute',
			opacity:'0.9'
		})
		.css(ortpos,'50%')
		.css(pos,0);

		var tog = $('<div/>')
		.addClass('dockertoggler')
		.css({
			position:'absolute',
			opacity:'0.5',
			width:(pos=='top'||pos=='bottom'?'100%':'5px'),
			height:(pos=='top'||pos=='bottom'?'5px':'100%'),
			background:'url('+imgPath+'img/'+dir+'bck.gif)'
		})
		.css(opos,0)
		.append(imgtog)
		.hover(
			function(){
				$(this).stop().animate({opacity:0.9},'fast');},
			function(){
				$(this).stop().animate({opacity:0.5},'fast');}
		)
		.toggle(
			function(){
				imgtog.attr('src',imgPath+'img/'+opos+'.gif');
				eval('$(this).parents("div.slidediv:first").animate({'
					+pos+':-siz-5,opacity:oph});');			
				eval("$('body').animate({padding"
					+pos.substring(0,1).toUpperCase()+pos.substring(1)+":10});");
			},
			function(){
				imgtog.attr('src',imgPath+'img/'+pos+'.gif');
				eval("$(this).parents('div.slidediv:first').animate({"
					+pos+":0,opacity:ops});");			
				eval("$('body').animate({padding"
					+pos.substring(0,1).toUpperCase()+pos.substring(1)+":siz+10});");
			}
		);

		eval('$("body").css("padding-'+pos+'",siz+10);');

		var sld = $('<div/>')
		.addClass('slidediv')
		.css({
			position:'fixed',
			background:bck,
			overflow:'hidden',
			color:clr,
			width:(pos=='top'||pos=='bottom'?'auto':(siz+10)+'px'),
			height:(pos=='top'||pos=='bottom'?(siz+5)+'px':'100%'),
			padding:0,
			margin:0
		})
		.css(pos,0)
		.append(tog)
		.append(content)
		.append(scrl)
		.prependTo('body');

		var content = $(this)
		.addClass('dockercontent')
		.css({
			background:bck,
			overflow:'hidden',
			color:clr,
			position:'absolute',
			width:(pos=='top'||pos=='bottom'?'auto':siz+'px'),
			height:'auto',
			margin:pos=='top'?'0px 0px 5px 5px':
				pos=='bottom'?'5px 0px 0px 5px':
				pos=='right'?'0px 0px 0px 10px':
				'0px 10px 0px 0px'
		})
		.prependTo(sld);
		
		var scrlbr = $('<div/>')
		.append('<img src="'+imgPath+'img/scrbt.gif" style="position:absolute;top:0;" alt=""/>')
		.append('<img src="'+imgPath+'img/scrbb.gif" style="position:absolute;bottom:0;" alt=""/>')
		.css({
			position:'absolute',
			width:'5px',
			height:'100%',
			opacity:'0.4',
			padding:0,
			margin:0,
			background:'#ccc',
			top:0
		})
		.mousedown(function(e){
			setScrollerSize();			
			//alert('mousedown'+e.pageY);
			var y = e.screenY;
			var st = $(this).offsetCore().top-scrl.offsetCore().top;
			var sh = scrl.height(); // scrollbar height
			var bh = $(this).height();// scroller height
			var newtop;
			var scroller = $(this);
			$(this).css('opacity',0.7);
			$('body').mousemove(function(e2){
				newtop = Math.max(0,Math.min(st+e2.screenY-y,sh-bh));
				scroller.css('top',newtop);
				content.css('top',-newtop*sh/bh);
			});
		});
		$('body')
		.mouseup(function(e){
			//alert('mouseup'+e.pageY);
			scrlbr.css('opacity',0.4);
			$('body').unbind('mousemove');
		})
		.resize(function(){setScrollerSize();});
		
		var scrl = $('<div/>')
		.addClass('dockerscroller')
		.css({
			position:'absolute',
			width:'5px',
			height:'100%',
			padding:0,
			margin:0,
			background:'url('+imgPath+'img/vbck.gif)'
		})
		.css(opos,'5px')
		.append(scrlbr)
		.appendTo(sld);

		var setScrollerSize=function(){
			var ct = content.offsetCore().top-sld.offsetCore().top;
			var ch = (pos=='top'||pos=='bottom'?5:10)+content.height();
			var wh = scrl.height();
			var sh = parseInt(wh*wh/ch);
			scrlbr.css({'top':parseInt(-ct*wh/ch),'height':sh})			
		};
		
		setScrollerSize();
	
	});

}//EOF docker

});// $.fn.extend

})(jQuery);
