var resizeWrapper;
var resetSize;
var resetHeight;

$(function() {	
		   
	var rightPos;
	var leftPos;
	var logoMargin;
	var marginRight;
	var backLeft;
	var backRight;
	var contentLeft;
	var contentLeftWidth;
	var contentRight;
	var contentRightWidth;
	var borderRight;
	var behindOverflow;
	var show;
	var hide;
	var calcLeftWidth;
	var calcRightWidth;
	var menuRight;
	var bodyHeight;
	
	var animateResize = function() {
		if (fold == 1) {
			$(".left .menu h1").hide();
		} else {
			$(".left .menu h1").show();
		}
		if (fold == 2) {
			$(".right .menu h1").hide();
		} else {
			$(".right .menu h1").show();
		}
		$(".left").animate({
			right: leftPos,
			width: calcLeftWidth
		}, 500, "swing");
		$(".right").animate({
			left: rightPos,
			width: calcRightWidth
		}, 500, "swing");
		$(".left .index .logo").animate({
			marginRight: logoMargin
		}, 500, "swing");		
		$(".behind .content-right").animate({
				left: contentRight,
				width: contentRightWidth
		}, 500, "swing");
		$(".behind .content-left").animate({
				left: contentLeft,
				width: contentLeftWidth
			}, 
			500, "swing", function() {
				if (hide != "all") {
					$(".behind .content-" + hide).hide();
				} else {
					$(".behind .content-right").css("left", 122);
					$(".behind").hide();
				}
				animationFinish();	
			}
		);								
	}
	
	var animationFinish = function() {
		$("body").css("margin-right", "0px");
		$(".left .background").fadeTo(1000, backLeft);
		$(".right .background").fadeTo(1000, backRight);
		if (fold == 1) {
			$(".content-right").html("");
			if (leftobj && rightobj) {
				if (leftobj.mainmenuVertical) leftobj.mainmenuVertical();
				if (rightobj.mainmenuHorizontal) rightobj.mainmenuHorizontal();
			}
		}
		if (fold == 2) {
			$(".content-left").html("");
			if (leftobj && rightobj) {
				if (leftobj.mainmenuHorizontal) leftobj.mainmenuHorizontal();
				if (rightobj.mainmenuVertical) rightobj.mainmenuVertical();
			}

		}
		if (fold == 0) {
			$(".content-right").html("");
			$(".content-left").html("");
			setSides();
			if (leftobj && rightobj) {
				if (leftobj.mainmenuHorizontal) leftobj.mainmenuHorizontal();
				if (rightobj.mainmenuHorizontal) rightobj.mainmenuHorizontal();
			}
		}
		sIFR.replace(futura, {
			selector: 'h1.sIFR-title',
			wmode: 'transparent',
			css: '.sIFR-root { color: #a9e3ed; font-size: 24px; }'
		});
		setTimeout("resetHeight();", 1000);
	}
		   
	resetSize = function() {
		$(".left").css("right", leftPos);
		$(".left").css("width", calcLeftWidth);
		$(".right").css("left", rightPos);
		$(".right").css("width", calcRightWidth);
		$(".left .index .logo").css("marginRight", logoMargin);
	}
	
	resetHeight = function() {
		if (show != "none") {
			$(".behind").show();
			$(".behind .content-" + show).height("auto");
			if (bodyHeight > $(".behind .content-" + show).height()) {
				$(".behind .content-" + show).height(bodyHeight - 52);
			} else {
				var addextra = 52;
				$(".left").height($(".behind .content-" + show).height() + addextra);
				$(".right").height($(".behind .content-" + show).height() + addextra);
				$(".behind .content-" + show).height($(".behind .content-" + show).height());
				$(".behind .content-" + show).height($(".behind .content-" + show).height());
			}
		}
	}
		   
	var calcDifference = function() {
		var difference = ($(window).width() - $("#wrapper > .body").width()) / 2;
		if (difference < 1) difference = 0;
		return difference;
	}
	
	var setSides = function() {
		if (bodyHeight > 692) {
			$("#wrapper > .body > .left").height(bodyHeight);
			$("#wrapper > .body > .right").height(bodyHeight);
		} else {
			$("#wrapper > .body > .left").height(692);
			$("#wrapper > .body > .right").height(692);
		}		
	}
	
	resizeWrapper = function(animated) {
		difference = calcDifference();
		bodyHeight = $(window).height();
		setSides();
		if (fold == 0) {
			leftWidth = 214;
			rightWidth = 786;
			rightPos = 214;
			leftPos = 787;
			logoMargin = -22;
			marginRight = 0;
			backLeft = 1;
			backRight = 1;
			contentLeft = -574;
			contentLeftWidth = 697;
			contentRight = 176;
			contentRightWidth = 803;
			borderRight = 1;
			behindOverflow = "hidden";
			menuRight = 10;
			show = "none";
			hide = "all";
		} else if (fold == 1) {
			leftWidth = 38;
			rightWidth = 215;
			leftPos = 963;
			rightPos = 786;
			logoMargin = 128;
			marginRight = 0;
			backLeft = 0;
			backRight = 1;
			contentLeft = 0;
			contentLeftWidth = 697;
			contentRight = 749;
			contentRightWidth = 803;
			borderRight = 1;
			menuRight = 10;
			behindOverflow = "visible";
			show = "left";
			hide = "right";
		} else if (fold == 2) {
			leftWidth = 160;
			rightWidth = 38;
			leftPos = 841;
			rightPos = 963;
			logoMargin = -22;
			marginRight = 35;
			backLeft = 1;
			backRight = 0;
			contentLeft = -452;
			contentLeftWidth = 697;
			contentRight = 122;
			contentRightWidth = 803;
			borderRight = 1;
			menuRight = -25;
			behindOverflow = "visible";
			show = "right";
			hide = "left";
		}
		
		resetHeight();
		
		$(".right .menu").css("left", menuRight);
		$(".right .inner").css("paddingLeft", marginRight);
		
		calcLeftWidth = leftWidth + Math.ceil(difference);
		calcRightWidth = rightWidth + Math.floor(difference);
		$(".right").css("border-left-width", borderRight);		
		if (animated) {
			animateResize();
		} else {
			resetSize();
		}
		previousfold = fold;
	}
	
	$(window).resize(function() {
		resizeWrapper(false);
	});
});