
if(!org) var org={};
if(!org.cleanriversandstreams) org.cleanriversandstreams={};
if(!org.cleanriversandstreams.www) org.cleanriversandstreams.www={};

org.cleanriversandstreams.www = {

	staging: false,
	
	trace: false,
	
	effectLength: 1000,
	
	section: null,
	featureCount: 0,
	
	siteTitle: "",

	effectInProgress: false,
	
	flashLoaded: false,

	flashRunningCount: 0,
	flashRunning: true,
	
	flashEnabled: true,
	flashHiding: false,
	
	easing: "jswing",
	
	onLoad: function() {
		var self = org.cleanriversandstreams.www;

		//
		// Get number of features.
		//
		self.featureCount = $(".feature").length;
		
		//
		// Enable console (if tracing).
		//
		if (self.trace) {
			var console = $("#console");
			console.css({
				//opacity: 0.8,
				display: "block"
			});
		}
		
		self.traceWrite('org.cleanriversandstreams.www.onLoad()');

		//
		// Get original site title.
		//
		self.siteTitle = document.title;

		//
		// Link up menus
		//
		self.linkUpMenu("menu");
		self.linkUpMenu("footer-menu");

		//
		// Create "close" button.
		//
		$("#closer").css("visibility", "hidden");
		$("#closer .hotspot").text("Close").click(org.cleanriversandstreams.www.onCloserClick);
		
		self.traceWrite('Loading Flash...');

		self.loadFlash();
		
		//
		// Show section?
		//
		if (location.hash) {
			self.showSection(location.hash.replace("#", ""));
		}
		
		/*
		if (self.flashEnabled) {
			self.enableFlash();
		} else {
			self.disableFlash();
		}
		*/
		
		/*
		$("#bottom").append('<button class="flash-toggle"></button>');
		$("#bottom").append("<button></button>");
		$("#bottom").append('Effect Length: <select onchange="org.cleanriversandstreams.www.setEffectLength()"><option>2000</option><option>3000</option><option>5000</option><option>10000</option></select>');

		$("#bottom button").eq(0).text("Toggle Flash").toggle(org.cleanriversandstreams.www.disableFlash, org.cleanriversandstreams.www.enableFlash);
		$("#bottom button").eq(1).text("Toggle Flash Hiding").click(function(){org.cleanriversandstreams.www.flashHiding = !org.cleanriversandstreams.www.flashHiding;});
		*/
	},

	setEffectLength: function() {
		var option = $("#bottom select option:selected")[0];
		var value = $(option).text();
		org.cleanriversandstreams.www.effectLength = parseInt(value);
	},
	
	enableFlash: function() {
		org.cleanriversandstreams.www.flashEnabled = true;
		org.cleanriversandstreams.www.showFlash();
//		$("#bottom .flash-toggle").text("Disable Flash").click(org.cleanriversandstreams.www.disableFlash);
	},
	
	disableFlash: function() {
		org.cleanriversandstreams.www.flashEnabled = false;
		org.cleanriversandstreams.www.hideFlash();
//		$("#bottom .flash-toggle").text("Enable Flash").click(org.cleanriversandstreams.www.enableFlash);
	},
	
	hideFlash: function() {
		$("#flash *").css({visibility: "hidden"});
		$("#flash-fader").css({visibility: "visible"});
	},
	
	showFlash: function() {
		$("#flash *").css({visibility: "visible"});
		$("#flash-fader").css({visibility: "hidden"});
	},

	concealFlash: function() {
		$("#flash-fader").css({
			display: "block",
			visibility: "visible",
			opacity: 0
		}).animate({
			opacity: 1
		},{
			duration: 500,
			complete: function() {
				org.cleanriversandstreams.www.hideFlash();
			}
		});
	},
	
	revealFlash: function() {
//		alert('reveal');
		org.cleanriversandstreams.www.showFlash();
		$("#flash-fader").animate({
			opacity: 0
		},{
			duration: 500,
			complete: function() {
				$("#flash-fader").css({
					display: "none",
					visibility: "hidden",
					opacity: 0
				});
			}
		});
	},

	loadFlash: function() {
		if (org.cleanriversandstreams.www.staging) {
			if (AC_FL_RunContent == 0) {
				alert("This page requires AC_RunActiveContent.js.");
			} else {
				AC_FL_RunContent(
					'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0',
					'width', '980',
					'height', '520',
					'src', 'cr_flash',
					'quality', 'high',
					'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
					'align', 'middle',
					'play', 'true',
					'loop', 'true',
					'scale', 'showall',
					'wmode', 'transparent',
					'devicefont', 'false',
					'id', 'cr_flash',
					'bgcolor', '#336699',
					'name', 'cr_flash',
					'menu', 'true',
					'allowFullScreen', 'false',
					'allowScriptAccess','sameDomain',
					'movie', 'cr_flash',
					'salign', ''
				); //end AC code
			}
		} else {
			var so = new SWFObject("CleanRivers.swf", "CleanRivers", "980", "520", "8", "#A8D7DD");   
			so.useExpressInstall('scripts/expressinstall.swf');
			so.addParam('menu', 'false');
			so.addParam('wmode', 'transparent');
			so.addParam('scale', 'noscale');
			so.write('flash');       
		}
	},
	
	linkUpMenu: function(id) {
		$("#" + id).find("a").click(org.cleanriversandstreams.www.onMenuClick);
	},

	onMenuClick: function(ev) {	
		var self = org.cleanriversandstreams.www;

		if (self.effectInProgress) {
			return;
		}
		
		ev = ev ? ev : (window.event ? window.event : null);

		//
		// Get target object and ID.
		//
		var target = ev.srcElement ? ev.srcElement : ev.target;			
		var id     = target.hash.replace("#", "");


		self.traceWrite("Clicked " + id);
		
		//
		// Show section.
		//
		if (self.section == id) {
			if (self.flashRunning) {
				self.hideSection(id);
			} else {
				self.concealSection(id);
			}
		} else {
			if (self.flashRunning) {
				self.showSection(id);
			} else {
				self.revealSection(id);
			}
		}
	},
	
	onCloserClick: function(ev) {
		var self = org.cleanriversandstreams.www;
		
		self.traceWrite("Closer Click");
		
		self.concealSection();
	},

	setTitle: function(s) {
		document.title = this.siteTitle + " - " + s;
	},

	clearTitle: function() {
		document.title = this.siteTitle;
	},

	jumpToBottom: function() {
		var body      = $(document.body);
		var winHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight;

		var pos = body.get(0).clientHeight - winHeight;

		body.get(0).scrollTop = pos;
		document.documentElement.scrollTop = pos;
	},

	showSection: function(id) {
		var self = org.cleanriversandstreams.www;
	
		var section = $("#" + id);
		var player  = $("#" + id + "-video");

		//
		// Stop any running effects.
		//
		$("#sections").stop();

		//
		// Hide all sections.
		//
		$(".section").hide();

		//
		// Deselect all menu items.
		//
		self.deselectMenuItems();
		
		//
		// Show closer.
		//
		$("#closer").css({
			visibility: "visible",
			opacity: 1
		});
		
		self.selectMenuItem(id);

		//
		// Hide video player (if it exists).
		//
		player.css("visibility", "visible");

		//
		// Make section visible.
		//
		section.css({
			visibility: "visible",
			display: "block",
			opacity: 1
		});
		
		self.jumpToBottom();

		//
		// Set window title from section.
		//
		var sectionTitle = section.find("h2").text();
		self.setTitle(sectionTitle);

		//
		// Reveal section.
		//
		$("#sections").css({
			height: section.get(0).clientHeight
		});

		//
		// Store section ID.
		//
		self.section = id;

		self.endEffect(true);
	},
	
	hideSection: function() {
		var self = org.cleanriversandstreams.www;
		
		//
		// Get current section ID.
		//
		var id = self.section;

		//
		// Reset document title.
		//
		self.clearTitle();

		//
		// Stop any running effects.
		//
		$("#sections").stop();

		//
		// Hide closer.
		//
		$("#closer").css({
			opacity: 0.0,
			visibility: "hidden"
		});

		//
		// Hide video player.
		//
		$("#" + id + "-video").css("visibility", "hidden");

		//
		// Hide content section.
		//
		$("#" + id).css("visibility", "hidden");
		
		//
		// Close up area.
		//
		$("#sections").css({
			height: "0px"
		});

		self.deselectMenuItem(id);
		self.section = null;
	},

	disableMenu: function() {
		$(".menu-item:not(.menu-item-selected)").addClass("menu-item-disabled");
	},
	
	enableMenu: function() {
		$(".menu-item:not(.menu-item-selected)").removeClass("menu-item-disabled");
	},
	
	startEffect: function() {

		this.effectInProgress = true;
		
		if (this.flashHiding && this.flashEnabled) {
			this.hideFlash();
		}
		
		this.disableMenu();
	},
	
	endEffect: function(quick) {

		this.enableMenu();
		
		if (this.flashHiding && this.flashEnabled) {
			this.showFlash();
		}

		this.effectInProgress = false;		
	},
	
	//
	// revealSection() : Show section using animation.
	// 
	revealSection: function(id) {
		var self = org.cleanriversandstreams.www;

		var section = $("#" + id);
		var player  = $("#" + id + "-video");

		self.deselectMenuItems();
		self.selectMenuItem(id);

		self.startEffect();
		
		setTimeout("org.cleanriversandstreams.www.showSection('" + id + "')", self.effectLength);
		
		//
		// If a section is visible, hide it.
		//
		if (self.section) {

			self.clearTitle();

			//
			// Hide content section.
			//
			section.fadeOut(self.effectLength, function() {
				$("#" + org.cleanriversandstreams.www.section).css({
					visibility: "hidden"
				});
			});

			self.deselectMenuItem(self.section);

			$("#" + self.section + "-video").css({
				visibility:"hidden"
			});

		} else {

			//
			// Reveal closer.
			//
			$("#closer").css({
				visibility: "visible",
				opacity: 0
			}).animate({
				opacity: 1
			}, {
				duration: self.effectLength
			});

		}

		//
		// Hide video player if it exists.
		//
		player.css("visibility", "hidden");

		//
		// Make section visible.
		//
		section.css({
			visibility: "visible",
			display: "block",
			opacity: 0
		});
		section.animate({
			opacity: 1
		}, {
			duration: self.effectLength
		});
		
		//
		// Set window title from section.
		//
//		var sectionTitle = section.get(0).getElementsByTagName("h2")[0].innerHTML;
		var sectionTitle = section.find("h2").text();
		self.setTitle(sectionTitle);

		//
		// Reveal section.
		//
		$("#sections").animate({
			height: section.get(0).clientHeight
		}, {
			duration: self.effectLength, 
			easing: self.easing,
			complete: function(){
		
				org.cleanriversandstreams.www.jumpToBottom();

				//
				// Show video player if it exists.
				//
				player.css("visibility", "visible");

				org.cleanriversandstreams.www.endEffect(false);
			},
			step: function(){
//				document.title += "!"; // testing
				org.cleanriversandstreams.www.jumpToBottom();
			}
		});

		//
		// Store section ID.
		//
		self.section = id;
	},

	concealSection: function() {
		var self = org.cleanriversandstreams.www;
		
		if (self.flashRunning) {
			self.hideSection();
			return;
		}
		
		//
		// Get current section ID.
		//
		var id = self.section;

		//
		// Reset document title.
		//
		self.clearTitle();

		self.startEffect();

		//
		// Hide closer.
		//
		$("#closer").animate({
			opacity: 0.0
		}, {
			duration: self.effectLength,
			complete: function() {
				$("#closer").css({
					visibility: "hidden"
				});
			}
		});

		//
		// Hide video player.
		//
		$("#" + id + "-video").css("visibility", "hidden");

		//
		// Hide content section.
		//
		$("#" + id).fadeOut(self.effectLength, function() {
			var id = org.cleanriversandstreams.www.section;
			$("#" + id).css("visibility", "hidden");		
		});
		
		//
		// Close up area.
		//
		$("#sections").animate({
			height: "0px"
		}, {
			duration: self.effectLength,
			easing: self.easing,
			complete: function(){
				var id = org.cleanriversandstreams.www.section;
				org.cleanriversandstreams.www.deselectMenuItem(id);
				org.cleanriversandstreams.www.section = null;

				org.cleanriversandstreams.www.endEffect(false);
			}
		});
	},

	selectMenuItem: function(id) {
		$("#" + id + "-menu-item").addClass("menu-item-selected");
	},

	deselectMenuItem: function(id) {
		$("#" + id + "-menu-item").removeClass("menu-item-selected");
	},
	
	deselectMenuItems: function() {
		$(".menu-item").removeClass("menu-item-selected");
	},
	
	onFlashLoaded: function() {
		this.traceWrite('org.cleanriversandstreams.www.onFlashLoaded()');
		
		this.flashLoaded = true;		
		if (!($.browser.msie && parseInt($.browser.version.match(/^(\d+)\./)[1]) < 7)) {
			$("#flash").addClass("loaded").ifixpng();
		}
		
		this.traceWrite('Added drop shadow.');
	},

	onFlashRunning: function() {
		this.traceWrite('org.cleanriversandstreams.www.onFlashRunning()');
		
		this.traceWrite("RUNNING");
		this.flashRunning = true;
		this.flashRunningCount = this.featureCount;
		
//		this.disableMenu();
//		this.traceWrite('Disabled menu.');
	},

	onFlashStopped: function() {
		this.traceWrite('org.cleanriversandstreams.www.onFlashStopped()');

		this.flashRunningCount--;
		this.traceWrite("Running Count: " + this.flashRunningCount);

		if (this.flashRunningCount == 0) {
			this.traceWrite("STOPPED");
			this.flashRunning = false;
		}
		
//		this.enableMenu();
//		this.traceWrite('Enabled menu.');
	},
	
	traceWrite: function(msg) {
		var self = org.cleanriversandstreams.www;
		if (self.trace) {
			var console = $("#console");
			console.get(0).innerHTML += msg + "<br />";
//			console.html(console.html() + msg + "<br/>");
			console.scrollTo({top:"+=200px"}, 0);
		}
	}

}

function org_cleanriversandstreams_www_onFlashLoaded() {
	org.cleanriversandstreams.www.traceWrite("org_cleanriversandstreams_www_onFlashLoaded()");
	org.cleanriversandstreams.www.onFlashLoaded();
}
function org_cleanriversandstreams_www_onFlashRunning() {
	org.cleanriversandstreams.www.traceWrite("org_cleanriversandstreams_www_onFlashRunning()");
	org.cleanriversandstreams.www.onFlashRunning();
}
function org_cleanriversandstreams_www_onFlashStopped() {
	org.cleanriversandstreams.www.traceWrite("org_cleanriversandstreams_www_onFlashStopped()");
	org.cleanriversandstreams.www.onFlashStopped();
}

$(document).ready(org.cleanriversandstreams.www.onLoad);