var currentSource = "";

function openPolaroidModal(src,imageLocation) {
	$(function() {
		var leftOrRightModal = ($(window).width()/2 >= imageLocation) ? "left" : "right";
		$('#leftPolaroidModal').html('');
		$('#rightPolaroidModal').html('');
		$('#leftPolaroidModal').css('display','none'); //Close the left modal
		$('#rightPolaroidModal').css('display','none'); //Close the left modal
		$('#'+leftOrRightModal+'PolaroidModal').css('display',''); //Open the correct modal
	
		/////////// Load in the content from the src //////////
		$('#'+leftOrRightModal+'PolaroidModal').load('agency-employment/polaroids/'+src,"sid="+Math.random());
		currentSource = src;
	});
}

$(function() {
	/////////// On close click, close the open polaroid modals ///////////
	$('#polaroid-close').livequery('click',function() {
		$('#leftPolaroidModal').html('');
		$('#rightPolaroidModal').html('');
		$('#leftPolaroidModal').css('display','none'); //Close the left modal
		$('#rightPolaroidModal').css('display','none'); //Close the right modal
	});
	
	////////// When a polaroid is opened, animate the images like a slot machine /////
	$('#individual-polaroid-container-mover img:eq(4)').livequery(function() {
		animatePolaroids();
	});
	
	function animatePolaroids() {
		//alert($('#individual-polaroid-container-mover img').size());
		var polaroidsDoneLoading = true;
		var polaroidCounter = 0;
		
		$('#individual-polaroid-container-mover img').each(function() {
			if($(this).height() < 147) {polaroidsDoneLoading = false;} else {polaroidCounter++;}
		});
		
		if(polaroidsDoneLoading && polaroidCounter > 4) {
			var heightToBottomPolaroid = $('#individual-polaroid-container-mover').height();
			//alert("Counter:"+polaroidCounter+":DoneLoading:"+polaroidsDoneLoading+":DivHeight:"+heightToBottomPolaroid+":Heights:"+polaroidHeights);
			$('#individual-polaroid-container-mover').animate({marginTop:'0px'},500,'linear')
													 .animate({marginTop:'-'+(heightToBottomPolaroid*.60)+'px'},1500,'linear')
													 .animate({marginTop:'-'+(heightToBottomPolaroid-$('#individual-polaroid-container-mover img:last').height())+'px'},1250,'linear');
		} else {
			return setTimeout(function() {animatePolaroids();},500);
		}
	}
	
	////////// On click, rerun the slot wheel ////////////////
	$('#individual-polaroid-container-mover img').livequery('click',function() {
		$('#individual-polaroid-container-mover').css('margin-top','0px');
		
		var whichSideID = $(this).parent().parent().parent().parent().parent().get(0).id;
		
		/////////// Load in the content from the src //////////
		$('#'+whichSideID).load('agency-employment/polaroids/'+currentSource,"sid="+Math.random());
		
		animatePolaroids();
	});

});

function closePolaroidModal() {
	$('#leftPolaroidModal').css('display','none');
	$('#rightPolaroidModal').css('display','none');
}
