deerfest.global.InitializeSearch = (function () {
	var label, input, notBlank;
	
	notBlank = /\S/;
	
	function onInputFocus ( _evt ) {
		label.hide();
	}
	function onInputBlur ( _evt ) {
		if ( notBlank.test( input.value ) ) { return; }
		input.value = '';
		label.show();
	}
	function testInputBlankness() { 
		if ( $F(input).blank() ) {
			onInputBlur();
		}
		else {
			onInputFocus();
		}
	}
	return function( _config ) {
		input = $(_config.input);
		label = $(_config.label);
		if ( ! ( input && label )  ) { return; }

		input.observe('focus', onInputFocus ); 
		input.observe('blur', onInputBlur );
		testInputBlankness();
		// IE7 populates this value between DOM and window load 
		Event.observe(window, 'load', testInputBlankness);
	};
})();
deerfest.global.prestitial			 = (function(){
	var __BODYELE			= 'com-deerfest';
	var __CONTENTWRAPPER	= 'com-deerfest-doc';
	var __PREELE			= 'com-deerfest-prestitial'
	var __PREHDELE			= 'com-deerfest-prestitial-header';
	var __TLIMIT			= 30000;
	var __CTIME				= 0;
	var __POLLINGRATE		= 1000;
	var __adIdLookup		= {
			'ebReportingFlash':'eyeDiv',
			'eyeDiv':'eyeDiv'
	}
	return {
		initialize: function(){
			console.log('deerfest.global.prestitial.initialize');
			if(document.loaded){
				try{$(__CONTENTWRAPPER).setStyle({'height':(document.viewport.getHeight()+'px')});}catch(e){}
				deerfest.global.prestitial.poll();
				deerfest.global.prestitial.background.poll();
				return
			}
			document.observe('prestitial:ready',function(){
				try{$(__CONTENTWRAPPER).setStyle({'height':(document.viewport.getHeight()+'px')});}catch(e){}
				deerfest.global.prestitial.poll();
				deerfest.global.prestitial.background.poll();
			});	
		},
		poll: function(){
			/* check to see if ad is still present */
			//console.log('deerfest.global.prestitial.poll: n_prestitialAdId = '+ n_prestitialAdId +'\n\r $(__BODYELE).hasClassName(\'prestitial-active\') = '+ $(__BODYELE).hasClassName('prestitial-active') +'\n\r$(__BODYELE).hasClassName(\'prestitial\') = '+ $(__BODYELE).hasClassName('prestitial'));
			if(typeof n_prestitialAdId === 'undefined'){ return false; }
			if(!($(__BODYELE).hasClassName('prestitial-active')) && !($(__BODYELE).hasClassName('prestitial'))){ return false /* there's no prestitial, do alittle dance */ }
			if(this.isDummy()){return false}
			if($(n_prestitialAdId)){
				/* code for checking whether or not the ad has finished displaying goes here */
				var eyefs		= this.getEyeFlash();
				if(eyefs){
					if(eyefs.style.top == '0px'){
						eyefs.style.top = ($(__PREHDELE).getHeight()+'px');
					}
					var stylesum	= parseInt(eyefs.style.width)+parseInt(eyefs.style.height);
					if(stylesum <= 0){
						this.remove()
					}
				}
			}
			/* rinse and repeat */
			setTimeout("deerfest.global.prestitial.poll()",__POLLINGRATE);
		},
		remove: function(){
			$(__BODYELE).removeClassName('prestitial-active');
			try{ document.getElementsByTagName('html')[0].style.overflow = 'scroll'; }catch(e){}
			console.log('removed prestitial-active class.');
		},
		close: function(){
			try{
				this.remove();
				try{ $(__CONTENTWRAPPER).setStyle({'height':'auto'}); }catch(e){};
				
				if($(__adIdLookup[n_prestitialAdId])){
					$(__adIdLookup[n_prestitialAdId]).remove();
					console.log('removed '+n_prestitialAdId+' element.');
				}
			}catch(e){}
		},
		isDummy: function(){
			if($(n_prestitialAdId)){
				//console.log('isDummy: ad id');
				if($(__adIdLookup[n_prestitialAdId])){
					//console.log('isDummy: ad container'+$(__adIdLookup[n_prestitialAdId]).innerHTML);
					if($(__adIdLookup[n_prestitialAdId]).innerHTML == ''){return true}
					//console.log('isDummy: ad container content:'+$(__adIdLookup[n_prestitialAdId]).innerHTML);
					return false
				}
				var __ad	= $(n_prestitialAdId);
				if(__ad.tagName.toLowerCase() == 'img'){return true}
			}
			return false
		},
		getEyeFlash: function(){
			if($(__adIdLookup[n_prestitialAdId])){
				var __adDiv		= $(__adIdLookup[n_prestitialAdId]);
				var __adObjects	= __adDiv.select('object');
				return __adObjects[1]
			}
			return false
		},
		background: {
			poll: function(){
				__CTIME				= __CTIME+__POLLINGRATE;
				if(typeof n_prestitialAdId === 'undefined'){ return false; }
				if($(n_prestitialAdId) || __CTIME > __TLIMIT){
					if(__CTIME > __TLIMIT){ console.log('deerfest.global.prestitial.background.poll: removing due to time limit expiration'); }
					this.remove();
				}else{
					setTimeout("deerfest.global.prestitial.background.poll()",__POLLINGRATE);
				}
			}, 
			remove: function(){
				$(__BODYELE).removeClassName('prestitial');
				if($(n_prestitialAdId) && !deerfest.global.prestitial.isDummy()){ $(__BODYELE).addClassName('prestitial-active'); }
				if(!deerfest.global.prestitial.isDummy()){
					try{ 
						$(__CONTENTWRAPPER).setStyle({'height':'auto'}); 
					}catch(e){}
				}else{
					/* remove overflow = hidden that some bad ad programmer monkeys put in *faceplant* */
					try{ document.getElementsByTagName('html')[0].style.overflow = 'scroll'; }catch(e){}
				}
				console.log('removed prestitial class.');
			}
		}
	}
})();

