window.addEvent('domready', initwindow);
window.addEvent('load', function() { resize_contentarea(); copy_footer(); });
window.addEvent('resize', function() { resize_contentarea(); });


function initwindow(context) {
	switch (context) {
		case 'metanarrative':
		case 'solotext':
		case 'stafflist':
		case 'regulartext':
		case 'listingtext':
		case 'closesolotext':
			$ES('a', $('contentcontainer')).addEvents({
				'click': function() {
					switch (this.getAttribute('rel')) {
						case 'external':
							this.setAttribute('target', '_blank');
							return true;
					}
				}
			});
			
			$ES('.tertiarynav li a', $('contentcontainer')).forEach(function(el) {
				el.onclick = function() { 
					switch (this.getAttribute('rel')) {
						case 'external':
							this.setAttribute('target', '_blank');
							return true;
						case 'internal':
							if (this.getAttribute('href').indexOf('#') == -1) {
								return true;
							}
						case 'regulartext':
						default:
							load_regulartext(this);
							return false;
					}
					return false;
				}
			});
			
			$ES('div#tertiarynavcontainer', $('contentcontainer')).addEvents({
				'click': function() {
					hide_photo();
				}
			});
			
			$ES('.quaternarynav li a', $('contentcontainer')).forEach(function(el) {
				el.onclick = function() {
					switch (this.getAttribute('rel')) {
						case 'external':
							this.setAttribute('target', '_blank');
							return true;
						case 'internal':
						default:
							if (this.getAttribute('href').indexOf('#') == -1) {
								return true;
							}
					}
					return false;
				}
			});

			$ES('.textpagecontent a', $('contentcontainer')).forEach(function(el) {
				el.onclick = function() {
					switch (this.getAttribute('rel')) {
						case 'external':
							this.setAttribute('target', '_blank');
							return true;
						default:
							if (this.getAttribute('href').indexOf('#') == -1) {
								return true;
							} else {
								var anch = new String(this.getAttribute('href').substring(this.getAttribute('href').lastIndexOf('#') + 1));
								var aa = $ES('a', $('contentcontainer'));
								for (var a = 0; a < aa.length; a++) {
									if (aa[a].getAttribute('name') == anch) {
										scrollTo(0, aa[a].getPosition().y);
										break;
									}
								}
								return false;
							}
					}
					return false;
				}
			});

			$ES('.stafflist a', $('contentcontainer')).forEach(function(el) {
				el.onclick = function() {
					switch (this.getAttribute('rel')) {
						case 'stafflisting':
							load_listingtext(this);
							return false;
						default:
							if (this.getAttribute('href').indexOf('#') == -1) {
								return true;
							} else {
								var anch = new String(this.getAttribute('href').substring(this.getAttribute('href').lastIndexOf('#') + 1));
								var aa = $ES('a', $('contentcontainer'));
								for (var a = 0; a < aa.length; a++) {
									if (aa[a].getAttribute('name') == anch) {
										scrollTo(0, aa[a].getPosition().y);
										break;
									}
								}
								return false;
							}
					}
				}
			});

			$ES('div.action_close', $('contentcontainer')).forEach(function(el) {
				el.style.background = 'url(../images/common/icon_close.gif) center top no-repeat';
				el.onmouseover = function() {
					this.style.cursor = 'pointer';
				}
				el.onclick = function() {
					close_textpage();
					return false;
				}
			});
	
			$ES('div.action_print', $('contentcontainer')).forEach(function(el) {
				el.style.background = 'url(../images/common/icon_print.gif) center top no-repeat';
				el.onmouseover = function() {
					this.style.cursor = 'pointer';
				}
				el.onclick = function() {
					print_textpage();
					return false;
				}
			});
	
			$ES('.solotext div.action_close', $('contentcontainer')).forEach(function(el) {
				el.onclick = function() {
					close_solotext();
					return false;
				}
			});

			$ES('div#sidebarcontainer').addEvents({
				'click': function() {
					hide_photo();
				}
			});

			$ES('div#metanarrativecontainer').addEvents({
				'click': function() {
					hide_photo();
				}
			});

			$ES('div#rightcontainer').addEvents({
				'click': function() {
					hide_photo();
				}
			});

			$ES('div#landingtextcontainer').addEvents({
				'click': function() {
					hide_photo();
				}
			});

			$ES('div#textpagecontainer').addEvents({
				'click': function() {
					hide_photo();
				}
			});
			
			break;
		
		default:
			$ES('a').addEvents({
				'click': function() {
					switch (this.getAttribute('rel')) {
						case 'external':
							this.setAttribute('target', '_blank');
							return true;
					}
				}
			});
			
			$ES('.primarynav .active a').addEvents({
				'click': function() {
					document.location = this.getAttribute('href');
				}
			});
		
			$ES('.secondarynav li a').forEach(function(el) {
				el.onclick = function() { 
					switch (el.getAttribute('rel')) {
						case 'metanarrative':
							load_metanarrative(el);
							break;
						case 'stafflist':
							load_stafflist(el);
							break;
						case 'solotext':
							load_solotext(el);
							break;
						case 'external':
							el.setAttribute('target', '_blank');
							return true;
						case 'internal':
						default:
							if (el.getAttribute('href').indexOf('#') == -1) {
								return true;
							}
						
					}
					return false;
				}
			});

			$ES('div#mainphotocontainer').addEvents({
				'mouseover': function() {
					this.style.cursor = 'pointer';
				},
				'click': function() {
					activate_photo(this);
				}
			});
			
			$ES('div#metanarrativecontainer').addEvents({
				'click': function() {
					hide_photo();
				}
			});

			$ES('div#rightcontainer').addEvents({
				'click': function() {
					hide_photo();
				}
			});

			$ES('div#landingtextcontainer').addEvents({
				'click': function() {
					hide_photo();
				}
			});

			$ES('div#textpagecontainer').addEvents({
				'click': function() {
					hide_photo();
				}
			});
			
			$ES('#mainphotocontainer img').addEvents({
				'mouseover': function() {
					if (document.all) {
						this.setAttribute('GALLERYIMG','false');
					}
				}
			});

			break;
	}
	
	resize_contentarea();
}