/*
* Javascript implementations for RTRfm website
* (c) Papercut Media. No reproduction without permission. Feel free to ask.
* Author: Patrick Pittman
* Date: June 2007
* Version: 1.0
* Requires: Prototype.js framework, ufo.js for flash loading, Dreamgirl on the other end for AJAXy JSON communication
* Also requires Prototype extension for ReadyState.addContentObserver
*/
var baseHref = "/";
showTab = function (i, prefix) {
	var i = i;
	$$('.'+prefix+'Header').each(function(el) {
		myid = /\d+$/i.exec(el.id);
		if (parseInt(myid) == i) {
			el.addClassName('active');
		} else {
		// Effect.BlindUp(el.down('dl'),{duration: 0.1});
			el.down('dl').hide();
			el.removeClassName('active');
		}
		
	});
//	$(prefix+'Content'+i).show();

	$(prefix+'Content'+i).show();
}

var initPapercut = function() {
	Element.extend(document.body);
	document.body.addClassName('dgActive');
	/*
	$$('.featureHeader').each(function(el) {	
		Event.observe(el.down('span'),'click',function(e) {
			tab = /\d+$/i.exec(Event.element(e).up('li').id);
			showTab(tab,'feature');
		} );
		Event.observe(el.down('span'),'mouseover',function(e) {
			Event.element(e).setStyle({cursor: 'pointer'});
		} );
		el.down('dl').hide();	
	});
	*/
	var targetHeight = 0;
	var liHeight = 0;
	if ($$('.tabHeader').length) {
		lastLi = $$('.tabHeader').last();

		if (lastLi){
		dlOffset = lastLi.offsetTop + lastLi.getHeight();
	
		$$('.tabHeader').each(function(el) {
			myDL = el.down('dl');	
			myHeight = myDL.getHeight();
			myDL.setStyle({top :  dlOffset+"px"});
			if (myHeight > targetHeight) {
				targetHeight = myHeight;
			}
			myLiHeight = el.getHeight();
			if (myLiHeight > liHeight) {
				liHeight = myLiHeight;
			}
			Event.observe(el.down('span'),'click',function(e) {
				tab = /\d+$/i.exec(Event.element(e).up('li').id);
				showTab(tab,'tab');
			} );	
			Event.observe(el.down('span'),'mouseover',function(e) {
				Event.element(e).setStyle({cursor: 'pointer'});
			} );		
		});
		targetHeight = targetHeight + liHeight +15;
		$('chartTab').setStyle({ height: dlOffset+"px", paddingBottom : targetHeight+"px" });
		showTab(1,'tab');
		}
	



	}
	
	$$('.liveStream').each(function(element) {
		Event.observe(element.down('a'),'click',function(e) { 
			Event.stop(e);
			element = Event.element(e);
			popup = window.open(baseHref+'mp3stream','rtrRestream','width=485,height=325,resizable=1,scrollbars=no,status=yes');
		  	if (window.focus) { popup.focus() };
		})
	});	
	

	if ($('keywords')) {
		var keywords = "enter keywords";					
		if ($('keywords').value=='') {
			$('keywords').value = keywords;
		}
		$('keywords').observe('focus',function(e){
			if (this.value==keywords) {
				this.value = '';
			}
		});
		$('keywords').observe('blur',function(e){
			if (this.value=='') {
				this.value = keywords;
			}
		});
	}	
	
	if (window.setupCols) {
		setupCols();		
	}
	
	/*
	if ($('banner')) {
		if ($('banner').down('iframe')) {
			banner = $('banner').down('iframe');
			$('banner').setStyle({
				height: banner.getHeight(),
				width: banner.getWidth()
			});
		}
	}
	*/
}


// this little guy loads on DOM load before window load event, to stop jumps and flickers
document.observe('dom:loaded', function(e) {
	initPapercut();
} );

function cleanupFP9IELeaks() {
  __flash_unloadHandler = function() {};
  __flash_savedUnloadHandler = function() {};
}

if (UFO.uaHas("ieWin")) {
  Event.observe(window,"beforeunload", cleanupFP9IELeaks);
}

