	//Define reusable items	
	var backgrounds = new Array;
	backgrounds[0] = "img/home/one.jpg";
	backgrounds[1] = "img/home/two.jpg";
	backgrounds[2] = "img/home/three.jpg";
	
	//case studies variables
	var item = $('.case');
	var closeItemWidth = 220;
	var openItemWidth = 350;
	var smallHeadingHeight = 38;
	var largeHeadingHeight = 75;
	var itemContentHeight = 180;
	var openItemHeight = itemContentHeight+largeHeadingHeight;
	var lastID = '#case1';
	
	//background image variables
	var home = true;
	var targetbackground = $('.background');
	var targetImage = $('#new-img');
	var opacitySpeed = 300;
	

	//image preloader
	function simplePreload() { 
		var args = simplePreload.arguments;
		document.imageArray = new Array(args.length);
		for(var i=0; i<args.length; i++) {
			document.imageArray[i] = new Image;
			document.imageArray[i].src = args[i];
		}
	}
	
	function blendimage(imagefile) {
		//set the current image as background
		var imgSrc = targetImage.attr("src");
		targetbackground.css("backgroundImage","url("+imgSrc+")");
		//make new image
		targetImage.attr("src",imagefile).css("opacity",0);
		//fade in image
		targetImage.animate({
			opacity: 1.0
		}, opacitySpeed)
	} 
	
	function setInit() {
		//hide image for image changing
		targetImage.css('opacity',0);
				
		//set item dimension
		item.css({
			'width': closeItemWidth+'px',
			'height': smallHeadingHeight+'px'
		});
				
		//hide content for items
		item
		.find('h2').css(
			{'opacity': 1.0}
		)
		.next().css('opacity',0);
		//load first case study
		openCloseItem(lastID);
	}
	
	function setImage(parent){
		if(home) {
		//set background images dependent on which button was clicked
		var strLength = parent.length;
		var index = parent.charAt(strLength-1);
		switch(index) {
		case "1":
		  blendimage(backgrounds[0]);
		  break;
		case "2":
		  blendimage(backgrounds[1]);
		  break;
		case "3":
		  blendimage(backgrounds[2]);
		  break;
		default:
			alert('problem');
		}
		}
		
	}
	
	function openCloseItem(parent) {
	
		item.removeClass('active');
		$(parent).addClass('active');
		
		var lastBlock = lastID;	

		//hide case study
		$(lastBlock).animate(
			{'width': closeItemWidth+'px'}, 
			{ queue:false, duration:400 }
		)
		.animate(
			{'opacity': 1.0},100
		)
		.animate(
			{'height': smallHeadingHeight+'px','opacity': 1.0},
			{ queue:false, duration:200 }
		)
		$(lastBlock+" .case_description")
			.animate({'opacity': 0},
			{ queue:false, duration:100 }
		);

		$(lastBlock).find('h2').animate(
			{'opacity': 1.0},
			{ queue:false, duration:100 }
		)
		
		//show case study
		$(parent).animate(
			{'width': openItemWidth+'px'}, 
			{ queue:false, duration:400 }
		)
		.animate(
			{'opacity': 1.0},200
		)
		.animate(
			{'height': openItemHeight+'px','opacity': 0.8 }, 
			400
		)
		.find('h2').animate(
			{'opacity': 0},
			250
		);
		$(parent+" .case_description").animate(
			{'opacity': 1.0}, 
			200,
			setImage(parent)
		);	
		
		//store last item clicked
		lastID = parent;
	}
	
	//callback function for each button
	function setItemState() {
		item.find('h2').click( function(){
			var parent = '#'+$(this).parent().parent().attr("id");
			openCloseItem(parent);
		});	
	}
	
	function LargeLayout() {
		openItemHeight = 365;
		$('.wrap, .background, .page').css('height','1200px');
		$('#case_studies').css('height', '375px');
	}

	function normalLayout() {
		openItemHeight = 255;
		$('.wrap, .background, .page').css('height','870px');
		$('#case_studies').css('height', '265px');
	}
		
	//Resize font
	function resize() {
		$('#font_large').click( function() {
			$('#font_size a').removeClass('active');
			$('.content').css({
				'fontSize': '1.4em'
			});
			$(this).addClass("active");
			LargeLayout();
			setInit();
			return false;
		});
		
		$('#font_normal').click( function() {
			$('#font_size a').removeClass('active');
			$(this).addClass("active");
			$('.content').css({
				'fontSize': '1em'
			});	
			normalLayout();		
			setInit();
			return false;
		});

	}
	
	$(document).ready ( function() {
		simplePreload( 'img/home/one.jpg', 'img/home/two.jpg', 'img/home/three.jpg' ); 
		setInit();
		setItemState();
		resize();
	});
	
	
	function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}