var client;
var section;

function getCSData(href) {
	var findHash = href.lastIndexOf('#');
	var data = href.substr(findHash+1).split('-');
	if (data.length == 3 && data[0] == 'CS') {
		client = data[1];
		section = data[2];
		return true;
	}
	else return false;
}

function changeCSImage() {
	
	var imageFile = client+'-'+section;
	var imageId = 'CS_BG-'+imageFile;
	$('#bg-overlay img').css('z-index',12);
	$('#bg-overlay').prepend('<img id="'+imageId+'" src="images/case-studies/'+imageFile+'.jpg" alt="" />');
	
	var ratio = 895/1120; // original image height divided by the width
	var overlayWidth = document.documentElement.clientWidth * ((100 - newdivisionx)/100);
	var overlayHeight = document.documentElement.clientHeight - 226;
	
	if ((overlayHeight/overlayWidth) > ratio) {
		var imageWidth = Math.round(overlayHeight / ratio) +1;
		var imageHeight = Math.round(overlayHeight) +2;
	} else {
		var imageWidth = overlayWidth +1;
		var imageHeight = Math.round(overlayWidth * ratio) +2;
		var imagePosTop = Math.round((imageHeight - overlayHeight) / 2);
	}
	
	$('#drawer h4').text($('#CS-'+client+'-'+section+'_d span').text());
	
	$('#'+imageId)
		.css({
			'width' : imageWidth+'px',
			'height' : imageHeight+'px',
			'display' : 'none',
			'position' : 'relative',
			'top' : (imagePosTop) ? '-'+imagePosTop+'px' : 0,
			'z-index' : 13
		})
		.fadeIn(500,function(){
			$(this).nextAll().remove();
		});
}

function attachCSActions() {
	$('#main').append(
		$('<a id="readCS" href="#"><span>Read the Study</span></a>').click(function(){
			var studyHolder = null;
			if ($('#readCS-'+client).length <= 0) {
				$('#scroll').append($('<div id="readCS-'+client+'" class="case-study pageCenter"></div>'));
				$('#readCS-'+client).load(
					'case-studies.php',
					{ 'c':client, 's':section, 'read':true },
					function(){
						$('.case-study a.close').click(function(){
							$(this).parent('.text').fadeOut(200,function(){
								$('#readCS-'+client).remove();
							});
							return false;
						});
						$('#readCS-'+client+' .text').fadeIn(500);
					}
				);
			}
			return false;
		}).css('display','none')
	);
	$('#readCS').fadeIn(700);
	
	$('#drawer').append(
		$('<a class="back" href="#CS">\u00ab Back to Case Studies</a>').click(function(){
			$('#bg-overlay, #readCS, .case-study .text').fadeOut(300,function(){
				$('#bg-overlay img, #readCS, .case-study').remove();
			});
			$('#drawer').load('case-studies.php',null,attachCSAjax);
			location.hash = '#CS';
		})
	);
	
	if ($('#bg-overlay img').length < 1) {
		$('#bg-overlay').css('display','block');
		changeCSImage();
	}
	
	$('#drawer a:not(a[class=back])').click(function(){
		if (getCSData($(this).attr('href'))) {
			$('#drawer li').removeClass('selected');
			$(this).parent('li').addClass('selected');
			$('#readCS-'+client+' .text').fadeOut(200,function(){
				$('#readCS-'+client).remove();
			});
			changeCSImage();
		}
	}).hover(function(){
		$('#drawer h4').text($(this).children('span').text());
	},function(){
		$('#drawer h4').text($('#CS-'+client+'-'+section+'_d span').text());
	});
}

function attachCSAjax() {
	$('#drawer a').click(function(){
		if (getCSData($(this).attr('href'))) {
			$('#biosList').fadeOut(300);
			$('#drawer').fadeOut(200,function(){
				$('#drawer').load(
					'case-studies.php',
					{ c:client, s:section },
					attachCSActions
				);
				$('#drawer').fadeIn(600);		
			});
		}
	});
}

$(document).ready(function(){
	
	$('#expandNav').toggle(function(){
		// Close the Twitter and Facebook widgets
		$('#twitter, #facebook').fadeOut(200);
		
		$(this).css('backgroundPosition','0 -33px');
		$(this).parent('#nav').animate({
			'height':'221px'
		},200);
		$('#wrapNav').animate({
			'paddingTop':'166px'
		},200);
		var fixOverlay = true;
		$.each($.browser, function(i, val) {
			if (i=="msie" && val==true && Number($.browser.version.substr(0,1)) <= 7) fixOverlay = false;
		});
		if (fixOverlay) $('#scroll').css('zIndex','auto');
		$('#drawer').load('case-studies.php',null,attachCSAjax);
		$('#drawer').fadeIn(1000);
		$(this).parent('#nav').addClass('open');
		$(this).html('<span>close<\/span>');
		location.hash = '#CS';
	},function(){
		$(this).css('backgroundPosition','0 0');
		$(this).parent('#nav').animate({
			'height':'55px'
		},200);
		$('#wrapNav').animate({
			'paddingTop':'0'
		},200);
		$('#bg-overlay, #drawer, #readCS, .case-study .text').fadeOut(200,function(){
			$('#bg-overlay img, #drawer *, #readCS, .case-study').remove();
			$('#scroll').css('zIndex',10);
		});
		$('#biosList').fadeIn(300);
		$(this).parent('#nav').removeClass('open');
		$(this).html('<span>open<\/span>');
		location.hash = '#';
	});
	
});