$(function() {
	//////// The iNeedPageArray comes from openClose.js /////////////////////
	var currentDoor = 0;
	var doorLeft = 0;
	var doorRight = 0;
	var marginAmount = (($(window).width() - 944)/2)+944;
	var slidingMovementComplete = true;
	
	$('#mainDoor').livequery(function() {
		
		$('#contentSlidingDivContainer').css('width',$(window).width() + (944*2) +'px');
		
		/////////Set the current page as the default dropdown ///
		for(var i=0;i<=iNeedPageArray.length;++i) {
			if(iNeedPageArray[i] == currentPage) {currentDoor = i;}
		}
		
		doorLeft = getNextDoorID('right');
		doorRight = getNextDoorID('left');
		
		loadSurroundingDoors();
	});
	
	$(window).resize(function() {
		if($('#contentSlidingDivContainer').size()) {
			$('#contentSlidingDivContainer').css('width',$(window).width() + (944*2) +'px');
			marginAmount = (($(window).width() - 944)/2)+944;
			
			$('#contentSlidingDivContainer .slidingDoorContainer:first').css('left','-'+marginAmount+'px');
			$('#contentSlidingDivContainer .slidingDoorContainer:last').css('left',marginAmount+'px');
		}
	});
	
	////////// The main function ////////////////////////////////////////////////////////////
	function slideDoors(dir) {
		slidingMovementComplete = false;

		//////////// Set the movement amout to positive or negative ///////
		var movementDir = (dir == "right") ? "+" : "-";
		
		/////////// Get the current door id ///////////////////////////////
		currentDoor = (dir == "right") ? getNextDoorID('right') : getNextDoorID('left');
		
		/////// Set teh current page and call a change for swfaddress /////
		currentPage = iNeedPageArray[currentDoor];
		
		///// Move the image to the right
		$('#contentSlidingDivContainer').animate({"left": movementDir+"="+marginAmount+"px"}, "1000","swing",function() {removeImages(dir);});
	}
	
	
	//////// Add the surround doors ////////////////
	function loadSurroundingDoors() {
		
		$.get('agency-interactive/'+iNeedPageArray[doorLeft]+'.php',"sid="+Math.random()+"&slidingDoorLeft=-"+marginAmount+"px",function(data) {
			$('#contentSlidingDivContainer').prepend(data);
		});
		
		$.get('agency-interactive/'+iNeedPageArray[doorRight]+'.php',"sid="+Math.random()+"&slidingDoorLeft=+"+marginAmount+"px",function(data) {
			$('#contentSlidingDivContainer').append(data);
		});
		
		
	}
	
	function removeImages(dir) {
		if(dir == "left") {			
			//Remove the element just moved over and reset the marginLeft
			$('#contentSlidingDivContainer .slidingDoorContainer:first').remove();
			
			//Reset the content margin and the new currentDoor left
			$('#contentSlidingDivContainer').css('left','0px');
			$('#contentSlidingDivContainer .slidingDoorContainer:first').css('left','-'+marginAmount+'px');
			
			//Change the content margin and left for the last door
			$('#contentSlidingDivContainer .slidingDoorContainer:last').css('left','0px');
			
			
				
		} else {
			//Remove the element just moved over and reset the marginLeft
			$('#contentSlidingDivContainer .slidingDoorContainer:last').remove();
			
			//Reset the content margin and the new currentDoor left
			$('#contentSlidingDivContainer').css('left','0px');
			$('#contentSlidingDivContainer .slidingDoorContainer:first').css('left','0px');
			
			//Change the content margin and left for the last door
			$('#contentSlidingDivContainer .slidingDoorContainer:last').css('left',marginAmount+'px');
			
		}
		
		//Add new image to left hand side 
		addNewDoor(dir);
		
		SWFAddress.setValue('/agency-interactive/'+currentPage+'.php');
		
	}
	
	function getNextDoorID(dir) {
		var nextDoor = currentDoor;
		
		if(dir == "right") {
			nextDoor--;
		} else {
			nextDoor++;
		}
		
		if(nextDoor > 6) {nextDoor = 0;}
		if(nextDoor < 0) {nextDoor = 6;}
		
		return nextDoor;
	}
	
	function addNewDoor(dir) {
		if(dir == "right") {
			$.get('agency-interactive/'+iNeedPageArray[getNextDoorID(dir)]+'.php',"sid="+Math.random()+"&slidingDoorLeft=-"+marginAmount+"px",function(data) {
				$('#contentSlidingDivContainer').prepend(data);
				checkDoorImgWidth();
				//slidingMovementComplete = true;
			});
		} else {
			$.get('agency-interactive/'+iNeedPageArray[getNextDoorID(dir)]+'.php',"sid="+Math.random()+"&slidingDoorLeft=+"+marginAmount+"px",function(data) {
				$('#contentSlidingDivContainer').append(data);
				slidingMovementComplete = true;
			});		
		}
	}
	
	function checkDoorImgWidth() {
		if($('#open-door:first').width() == 0) {
			return setTimeout(function() {checkDoorImgWidth();},500); ////////If not loaded, wait 100 milliseconds and try again ///////
		} else {
			//alert($('#open-door:first').attr('src') +":" +$('#open-door:first').width());
			setTimeout(function() {slidingMovementComplete = true;;},500);
		}	
	}
	
	////////////// Setup on click events //////////////////////////////////////////////////////////
	$('#arrow-right').livequery('click',function() {
		if(slidingMovementComplete && $('#contentSlidingDivContainer').size()) {slideDoors('left');}
		return false;
	});
	
	$('#arrow-left').livequery('click',function() {
		if(slidingMovementComplete && $('#contentSlidingDivContainer').size()) {slideDoors('right');}
		return false;
	});
	/////////////////////////////////////////////////////////////////////////////////////////////////
});
