// JavaScript Document

(function($) {
		  
		  
	$.fn.myposition = function(o) {
        return this.each(function() {
            new $jpos(this, o);
        });
    };	  
	
	
	    // Default configuration properties.
    var defaults = {
        left: 0,
		top: 0,
        offset: 1,
		marginleft:15,
		shoulder:'#container'
    };
		  
   $.myposition = function(e, o) {
	   this.options    = $.extend({}, defaults, o || {});
	   this.conteiner = e;
       this.timer     = null;
	   //alert(e);
		this.setup();
   }
    // Create shortcut for internal use
    var $jpos = $.myposition;

    $jpos.fn = $jpos.prototype = {
        myposition: '0.2.2'
    };

    $jpos.fn.extend = $jpos.extend = $.extend;
	
	    
	$jpos.fn.extend({
		
		setup: function(){
			if(this.options.shoulder==null){
				this.setposition();
			}else{
				this.getposition();
				this.setposition();	  	
				//alert($(this.conteiner).css("left"));
			}
			this.autosetop();
		},
		
		setposition: function(){
			$(this.conteiner).css({left:this.options.left});
			$(this.conteiner).animate({top:this.options.top}, 1000);
			//$(this.conteiner).animate({left:this.options.left}, 1000);
		},
		
		getposition: function(){
			var offset = $(this.options.shoulder).offset();
			//alert($(this.options.shoulder).offset());
			this.options.top = $(window).scrollTop();
			this.options.left = this.options.marginleft + offset.left + this.getwidth();
		},
		
		animatetop:function(){
			//this.options.top = y;
			//alert('suka');
			//alert(this.findpos());
			//$(this.conteiner).css({top:this.options.top});
			this.getposition();
			this.setposition();
			//$(this.conteiner).animate({top: $(window).scrollTop()}, 1000);
			clearTimeout(this.timer);
			this.timer = null;
			return false;
		},
		
		getwidth: function(){
			return ($(this.options.shoulder).width());
		},
		
		autosetop: function(){
			var self = this;
			var c = this.conteiner;
			this.animatetop();
			this.timer = null;
			$(window).scroll(function(){
				clearTimeout(this.timer);
				this.timer = setTimeout(function() { self.animatetop(); }, 1200);
			})
			$(window).resize(function(){
				this.timer = setTimeout(function() { self.animatetop(); }, 1200);
			})
			return false;


		},
		findpos: function() {
			return $(this).offset();
		}

	})
})(jQuery);
