cboxWinPos = 0;

$(document).ready(function(){

	// Global Colorbox Adjustments
	$(document).bind('cbox_load', function(){
		cboxWinPos = $(window).scrollTop();
		var winW = $(window).width();
		var winT = '-' + cboxWinPos + 'px';
		$('#cboxClose').css('display', 'none');
		$("#container").css({"position": "fixed", "width": winW, 'top': winT});
		$(window).resize(function(){
			winW = $(window).width();
			$("#container").css({"width": winW});
		});
	});
	
	$(document).bind('cbox_complete', function(){
		$('#colorbox').css({'top': '0px'});
		$.colorbox.resize();
		$('#cboxClose').fadeIn();
	});
	
	$(document).bind('cbox_cleanup', function(){
		$("#container").css({"position": "relative", "width": "auto", 'top':'0px'});
		$(window).scrollTop(cboxWinPos);
		$(window).unbind('resize');
	});
	
	if($("#sidebar-wrapper").length > 0) {
		var doc = $(this);
		var winTop = $(window).scrollTop(); // This will be zero initially, but we're declaring the variable for later use 
		var sb = $("#sidebar-wrapper");
		var box = sb.closest(".box-middle");
		var boxTop = box.offset().top; // Top of box-middle, relative to top of page
		var minTop = 0;
		var hasBanner = ($(".banner").length > 0);
		var sbTop = 0;
		var sbBottom = sb.offset().top + sb.height();
		var limited = false;
		var stopFromBottom = 16;
		var boxBottom = boxTop + box.outerHeight(true) - stopFromBottom;
		var boxRemaining = boxBottom - winTop;
		// alert(sbBottom);
	
		// Determine horizontal offset between edge of sidebar and edge of box-middle for later use
		var sbToBoxHOffset = sb.offset().left - box.offset().left; 
		
		// IF NOT SET MANUALLY, DETERMINE STARTING VERTICAL POSITION FOR SIDEBAR
		if (!sb.hasClass("positioned")) {
			// If there's one or more banners, sidebar goes below bottom-most one
			if(hasBanner) {
				var lastBanner = $(".banner").last();
				minTop = (lastBanner.offset().top + lastBanner.outerHeight(false) + 20) - boxTop;
			} else {
				// If no banner, position sidebar below top of box-middle with some padding
				minTop = 20;
			}
			sb.css("top", minTop);
		} else {
			// If top: is set manually, use it for minTop
			minTop = parseFloat(sb.css('top'));
		}
	
		// If bottom of sidebar would otherwise go below bottom of box-middle,
		// adjust the height of the box-middle as needed
		window.onload = function(){
			box_resize(box, sb);
		}
	
		sb.resize(function(){
			box_resize(box, sb);
		});
		
		box.resize(function(){
			box_resize(box, sb);
		});
	
		// Fix for playlist, so box will be resized as needed on each slide, despite lack of page reload
		doc.click(function(){
			box_resize(box, sb);
		});
	
		// FOLLOWING LINES CAN BE USED TO SEE WHAT'S GOING ON
		// box.append("<p id='debugBox' style='z-index:999; width:200px; font-size:1.4em; position:fixed; top:0px; right:0px; background:white;'>Debug info goes here</p>");
		// var debugBox = $("#debugBox");
		// var debugInfo = "";
		// debugInfo += "boxTop: " + boxTop;
		// debugInfo += "<br />sbTop: " + sbTop;
		// debugInfo += "<br />hasBanner: " + hasBanner;
		// debugInfo += "<br />minTop: " + minTop;
		// debugInfo += "<br />boxBottom = boxTop + box.outerHeight(true) - 30: " + boxBottom;
		// debugInfo += "<br />boxRemaining = boxBottom - winTop: " + boxRemaining;
		// debugInfo += "<br />sbBottom = sb.offset().top + sb.height(): " + sbBottom;
		// debugInfo += "<br />" + sbAtTop;
		// debugInfo += "<br />" + sbSliding;
		// debugInfo += "<br />" + sbAtBottom;
		// debugBox.html(debugInfo);
	
		// If we're limiting the sidebar's range of movement...
		if(sb.hasClass("limited")) {
			limited = true;
			stopFromBottom = parseFloat(sb.css("margin-bottom"));
			sb.css("margin-bottom", "");
		}
		
		var sbAtTop = true;
		var sbSliding = false;
		var sbAtBottom = false;
	
		if (!sb.hasClass("fixed")) {
			$(window).scroll(function(){
				if(!(sb.outerHeight(true) > $(window).height())) {
					var containerFixed = ($("#container").css("position") == "fixed");
					winTop = doc.scrollTop();
					if(limited) {
						boxBottom = boxTop + box.outerHeight(true) - stopFromBottom;
					} else {
						boxBottom = boxTop + box.outerHeight(true) - 16;
					}
					boxRemaining = boxBottom - (winTop + 16);
					if(sbTop === 0) {
						sbTop = sb.offset().top;
					}
		
					sbBottom = sb.offset().top + sb.height();
		
					sbAtTop = (winTop <= (sbTop - 16));
					sbSliding = ((winTop > (sbTop - 16)) && (sb.height() <= boxRemaining));
					sbAtBottom = (sb.height() > boxRemaining);
		
					sb.attr('class', function (){
						if(sbAtTop || containerFixed) {
							sb.css("left", "").css("top", minTop).css("bottom", "");
							return 'sbAtTop';
						} else if(sbSliding) {
							sb.css("left", box.offset().left + sbToBoxHOffset).css("top", "").css("bottom", "");
							return 'sbSliding';
						} else if(sbAtBottom) {
							sb.css("left", "").css("top", "").css("bottom", stopFromBottom);
							return 'sbAtBottom';
						}
					});
					// debugInfo = "";
					// debugInfo += "boxTop: " + boxTop;
					// debugInfo += "<br />sbTop: " + sbTop;
					// debugInfo += "<br />hasBanner: " + hasBanner;
					// debugInfo += "<br />minTop: " + minTop;
					// debugInfo += "<br />boxBottom = boxTop + box.outerHeight(true) - 30: " + boxBottom;
					// debugInfo += "<br />boxRemaining = boxBottom - winTop: " + boxRemaining;
					// debugInfo += "<br />sbBottom = sb.offset().top + sb.height(): " + sbBottom;
					// debugInfo += "<br />" + sbAtTop;
					// debugInfo += "<br />" + sbSliding;
					// debugInfo += "<br />" + sbAtBottom;
					// debugBox.html(debugInfo);
				} else {
					sb.attr('class', 'sbAtTop');
					sb.css("left", "").css("top", minTop).css("bottom", "");
				}
			});
	
			$(window).resize(function(){
				if(sbSliding) {
					sb.css("left", box.offset().left + sbToBoxHOffset);
				}
			});
		}
		// If the sidebar is too tall for the box-middle, 
		// adjust the height of the box-middle as needed
		function box_resize(box, sb){
			if((sb.offset().top + sb.outerHeight(false)) > (box.offset().top + box.outerHeight(false))) {
				box.css("min-height", (sb.outerHeight(false) + minTop + 30));
			} else {
				box.css("height", "auto");
			}
		}
	}
});

