/*!
 * Contained Sticky Scroll v1.1
 * http://blog.echoenduring.com/2010/11/15/freebie-contained-sticky-scroll-jquery-plugin/
 *
 * Copyright 2010, Matt Ward
*/
(function( $ ){

  $.fn.containedStickyScroll = function( options ) {
  
	var defaults = {  
		unstick : true,
		easing: 'linear',
		duration: 500,
		queue: false,
		closeChar: 'STOP',
		closeTop: 10,
		closeRight:10,
		position: 'relative'
	}  
				  
	var options =  $.extend(defaults, options);
	var $getObject = $(this).selector;
	
	if(options.unstick == true){  
		this.css('position',options.position);
		this.append('<a class="scrollFixIt">' + options.closeChar + '</a>');
		jQuery($getObject + ' .scrollFixIt').css('position','absolute');
		jQuery($getObject + ' .scrollFixIt').css('top',options.closeTop + 'px');
		jQuery($getObject + ' .scrollFixIt').css('right',options.closeTop + 'px');
		jQuery($getObject + ' .scrollFixIt').css('cursor','pointer');
		jQuery($getObject + ' .scrollFixIt').click(function() {
			jQuery($getObject).animate({ top: "96px" },
				{ queue: options.queue, easing: options.easing, duration: options.duration });
			jQuery(window).unbind();
			jQuery('.scrollFixIt').remove();
		});
	} 
	  jQuery(window).scroll(function() {
		//alert(jQuery(window).scrollTop() + ' > ' + jQuery($getObject).parent().offset().top);
		//alert( (jQuery($getObject).parent().height() + jQuery($getObject).parent().position().top) + ' > ' + (jQuery(window).scrollTop() + jQuery($getObject).height()) ) ;
		if(jQuery(window).scrollTop() > (jQuery($getObject).parent().offset().top) &&
		   (jQuery($getObject).parent().height() + jQuery($getObject).parent().position().top) > (jQuery(window).scrollTop() + jQuery($getObject).height()))
		{
			jQuery($getObject).animate(
			   { top: (jQuery(window).scrollTop() - jQuery($getObject).parent().offset().top) + 150 + "px" }, 
			   { queue: options.queue, easing: options.easing, duration: options.duration } 
			);
		}
		else if(jQuery(window).scrollTop() < (jQuery($getObject).parent().offset().top)){
			jQuery($getObject).animate({ top: "96px" },
			{ queue: options.queue, easing: options.easing, duration: options.duration });
		}
	});

  };

  // 광고용 슬라이드 함수 추가 (2011-09-02)
  $.fn.containedStickyScroll_ad = function( options ) {
  
	var defaults = {  
		unstick : true,
		easing: 'linear',
		duration: 500,
		queue: false,
		closeChar: 'STOP',
		closeTop: 10,
		closeRight:10,
		position: 'relative'
	}  
				  
	var options =  $.extend(defaults, options);
	var $getObject = $(this).selector;
	
	if(options.unstick == true){  
		this.css('position',options.position);
		this.append('<a class="scrollFixIt">' + options.closeChar + '</a>');
		jQuery($getObject + ' .scrollFixIt').css('position','absolute');
		jQuery($getObject + ' .scrollFixIt').css('top',options.closeTop + 'px');
		jQuery($getObject + ' .scrollFixIt').css('right',options.closeRight + 'px');
		jQuery($getObject + ' .scrollFixIt').css('cursor','pointer');
		jQuery($getObject + ' .scrollFixIt').click(function() {
			jQuery($getObject).animate({ top: "96px" },
				{ queue: options.queue, easing: options.easing, duration: options.duration });
			jQuery(window).unbind();
			jQuery('.scrollFixIt').remove();
		});
	} 
	jQuery(window).scroll(function() {
		var vh = jQuery('#contentSub').offset().top; //310 헤더위치
		//alert(jQuery('#contentCenter div.box_center:last').offset().top);
		//var vt = (jQuery('#contentCenter div.box_center:last').offset().top + jQuery('#contentCenter div.box_center:last').height()); //1558 시작점.
		var vt = jQuery($getObject).parent().offset().top + jQuery($getObject).parent().height(); //시작점
		var oh = jQuery($getObject).height();//663
		//alert(jQuery('#contentSub').height());
		var vb = (vh + jQuery('#contentSub').height() - oh); //가장하단위치에 위치할 값//2776
  var objTop = "";
		if(jQuery(window).scrollTop() > vt) {			
			var st = jQuery(window).scrollTop(); //2690
			if((st-vb)>0) {jQuery($getObject).animate({top:(vb+"px") },  { queue: options.queue, easing: options.easing, duration: options.duration } ); return; }
			jQuery($getObject).animate({top:jQuery(window).scrollTop() + "px" },  { queue: options.queue, easing: options.easing, duration: options.duration } );
		}else{
   jQuery($getObject).animate({top:vt + "px" },  { queue: options.queue, easing: options.easing, duration: options.duration } );
  }
		/*
		if(jQuery(window).scrollTop() > (jQuery($getObject).parent().offset().top + jQuery($getObject).parent().height()) && (jQuery(window).scrollTop() < (jQuery('#footer').offset().top - jQuery($getObject).height())))
		{
			jQuery($getObject).animate(
			   { top: (jQuery(window).scrollTop() - jQuery($getObject).parent().offset().top) + 385 + "px" }, 
			   { queue: options.queue, easing: options.easing, duration: options.duration } 
			);
		}
		else if(jQuery(window).scrollTop() < (jQuery($getObject).parent().offset().top) + jQuery($getObject).parent().height()){
			jQuery($getObject).animate({ top: jQuery($getObject).parent().offset().top + jQuery($getObject).parent().height() + "px" },
			{ queue: options.queue, easing: options.easing, duration: options.duration });
		}
		*/
	});
  };

})( jQuery );
