if (typeof AQ == "undefined" || !AQ) { var AQ = {}; };
AQ.addLoadEvent = function(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}

/**
 * AQ CSS
 */

AQ.CSS = {
	hasClass: function(o, strClass) {
		if (o.className) {
			var arrList = o.className.split(' ');
			var strClassUpper = strClass.toUpperCase();
			for (var i = 0; i < arrList.length; i++) {
				if (arrList[i].toUpperCase() == strClassUpper) {
					return true;
				}
			}
		}
		return false;
	},
	addClass: function(o, strClass, check_existing)	{
		if (o.className) {
			var arrList = o.className.split(' ');
			if (check_existing) {
				var strClassUpper = strClass.toUpperCase();
				for (var i = 0; i < arrList.length; i++) {
					if (arrList[i].toUpperCase() == strClassUpper) {
						arrList.splice(i, 1);
						i--;
					}
				}
			}
			arrList[arrList.length] = strClass;
			o.className = arrList.join(' ');
		} else {	   
			o.className = strClass;
		}
	},
	removeClass: function(o, strClass) {
		if (o.className) {
			var arrList = o.className.split(' ');
			var strClassUpper = strClass.toUpperCase();
			for (var i = 0; i < arrList.length; i++) {
				if (arrList[i].toUpperCase() == strClassUpper) {
					arrList.splice(i, 1);
					i--;
				}
			}
			o.className = arrList.join(' ');
		}
	},
	replaceClass	: function(o, oldClass, newClass) {
		// !define AQ.CSS.replaceClass
	}
};

/**
 * AQ Home
 */
AQ.Home = {
	transaction: null,
	processing: false,
	switchTabs: function(el) {
		var tabid = el.parentNode.id;
		var tabEl = document.getElementById('feature-tabs');
		var tab = tabEl.getElementsByTagName('li');
		for (var i = 0; i < tab.length; i++) {
			var tabnum = tab[i].id.substr(3, 1);
			if (tab[i].id == tabid) {
				AQ.CSS.addClass(tab[i], 'active', true);
				AQ.CSS.addClass(document.getElementById('section'+tabnum), 'active', true);
			} else {
				AQ.CSS.removeClass(tab[i], 'active');
				AQ.CSS.removeClass(document.getElementById('section'+tabnum), 'active');
			}
		}
	},
	checkDomain: function(id) {
		var domain = document.getElementById(id).value;
		var el = document.getElementById('domain_result');
		domain = this.cleanDomain(domain);
		document.getElementById(id).value = domain;
		var re = /^[a-zA-Z0-9-]+(\.[a-zA-Z]{2,4}){1,2}$/;
		if (re.test(domain)) {
			if (!YAHOO.util.Connect.isCallInProgress(this.transaction)) {
				el.innerHTML = '<img src="/img/loading.gif" alt="" width="16" height="16" /><span>Checking availability&hellip;</span>';
				var url = '/whois/?domain='+domain;
				var callback = 
				{ 
					success:	this.whoisSuccess,
					failure:	this.whoisFailure,
					scope:		this
				}
				this.transaction = YAHOO.util.Connect.asyncRequest('GET', url, callback, null);
			}
		} else {
			el.innerHTML = '<img src="/img/warn.png" alt="" width="16" height="16" /><span class="warn">Invalid Domain</span>';
		}
	},
	whoisSuccess: function(o) {
		document.getElementById('domain_result').innerHTML = o.responseText;
	},
	whoisFailure: function(o) {
		document.getElementById('domain_result').innerHTML =
			'<img src="/img/warn.png" alt="" width="16" height="16" /><span class="warn">An error occurred.</span>';
	},
	cleanDomain: function(domain) {
		var reTrim = /^\s*|\s*$/g;
		domain = domain.replace(reTrim,"");
		if (domain.indexOf("www.") != -1) {
			domArray = domain.split("www.");
			domain = domArray[1];
		}
		return domain;
	},
	createSite: function(id) {
		if (!this.processing) {
			this.processing = true;
			var err = false;
			var msg = "The following items are required:\n";
			var form = document.getElementById(id);
			
			// clean-up the domain
			var domain = form.domain.value;
			domain = this.cleanDomain(domain);
			form.domain.value = domain;
			
			// validate the form
			re = /^[a-zA-Z0-9-]+(\.[a-zA-Z]{2,4}){1,2}$/;
			if (!re.test(form.domain.value)) {
				msg += "Valid Domain Name\n";
				err = true;
			}
			if (form.company_name.value == '') {
				msg += "Company Name\n";
				err = true;
			}
			if (form.first_name.value == '') {
				msg += "First Name\n";
				err = true;
			}
			if (form.last_name.value == '') {
				msg += "Last Name\n";
				err = true;
			}
			if (form.address.value == '') {
				msg += "Street Address\n";
				err = true;
			}
			if (form.city.value == '') {
				msg += "City\n";
				err = true;
			}
			if (form.state.value == '') {
				msg += "State\n";
				err = true;
			}
			if (form.zip_code.value == '') {
				msg += "Zip Code\n";
				err = true;
			}
			if (form.phone.value == '') {
				msg += "Phone Number\n";
				err = true;
			}
			re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/;
			if (!re.test(form.email.value)) {
				msg += "Valid Email Address\n";
				err = true;
			}
			if (form.howheard.value == '') {
				msg += "How You Heard About Us\n";
				err = true;
			}
			if (form.username.value == '') {
				msg += "Username\n";
				err = true;
			}
			if (form.password.value == '') {
				msg += "Password\n";
				err = true;
			}
			if (form.password.value != form.password_confirm.value) {
				msg += "Passwords do not match\n";
				err = true;
			}
			
			if (err) {
				alert(msg);
				this.processing = false;
				return false;
			}
			
			// submit the form
			form.submit();
		}
	}
};

