/*
# A component of the Realistic Book software
# from the New Zealand Digital Library Project at the 
# University of Waikato, New Zealand.
#
# Copyright (C) 2007 New Zealand Digital Library Project
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

/*****************************************************************
Realistic Book application
	Detect Flash Player 8.0.24 or more plugin 
	Specify the XHTML input and image cover URL to program
	Embed the program to the div tag with id = bookdiv

by Veronica Liesaputra --- 1 August 2007
******************************************************************/

var doc_url;//XHTML url
var img_cover;//image cover url

//check internet browser type
var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;

//These functions will be called by the program
function getDocURL() { return escape(doc_url); }
function getImgCover() { return escape(img_cover); }

/*
	If user has Flash player 8.0.24 or more,
		Embed the program to your HTML file inside the div tag with id = bookdiv
		Set the doc_url and img_cover values
	Otherwise, 
		Show user the links to get the flash player
*/
function embedProgram(xhtml_url, image_url)
{
    var flash_plug_html = "";
    
    if (detectFlashPlayerVersion(8,0,24) == false)
    {
	flash_plug_html += 'This content requires the Adobe Flash Player 8.0.24 or greater.';
	flash_plug_html += '<a href=http://www.macromedia.com/go/getflash/>Get Flash</a>';
    }
    else
    {
    	doc_url = escape(xhtml_url);
    	img_cover = escape(image_url);

	//embed inside object tag for IE
    	flash_plug_html += '<OBJECT align="middle" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" \n';
	flash_plug_html += '  codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" \n';
    	flash_plug_html += '  height="800" id="Book" swLiveConnect="true" align="middle"\n';
	flash_plug_html += '  width="100%">\n';
	//embed inside embed tag for netscape, mozilla
    	flash_plug_html += '<PARAM name="allowScriptAccess" value="sameDomain" />\n';
    	flash_plug_html += '<PARAM name="movie" value="RealisticBook.swf';
	flash_plug_html += '?src_image=' + img_cover;
	flash_plug_html += '&amp;doc_url=' + doc_url;
	flash_plug_html += '" />\n';
	flash_plug_html += '<PARAM name="quality" value="high" />\n';
	flash_plug_html += '<PARAM name="bgcolor" value="#FFFFFF" />\n';
	flash_plug_html += '<EMBED align="middle" \n';
	flash_plug_html += '      allowScriptAccess="sameDomain" swLiveConnect="true" \n';
	flash_plug_html += '      bgcolor="#FFFFFF" height="800" name="Book" \n';
	flash_plug_html += '      pluginspage="http://www.macromedia.com/go/getflashplayer" \n';
	flash_plug_html += '      quality="high" \n';
	flash_plug_html += '      src=\'RealisticBook.swf';
	flash_plug_html += '?src_image=' + img_cover;
	flash_plug_html += '&amp;doc_url=' + doc_url;
	flash_plug_html += '\'\n'; 
	flash_plug_html += '      type="application/x-shockwave-flash" width="100%" />\n';
	flash_plug_html += '</OBJECT>\n';
    }

    var flash_div = document.getElementById('bookdiv');
    flash_div.innerHTML = flash_plug_html;
}

function detectFlashPlayerVersion(reqMajor,reqMinor,reqRevision)
{
	if (navigator.plugins != null && navigator.plugins.length > 0) 
	{
		if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) 
		{
			var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
			var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
			var descArray = flashDescription.split(" ");
			var tempArrayMajor = descArray[2].split(".");			
			var versionMajor = tempArrayMajor[0];
			var versionMinor = tempArrayMajor[1];
			var versionRevision = descArray[3];
			if (versionRevision == "") 
			{
				versionRevision = descArray[4];
			}
			if (versionRevision[0] == "d") 
			{
				versionRevision = versionRevision.substring(1);
			}
			else if (versionRevision[0] == "r") 
			{
				versionRevision = versionRevision.substring(1);
				if (versionRevision.indexOf("d") > 0) 
				{
					versionRevision = versionRevision.substring(0, versionRevision.indexOf("d"));
				}
			}

			var isGood = isGoodPlugin(reqMajor,reqMinor,reqRevision,versionMajor,versionMinor,versionRevision);
			return isGood;
		}
	}
	else if ( isIE && isWin && !isOpera ) 
	{
		var version;
		var axo;
		var e;

		try 
		{
			// version will be set for 7.X or greater players
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
			version = axo.GetVariable("$version");

			// Given "WIN 2,0,0,11"
			var tempArray = version.split(" "); 	// ["WIN", "2,0,0,11"]
			var tempString = tempArray[1];		// "2,0,0,11"
			var versionArray = tempString.split(",");	// ['2', '0', '0', '11']

			var isGood = isGoodPlugin(reqMajor,reqMinor,reqRevision,versionArray[0],versionArray[1],versionArray[2]);
			return isGood;
		} 
		catch (e) {}
	}

	return false;
}

/*
	Check whether the current flash player plugin passed the required version
*/
function isGoodPlugin(reqMajor,reqMinor,reqRevision,currMajor,currMinor,currRevision)
{
	if (currMajor > parseFloat(reqMajor)) 
	{
		return true;
	} 
	else if (currMajor == parseFloat(reqMajor)) 
	{
		if (currMinor > parseFloat(reqMinor))
		{
			return true;
		}
		else if (currMinor == parseFloat(reqMinor)) 
		{
			if (currRevision >= parseFloat(reqRevision))
			{		
				return true;
			}
		}
	}
	
	return false;
}
