jQuery(document).ready(function($){
	// manage resize event
	$(window).resize(function() {
		var actualWindowHeight = getWindowHeight();
		if (actualWindowHeight !== globalWindowHeight) {
			globalWindowHeight = actualWindowHeight;
			if (fixed && actualWindowHeight < domHeader.height() + domSidebar.height()) {
				domLogo.css("position","absolute");
				domSidebar.css("position","absolute");
				if ($("#control").length) {
					var oldControl = $("#control");
					var newControl = oldControl.clone();
					oldControl.remove();
					domSidebar.before(newControl);
					$("#control").css("position","absolute");
				}
				fixed = false;
			}
			if (!fixed && actualWindowHeight >= domHeader.height() + domSidebar.height()) {
				domLogo.css("position","fixed");
				domSidebar.css("position","fixed");
				if ($("#control").length) {
					var oldControl = $("#control");
					var newControl = oldControl.clone();
					oldControl.remove();
					domSidebar.prepend(newControl);
					$("#control").css("position","fixed");
				}
				fixed = true;
			}
		}
	});

	function getDocumentWidth() { return $(document).width(); }
	function getDocumentHeight() { return $(document).height(); }
	function getWindowWidth() { return $(window).width(); }
	function getWindowHeight() { return $(window).height(); }
	function getPostBoxWidth() { return 268; }

	/* START "main" */
	$.preloadCssImages();
		
	domSidebar = $("#sidebar");
	domHeader = $("#header");
	domLogo = $("#header>h1");
	globalWindowHeight = getWindowHeight();
	globalDocumentHeight = getDocumentHeight();
	globalWidth = (!$.browser.opera) ? getDocumentWidth() : getWindowWidth();
	globalPostWidth = getPostBoxWidth();
	
	$(document).scrollTop(0);
	if($.browser.msie && window.XMLHttpRequest == undefined && ActiveXObject != undefined) {
		domSidebar.css("visibility","visible");
	}
	else {
		$("div.search-box",domSidebar).css("marginTop","4em");
		domSidebar.animate({marginLeft:"-140"},1).addClass("hide");
		fixed = false;
		if (globalWindowHeight >= domHeader.height() + domSidebar.height()) {
			domLogo.css("position","fixed");
			domSidebar.css("position","fixed");
			fixed = true;
		}

		var controlLink = jQuery('<a></a>').attr("href","#").attr('id', 'control');
		controlLink.css("visibility","visible");
		if ($.browser.msie) { controlLink.css("opacity",.99); }
		if (fixed) {
			controlLink.css("position","fixed");
			domSidebar.prepend(controlLink);
		}
		else { domSidebar.before(controlLink);
		}
	}
	
	// click event management
	$("#control").live("click",function(e){
		e.preventDefault();
		var slidepx = domSidebar.width();
		if (!domSidebar.hasClass('hide')) {
			if (!$.browser.msie) { $(this).fadeOut("slow", function() { $(this).removeClass('close');});}
			//else { $(this).hide("fast", function() { $(this).removeClass('close');}); }
			else { $(this).animate({opacity:.00}, 500, function() { $(this).removeClass('close');}); }
			margin = "-=" + slidepx;
			domSidebar.addClass('hide');
		}
		else {
			domSidebar.css("visibility","visible");
			if (!$.browser.msie) { $(this).fadeOut("slow", function() { $(this).addClass('close');}); }
			//else { $(this).hide("fast", function() { $(this).addClass('close');}); }
			else { $(this).animate({opacity:.00}, 500, function() { $(this).addClass('close');}); }
			margin = "+=" + slidepx;
			domSidebar.removeClass('hide');
		}
		domSidebar.animate({ left: margin }, "slow");
		if ($.browser.msie) {
			domSidebar.css('zIndex',90);
			$("#control").css('zIndex',100);
		}
		if (!$.browser.msie) { $("#control").fadeIn("fast"); }
		//else { $("#control").show("fast"); }
		else { $("#control").animate({opacity:.99}, 500); }
	});		
});
