jQuery(function () {

/* Back to Top */
	var scrollDiv = document.createElement('div');
	jQuery(scrollDiv).attr('id', 'toTop').html('^ Back to Top').appendTo('body');    
	jQuery(window).scroll(function () {
		if (jQuery(this).scrollTop() != 0) {
			jQuery('#toTop').fadeIn();
		} else {
			jQuery('#toTop').fadeOut();
		}
	});
	jQuery('#toTop').click(function () {
		jQuery('body,html').animate({scrollTop: 0},800);
	});
	
/* Fixed sidebar to scroll */
	$.fn.basketFloat = function () {
        $me = this;
        var position = this.position();
        var yPos;
        $me.wrap('<div id="fixedFloatWrapper" style="position:absolute;left:' + position.left + 'px;" />');
		
		var altura = $('#sidebar').height(); /* var altura = $('#sidebar').height()-80; */
      	var alturaBrowser = $(window).height();
      	var pos;
		
        $(window).scroll(function () {
        	pos = $('#sidebar').position();
        	
        	if ((altura+pos.top) < alturaBrowser) {
            	yPos = $(window).scrollTop();
            	if (yPos >= position.top) {
                	$me.css('position', 'fixed').css('top', '0').css('left', position.left);
            	} else {
                	$me.removeAttr("style");
            	}
            }
        });

        $(window).resize(function () {
            $('#fixedFloatWrapper').css('position', 'static');
            $me.removeAttr("style");
            position = $me.position();
            $('#fixedFloatWrapper').css({ 'position': 'absolute', 'left': position.left + 'px' });
            if (yPos >= position.top) {
                $me.css('position', 'fixed').css('top', '0').css('left', position.left);
            } else {
                $me.removeAttr("style");
            }
        });

    };
});
