// Iowa Public Radio - Global JavaScript

//Content Height matching
function content_heights(match_min_height) {
	if(!match_min_height) var match_min_height = false;
	var ads_height = $('#right_container').height();
	var body_height = $('#body').height();
	if(body_height <= ads_height) {
		var new_content_height = ads_height - 35 - 30 - 15;
		var min_height = $('#body > .general_box').css('min-height');
		if(min_height=='500px') {
			if(match_min_height == true) {
				$('#body > .general_box').css('min-height', new_content_height+'px');
				$('#body > .general_box').css('height', 'auto');
			} else {
				$('#body > .general_box').css('height', new_content_height+'px');
			}
		}
	}
}

//IPR Streams display
function display_stream(stream) {
	$('.station_guide').hide();
	$('#' + stream).show();
	$('#guides a.active').removeClass('active');
	$('#guides a[rel="' + stream + '"]').addClass('active');
	var title;
	if(stream=='all-streams') {
		title = 'Station Guide';
	} else {
		stream = stream.replace('-', ' ');
		stream = stream.replace(/(^|\s)([a-z])/g, function(m,p1,p2){ return p1+p2.toUpperCase(); });
		title =  stream + ' Station Guide';
	}
	$('#guide_h1').text(title);
}

//Run processes when DOM is set
$(document).ready(function() {
	//Standard Colorbox
	if(jQuery().colorbox) {
		$('.cbox').colorbox({ opacity:0.7 });
		$('.find_a_station').colorbox({ opacity:0.7, innerWidth:'770px', height:'97%', title:'Find a Station', iframe:true });
	}
	
	//Drop Down Menu - for IE6
	if($.browser.msie && parseFloat($.browser.version)<=6.0) {
		$("#menu li:has(ul)").hover(function() {
			$('.sub',this).addClass('iehover').show();
		}, function() {
			$('.sub',this).removeClass('iehover').hide();
		});
	}
	
	//IPR LIVE LINKS
	$('a.live_stream_link').click(function() {
		live_stream_player = window.open($(this).attr('href'),'live_stream_window','status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0,scrollbars=0,height=320,width=480',true);
		return false;
	});
	
	//IPR Streams selection
	$('#guides a').click(function() {
		display_stream($(this).attr('rel'));
		return false;
	});
	
	//Stream Box hide/show
	$('#programs_arrow').click(function() {
		$('ul.program_list').slideToggle();
		$(this).toggleClass('expand');
	});
	
	$('#programs_arrow').hover(function() {
		$(this).css('cursor', 'pointer');
	});
	
	$('#weather_widget').click(function(event) {
		event.preventDefault();
		$('#weather_form').show();
		$(this).hide();
		$('#weather_zip').focus();
		return false;
	});

	$('#weather_zip').blur(function() {
		var weather_zip = $(this).val();
		if(weather_zip.length != 5) {
			$('#weather_form').hide();
			$('#weather_widget').show();
		}
	});

});

//Run processes when window is loaded
$(window).load(function() {
	content_heights();
});
