(function($){
    
    var methods = {


    
        
    init : function(options) {
        
        this.traveller.settings = $.extend({}, this.traveller.defaults, options);
        
        var $this;
        var opt;
        return this.each(function(){
            $this = $(this);
            opt = $.fn.traveller.settings;

            $(this).css({zIndex : 99});
            
            if($.fn.traveller.settings.fixed){
                
                $(window).bind('scroll.traveller resize.traveller', function(){
                    var xPos = opt.fixedXPos;
                    var scroll = false;

                    if($(window).scrollTop() > 0 && $(window).scrollTop() > opt.offset){
                        scroll = true;
                        //$.fn.traveller.settings.$this.find('.travellerHbar, .travellerHbarCart').show();

                        if($(window).width() > $('#wrapper').width()){
                            xPos = ($(window).width() / 2 - $('#wrapper').width()) + opt.fixedXPos;
                        }else{
                            xPos = 800;
                        }

                        $this.css({
                            position : 'fixed',
                            left: xPos,
                            top: 30
                        });
                    }else{
                        $this.css({
                            position: 'absolute',
                            left: xPos,
                            top: opt.origY
                        })

                        
                        $this.animate({
                            'marginTop' : 0
                        }, 'fast');
                    }
                });
            }else{
                $(window).bind('scroll.traveller', function(){
                    var offset = 0;
                    var scroll = false;

                    if($(window).scrollTop() > 0 && $(window).scrollTop() > opt.offset){
                        offset = opt.offset;
                        scroll = true;
                    }else{
                        $this.animate({'marginTop' : 0}, 'fast');
                    }

                    if(scroll){
                        $this
                            .stop()
                            .animate({'marginTop': ($(window).scrollTop() - offset )}, 'fast');
                    }
                });
            }
        });

    },

    fixMargin : function(scrolling){
        var fix = 0;
        if(scrolling)
            fix = -15
        
        $('.cartContent').css({
            marginTop: fix
        })
    },

    destroy : function() {

        return this.each(function(){
            $(window).unbind('.traveller');
        })

    }

    };

    $.fn.traveller = function(method) {

        if (methods[method]) {
            return methods[method].apply( this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || ! method) {
            return methods.init.apply( this, arguments );
        } else {
            $.error( 'Method ' +  method + ' does not exist on jQuery.tooltip' );
        }

    };
    
    $.fn.traveller.defaults = {
        offset : 80,
        // pass true is you want an sticky cart item without scrolling
        fixed : false,
        fixedXPos : 1300
    };

    $.fn.traveller.settings = {}

})(jQuery);

