/* 
 * ApproachAnxiety.com Application Controller
 * @author Chris Yap for Eric Monse / Approach Anxiety
 */


var AAController = Class.create();

Object.extend(AAController.prototype, {
	initialize: function() {
		this.analyticsURL = '';
		
		// Event.observe(window, 'load', function() {
		// 	// this.fixPngs();
		// }.bind(this));

	},

	/* BEGIN DIALOG METHODS */
	initDialog: function(dialogPath, isNewDialog) {
		
		this.dialogPath = dialogPath;
		isNewDialog = (typeof isNewDialog == 'undefined') ? true : isNewDialog;
		this.isNewDialog = isNewDialog;
		if (this.isNewDialog) {
			this.createBackdrop();
		}
		else {
			this.populateBackdrop();
		}
		
	},
	
	// set backdrop behind dialog
	createBackdrop: function() {
		
		// create backdrop element
		this.backdrop_element = document.createElement('div');
		this.backdrop_element.id = 'backdrop';
		Element.insert($('container'), { before: this.backdrop_element });
		
		// create dialog content wrapper
		this.wrapper_backdrop_content = document.createElement('div');
		this.wrapper_backdrop_content.id = 'wrapper_backdrop_content';
		Element.insert(this.backdrop_element, { after: this.wrapper_backdrop_content });
		//document.body.appendChild(this.wrapper_backdrop_content);
		
		// create dialog content div
		this.backdrop_content = document.createElement('div');
		this.backdrop_content.id = 'backdrop_content';
		this.wrapper_backdrop_content.appendChild(this.backdrop_content);
		
		// handle backdrop height
		this.handleWindowResize();
		
		// set event to handle window resize and scroll
		this.resizeCache = this.handleWindowResize.bindAsEventListener(this); // necessary to cache the function for proper unregistration later
		// this.scrollCache = this.handleWindowScroll.bindAsEventListener(this); // necessary to cache the function for proper unregistration later
		Event.observe(window, 'resize', this.resizeCache);
		//Event.observe(window, 'scroll', this.scrollCache);
		
		this.populateBackdrop();
		
	},
	
	// populate backdrop content
	populateBackdrop: function() {
		var contentUpdater = new Ajax.Updater(this.backdrop_content, this.dialogPath, 
		{
			method: 'GET',
			evalScripts: true,
			onComplete: function()
				{ 
					 
					if (this.isNewDialog) {
						new Effect.Appear($('dialog'),{duration:.5});
					}
					else {
						$('dialog').style.display = 'block';
					}
					var dialog_width = $('dialog').style.width;
					$('backdrop_content').style.width = dialog_width;
					
					var close_elements = $('dialog').select('[class="dialog_close"]');
					
					for (var i=0; i < close_elements.length; i++) {
						// set close button event
						Event.observe(close_elements[i], 'click', function()
						{
							this.removeBackdrop();
						}
						.bindAsEventListener(this));
					}
					
				}.bind(this)
		});
		
		// set dialog position based on viewport
		if (this.isNewDialog) {
			var viewport_y_offset = this.getScrollY();
			$('wrapper_backdrop_content').style.top = viewport_y_offset + 20 + 'px';
		};
		
	},
	
	// remove backdrop
	removeBackdrop: function() {
		
		$('backdrop_content').removeChild($('dialog'));
		$('wrapper_backdrop_content').removeChild($('backdrop_content'));
		document.body.removeChild($('wrapper_backdrop_content'));
		document.body.removeChild($('backdrop'));
		
		// stop observing window resize and scroll
		Event.stopObserving(window, 'resize', this.resizeCache);
		Event.stopObserving(window, 'scroll', this.scrollCache);
		
	},
	
	// handle window resize
	handleWindowResize: function() {
		
		// set backdrop height
		var windowHeightBodyOffset = document.body.offsetHeight;
		var windowHeightViewport = document.viewport.getHeight();
		if (windowHeightBodyOffset >= windowHeightViewport) {
			var windowHeight = windowHeightBodyOffset;
		}
		
		else {
			var windowHeight = windowHeightViewport;
		}
		
		this.backdrop_element.style.height = windowHeight + 'px';
		
	},
	
	// handle window scroll for modal dialog
	handleWindowScroll: function() {
		// adjust dialog position on scroll
		var windowScrollOffset = this.getScrollY();
		this.wrapper_backdrop_content.style.top = windowScrollOffset + 20 + 'px';	
	},
	/* END DIALOG METHODS */
	
	/* BEGIN ANALYTICS METHODS */
	recordAnalytics: function(analyticsURL) {
		pageTracker._trackPageview(analyticsURL); // send analytics data
	},
	/* END ANALYTICS METHODS */
	
	/* BEGIN UTILITY METHODS */
	/* BEGIN IE PNG FIX */
	fixPngs: function() {
		//	this will iterate with each element with the class 'ie-fix-opacity' and add an IE filter,
		//	replacing the background-image for the filter of that image
		var version = parseFloat(navigator.appVersion.split('MSIE')[1]);
		if ((version >= 5.5) && (version < 7) && (document.body.filters)) {
			$$('.pngfix').each(function(poElement){
				// if IE5.5+ on win32, then display PNGs with AlphaImageLoader
				var cBGImg = poElement.currentStyle.backgroundImage;
				var cImage = cBGImg.substring(cBGImg.indexOf('"') + 1, cBGImg.lastIndexOf('"'));
				if (poElement.currentStyle.backgroundRepeat == 'repeat-x' || poElement.currentStyle.backgroundRepeat == 'repeat-y') {
					var sizingMethod = 'scale';
				}
				else {
					var sizingMethod = 'crop';
				}
				poElement.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + cImage + "', sizingMethod='" + sizingMethod + "')";
				poElement.style.backgroundImage = "none";
			});
			$$('img.pngfix').each(function(poElement){
				poElement.runtimeStyle.backgroundImage = "none";
				poElement.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + poElement.src + "', sizingMethod='image')";
				poElement.src = '/images/spacer.gif';
			});
		}
	},
	/* END IE PNG FIX */
	
	// general function for hiding/showing items given a parent element and classnames used to identify items to show
	hideShow: function(parentElement, tagName, classNameToShow) {
		allElements = parentElement.getElementsByTagName(tagName);
		allElements.each(function(item) {
			if (item.className == classNameToShow) {
				item.style.display = 'block';
			}
			else {
				item.style.display = 'none';
			}
		});
	},
	
	// gets the total viewport height for most platforms and browsers
	getWindowHeight: function() {
		var windowHeight=0;
		if (typeof(window.innerHeight)=='number') {
			windowHeight=window.innerHeight;
		}
		else {
			if (document.documentElement && document.documentElement.clientHeight) {
				windowHeight=document.documentElement.clientHeight;
			}
			else {
				if (document.body && document.body.clientHeight) {
					windowHeight=document.body.clientHeight;
				}
			}
		}
		return windowHeight;
	},
	
	// get offset for top of viewport
	getScrollY: function() {
		var scrOfY = 0;
		if( typeof( window.pageYOffset ) == 'number' ) {
		    //Netscape compliant
		    scrOfY = window.pageYOffset;
		} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		    //DOM compliant
		    scrOfY = document.body.scrollTop;
		} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		    //IE6 standards compliant mode
		    scrOfY = document.documentElement.scrollTop;
		}
		return scrOfY;
	},

	preloadImages: function(image_array) {
		document.imageArray = new Array(image_array.length);
		for(var i=0; i<image_array.length; i++) {
			document.imageArray[i] = new Image;
			document.imageArray[i].src = image_array[i];
		}
	}
	/* END UTILITY METHODS */

});