deerfest.global.Navigation = (function () {
	var ID = {
			CONTAINER: 'alt-nv-main',
			SCORESTRIP: 'hd-scorestrip'
		},
		ATTR = { ORIGINAL_HEIGHT: 'data-oheight' },
		CSS = {
			CONTENT: 'div.channel-content',
			GROUP: 'li.alt-nv-group',
			SHADOW: 'shadow',
			PREPARED: 'prepared'
		},
		CLOSE_HEIGHT = { height: '0px' },
		DURATION = 0.5,
	    sexy = false,
	    ss,
	    timeout,
	    mousedOver,
	    TO_DURATION = 750;
	
	function prepareChannel ( _el ) {
		var group, iframe, newStyle, oHeight, div;

		newStyle  = { display: 'none' };
		
		group    = _el.up( 'li' );
		group.channels = _el;

		oHeight = _el.offsetHeight - parseFloat( _el.getStyle('paddingBottom') ) - parseFloat( _el.getStyle('paddingTop') );
		_el.setAttribute( ATTR.ORIGINAL_HEIGHT, oHeight + 'px' );
		_el.addClassName( CSS.PREPARED );
		
		// Fixes select see-through in IE6
		if ( Prototype.Browser.IE ) {
			iframe    = document.createElement( 'iframe' );
			iframe.id = "DivShim";
			iframe.setAttribute("scrolling","no");
			iframe.setAttribute("frameborder","0");
			_el.insertBefore( iframe, _el.firstChild );
		}
		
		div = document.createElement('div');
		div.className = CSS.SHADOW;
		_el.appendChild( div );
		
		if ( sexy ) {
			newStyle.backgroundPosition = '0 ' + oHeight + 'px';
			newStyle.height = '0px';
		}
		_el.setStyle( newStyle );
	}
	
	function beforeOpenStart(_effect) {
		_effect.element.show().setStyle({ zIndex: 1000 });
	}
	function afterOpenFinish(_effect) {
		_effect.element.setStyle({ zIndex: 1000 }).transitionEffect = null;
	}
	function afterCloseFinish(_effect) {
		_effect.element.hide().transitionEffect = null;
	}
	
	function toggleChannels( _channels, _open) {
		// if we're sexy, do a slide
		if ( sexy ) {
			// cancel any current effects on this element
			if ( _channels.transitionEffect ) {
				_channels.transitionEffect.cancel();
			}
			// create an effect with different options based on whether or not we're opening
			_channels.transitionEffect = new Effect.Morph( _channels, _open ?
				{ style: { height: _channels.getAttribute( ATTR.ORIGINAL_HEIGHT ) }, duration: DURATION,
				  beforeStart: beforeOpenStart, afterFinish: afterOpenFinish } :
				{ style: CLOSE_HEIGHT, duration: DURATION, afterFinish: afterCloseFinish }
			);
			return;
		}
		// otherwise just show or hide based on the direction
		_channels[ _open ? 'show' : 'hide' ]();
	}
	
	function onMouseOver ( _e ) {
		var group, from, channels;
		_e.stop();

		// figure out your context
		group    = _e.findElement( CSS.GROUP );
		if ( ! group ) { return; }
		channels = group.channels;
		from     = $( _e.relatedTarget || _e.fromElement );
		
		// if you're not over the correct element, exit
		if ( (! channels) || (! from) || ( from && from.descendantOf && from.descendantOf( group ) ) ) { return; }
		
		// if we come from the scorestrip, hold off on sliding out
		if ( ss && ( ss === from || ( from.descendantOf && from.descendantOf(ss) )) ) {
			mousedOver = true;
			// if we're locked, cancel the old timeout
			if ( channels.timeout ) { clearTimeout(channels.timeout); }
			// set a timeout to slide the current channel out after a decent wait
			channels.timeout = setTimeout(function() {
				// clear the lock
				channels.timeout = null;
				// if we're still moused over, exit
				if ( ! mousedOver ) { return; }
				// toggle the channels open
				toggleChannels( channels, true );
			}, TO_DURATION);
			return;
		}
		// otherwise toggle the channels open
		toggleChannels( channels, true );
	}
	function onMouseOut ( _e ) {
		var group, to, channels;

		// stop the event
		_e.stop();

		// figure out your context
		group    = _e.findElement( CSS.GROUP );
		if ( ! group ) { return; }
		channels = group.channels;
		to       = $( _e.relatedTarget || _e.toElement );

		// if you're not over the correct element, exit
		if ( (! channels) || ( to  && ( to === group || ( to.descendantOf && to.descendantOf( group ) ) ) ) ) { return }

		// set a flag alerting that we've moused out, in case someone is waiting
		mousedOver = false;

		// if we're locked, don't slide out
		if (channels.timeout) { return;}
		
		// toggle the channels open
		toggleChannels( channels, false );
	}
	
	function removeReferences(group) {
		if ( ! group.channels ) { return; }
		delete group.channels.transitionEffect;
		delete group.channels.timeout;
		delete group.channels;
	}
	
	function cleanUp(e) {
		$(ID.CONTAINER).
			stopObserving('mouseover', onMouseOver).
			stopObserving('mouseout', onMouseOut);
		try {
			$$(CSS.GROUP).each(removeReferences);
		} catch (e) {}
		ss = null;
	}

	return {
		initialize: function() {
			var el = $(ID.CONTAINER);
			if ( ! el ) { return; }

			// If we have scriptaculous, go the extra mile
			if ( typeof Effect !== "undefined" ) { sexy = true; }
			
			el.
				observe('mouseover', onMouseOver ).
				observe('mouseout', onMouseOut ).
				select( CSS.CONTENT ).
					each( prepareChannel );
			Event.observe(window, 'unload', cleanUp);
			document.observe('dom:loaded', function() { ss = $(ID.SCORESTRIP); });
		},
		setAnimation: function( _value ) {
			if ( sexy === _value ) { return; }
			sexy = _value;
			$$('#' + ID.CONTAINER + ' ' + CSS.CONTENT).each(
				_value ? function( _el ) {
					_el.setStyle({ backgroundPosition: '0 ' + _el.getAttribute( ATTR.ORIGINAL_HEIGHT ) + 'px', height: '0px', paddingBottom: null });
					_el.addClassName( CSS.PREPARED );
				} : function( _el ) {
					_el.setStyle({ backgroundPosition: 'left top', height: 'auto', paddingBottom: '0px' });
					_el.removeClassName( CSS.PREPARED );
				});
		}
	};
})();