/**
 * AQ Gallery
 */
AQ.Gallery = {
	transaction: null,
	overlay: null,
	loadGallery: function(start) {
		if (!YAHOO.util.Connect.isCallInProgress(this.transaction)) {
			var url = '/gallery/?s='+start;
			if (document.getElementById('gallery-color').value != '') {
				url = url + '&color='+document.getElementById('gallery-color').value;
			}
			if (document.getElementById('gallery-product').value != '') {
				url = url + '&product='+document.getElementById('gallery-product').value;
			}
			var callback = 
			{ 
				success:	this.gallerySuccess,
				failure:	this.galleryFailure,
				scope:		this,
				cache:		false
			}
			this.transaction = YAHOO.util.Connect.asyncRequest('GET', url, callback, null);
		}
	},
	gallerySuccess: function(o) {
		des = document.getElementById('aq_design_gallery');
		des.innerHTML = o.responseText;
	},
	galleryFailure: function(o) {
		// ! do something to respond to failure
	},
	loadDetail: function(url, el) {
		if (el) {AQ.Zoom.retract(el, 100);}
		if (this.overlay != null) {
			this.hideDetail();
		}
		this.overlay = new AQ.Overlay();
		this.overlay.loadGraphic = '/img/loading.gif';
		this.overlay.zIndex = 1;
		this.overlay.create();
		this.overlay.load(url, AQ.Gallery.detailSuccess, AQ.Gallery.detailFailure);
	},
	hideDetail: function() {
		this.overlay.remove();
		this.overlay = null;
	},
	detailSuccess: function(o) {
		AQ.Gallery.overlay.content.innerHTML = o.responseText;
	},
	detailFailure: function(o) {
		// !do something to respond to failure
	},
	selectDesign: function() {
		document.getElementById('signup-form').submit();
	},
	addFavorite: function(ref) {
		var data = ref.id.split('_');
		var id = data[1];
		var type = data[2];
		var el = document.getElementById('saved-favorites');
		if (el) {
			// add thumbnail
			var url = '/gallery-detail/?id='+id+'&t='+type;
			var html =
				'<img src="/img/designs/sm/d'+id+'-'+type+'.jpg" width="100" height="100"'+
				' onclick="AQ.Gallery.loadDetail(\''+url+'\')" id="gal-fav-'+id+'-'+type+'" />';
			
			el.innerHTML = el.innerHTML + html;
			
			// set cookie
			YAHOO.util.Cookie.set('fav_'+id+'_'+type, 'Y', {path: '/'});
			
			// update button
			AQ.CSS.removeClass(ref, 'add-fav');
			AQ.CSS.addClass(ref, 'remove-fav');
			ref.onclick = function() {AQ.Gallery.removeFavorite(this);};
		}
	},
	removeFavorite: function(ref) {
		var data = ref.id.split('_');
		var id = data[1];
		var type = data[2];
		var el = document.getElementById('saved-favorites');
		var fav = document.getElementById('gal-fav-'+id+'-'+type);
		if (el && fav) {
			// delete thumbnail
			el.removeChild(fav);
			
			// delete cookie
			YAHOO.util.Cookie.remove('fav_'+id+'_'+type, {path: '/'});
			
			// update button
			AQ.CSS.removeClass(ref, 'remove-fav');
			AQ.CSS.addClass(ref, 'add-fav');
			ref.onclick = function() {AQ.Gallery.addFavorite(this);};
		}
	}
};

