(function($){
    $.fn.tabSlideOut = function(callerSettings) {
        var settings = $.extend({
            tabHandle: '.handle',
            speed: 300, 
            url:'',
            loadid:'',
            action: 'click',
            tabLocation: 'left',
            topPos: '200px',
            leftPos: '20px',
            fixedPosition: false,
            positioning: 'absolute',
            pathToTabImage: null,
            pathToReplaceTabImage: null,
            imageHeight: null,
            imageWidth: null,
            onLoadSlideOut: false,
            callback:null
        }, callerSettings||{});

        settings.tabHandle = $(settings.tabHandle);
        var obj = this;
        if (settings.fixedPosition === true) {
            settings.positioning = 'fixed';
        } else {
            settings.positioning = 'absolute';
        }
        
        //ie6 doesn't do well with the fixed option
        if (document.all && !window.opera && !window.XMLHttpRequest) {
            settings.positioning = 'absolute';
        }
        

        
        //set initial tabHandle css
        
        if (settings.pathToTabImage != null) {
        	/*
            settings.tabHandle.css({
            'background' : 'url('+settings.pathToTabImage+') no-repeat',
            'width' : settings.imageWidth,
            'height': settings.imageHeight
            });
            */
        	
        	settings.tabHandle.css({
                'width' : settings.imageWidth,
                'height': settings.imageHeight
                });
        }
        
        settings.tabHandle.css({ 
            'display': 'block',
            'textIndent' : '-99999px',
            'outline' : 'none',
            'position' : 'absolute'
        });
        
        obj.css({
            'line-height' : '1',
            'position' : settings.positioning
        });

        
        var properties = {
                    containerWidth: parseInt(obj.outerWidth(), 10) + 'px',
                    containerHeight: parseInt(obj.outerHeight(), 10) + 'px',
                    tabWidth: parseInt(settings.tabHandle.outerWidth(), 10) + 'px',
                    tabHeight: parseInt(settings.tabHandle.outerHeight(), 10) + 'px'
                };

        //set calculated css
        if(settings.tabLocation === 'top' || settings.tabLocation === 'bottom') {
            obj.css({'left' : settings.leftPos});
            settings.tabHandle.css({'right' : 0});
        }
        
        if(settings.tabLocation === 'top') {
            obj.css({'top' : '-' + properties.containerHeight});
            settings.tabHandle.css({'bottom' : '-' + properties.tabHeight});
        }

        if(settings.tabLocation === 'bottom') {
            obj.css({'bottom' : '-' + properties.containerHeight, 'position' : 'fixed'});
            settings.tabHandle.css({'top' : '-' + properties.tabHeight});
            
        }
        
        if(settings.tabLocation === 'left' || settings.tabLocation === 'right') {
            obj.css({
                'height' : properties.containerHeight,
                'top' : settings.topPos
            });
            
            settings.tabHandle.css({'top' : 0});
        }
        
        if(settings.tabLocation === 'left') {
            obj.css({ 'left': '-' + properties.containerWidth});
            settings.tabHandle.css({'right' : '-' + properties.tabWidth});
        }

        if(settings.tabLocation === 'right') {
            obj.css({ 'right': '-' + properties.containerWidth});
            settings.tabHandle.css({'left' : '-' + properties.tabWidth});
        }
        
       // $("#overlay_container_handler_id").addClass("overlay_container_handler_in");
        //functions for animation events
        
        settings.tabHandle.click(function(event){
            event.preventDefault();
        });
        var loaded=false;
        var slideIn = function(redirect,URL) {
        	
        
        	obj.hide();
        	
            if (settings.tabLocation === 'top') {
                obj.animate({top:'-' + properties.containerHeight}, settings.speed).removeClass('open');
            } else if (settings.tabLocation === 'left') {
                obj.animate({left: '-' + properties.containerWidth}, settings.speed).removeClass('open');
            } else if (settings.tabLocation === 'right') {
                obj.animate({right: '-' + properties.containerWidth}, settings.speed,function(){if(redirect){window.location.href=URL;} $.unblockUI(); try{$("#overlay_container_handler_id").removeClass("overlay_container_handler_out").addClass("overlay_container_handler_in");}catch(e){}}).removeClass('open');
            } else if (settings.tabLocation === 'bottom') {
                obj.animate({bottom: '-' + properties.containerHeight}, settings.speed).removeClass('open');
            }    
           
           
           
            $("#my_concierge_config_overlay").show();
        };
        
        var slideOut = function() {
        	if(settings.url!=''){
	        	if(loaded==false){
		        	$.ajax({url:settings.url,success:
						function(data){
			        		if(settings.loadid!=''){
			        			document.getElementById(settings.loadid).innerHTML='';
			            		document.getElementById(settings.loadid).innerHTML=data;
			            		if(typeof settings.callback == 'function'){
				                	settings.callback.call();
				                 }
			            		
			            		
			            		
			            		slideoutAnimate();
			            		initCancelButton();
			            		loaded=true;
			        		}
		        		},error:function(){
						
		        	}});
	        	
	        	}else{
	        		//$("#overlay_container_handler_id").removeClass("overlay_container_handler_in").addClass("overlay_container_handler_out");
	        		if(typeof settings.callback == 'function'){
	                	settings.callback.call();
	                 }
	        		
	        		slideoutAnimate();
	        	}
        	}
           
        };
        
        var slideoutAnimate=function(){
        	obj.show();
            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');
            } else if (settings.tabLocation == 'right') {
                obj.animate({right:'-3px'},  settings.speed,function(){try{
	                	 $.blockUI({message:"",css: { 
	        				 backgroundColor:'transparent',
	        				 border:'',
	        				 top:'20%'
	        		    }, growlCSS: { 
	        		    	 backgroundColor:'transparent'
	        		    },overlayCSS:{
	        		    	backgroundColor:'#808080'
	        		    }});
                	
                	 	$("#overlay_container_handler_id").removeClass("overlay_container_handler_in").addClass("overlay_container_handler_out");
                	}catch(e){}}).addClass('open');
            } else if (settings.tabLocation == 'bottom') {
                obj.animate({bottom:'-3px'},  settings.speed).addClass('open');
            }
        	
        };
        
        var clickScreenToClose = function() {
            obj.click(function(event){
                event.stopPropagation();
            });
           
            
            $(document).click(function(){
                slideIn(false,null);
            });
        };
        
        var clickAction = function(){
            settings.tabHandle.click(function(event){
                if (obj.hasClass('open')) {
               
                	 slideIn(false,null);
                   
                } else {
                    slideOut();
                }
            });
            
            //clickScreenToClose();
        };
        
        var hoverAction = function(){
            obj.hover(
                function(){
                    slideOut();
                },
                
                function(){
                    slideIn(false,null);
                });
                
                settings.tabHandle.click(function(event){
                    if (obj.hasClass('open')) {
                        slideIn();
                    }
                });
               // clickScreenToClose();
                
        };
        
        var slideOutOnLoad = function(){
            slideIn(false,null);
            setTimeout(slideOut, 500);
        };
        
        var changeBackgroundCSS=function(path){
        	if(path!='' && path!=null){
        		 settings.tabHandle.css({
        	            'background' : 'url('+path+') no-repeat',
        	            'width' : settings.imageWidth,
        	            'height': settings.imageHeight
        	            });
        		
        	}
        
        }
        
        //choose which type of action to bind
        if (settings.action === 'click') {
            clickAction();
        }
        
        if (settings.action === 'hover') {
            hoverAction();
        }
        
        if (settings.onLoadSlideOut) {
            slideOutOnLoad();
        };
        
        function initCancelButton(){
        	$(".slideTabsubmit_button_").unbind("click");
        	$(".slideTabsubmit_button_").click(function(){
        		updateCookie();
        		slideIn(true,"/content/mytransport/home/myconcierge.html");
        	});
        	
        }
        
    };
})(jQuery);

