/*
 * Floating Div jQuery Plugin v1.0
 *
 * Copyright 2010, Truc Le <truc.le@webtretho.com>
 * Released under the GPL Version 3 license.
 *
 */
(function($) {
	jQuery.fn.floatingdiv = function (options) {

        var settings = jQuery.extend({
	     ie6: true,
             wrapperWidth: 980,
             top: 100,
             position: 'left',
             type: 'normal',
             speed: 500
	  }, options);

	  //this is the floating content
        var floatingbox = this;
        
        //var bodyY = parseInt($('#main-content').offset().top) - 20;
        var bodyY = settings.top; //header height

        documentWidth   = settings.wrapperWidth;
        windowWidth     = parseInt($(window).width());
        objectWidth     = parseInt(floatingbox.width());
        w = (windowWidth-documentWidth)/2;
        if (settings.position == 'left') {
            left = w-objectWidth;
        } else {
            left = w + documentWidth; // right 2px
        }
        floatingbox.css('left', left+"px");
        //console.log(floatingbox.css('left'));

        floatingbox.css('display', 'block');
        
        if(w < objectWidth){
            floatingbox.css('display', 'none');
        }else{
            floatingbox.css('display', 'block');
        }

        $(window).scroll(function () {
           var scrollY = $(window).scrollTop();
           if(floatingbox.length > 0){

                //floatingbox.html('srollY : ' + scrollY + ', bodyY : ' + bodyY + ', isfixed : ' + isfixed);
                switch (settings.type) {
                   case 'normal':
                        break;
                   case 'fixed':
                       var isfixed = floatingbox.css('position') == 'fixed';
                       if ( scrollY > bodyY && !isfixed) {
                            floatingbox.stop().css({
                              position: 'fixed',
                              top: 1,
                              left: left
                            });
                        } else if ( scrollY < bodyY && isfixed ) {
                                  floatingbox.css({
                                          position: 'absolute',
                                          top: settings.top,
                                          left: left
                                });
                        }
                       break;
                    case 'scroll':
                        if ( scrollY < bodyY ) {
                            top = settings.top;
                        } else {
                            top = $(window).scrollTop();
                        }                     
                        
                        if (top < $(document).height()-floatingbox.height()) {
                            top = top;
                        } else {                            
                            top = $(document).height()-floatingbox.height();                            
                        }
                        
                        floatingbox.animate({top:top+"px" },{queue: false, duration: settings.speed});
                        break;
                    
                }
            }
        }); //end window.scroll

        $(window).resize(function() {
            documentWidth   = settings.wrapperWidth;
            windowWidth     = parseInt($(window).width());
            objectWidth     = parseInt(floatingbox.width());
            w = (windowWidth-documentWidth)/2;
            if (settings.position == 'left') {
                left = w-objectWidth;
            } else {
                left = w + documentWidth; // right 2px
            }
            floatingbox.css('left', left+"px");            

            if(w < objectWidth){
                floatingbox.css('display', 'none');
            }else{
                floatingbox.css('display', 'block');
            }
            
        }); //end window.resize

	};
})(jQuery);


