// http://localhost/~dahmianowen/CPA/incomeTax.htm
// #../pages/incomeTax.htm



// creates a object that creates a popUp window
// popUp window appends to element with id specified in the object argument
// popUp window's content is taken from file argument, this uses ajax
// this.show and this.hide methods can be used to hide and show the popUp

// requires jQuery for visual effect, can be modified to not use jQuery

function popUpDiv( file , id ) {

	var popUp = new function() { // creation of element 

		var div = document.createElement('div');
		div.style.width = '762px';
		div.style.padding = '40px';
		div.style.paddingTop = '10px';
		div.style.paddingBottom = '25px';
		div.style.backgroundColor = 'white';
		div.style.borderStyle = 'solid';
		div.style.borderTopStyle = 'none';
		div.style.borderWidth = '2px';
		div.style.position = 'absolute';
		div.style.top = '0px';
		div.style.left = '-2px';
		div.style.display = 'none';
		div.className = 'popUp';
		div.appendChild( closeBtn() );
		div.appendChild( contentDiv() );
		return div;

		function closeBtn() {
			var div = document.createElement( 'div' );
			div.style.textAlign = 'right';
			div.style.paddingTop = '5px';
			div.style.paddingRight = '0px';
			div.style.fontSize = '120%';
			div.style.fontWeight = 'bold';
			div.style.color = 'darkred';
			div.style.textDecoration = 'underline';
			div.style.cursor = 'pointer';
			div.innerHTML = 'Back';
			div.onclick =  hide;
			return div;
		}

		function contentDiv() {
			var div = document.createElement( 'div' );
			div.innnerHTML = '<p>LOADING...</p>';
			content();
			return div;

			function content() {
				var xmlhttp;
				if (window.XMLHttpRequest) {
					// code for IE7+, Firefox, Chrome, Opera, Safari
					xmlhttp=new XMLHttpRequest();
				}
				else{
					// code for IE6, IE5
					xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
				}
					xmlhttp.onreadystatechange=function() {
					if(xmlhttp.readyState==4) {
						div.innerHTML=xmlhttp.responseText;
					}
				}
				xmlhttp.open("GET", 'code/ajaxFilereader.php?file=' + file ,true);
				xmlhttp.send(null);
			}
		}
	}


	function hide() {
		hide();
		changeBannerToShore();
		clearHash();

		function hide() {
			$( popUp ).hide( 'slow' );
		}

		function changeBannerToShore() {
			document.getElementById( 'bannerImage' ).src = 'images/cove2.jpg';
		}

		function clearHash() {
			window.location.hash = '#null';
		}
	}

	this.show = function() {
		append();
		setHash();
		show();
		changeBannerToFlower();
		return false;

		function setHash() {
			window.location.hash = '#'+file;
		}

		function changeBannerToFlower() {
			document.getElementById( 'bannerImage').src = 'images/flower.jpg'
		}



		function show() {
			$( popUp ).show( 'slow' , adjustHeight );

			function adjustHeight() { // if the popUp div is shorter than the container, sets the popUp div height to 100%;
				var containerHeight = document.getElementById( id ).clientHeight  ;
				var popUpHeight = popUp.clientHeight;

				


				if ( popUpHeight < containerHeight ) {
					popUp.style.height = '100%';
				}
			}
		}

		function append() {
			document.getElementById( id ).appendChild( popUp );
		}
	}


	this.hide = hide;


	// Check hash
	
	var that = this;

	this.checkHash = function(){
		if ( '#' + file == window.location.hash ) {
			this.show();
		}

		if ( window.location.hash == '#null' || window.location.hash == '' ) {
			var divs = document.getElementsByTagName( 'div' );
			for ( i = 0 , len = divs.length ; i < len; i = i +1 ) {
				if ( divs[i].className == 'popUp' ) {
					this.hide();
//					$( divs[i] ).hide('slow');
//					divs[i].style.display = 'none';
				}
			}
		}
	}

	window.setInterval(function() { that.checkHash() }, 300 );
}