/**
 * AQ Overlay
 */

AQ.Overlay = function() {
	this.overlay = null;
	this.overlayId = 'aq-overlay';
	this.wrapper = null;
	this.wrapperId = 'aq-overlay-content-wrap';
	this.wrapperClass = '';
	this.content = null;
	this.contentId = 'aq-overlay-content';
	this.zIndex = 5000;
	this.dim = true;
	this.shadow = false;
	this.shadowElement = null;
	this.shadowInner = null;
	this.loadGraphic = null;
	this.loadMessage = 'Loading...';
	this.scope = null;
	this.position = 'fixed';
	this.create = function() {
		if (this.overlay == null) {
			//AQ.Debug.output('Overlay.create fired');
			if (AQ.IE) { this.position = 'absolute'; }
			this.overlay = document.createElement('div');
			this.overlay.id					= this.overlayId;
			this.overlay.style.position 	= this.position;
	//		this.overlay.style.top 			= '0';
	//		this.overlay.style.bottom 		= '0';
	//		this.overlay.style.right 		= '0';
	//		this.overlay.style.left 		= '0';
	//		this.overlay.width				= '100%';
	//		this.overlay.height				= '100%';
			this.overlay.style.backgroundColor = '#444';
			this.overlay.style.zIndex		= this.zIndex;
			var opacity = (this.dim) ? 0.5 : 0;
			YAHOO.util.Dom.setStyle(this.overlay, 'opacity', opacity);
			document.body.appendChild(this.overlay); 
		} else {
			//AQ.Debug.output('Overlay.create says overlay already exists',1);
		}
	};
	this.load = function(url, successHandler, failureHandler) {
		if (this.overlay != null && this.wrapper == null) {
			if (AQ.IE) { AQ.IE.hideWindowed(); }// !buggy if more than one overlay is used on a page
			//AQ.Debug.output('Overlay.load fired');
			this.wrapper = document.createElement('div');
			// set required styles for the overlay to function
			this.wrapper.id = this.wrapperId;
			this.wrapper.className = this.wrapperClass;
			this.wrapper.style.position = this.position;
			this.wrapper.style.zIndex = this.zIndex + 1;
			document.body.appendChild(this.wrapper);
			if (this.shadow) { this.addShadow(); }
			this.content = document.createElement('div');
			this.content.id = this.contentId;
			this.content.innerHTML = this.showLoader();
			if (this.shadow) {
				this.shadowInner.appendChild(this.content);
			} else {
				this.wrapper.appendChild(this.content);
			}
			// send asynchronous request *** !optionally, we could accept optional content from the caller...instead of requiring an ajax call
			if (!YAHOO.util.Connect.isCallInProgress(this.transaction)) {
				//AQ.Debug.output('Overlay.load fired');
				if (this.scope) {
					var callback = 
					{
						success:	successHandler,
						failure:	failureHandler,
						scope:		this.scope,
						cache:		false
					}
				} else {
					var callback = 
					{
						success:	successHandler,
						failure:	failureHandler,
						scope:		this.scope,
						cache:		false
					}
				}
				//AQ.Debug.output('Sending asyncRequst to: '+url,1); 
				this.transaction = YAHOO.util.Connect.asyncRequest('GET', url, callback, null);
			} else {
				//AQ.Debug.output('Overlay.load is busy, please try again.',1);
			}
		} else {
			//AQ.Debug.output('Overlay.load says either overlay does not exist or wrapper already exists',1);
		}
	};
	this.showLoader = function() {
		var loader = '<div class="loader">';
		
		if (this.loadGraphic) {
			loader += '<img src="'+this.loadGraphic+'" alt="Loading..." />';
		}
		
		if (this.loadMessage) {
			loader += '<span>'+this.loadMessage+'</span>';
		}
		
		loader += '</div>';
		
		return loader;
	}
	this.remove = function() {
		// !need to check for altered content before blinding removing
		//AQ.Debug.output('Overlay.remove fired');
		if (this.overlay != null) {
			document.body.removeChild(this.overlay);
			this.overlay = null;
		}
		if (this.wrapper != null) {
			document.body.removeChild(this.wrapper);
			this.wrapper = null;
			this.content = null;
			this.shadowElement = null;
			this.shadowInner = null;
		}
		if (AQ.IE) { AQ.IE.showWindowed(); } // !buggy if more than one overlay is used
	};
	/**
	 * Adds a four-sided drop shadow around whatever is contained in the box
	 * - requires you to define the shadow images in your CSS files
	 */
	this.addShadow = function() {
		this.shadowElement = document.createElement('div');
		this.shadowElement.className = 'aq-overlay-sh1';
		this.shadowElement.style.position = 'absolute';
		var sh2 = document.createElement('div');
		sh2.className = 'aq-overlay-sh2';
		sh2.style.paddingTop = '50px';
		sh2.style.paddingLeft = '50px';
		this.shadowElement.appendChild(sh2);
		var sh3 = document.createElement('div');
		sh3.className = 'aq-overlay-sh3';
		sh2.appendChild(sh3);
		var sh4 = document.createElement('div');
		sh4.className = 'aq-overlay-sh4';
		sh4.style.position = 'relative';
		sh4.style.top = '-50px';
		sh4.style.left = '-50px';
		sh3.appendChild(sh4);
		this.shadowInner = document.createElement('div');
		this.shadowInner.className = 'aq-overlay-sh5';
		this.shadowInner.style.position = 'relative';
		this.shadowInner.style.top = '25px';
		this.shadowInner.style.left = '25px';
		sh4.appendChild(this.shadowInner);
		this.wrapper.appendChild(this.shadowElement);
	};
};

