;(function(jQuery){

	jQuery.fn.smoothdrag = function(config){
		var dc = jQuery(this);
		var defaults = {
			parent: '#timeline',
			//wrapid: 'dw',
			smooth: 2.3,
			normalease: 'easeOutQuad',
			endsease: 'easeOutBack'
		}
		var options = jQuery.extend(defaults, config);
		//console.log(options);

		var tm = 0;
		var t = 0;
		var mx = 0;
		var ml = 0;
		var bwidth = jQuery(this).width();
		var pwidth = jQuery(defaults.parent).width();
		var pheight = jQuery(defaults.parent).height();
		var defpos = pwidth - bwidth;
		var defleft = -1*(defpos/2)+'px'
		//jQuery('#header ul li').text(defleft);
		/*
		jQuery(defaults.parent).wrapInner('<div id="'+defaults.wrapid+'"></div>');
		jQuery('#'+defaults.wrapid).css({
			position: 'absolute',
			width: (bwidth*2)-pwidth+'px',
			height: pheight+'px',
			left: 0
		});
		*/
		jQuery(this).css({
			//left: defleft
		}).draggable({
			//containment: '#dw',
			//snapTolerance: 20,
			addClasses: false,
			refreshPositions: true,
			cursor: 'all-scroll',
			axis: 'x',
			start: function(e) {
				mx = e.pageX;
				tm = new Date().getTime();
			},
			//drag: function() {},
			stop: function(e) {
				// adjustment parameter
				// var smooth = 1.618;
				var smooth = defaults.smooth;
				var np = parseInt(jQuery(this).css('left'));
				ml = e.pageX-mx;
				t = new Date().getTime();
				var speed = parseInt((t-tm)*smooth);
				var turn = 0;
				// moving pixels
				var mw = Math.abs(ml);
				// friction
				var friction = (Math.abs(mw)/speed/smooth).toFixed(2);
				// var friction = friction>0.9?0.9:friction;
				// direction
				var direction = ml<0?1:-1;
				// normal new position
				var newleft = parseInt(np+ml);
				// moving distance add friction params
				var newmw = mw*friction*direction;
				// recalculation
				newleft = np-parseInt(newmw);
				// params check
				var check = ' left : '+np+' : newleft : '+newleft+' : speed : '+speed+' : friction :'+friction+' : mw :'+mw+' : direction :'+direction;
				//console.log(check);
				if (newleft <= 0) {
					turn = 1;
					newleft = 0;
					speed = speed*smooth;
					//jQuery('#note').css('background','red');
				} else if (newleft >= (bwidth-pwidth)) {
					turn = 1;
					newleft = bwidth-pwidth;
					speed = speed*smooth;
					//jQuery('#note').css('background','blue');
				} else {
					//jQuery('#note').css('background','transparent');
				}
				//var ease = 'easeOutQuad';
				var ease = defaults.normalease;
					if (turn) {
						ease = defaults.endsease;
					}
					jQuery(this).animate(
						{ left: newleft+'px' },
						{ duration: speed, easing: ease, queue: true, step: function(s) {
							jQuery(this).bind('mousedown',function() {
								//jQuery(this).removeClass('ui-draggable-disabled').addClass('ui-draggable');
								jQuery(this).stop();
							}
					)}, complete: function() {
						//jQuery(this).removeClass('ui-draggable-disabled').addClass('ui-draggable');
					}
				});
			}
		});

	};

})(jQuery);

jQuery(function(jQuery){

	jQuery("#draggable").css({
		opacity: 0,
		color: '#ddd'
	});

	// columns
	var cols = 8;
	var colw = jQuery('.column').width();
	var colh = jQuery('.column').height();
	var dwidth = colw*cols;

	jQuery('#draggable').css({
		opacity: 0,
		visibility: 'visible',
		width: dwidth+'px'
	});

	// start animation
	var pwidth = jQuery('#memberlist').width();
	//var bwidth = jQuery('#draggable').width();
	var bwidth = dwidth;
	var defpos = pwidth - bwidth;
	var wrapwidth = (bwidth*2)-pwidth;
	var wrapleft = defpos;
	// console.log('pwidth:'+pwidth+' bwidth:'+bwidth+' defpos'+defpos+' wrapwidth:'+wrapwidth);
	// var ppos = jQuery('#draggable').position();

	jQuery('#draggable').wrap('<div id="dragwrap"></div>').animate({
			left: -1*(defpos/2)-120+'px',
			//left: -1*(defpos)+'px',
			opacity: '1'
	},{
		duration: 3000,
		easing: 'cubicEaseOut',
		queue: true, 
		complete: function() {
			//if (jQuery.browser.msie){this.style.removeAttribute('filter')}
			if(isIE && version < 7) {
				this.style.removeAttribute('filter');
				DD_belatedPNG.fix('dl.prof');
			}
			// add drag containment box
			jQuery(this).smoothdrag({
				parent: '#memberlist',
				//wrapid: 'dw',
				smooth: 2.2
			});
			//alert(jQuery(this).css('left'));
			//window.status = jQuery(this).css('left');
		}
	});
	jQuery('#dragwrap').css({
		position: 'absolute',
		width: wrapwidth+'px',
		top: 0,
		left: wrapleft+'px',
		height: colh+'px'
	});

	jQuery('dl.prof').hover(
		function (e) {
			jQuery(this).find('.catch').stop(true, false).animate({
				height: '3.0em',
				opacity: 1
			},200, 'easeOutQuad', function(){
				if (jQuery.browser.msie){this.style.removeAttribute('filter')}
			});
			jQuery(this).click(function() {
				var profhref = jQuery(this).find('.viewprof').attr('href');
				window.open(profhref,'_self');
			});
			//e.stopPropagation();
			//return false;
		},
		function (e) {
			jQuery(this).find('.catch').stop(true, false).animate({
				height: 0,
				opacity: 0
			},200, 'easeOutQuad'
			);
			//e.stopPropagation();
			//return false;
		}
	);

});
