/*
**********************************
**********************************
Jquery plugin: resizewidth

Toggle the width between fixed and fullscreen.

**********************************
**********************************
*/
jQuery.fn.resizewidth = function (settings){
    return this.each(function (){
        var $this = jQuery(this);
        var default_config = {
            getHandler : function ($container){
                $container.prepend('<div class="full-width-toggle" />');
                return $container.children('.full-width-toggle');
            },
            duration: 'slow',
            easing : 'linear'
        };
        // merge config with defaults
        var config = jQuery.extend(default_config,settings);

        var $handler = config.getHandler($this);
    
        var $that = $this;
        // resize width
        var resize_portal = function (){
            var w = $that.parent().width();
            $that.animate({width:w},config.duration,config.easing);
        };

        // restore original width
        var restore_portal_size = function (){
            var original_width = $that.data('original_width');
            if (!original_width){
                return ;
            }
            $that.animate({width:parseInt(original_width)},config.duration,config.easing);
            jQuery(window).unbind('resize',resize_portal);
        };

        // toggle
        $handler.click(function (){
            var w;
            jQuery(this).toggleClass('full-width-on');
            if(jQuery(this).hasClass('full-width-on')){
                // get original width
                w = $that.width();
                $that.data('original_width',w.toString());
                resize_portal();
                jQuery(window).bind('resize',resize_portal);
            }
            else{
                restore_portal_size();
            }        
        });
                
    });
};





