// levitt.tv
// JavaScript functions using jquery to build video player choices
// By Greg Hartwig (webmaster at levitt dot com)
// 2007-11-05


// Set up basic movie player layout
var flvStd = new SWFObject("/mediaplayer.swf","player",320,240+20,"7","#FFFFFF", "high");
flvStd.addParam("allowfullscreen","true");
flvStd.addVariable("showfsbutton","true");
flvStd.addVariable("overstretch","false");
flvStd.addVariable("showdigits","true");
flvStd.addVariable("showstop","true");
flvStd.addVariable("displaywidth",320);
flvStd.addVariable("displayheight",240);

var flvGoogle = new SWFObject("http://video.google.com/googleplayer.swf","player",320,240+26,'7','#FFFFFF', 'best');
flvGoogle.addVariable("hl", "en");
flvGoogle.addParam("allowfullscreen","true");

gPlayerNum = 0;


$(document).ready(function() {
	$("img.buylink").mouseover(function() {
		this.src = "/pics/buy-over";
	});
	
	$("img.buylink").mouseout(function() {
		this.src = "/pics/buy";
	});
	
	$("img.playlink").mouseover(function() {
		this.src = "/pics/play-over";
	});
	
	$("img.playlink").mouseout(function() {
		this.src = "/pics/play";
	});
});



function makeChoices(prodnum, idval) {
	if (!idval) idval="V"+prodnum;
	var msgboxid  = "choices-"+idval;
	
	// If it's showing already, hide it
	if ($("#"+msgboxid+":visible").is("div")) {
		hideChoices(idval, msgboxid)
	} else {
		showChoices(prodnum, idval, msgboxid);
	}
	
	return false;
}



function loadChoices(prodnum, idval, msgbox) {
	//$(msgbox).show('normal');  // "Loading..." message won't show without this, but it's smoother this way

	// Get the data about the program from the server
	$.ajax({
		url: '/bin/proginfo.php',
		type: "get",
		data: {prodnum: prodnum},
		
		error: function() {
			$(msgbox).html("<!-- STATUS:ERROR --><i><small>Error:  Try again later.<"+"/small><" + "/i>").show('normal');
		},
		
		success: function(xml) {
			var flvGoogle = $("Media > googleid" ,xml).text();
			var flvfile   = $("Media > flvfile"  ,xml).text();
			var flvsize   = $("Media > flvfile"  ,xml).attr('size');
			var thumbfile = $("Media > thumbfile",xml).text();
			var mp4file   = $("Media > mp4file"  ,xml).text();
			var rmfile    = $("Media > rmfile"   ,xml).text();
			var txt = "<!-- STATUS:OK -->Choose format:\n" + 
						 "<ul class=\"compact\">\n";
			
			if ( rmfile || mp4file || flvfile || flvGoogle) {
				var doFlvArgs;
				if (flvGoogle)
					doFlvArgs = "'"+idval+"','"+flvGoogle+"','"+mp4file+"','"+thumbfile+"','320x240',true";
				else if (flvfile)
					doFlvArgs = "'"+idval+"','"+flvfile+"','"+mp4file+"','"+thumbfile+"','"+flvsize+"',false";

				if (doFlvArgs)
					txt += "<li><a href=\"#\" onclick=\"return doFlv("+doFlvArgs+");\" title=\"Best for most&#8212;plays video in your browser or full-screen\">Play Flash<"+"/a> (in your browser)<"+"/li>";

				if (mp4file)
					txt += "<li><a href=\""+path_tv+mp4file+"?pl\" target=\"mp4window\" title=\"Best for iPhone or no Flash&#8212;plays in a new browser window\">Play MP4<"+"/a> (Quicktime) <a href=\""+path_tv+"/download"+mp4file+"?dl\" title=\"Download MP4 movie to your computer\">download MP4</a><"+"/li>";

				if (rmfile)
					txt += "<li><a href=\""+path_tv+rmfile+"\" title=\"Best for slow connections&#8212;plays outside of your browser\">Play RealMedia<"+"/a> (RealPlayer)<"+"/li>";
			} else {
				txt = "<i><small>No media available for this program.<"+"/small><" + "/i>";
			}
			
			$(msgbox).html(txt).show('normal');
		}
	});
}



function doFlv(idval, flvfile, mp4file, thumbfile, flvsize, isGoogle) {
	flvsize = "320x240";   // #################################  KEEP FROM CHANGING PLAYER HEIGHT (for now)
	var playerid = "player-"+idval;
	if ($("#"+playerid).is("div")) {
		hidePlayer(idval, playerid);
	} else {
		showPlayer(idval, playerid, flvfile, mp4file, thumbfile, flvsize, isGoogle);
	}
	return false;  // Keep browser on the page
}



