jQuery(document).ready(function() {
	// preload bubble image
	var bubbleImage = new Image();
	bubbleImage.src = "/images/podcasts/podcastInfoBubble.png";
	
	jQuery(".podcasts li").each(function() {
		var listItem = jQuery(this);
		var id = this.id.split("_").pop();
		var thumb = jQuery("#thumb_" + id).val();
		var title = jQuery("#title_" + id).html();
		jQuery(".thumb", this).load("thumb_loader.php", {thumb: thumb, title: title});
		
		jQuery("a", this).hover(
			function () {
				var width = jQuery(".thumb", this).width();
				var height = jQuery(".thumb", this).height();
				var offset = jQuery(".thumb", this).offset();
				
				var infoBubble = jQuery(this).siblings(".infoBubble");
				var infoBubbleWidth = 250;
				var infoBubbleHeight = 150;
				
				var top = offset.top - infoBubbleHeight;
				var left = offset.left - (infoBubbleWidth - width) / 2;
				
				infoBubble.css({top: top, left: left});
				
				infoBubble.fadeIn();
			}, 
			function () {
				var infoBubble = jQuery(this).siblings(".infoBubble");
				infoBubble.hide();
			}
		);
	});
});

function toggle_image(img) {
	var src = img.src;
	if (src.indexOf("_h.jpg") == -1) {
		img.src = src.replace(".jpg", "_h.jpg");
	}
	else {
		img.src = src.replace("_h.jpg", ".jpg");
	}
}

function playMovie(movieSRC) {
	var bodyWidth = jQuery("body").width();
	var popupWidth = jQuery("#qtmovieWrapper").width();
	var popupHeight = jQuery("#qtmovieWrapper").height();
	
	var leftOffset = parseInt((bodyWidth - popupWidth) / 2);
	var topOffset = getViewpointTop() + parseInt((getViewpointHeight() - popupHeight) / 2);
	
	var htmlContent = '<div id="overlayBG"></div>';
	var documentWidth = jQuery(document).width();
	var documentHeight = jQuery(document).height();
	jQuery("body").prepend(htmlContent);
	jQuery("#overlayBG").css({opacity: 0.7, width: documentWidth, height: documentHeight});	
	
	var frameURL = "quicktime.php?episodeURL=" + movieSRC;
	jQuery("#qtmovieFrame").attr("src", frameURL);
	
	jQuery("#episodeDownload").attr("href", movieSRC);
	
	jQuery("#qtmovieWrapper").css({left: leftOffset, top: topOffset});
	jQuery("#qtmovieWrapper").show();
}

function closeMovie() {
	jQuery("#overlayBG").remove();
	jQuery("#qtmovieWrapper").hide();
	jQuery("#qtmovieFrame").attr("src", "");
}

function getViewpointTop() {
	return window.pageYOffset || document.documentElement && document.documentElement.scrollTop || document.body.scrollTop;
}
function getViewpointHeight() {
	return window.innerHeight || document.documentElement && document.documentElement.clientHeight || document.body.clientHeight;
}	