/*
JavaScript Document
Dynamically adds iOS CMU App ad to CMU's Homepage
(requires jQuery or ad will remain off-screen)
*/


// if iOS device
if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPad/i))) {

	// onLoad, do this function
	window.onload=function(){
				
		// create div and set div attributes
		var ad = document.createElement("div");
		ad.id = "ad";
		ad.style.background="url(http://www.cmu.edu/homeimages/ad_app.png) no-repeat scroll 0 0 transparent";
		ad.style.bottom="-225px";
		ad.style.height="206px";
		ad.style.position="fixed";
		ad.style.width="992px";
		document.body.appendChild(ad); // append div to end of document
		
		// create a clickable area which directs the user to the app website
		var download = document.createElement("a");
		download.setAttribute("href","http://www.cmu.edu/cmuapp")
		download.setAttribute("target","_blank")
		download.style.bottom="0px";
		download.style.display="block";
		download.style.height="164px";
		download.style.left="0px";
		download.style.position="absolute";
		download.style.width="935px";
		ad.appendChild(download); // append a#download to div
		
		/// creates a clickable area to close the ad and set increment
		var x = document.createElement("a");
		x.setAttribute("onclick","javascript:closeAd()");
		x.style.display="block";
		x.style.height="50px";
		x.style.position="absolute";
		x.style.right="7px";
		x.style.top="50px";
		x.style.width="44px";
		ad.appendChild(x); // append a#x to div
		
		animate();
		window.onscroll=function(){if(done=="yes") document.getElementById("ad").style.display="none"} // on scroll, unanimate
	}
	
	var up, down, remove, done, isDone // create variables for timeouts
	
	function animate() {
		done="no"; // animation not done
		up = setTimeout("document.getElementById('ad').style.bottom='-225px'; document.getElementById('ad').style.display='block'; $('#ad').animate({bottom: '-15px'}, 1000);",500); // hide ad, set ad visible, show ad after .5 seconds
		isDone = setTimeout("done='yes'",1600); //set done='yes' after 1.6 seconds
		down = setTimeout("$('#ad').animate({bottom: '-225px'}, 1000)",9500); // after 9.5 seconds, hide ad
		remove = setTimeout("document.getElementById('ad').style.display='none'",12000); // after 12 seconds, remove ad
	}
		
	function closeAd() {
		clearTimeout(up); // clear running timeout
		clearTimeout(down); // clear running timeout
		clearTimeout(remove); // clear running timeout
		$('#ad').animate({bottom: '-225px'}, 1000) // hide ad
		setTimeout("document.getElementById('ad').style.display='none'",1200); // after 1.2 seconds, remove ad
	}
	
}
