var onloadEvents = new Array();

function addToOnload(event){
	onloadEvents.push(event);
	window.onload = function() { 
		for (var x = 0; x < onloadEvents.length; x ++) {
			eval(onloadEvents[x]);
		}
	}
}

var PageControl = {
	currentTextSize : 0,
	// percentages, lowest to highest
	allowedTextSizes : new Array(100, 110, 120, 130, 140, 150),
	// lifetime of the text size cookie
	textCookieExpires : 10 * 365,
	// name of cookie ... doh
	cookieName: 'textSize',
	
	// is the nav hidden
	navHidden : false,
		
	printPage : function () {
		window.print();
	},
	
	textBigger : function () {
		if(PageControl.currentTextSize == 0) {
			PageControl.currentTextSize = 100;
		}
		
		var mySize = PageControl.allowedTextSizes.indexOf(PageControl.currentTextSize.toInt());
		var mySizeUp = mySize+1;
		
		if(PageControl.allowedTextSizes[mySizeUp] > PageControl.currentTextSize) {
			PageControl.currentTextSize = PageControl.allowedTextSizes[mySizeUp];
		}
		// saving the increase in text size
		PageControl.saveTextSizeCookie();
		PageControl.changeText();
	},
	
	textSmaller : function () {
		if(PageControl.currentTextSize == 0) {
			PageControl.currentTextSize = 100;
		}
	
		var mySize = PageControl.allowedTextSizes.indexOf(PageControl.currentTextSize.toInt());
		var mySizeDown = mySize-1;
	
		if(PageControl.allowedTextSizes[mySizeDown] < PageControl.currentTextSize) {
			PageControl.currentTextSize = PageControl.allowedTextSizes[mySizeDown];
		}
		// saving the decrease in text size
		PageControl.saveTextSizeCookie();
		PageControl.changeText();
	},
	
	/**
	 * saves the text size to a cookie on the users computer, 
	 * cookie is persitant (well 10 years)
	 */
	saveTextSizeCookie: function() {
		var sizeCookie = Cookie.write(PageControl.cookieName, PageControl.currentTextSize, {path: '/', duration: PageControl.textCookieExpires});
	},
	
	/**
	 * retrieves the text size stored on the users computer if no such cookie exists returns false
	 */
	retrieveTextSizeCookie: function() {
		var sizeCookie = Cookie.read(PageControl.cookieName);
		if(sizeCookie == null) {
			return false;
		} else {
			return sizeCookie;
		}
	},
	/**
	 * called onload, checks for a saved value for the text sizes and ahjusts the pae accordingly
	 */
	initTextSize: function() {
		var textSize = PageControl.retrieveTextSizeCookie();
		if(textSize != false) {
			PageControl.currentTextSize = textSize;
			PageControl.changeText();
		}
		
	},
	
	changeText : function () {
		$('content').getElements('p').each(function(e) {
			e.style.fontSize = PageControl.currentTextSize+'%';
		});
		
		$('content').getElements('h3').each(function(e) {
			e.style.fontSize = PageControl.currentTextSize+'%';
		});
		
		$('content').getElements('span').each(function(e) {
			if(e.className != 'step-no' && e.className != 'step-text' && e.className != 'step-complete' && e.className != 'sec-no') {
				e.style.fontSize = PageControl.currentTextSize+'%';
			}
		});
		
		$('content').getElements('label').each(function(e) {
			e.style.fontSize = PageControl.currentTextSize+'%';
		});
		
		$('content').getElements('a').each(function(e) {
			if(e.className != 'arrowLink' && e.className != 'section-done' && e.className != 'section-done-green' && e.className != 'section-done-blue') {
				e.style.fontSize = PageControl.currentTextSize+'%';
			}
		});
	},
	
	hideNav : function () {

		var myHideShow = document.getElementById('hide-nav');
		
		if(PageControl.navHidden) {
			// show the nav
			$('stage-menu').style.display = '';
			$$('a.hide-nav-first').getElements('img')[0].set('src', "/css/images/journey/hide-nav.gif");
			$$('a.hide-nav').getElements('img')[0].set('src', "/css/images/journey/hide-nav.gif");
			
			myHideShow.style.borderLeft = 'none';
			PageControl.navHidden = false;
			
			$('content').getElements('h2').each(function(e) {
				if(e.className == 'journey') {
					e.style.paddingLeft = '283px';
					e.style.width = '498px';
				}
			});
			
			$('journey').style.width = '498px';
		} else {
			// hide the nav
			$$('a.hide-nav-first').getElements('img')[0].set('src', "/css/images/journey/show-nav.gif");
			$$('a.hide-nav').getElements('img')[0].set('src', "/css/images/journey/show-nav.gif");
			$('stage-menu').style.display = 'none';
			
			
			myHideShow.style.borderLeft = '1px solid #a2bacf';
			PageControl.navHidden = true;
			
			$('content').getElements('h2').each(function(e) {
				if(e.className == 'journey') {
					e.style.paddingLeft = '17px';
					e.style.width = '764px';
				}
			});
			
			$('journey').style.width = '765px';
		}
	},
	
	pageAttach : function () {
		PageControl.initTextSize()
		
		$('content').getElements('a').each(function(e) {
			if(e.className == 'print-page') {
				e.addEvent('click', function() {
					PageControl.printPage();
				});
			} else if(e.className == 'text-bigger') {
				e.addEvent('click', function () {
					PageControl.textBigger();
				});
			} else if(e.className == 'text-smaller') {
				e.addEvent('click', function () {
					PageControl.textSmaller();
				});
			} else if(e.className == 'hide-nav-first' || e.className == 'hide-nav') {
				e.addEvent('click', function () {
					PageControl.hideNav();
				});
			}
		});
	},
	
	journeySet : function(greenPix, bluePix) {
		var sideNav = $('sideNav');
		sideNav.getElements('div').each(function(e) {
			if(e.className == 'my-progress') {
				e.getElements('img').each(function(ele) {
					if(ele.src.search('progress-green') !== -1) {
						// dealing with green bar
						ele.setStyles({'width' : greenPix, 'height' : 13});
					} else if(ele.src.search('progress-blue') !== -1) {
						// dealing with blue bar
						ele.setStyles({'width' : bluePix, 'height' : 13});
					}
				});
			}
		});
		
		var contentArea = $('content');
		contentArea.getElements('div').each(function(e) {
			if(e.className == 'my-progress') {
				e.getElements('img').each(function(ele) {
					if(ele.src.search('progress-green') !== -1) {
						// dealing with green bar
						ele.setStyles({'width' : greenPix, 'height' : 13});
					} else if(ele.src.search('progress-blue') !== -1) {
						// dealing with blue bar
						ele.setStyles({'width' : bluePix, 'height' : 13});
					}
				});
			}
		});
	}
}
var submitFinalSuccess = {
	init: function() {
		var myForm = $('final-review-form');
		if(myForm) {
			// set the script to process the form
			//myForm.set('action', '/career-save.php');
			//myForm.set('method', 'post');
			
			myForm.addEvent('submit', function(e) {
				//Prevents the default submit event from loading a new page.
				e.stop();
				//Empty the log and show the spinning indicator.
				$('final-review-success').addClass('ajax-spinner');
				//Set the options of the form's Request handler. 
				//("this" refers to the $('myForm') element).
				this.set('send', {
					url: this.action,
					method: 'post',
					onSuccess: function(response) {
						$('final-review-success').removeClass('ajax-spinner');
						
						var messageEl = $$('span#message');
						messageEl.set('html', '<span class="successful">Save successful...</span>');
						$('final-review-success').set('text', response);
						
						new Fx.Tween('message', {duration:3000}).start('opacity', 1, 0);
					},
					onFailure: function(response) { 
						$('final-review-success').removeClass('ajax-spinner');
						
						var messageEl = $$('span#message');
						messageEl.set('html', '<span class="failed">Save failed...</span>');
						$('final-review-success').set('textContent', responseObj.answer);
						
						new Fx.Tween('message', {duration:3000}).start('opacity', 1, 0);
					}
				});
				//Send the form.
				this.send();
			});
		}
	}
}

var orderTips = {
	init: function (){
		var myTips = new Tips('td.header span', {
			className: 'headerTip',
			showDelay: 50
			}	
		);

		var reOrderTips = new Tips('td.price span', {
			className: 'headerTip',
			showDelay: 50
			}
		);
	}
}


addToOnload('PageControl.pageAttach()');
addToOnload('submitFinalSuccess.init()');
addToOnload('orderTips.init()');

