/**
 * jquery.scrollFollow.custom
 */

(function($) {	
	$.scrollFollow = function ( box, opts ){ 
		box = $( box );
		var position = box.css( 'position' );
		
		function ani(){
			box.queue( [ ] );		
			var viewportHeight = parseInt( $( window ).height() );	
			var pageScroll =  parseInt( $( document ).scrollTop() );
			var parentTop =  parseInt( box.cont.offset().top );
			var parentHeight = parseInt( box.cont.attr( 'offsetHeight' ) );
			var boxHeight = parseInt( box.attr( 'offsetHeight' ) + ( parseInt( box.css( 'marginTop' ) ) || 0 ) + ( parseInt( box.css( 'marginBottom' ) ) || 0 ) );
			var aniTop;
			
			// Make sure the user wants the animation to happen
			if ( isActive ){
				if ( opts.relativeTo == 'top' ){
					if ( box.initialOffsetTop >= ( pageScroll + opts.offset ) ){
						aniTop = box.initialTop;
					} else {
						aniTop = Math.min( ( Math.max( ( -parentTop ), ( pageScroll - box.initialOffsetTop + box.initialTop ) ) + opts.offset ), ( parentHeight - boxHeight - box.paddingAdjustment ) );
					}
				} else if ( opts.relativeTo == 'bottom' ){
					if ( ( box.initialOffsetTop + boxHeight ) >= ( pageScroll + opts.offset + viewportHeight ) ){
						aniTop = box.initialTop;
					} else {
						aniTop = Math.min( ( pageScroll + viewportHeight - boxHeight - opts.offset ), ( parentHeight - boxHeight ) );
					}
				}
				
				if ( ( new Date().getTime() - box.lastScroll ) >= ( opts.delay - 20 ) ){
					box.animate({
							top: aniTop
						}, opts.speed, opts.easing
					);
				}
			}
		};
		
		// For user-initiated stopping of the slide
		var isActive = true;
		
		//turn off config
		if ( $.cookie != undefined ){
			if( $.cookie( 'scrollFollowSetting' + box.attr( 'id' ) ) == 'false' ){
				var isActive = false;
				
				$( '#' + opts.killSwitch ).text( opts.offText )
					.toggle( 
						function (){
							isActive = true;							
							$( this ).text( opts.onText );							
							$.cookie( 'scrollFollowSetting' + box.attr( 'id' ), true, { expires: 365, path: '/'} );							
							ani();
						},
						function (){
							isActive = false;							
							$( this ).text( opts.offText );							
							box.animate(
								{ top: box.initialTop}, 
								opts.speed, opts.easing
							);							
							$.cookie( 'scrollFollowSetting' + box.attr( 'id' ), false, { expires: 365, path: '/'} );
						}
					);
			} else {
				$( '#' + opts.killSwitch ).text( opts.onText )
					.toggle( 
						function (){
							isActive = false;							
							$( this ).text( opts.offText );							
							box.animate(
								{ top: box.initialTop}, 0
							);	
							
							$.cookie( 'scrollFollowSetting' + box.attr( 'id' ), false, { expires: 365, path: '/'} );
						},
						function () {
							isActive = true;							
							$( this ).text( opts.onText );							
							$.cookie( 'scrollFollowSetting' + box.attr( 'id' ), true, { expires: 365, path: '/'} );							
							ani();
						}
					);
			}
		}
		
		// 스크롤할 부모 객체 지정
		if ( opts.container == ''){
			box.cont = box.parent();
		} else {
			box.cont = $( '#' + opts.container );
		}		
		// 초기좌표
		box.initialOffsetTop =  parseInt( box.offset().top );
		box.initialTop = parseInt( box.css( 'top' ) ) || 0;
		
		// Hack to fix different treatment of boxes positioned 'absolute' and 'relative'
		if ( box.css( 'position' ) == 'relative' ){
			box.paddingAdjustment = parseInt( box.cont.css( 'paddingTop' ) ) + parseInt( box.cont.css( 'paddingBottom' ) );
		} else {
			box.paddingAdjustment = 0;
		}
		
		// 페이지 스크롤시 이벤트 발생
		$( window ).scroll( function () {
				// interval 지정
				$.fn.scrollFollow.interval = setTimeout( function(){ ani();} , opts.delay );				
				// To check against right before setting the animation
				box.lastScroll = new Date().getTime();
			}
		);
		
		// Animate the box when the page is resized
		$( window ).resize( function(){
				// Sets up the delay of the animation
				$.fn.scrollFollow.interval = setTimeout( function(){ ani();} , opts.delay );				
				// To check against right before setting the animation
				box.lastScroll = new Date().getTime();
			}
		);

		//Run an initial animation on page load
		box.lastScroll = 0;
		ani();
	};
	
	$.fn.scrollFollow = function ( options ){
		//alert(options);
		//return false;
		options = options || {};
		options.relativeTo = options.relativeTo || 'top';
		options.speed = options.speed || 500;
		options.offset = options.offset || 0;
		options.easing = options.easing || 'swing';
		options.container = options.container || this.parent().attr( 'id' );
		options.killSwitch = options.killSwitch || 'killSwitch';
		options.onText = options.onText || 'Turn Slide Off';
		options.offText = options.offText || 'Turn Slide On';
		options.delay = options.delay || 0;
		
		this.each( function(){
			new $.scrollFollow( this, options );
		});		
		return this;
	};
})( jQuery );

	var quickmenu = Class.create({
		obj : null,
		speed : null,
		offset : 20,
		container : null,
		killSwitch : "killSwitch",
		relativeTo : "",
		delay : 0,
		opts : null,
		init : function(arg){
			this.obj = $(arg);
		},
		start : function(){						
			this.opts = {
				speed : this.speed,
				offset : this.offset,
				container : this.container,
				killSwitch : this.killSwitch,
				relativeTo : this.relativeTo,
				delay : this.delay
			};
			this.obj.scrollFollow(this.opts);
		}
	});