/*
 * jQuery resize event - v1.1 - 3/14/2010
 * http://benalman.com/projects/jquery-resize-plugin/
 * 
 * Copyright (c) 2010 "Cowboy" Ben Alman
 * Dual licensed under the MIT and GPL licenses.
 * http://benalman.com/about/license/
 */
(function($,h,c){var a=$([]),e=$.resize=$.extend($.resize,{}),i,k="setTimeout",j="resize",d=j+"-special-event",b="delay",f="throttleWindow";e[b]=250;e[f]=true;$.event.special[j]={setup:function(){if(!e[f]&&this[k]){return false}var l=$(this);a=a.add(l);$.data(this,d,{w:l.width(),h:l.height()});if(a.length===1){g()}},teardown:function(){if(!e[f]&&this[k]){return false}var l=$(this);a=a.not(l);l.removeData(d);if(!a.length){clearTimeout(i)}},add:function(l){if(!e[f]&&this[k]){return false}var n;function m(s,o,p){var q=$(this),r=$.data(this,d);r.w=o!==c?o:q.width();r.h=p!==c?p:q.height();n.apply(this,arguments)}if($.isFunction(l)){n=l;return m}else{n=l.handler;l.handler=m}}};function g(){i=h[k](function(){a.each(function(){var n=$(this),m=n.width(),l=n.height(),o=$.data(this,d);if(m!==o.w||l!==o.h){n.trigger(j,[o.w=m,o.h=l])}});g()},e[b])}})(jQuery,this);

