(function(jQuery){
    jQuery.fn.tabSlideOut = function(callerSettings) {
        var settings = jQuery.extend({
            tabHandle: '.handle',
            speed: 600, 
            action: 'click',
            tabLocation: 'left',
            topPos: '120px',
            leftPos: '20px',
            fixedPosition: true, 
            pathToTabImage: null,
            imageHeight: null,
            imageWidth: null                       
        }, callerSettings||{});
		
// Warning! Hereinafter in code nothing not to modify	
        settings.tabHandle = jQuery(settings.tabHandle);
        var obj = this;
        if (settings.fixedPosition === true) {
            var positioning = 'fixed';
        } else {
            var positioning = 'absolute';
        }
        if (document.all && !window.opera && !window.XMLHttpRequest) {
            positioning = 'absolute';}
        settings.tabHandle.css({ 
            'display': 'block',
            'width' : settings.imageWidth,
            'height': settings.imageHeight,
            'textIndent' : '-99999px',
            'background' : 'url('+settings.pathToTabImage+') no-repeat',
            'outline' : 'none',
            'position' : 'absolute'
        });        
        obj.css({'line-height' : '1'});        
        var properties = {
                    containerWidth: obj.outerWidth(),
                    containerHeight: obj.outerHeight(),
                    containerPaddingTop: obj.css('paddingTop'),
                    containerPaddingBottom: obj.css('paddingBottom'),
                    containerPaddingLeft: parseInt(obj.css('paddingRight'), 10),
                    tabWidth: settings.tabHandle.outerWidth(),
                    tabHeight: settings.tabHandle.outerHeight()
                };       
        if(settings.tabLocation === 'top') {
            var objTopCss = {
                'position' : 'absolute',
                'top' : '-' + parseInt(properties.containerHeight) + 'px',
                'left' : settings.leftPos
            };
                        var handleTopCss = {
                'bottom' : '-' + ((parseInt(properties.tabHeight)) + 'px'),
                'right' : 0
            };
            obj.css(objTopCss);
            settings.tabHandle.css(handleTopCss);
        }
        if(settings.tabLocation === 'left') {
            var objLeftCss = {
                'height' : properties.containerHeight + 'px', //do this for when submit ajax form
                'left': '-' + properties.containerWidth + 'px',
                'top' : settings.topPos,
                'position' : positioning    
            };        
            var handleLeftCss = {
                'top' :0,
                'right' : '-' + (parseInt(properties.tabWidth)) + 'px'
            };        
            obj.css(objLeftCss);
            settings.tabHandle.css(handleLeftCss);            
        }
        settings.tabHandle.click(function(event){
            event.preventDefault();
        });        
        var slideIn = function() {           
            if (settings.tabLocation == 'top') {
                obj.animate({top:'-' + (parseInt(properties.containerHeight))+ 'px'}, settings.speed).removeClass('open');
            } else if (settings.tabLocation == 'left') {
                obj.animate({left: '-' + properties.containerWidth}, settings.speed).removeClass('open');
            }           
        }       
        var slideOut = function() {
            
            if (settings.tabLocation == 'top') {
                obj.animate({top:'-3px'},  settings.speed).addClass('open');
            } else if (settings.tabLocation == 'left') {
                obj.animate({left:'-3px'},  settings.speed).addClass('open');;
            }
        }
        var clickScreenToClose = function() {
            obj.click(function(event){
                event.stopPropagation();
            });            
            jQuery(document).click(function(){
                slideIn();
            });
        }
        
        var clickAction = function(){
            settings.tabHandle.click(function(event){
                if (obj.hasClass('open')) {
                    slideIn();
                } else {
                    slideOut();
                }
            });            
            clickScreenToClose();
        }        
        var hoverAction = function(){
            obj.hover(
                function(){
                    slideOut();
                },                
                function(){
                    slideIn();
                });                
                settings.tabHandle.click(function(event){
                    if (obj.hasClass('open')) {
                        slideIn();
                    }
                });
                clickScreenToClose();               
        };

        if (settings.action === 'click') {
            clickAction();
        }        
        if (settings.action === 'hover') {
            hoverAction();
        }
    };
})(jQuery);