/**
 * AQ Zoom
 */
var into, outto;
AQ.Zoom = {
	el: null,
	startsize: 100,
	cursize: 0,
	targetsize: 0,
	startx: 0,
	starty: 0,
	start: function(el, targetsize) {
		if (this.el) {
			this.retract(this.el, 100);
		}
		this.el = el;
		this.cursize = this.startsize;
		this.targetsize = targetsize;
		this.startx = YAHOO.util.Dom.getX(this.el);
		this.starty = YAHOO.util.Dom.getY(this.el);
		//alert(this.startx+' - '+this.starty);
		this.zoomify();
	},
	zoomify: function() {
		var startleft = YAHOO.util.Dom.getX(this.el);
		var starttop = YAHOO.util.Dom.getY(this.el);
		newsize = this.cursize + 20;
		if (this.el && this.cursize < this.targetsize) {
			this.el.width = newsize;
			this.el.height = newsize;
			this.el.style.zIndex = "1000";
			var newleft = startleft - 10;
			var newtop = starttop - 10;
			this.el.style.left = newleft + "px";
			this.el.style.top = newtop + "px";
			this.cursize = newsize;
			if (this.cursize <= this.targetsize) {
				into = setTimeout('AQ.Zoom.zoomify()', 5);
			}
		}
	},
	retract: function(el, targetsize) {
		if (this.el) { clearTimeout(into); }
		var left = YAHOO.util.Dom.getX(el) + 100;
		var top = YAHOO.util.Dom.getY(el) + 100
		el.width = targetsize;
		el.height = targetsize;
		el.style.zIndex = "";
		//el.style.left = left + "px";
		//el.style.top = top + "px";
		//var xy = new Array();
		//xy[0] = this.startx;
		//xy[1] = this.starty;
		//YAHOO.util.Dom.setXY(this.el, xy);
		el.style.top = "";
		el.style.left = "";
		this.el = null;
		this.cursize = 0;
/**/	
		/* ZOOM OUT FUNCTIONALITY - NEEDS WORK
		    - very basic and breaks if you don't let image zoom all the 
			  way out before moving to the next image.
		
		var startleft = YAHOO.util.Dom.getX(this.el);
		var starttop = YAHOO.util.Dom.getY(this.el);
		newsize = this.cursize - 10
		if (this.el) {
			this.el.width = newsize;
			this.el.height = newsize;
			this.el.style.zIndex = "";
			var newleft = startleft + 5;
			var newtop = starttop + 5;
			this.el.style.left = newleft + "px";
			this.el.style.top = newtop + "px";
			this.cursize = newsize;
			if (this.cursize > this.startsize) {
				outto = setTimeout('AQ.Zoom.retract()', 10);
			} else {
				this.el = null;
				this.cursize = 0;
			}
		}/**/
	}
}

/*
EVENTS & THE ALGORITHM

onMouseOver:
- store which element we are zooming
- begin incremental zoom process
  - use setTimeout to recursively call the zoom process until we reach our target size

onMouseOut:
- unzoom the element
  - clearTimeout for the zoom process so it stops trying to zoom
  - recursively call unzoom process until we return to start size

Notes:
- need way of not canceling the zoom out while continuing to zoom in on the next item
  - in other words, all zoom outs need to be allowed to complete (no matter how many)
    imagine moving the mouse quickly over the items, there could easily be multiple zoom outs
	with a single item currently magnifying
*/
