window.onload = function() { fix_chart_height(); resize_footer(); };

window.onresize = resize_footer;

var footer_fill = 0;

var default_webcam = 'studio1';

function update_onair(){
	$.get("/ajax/onairbox.php?random="+Math.random(), { }, function(data){
		$("div#onair").html(data.box);
		if(data.message){
			$("div#top-button-message").fadeIn();
		} else
		{
			$("div#top-button-message").fadeOut();
		}
		default_webcam = data.webcam;
	}, "json");
	setTimeout(update_onair, 15000);
}

function resize_footer(){
	// make the footer fill to the bottom of the page
	var windowHeight = window.innerHeight;
	var bodyHeight = document.body.scrollHeight - footer_fill;
	if(windowHeight > bodyHeight){
		footer_fill = (windowHeight - bodyHeight);
		
	} else {
		footer_fill = 0;
	}
	$("#footer-fill").css("height", footer_fill);
}

function fix_chart_height(){
	var heightFeatures = $("#hp-features").height();
	var heightSchedule = $("#hp-schedule").height();
	var heightPodcasts = $("#hp-podcasts").height();
	if(heightSchedule >= heightPodcasts && heightSchedule >= heightFeatures){
		$("#hp-chart").addClass("chart-much-wider");
	} else if(heightSchedule >= heightFeatures){
		$("#hp-chart").addClass("chart-wider");
	}
};

$(document).ready(function(){
	update_onair();
	resize_footer();
	setTimeout(resize_footer, 1000);

	$("#top-button-listen a").click(function(){
	
		launchFlashPlayer();
		return false;
	
	});
	
	$(".top-button:not(#top-button-message)").click(function(){
	   
	   window.location = $("a", this).attr("href");
	   
	});
	
	$("#dialog-message-box").dialog({autoOpen: false, draggable: false, modal: true, closeOnEscape: false, title: 'Send Message', closeText: '', buttons: { "Ok": function() { $(this).dialog("close"); } }  });
	
	$("#top-button-message").click(function(){
		$("#dialog-message-box").dialog('open');
		return false;
	});
	
	$(".hp-feature img").mouseenter(function(){
		var div = $(this).parents("div.hp-feature")[0];
		var a = $(div).find("a");
		a.css("text-decoration", "underline");
		$(this).addClass("hover");
	});
	
	$(".hp-feature img").mouseout(function(){
		var div = $(this).parents("div.hp-feature")[0];
		var a = $(div).find("a");
		a.css("text-decoration", "");
		$(this).removeClass("hover");
	});	
	
	$("a.play-podcast").click(function(){
		var href = $(this).attr("href");
		var id = $(this).parents("div.hp-podcast-download").find("span").attr("id");
		var name = $(this).parents("div.hp-podcast").find("div.hp-podcast-name a").attr("title");
		href = href.replace(/\&/, '%26'); // ampersands messing with my life!
		swfobject.embedSWF("/player/xspf_player_slim.swf", id, "150", "20", "10.0.0", "", { song_url: href, song_title: name,  autoplay: true }, { wmode: "transparent" });
		$("#"+id).css("float", "left");
		$(this).hide();
		return false;
	});
	
	$(".playlist-title a").click(function(){
		var div = $(this).parent().parent();
		div.find("ul").slideUp();
		var ul = $(this).parent().next("ul");
		ul.stop();
		ul.slideDown();
		return false;
	});
	
	hide_banners();
	
	$("#banner>div.banner_inner").mouseenter(function(){
		clearTimeout(bannerTimeout);
	});
	
	$("#banner>div.banner_inner").mouseout(function(){
		reset_banner_timer(2500);
	});
	
	setTimeout(update_webcam, 10000);

});

function update_webcam(){

	$("#banner_webcam img").attr("src", "/onair/webcam/images/"+default_webcam+"_medium.jpeg?"+Math.random());
	setTimeout(update_webcam, 10000);
}

var bannerTimeout = null;

function change_banner(){
	var current = $("#banner>div.banner_inner:visible");
	var next = current.next(".banner_inner");
	if(next.length == 0){
		next = $("#banner>div.banner_inner:first");
	}
	//current.css("z-index", -1);
	current.fadeOut(3000);
	//next.css("z-index", 0);
	next.fadeIn(3000);
	reset_banner_timer();
}

function hide_banners(){
	//$("#banner>div:not(:last)").hide();
	reset_banner_timer();
}

function reset_banner_timer(time){
	if(time == null) time = 15000;
	clearTimeout(bannerTimeout);
	bannerTimeout = setTimeout('change_banner()', time);
}


// Dropdown menu fix for IE
function initNavigation() {
  if (document.all&&document.getElementById) {
    navRoot = document.getElementById("nav");
    for (i=0; i<navRoot.childNodes.length; i++) {
      node = navRoot.childNodes[i];
      if (node.nodeName=="LI") {
        node.onmouseover=function() {
          this.className+=" over";
        }
        node.onmouseout=function() {
          this.className=this.className.replace(" over", "");
        }
      }
    }
  }
  var menuitems = $("#nav li.menutitle ul.menu li.menuitem");
  $(menuitems).mouseenter(function(){
    $(this).parents(".menutitle").find("a").addClass("over");
  });
  $(menuitems).mouseout(function(){
    $(this).parents(".menutitle").find("a").removeClass("over");
  });  
}
window.onload=initNavigation;


// Opens Surge webplayer in new window
function launchPlayer() {
  launchFlashPlayer();
}

function launchFlashPlayer() {
  if (window.location != "http://www.surgeradio.co.uk/onair/listen/") {
    window.location = "http://www.surgeradio.co.uk/onair/listen/";
  }
  window.open("/player/","surgeFlashPlayer","width=400,height=350,resizable=no,scrollbars=no");
}
