$(document).one('ready',function(){ //for some reason this runs twice on clicking through to full post without using 'one'
	
	//scoped variables
		var availabilityInterval, //setInterval used to re-fire resumeCode function until race condition disappears (for IE (and webkit?)) and document.domain setting in child window is recognized by parent.  
			$childBody, //iframe window's document.body
			iframe, //iframe
			iframeOnload,//iframe onload function
			preIframeHeight, //measure of childBody's offsetHeight before iframe's height is reset.
			postIframeHeight, //measure of childBody's offsetHeight after iframe's height is reset. 
			resizingInterval, //setInterval used to re-fire setHeight function until preIframeHeight = postIframeHeight. used to account for animations within childBody.
			resumeCode, //function: fires until communication between parent and child window is established and at that point re-sizes and shows iframe. Also binds the resizing function to all future iframe page loads.   
			setIframeHeight; //function: sets height of iframe according to its child window content.
	
	document.domain='alexaitken.net';
	
	resumeCode=function(){	
		try{
			if(iframe.contentWindow.okToResumeParent===true){
				iframeOnload();
				clearInterval(availabilityInterval);
			}
		}catch(error){};
	};
	
	setIframeHeight=function(){
		preIframeHeight=iframe.style.height;
		iframe.style.height=$childBody.height()+20+'px';//50 handles items getting cut off vertically sometimes...
		postIframeHeight=iframe.style.height;
		if(preIframeHeight===postIframeHeight){clearInterval(resizingInterval);}
	};
	
	iframeOnload=function(){
		$childBody=$(iframe.contentWindow.document.body);
		setIframeHeight();
		$childBody.bind('click',function(){ //tests and resizes child page as necessary for each click within
			resizingInterval=setInterval(setIframeHeight,100);
		});
	};
	
	//Beahavior attachment logic
	$('#ama_demoLink').live('click',function(){
		iframe=$('iframe').get(0);//returns native dom element - at this point, we know it's been created and added
		if($(this).text().indexOf('Return to Post')!==-1){//because of event binding order, text has already switched by the time this is picked up
			$('iframe').unbind().bind('load',function(){
				availabilityInterval=setInterval(resumeCode,50); //check 20 times/second for disappearance of race condition (resumCode clears check and kicks off the page init);			
			});
		};
	});

});