function showPlayer(idval, playerid, flvfile, mp4file, thumbfile, flvsize, isGoogle) {
	var msgboxid  = "choices-"+idval;
	var msgbox = $("#"+msgboxid);
	$(msgbox).append("<div id=\""+ playerid +"\" class=\"playwindow\"><"+"/div>");
	var playerobj = $("#"+playerid);
	var flv, control_height;
	var video_width, video_height;
	
	// Get video width/height
	if (flvsize == "")
		flvsize = "320x240";
	var t = flvsize.match(/(\d+)x(\d+)/);
	video_width  = t[1]-0;  // Convert to a number
	video_height = t[2]-0;
	
	if (isGoogle) {
		flv = flvGoogle;
		control_height = 26;
	} else {
		flv = flvStd;
		control_height = 20;
	}
	//alert("flvsize="+flvsize+", vwidth="+video_width+",vheight="+video_height+", player_h="+(video_height+control_height));
	
	// Set video size
	flv.addVariable("displaywidth", video_width);
	flv.addVariable("displayheight",video_height);
	
	// Set player size
	flv.setAttribute("width", video_width);
	flv.setAttribute("height",video_height+control_height);
	

	// Set DIV size
	$(playerobj).width(video_width);
	$(playerobj).height(video_height+control_height);
	
	flv.setAttribute("id","player-" + (++gPlayerNum));
	
	if (isGoogle) {
		flv.addVariable("docId", flvfile);
	} else {
		if (mp4file != '') {
			flv.addVariable("file",     path_tv + mp4file);
			flv.addVariable("fallback", path_tv + flvfile);
			flv.addVariable("image",    path_tv + thumbfile);
			flv.addVariable("link",     path_tv + "/download" + mp4file);
			flv.addVariable("showdownload", true);
		} else {
			flv.addVariable("file",     path_tv + flvfile);
			flv.addVariable("fallback", "");
			flv.addVariable("image",    path_tv + thumbfile);
			flv.addVariable("link",     "");
			flv.addVariable("showdownload", false);
		}
	}

	$(playerobj).show('normal', function() {
		flv.write(playerid);
	});
}



function hidePlayer(idval, playerid) {
	var playerobj = $("#"+playerid);
	if ($(playerobj).is("div"))
		$(playerobj).empty().hide('normal', function() {
			$(playerobj).remove();
		});
}



function showChoices(prodnum, idval, msgboxid) {
	var msgbox;
	
	// If the div does not exists, create it
	if (!$("#"+msgboxid).is("div"))
		$("#"+idval).append("<div id=\"" + msgboxid + "\" class=\"choices\"><i><small>Loading...<"+"/small><" + "/i><"+"/div");
	
	msgbox = $('#'+msgboxid);
		
	// If msgbox contains "STATUS:OK" then just show it, otherwise load it
	if ($(msgbox).html().indexOf("STATUS:OK") == -1)
		loadChoices(prodnum, idval, msgbox);
	else
		$(msgbox).show('normal');
}



function hideChoices(idval, msgboxid) {	
	//hidePlayer(idval);
	$("#"+msgboxid).hide('normal');
}









// Just show the player window (for individual video pages)
function showPlayerWindow(playerid, flvfile, mp4file, thumbfile, flvsize, isGoogle) {
	var flv, control_height;
	
	// Get video width/height
	if (flvsize == "") flvsize = "320x240";
	var t = flvsize.match(/(\d+)x(\d+)/);
	video_width  = t[1]-0;  // Convert to a number
	video_height = t[2]-0;
	

	if (isGoogle) {
		flv = flvGoogle;
		control_height = 26;
	} else {
		flv = flvStd;
		control_height = 20;
	}
	
	// Set video size
	flv.addVariable("displaywidth", video_width);
	flv.addVariable("displayheight",video_height);
	
	// Set player size
	flv.setAttribute("width", video_width);
	flv.setAttribute("height",video_height+control_height);
		
	//flv.addVariable("overstretch","true");

	if (isGoogle) {
		flv.addVariable("docId", flvfile);
	} else {
		if (mp4file != '') {
			flv.addVariable("file",     path_tv + mp4file);
			if (flvfile)
				flv.addVariable("fallback", path_tv + flvfile);
			else
				delete flv.variables["fallback"];  // If we just set it to null, it doesn't work.
			flv.addVariable("image",    path_tv + thumbfile);
			flv.addVariable("link",     path_tv + "/download" + mp4file);
			flv.addVariable("showdownload", true);
		} else {
			flv.addVariable("file",     path_tv + flvfile);
			delete flv.variables["fallback"];
			flv.addVariable("image",    path_tv + thumbfile);
			delete flv.variables["link"];
			flv.addVariable("showdownload", false);
		}
	}
	
	if (mp4file != '' || flvfile != '')
		flv.write(playerid);
}


