// Google id
_uacct = "UA-1764356-1";

// Site object
SITE	= {
		isMorphing	: false,
		pageCache	: {
						// TODO: Make LRU?  OR just fixed size?
						cache	: {},
						store	: function (key, value) {
							this.cache[key]	= value;
						},
						get		: function (key) {
							return this.cache[key];
						},
						expire	: function (key) {
							if (key) {
								this.store(key, null);
								delete store.key;
							} else {
								this.cache	= {};
							}
						}
		},
		http			: new Request.HTML({link:'cancel'}),
		lastHash		: null,
		isDrawing		: false,
		lastSectionBtn	: null,
		historyHelper	: setInterval(function () {
			//alert ([location.hash, SITE.lastHash, SITE.isDrawing])
			if (SITE.isDrawing) return;
			
			if (!window.location.hash && SITE.lastHash != null) {
				location.reload();
			} else if (window.location.hash.toString().substring(1) != SITE.lastHash) {
				handleNav(window.location.hash.toString().substring(1));
			}
		},500)
};

var paramDelimiter	= ";";

// Handle nav requests.
function handleNav(type) {
	SITE.isDrawing	= true;
	var hash	= location.hash.toString();
	var isHome	= !hash || hash.length < 2;
	
	//alert ([type, "isHome", isHome]);

	if (isHome && type || type && $('sub_page').offsetWidth == 0) {
		morphSite();
	}
	
	if (!type && !isHome) {
		type	= hash.substring(1);
		handleNav(type);
		morphSite();
	} else {
		var dataUrl;
		var args;
		try {
			args	= type.split(paramDelimiter);
		} catch (e) {
			//oops
			args	= [];
		}
		if (SITE.lastSectionBtn) {
			rollover(0,SITE.lastSectionBtn);
			SITE.lastSectionBtn = null;
		}
		//alert (args);
		var tmp	= "#" + args.join(paramDelimiter);
		if (Browser.Engine.trident && tmp && tmp != "#") {
			//alert ("::" +tmp+"::")
			location.href	= tmp;
		}
		switch (args[0]) {
			case "people":
			case "theatre":
				
				dataUrl	= "/cgi-bin/galleryBuilder_beta.pl?gallery="+args[0];
				
				if (args.length == 1 || (args[1] == args[2])) {
					dataUrl	+= "&sz=thumb";
				} else {
					dataUrl	+= "&v="+args[1]+"&p="+args[2];
				}
				if (args[0] == "people") {
					SITE.lastSectionBtn	= $('navbtn1');
				} else {
					SITE.lastSectionBtn	= $('navbtn2');
				}
			break;
			case "blog":
				dataUrl	= "/cgi-bin/redBlogReader_beta.pl";
				if (args.length != 1 && (args[1] != args[2])) {
					// action, section, article
					dataUrl	+= "?action="+args[1]+"&section="+args[2]+"&article="+args[3];
				}
				SITE.lastSectionBtn	= $('navbtn3');
			break;
			case "pricing":
				dataUrl	= "/pricing.html";
			break;
			case "contact":
			case "about":
				dataUrl	= "/contact.html";
			break;
		}
		
		//alert (dataUrl);
		if (dataUrl) {
			if (!SITE.pageCache.get(dataUrl)) {
				
				$('sub_page').set('text','');
				if (!SITE.isMorphing) {
					$('sub_page').setStyles({
						backgroundImage	: 'url(images/ajax-loader.gif)',
						backgroundPosition: 'center center',
						backgroundRepeat	: 'no-repeat'
					});
				}
				
				setTimeout (function () {
					// Set up handler
					SITE.http.onSuccess	= function (tree, elements, html, script) {
						$('sub_page').style.backgroundImage	= "";
						$('sub_page').set('text','');
						$('sub_page').adopt(tree);
						SITE.pageCache.store(dataUrl, html);
						processATags(elements);
					};
					// Fire async request
					SITE.http.send({
						method : 'get',
						url		: dataUrl
					});
				}, 500);
			} else {
				//alert (SITE.pageCache[dataUrl]);
				$('sub_page').innerHTML	= SITE.pageCache.get(dataUrl);
			}
			
			SITE.lastHash	= args.join(paramDelimiter);
		
		} else {
			SITE.lastHash	= null;
		}
		if (SITE.lastSectionBtn) {
			rollover(1,SITE.lastSectionBtn);
		}
		
		if (isHome) {
			$('footer').className	= "footer footerBg_home";
		} else {
			$('footer').className	= "footer footerBg_sub";
		}
		
		// Set up history helper tags.
		processATags($$('a'));
		
		SITE.isDrawing	= false;
		
		// Google
		if (location.toString().indexOf("/beta") == -1) {
			if (SITE.lastHash) {
				urchinTracker(SITE.lastHash);
			} else {
				urchinTracker("home");
			}
		}
	}
}

function morphSite() {
	/**
	 * elementId : chain
	 */
	SITE.isMorphing	= true;
	
	var morphRules	= {
			top_row	: false,
			top_row_left	: false,
			top_row_right	: false,
			main_navTD : true,
			home_logo	: false,
			nav_container	: false,
			nav_container_shadow	: false,
			sub_page	: false,
			navbtn1	: false,
			navbtn2	: false,
			navbtn3	: false,
			tagline		: false
	};
	
	for (el in morphRules) {
		var morph	= null;
		
		if (!morph) {
			morph	= new Fx.Morph(el,{duration:'long',transition:Fx.Transitions.Sine.easeOut});
		}
		
		// apply effect
		_applyMorph(morph, el);
	}
	
	SITE.isMorphing	= false;
}

function _applyMorph(morph, el) {
	setTimeout(function () {
		
		if (el.indexOf("navbtn") > -1) {
			$(el).style.position	 	= "relative";
			$(el).style.backgroundImage	= "url(images/"+el+".png)";
		}
		if (el == "top_row") {
			$(el).style.display	= "";
		}
		if (el == "sub_page") {
			$(el).style.display = "block";
		}
		if (el == "home_logo" && Browser.Engine.trident) {
			$(el).style.display	= "none";
		}
		if (el == "nav_container") {
			$(el).style.backgroundImage	= "url(images/sub_nav_bg.png)";
		}
		if (el.indexOf("top_row_") > -1) {
			$(el).style.display	= "block";
		} else {
			morph.start("."+el+"_sub");
		}
		
		
	}, 100);
}

function rollover(action, img) {
	if (img.tagName == "DIV" && location.hash.toString().length > 1) {
		var h	= parseInt(img.offsetHeight);
		
		if (action || isSection(img.id)) {
			img.style.backgroundPosition	= "0 " + "" + (-80);
		} else {
			img.style.backgroundPosition	= "0 0";
		}
	}
}

function processATags(a) {
	// Set up history helper tags.
	var hash, tmp;
	for (var i=0;i<a.length;i++) {
		hash	= null;
		if (a.get('href')) {
			tmp		= a[i].toString();
			if (tmp.indexOf ("#") > -1) {
				updateHistoryHelper(tmp.substring(tmp.indexOf ("#")));
			}
		}
	}
}

function updateHistoryHelper(hash) {
	if (!$(hash)) {
		var helper = new Element( "a" , { name:hash, id:hash  }) ;
		helper.injectBefore($('main_canvas'));
	}
}

function isSection(elId) {
	switch (elId) {
		case "navbtn1":
			return location.hash.toString() == "#people";
		break;
		case "navbtn2":
			return location.hash.toString() == "#theatre";
		break;
		case "navbtn3":
			return location.hash.toString() == "#blog";
		break;
	}
	
	return false;
}
