// ===========================================================================================
// Created by Paul Melville
// ===========================================================================================
var navNS = (navigator.appName.indexOf("Netscape")  != -1);
var navMZ = (navigator.appName.indexOf("Mozilla")   != -1);
var navFF = (navigator.appName.indexOf("FireFox")   != -1);
var navOP = (navigator.appName.indexOf("Opera")     != -1);
var navSF = (navigator.appName.indexOf("Safari")    != -1);
var navIE = (navigator.appName.indexOf("Microsoft") != -1);

var TABITZ_BROWSER_INFO  = 0x40000000;
var TABITZ_STARTUP       = 0x20000000;
var TABITZ_ONE_TIME      = 0x10000000;

var TABITZ_JS_INFO       = 0x08000000;
var TABITZ_JS_ERROR      = 0x04000000;

var TABITZ_OBJ_INFO      = 0x00800000;
var TABITZ_OBJ_CREATE    = 0x00400000;

var TABITZ_USER_DEF      = 0x0000FFFF;

var test_alert_bitz = 0x00000000;

function test_alert(bit, str)
{
	if (test_alert_bitz & bit)
		alert(str);
}

function alertMyBrowserType()
{
	if (false)
	  test_alert(TABITZ_BROWSER_INFO, "You should never see this message...WTF??");
	else if (navNS)
	  test_alert(TABITZ_BROWSER_INFO, "Browser is Netscape");
	else if (navMZ)
	  test_alert(TABITZ_BROWSER_INFO, "Browser is Mozilla");
	else if (navFF)
	  test_alert(TABITZ_BROWSER_INFO, "Browser is FireFox");
	else if (navOP)
	  test_alert(TABITZ_BROWSER_INFO, "Browser is Opera");
	else if (navSF)
	  test_alert(TABITZ_BROWSER_INFO, "Browser is Safari");
	else if (navIE)
	  test_alert(TABITZ_BROWSER_INFO, "Browser is Microsoft Internet Explorer");
	else
	  test_alert(TABITZ_BROWSER_INFO, "Browser is an Unknown Type: '"+navigator.appName+"'");
}

// ===========================================================================================
Object.prototype.clone = function()
{
	var newObj = (this instanceof Array) ? [] : {};
	for (i in this)
	{
		if (i == 'clone') continue;
		if (this[i] && typeof(this[i]) == "object")
		{
			newObj[i] = this[i].clone();
		}
		else
			newObj[i] = this[i];
	}
	return(newObj);
};

//Object.prototype.derivedFrom = function(super)
//{
//	this.prototype = new super();
//	this.prototype.constructor = this;
//}

/*
function objIsSubjectTo(sub, sup) {
   sub.prototype = new sup();
   sub.prototype.constructor = sub;
};

function objIsSubjectTo(sub, sup) {
   function F() {}
   F.prototype = sup.prototype;
   sub.prototype = new F();
   sub.prototype.constructor = sub;
   sub.superConstructor = sup;
   sub.superPrototype = sup.prototype;
};
*/

// ===========================================================================================
function cloneObj(obj)
{
	if (obj == null) return obj;
	if (typeof(obj) != 'object') return obj;

	var newObj = new Object();
	for(var i in obj) newObj[i] = cloneObj(obj[i]);

	return newObj;
}
 
// ===========================================================================================
function funcObj(obj, func)
{
	this.funk  =  func;
	this.thiss =  obj;
    this.args  =  new Array();
    // arguments[0] == object
    // arguments[1] == func
    // copy all other arguments we want to "pass through"
    for(var i = 2; i < arguments.length; i++)
    {
        this.args.push(arguments[i]);
    }
}

funcObj.prototype.XEQ = function()
{
	this.funk.apply(this.thiss, this.args);
}

// ===========================================================================================
function funcListObj()
{
	this.todo  =  new Array();
}

funcListObj.prototype.funkAdd = function(funkObj)
{
	this.todo.push(funkObj);
}

funcListObj.prototype.funkXEQAll = function()
{
	do
	{
		var funk = this.todo.pop();
		if (funk)
			funk.XEQ();
    }
	while(funk);
}

var windowload_todo_list = new funcListObj();
//  windowload_todo_list.funkAdd(new funcObj(this, this.prototype.funkshun, ...));
window.onload = "windowload_todo_list.funkXEQAll();";

// ===========================================================================================
/*
if (/msie/i.test (navigator.userAgent)) //only override IE
{
  document.nativeGetElementById = document.getElementById; 
  document.getElementById = function(id)
	{
      var elem = document.nativeGetElementById(id);
      if(elem)
      {
    	//make sure that it is a valid match on id
    	if(elem.attributes['id'].value == id)
    	{
    	  return elem;
    	}
    	else
    	{
    	  //otherwise find the correct element
    	  for(var i=1;i<document.all[id].length;i++)
    	  {
    	    if(document.all[id][i].attributes['id'].value == id)
    	    {
    	      return document.all[id][i];
    	    }
    	  }
    	}
      }
      return null;
	}
}
*/
// ===========================================================================================
function objStyleRef(obj)
{
	if ( obj.style )
		return(obj.style);

	// IE objects have no style
	return(obj);
}

function objById(id)
{
	var myObj = null;
	
	if (document.getElementById)
        myObj = document.getElementById(id);
    else if (document.all)
        myObj = document.all[id];
    else if (document.layers)
        myObj = document.layers[id];

	if (!myObj)
	{
//		alert("You Browser is not very Javascript-friendly ... try FireFox!");
	}

	return(myObj);
}

// ===========================================================================================
// Functions to set the style attributes of...any object!!

function refPosSet(ref, x, y)
{
	ref.left     = parseInt(x) + "px";
	ref.top      = parseInt(y) + "px";
}

function refZeeSet(ref, z)
{
	ref.zIndex   = parseInt(z);
}

function refSizeSet(ref, w, h)
{
	ref.width    = parseInt(w) + "px";
	ref.height   = parseInt(h) + "px";
}

function objPosSet(obj, x, y)
{
	refPosSet(objStyleRef(obj), x, y);
}

function objZeeSet(obj, z)
{
	refZeeSet(objStyleRef(obj), z);
}

function objSizeSet(obj, w, h)
{
	refSizeSet(objStyleRef(obj), w, h);
}

// ===========================================================================================
// Functions to set the opacity of...any object!!

function objOpacitySet(obj, opa)
{
	testVerify(obj, opa, "Opacity");

	if ( obj.filters && obj.filters.alpha )				// IE objects have filters
		obj.filters.alpha.opacity = opa;
	else
		obj.style.opacity = opa / 100;

//	if (navNS && parseInt(navigator.appVersion) >= 5)
//	    obj.style.MozOpacity = opa / 100;
//
//	else if (navIE && parseInt(navigator.appVersion) >= 4)
//	    obj.filters.alpha.opacity = opa;
}

function objOpacityGet(obj)
{
	if ( obj.filters && obj.filters.alpha )				// IE objects have filters
		return(obj.filters.alpha.opacity);
	else
		return(parseInt(100 * obj.style.opacity));

//	if (navNS && parseInt(navigator.appVersion) >= 5)
//	    return(parseInt(100 * obj.style.MozOpacity));
//
//	else if (navIE && parseInt(navigator.appVersion) >= 4)
//		return(obj.filters.alpha.opacity);
}

// ===========================================================================================
function alertAllAtt(myobj)
{
	test_alert(TABITZ_OBJECT_INFO, "Begin Obj properties...");
	
	for(att in myobj)
	{
		test_alert(TABITZ_OBJECT_INFO, "Value of '"+att+"' is '"+myobj[att]+"' :-)");
	}

	test_alert(TABITZ_OBJECT_INFO, "Endof Obj properties...");
}

// ===========================================================================================
function num2price(num)
{
	var price = "$" + parseInt(num) + ".";
	price += parseInt((num *  10) % 10);
	price += parseInt((num * 100) % 10);
	return(price);
}

// ===========================================================================================
function num2signed(num)
{
	if (num < 0)
		return(num);
	return("+" + num);
}

// ===========================================================================================
function imgWidthPercent(imgsrc, maxw, maxh)
{
	var pahsent = 100;
	var img = new Image();
	img.src = imgsrc;

	var w = img.width;
	var h = img.height;
	if ((w > 0) && (h > 0))
	{
		pahsent = parseInt(100 * maxh / (h * (maxw / w)));
		if (pahsent > 100)
			pahsent = 100;
	}

	return(pahsent);
}

function imgHeightPercent(imgsrc, maxw, maxh)
{
	var pahsent = 100;
	var img = new Image();
	img.src = imgsrc;

	var w = img.width;
	var h = img.height;
	if ((w > 0) && (h > 0))
	{
		pahsent = parseInt(100 * maxw / (w * (maxh / h)));
		if (pahsent > 100)
			pahsent = 100;
	}

	return(pahsent);
}

// ===========================================================================================
function imgWidth(imgsrc, maxw, maxh)
{
	return(Math.floor(maxw * imgWidthPercent(imgsrc, maxw, maxh) / 100));
}

function imgHeight(imgsrc, maxw, maxh)
{
	return(Math.floor(maxh * imgHeightPercent(imgsrc, maxw, maxh) / 100));
}

// ===========================================================================================
function drawImgMax(myimg, w, h) {
	echo("<table width=\"" + w + "\" height=\"" + h + "\"><tr><td align=\"center\" valign=\"cetner\">\n");
	echo("    <img src=\"" + myimg + "\" width=\"" + imgWidthPercent(myimg, w, h) + "%\"/>\n");
	echo("</td></tr></table>\n");
}

// ===========================================================================================
function regExpfindValueByKey(str, key, v_delim)
{
	var loc = -1;
	var retval = null;
	loc = str.search(key);
	if (loc >= 0)
	{
		loc += key.length;
		retval = str.slice(loc);
		loc = retval.search(v_delim);
		if (loc >= 1)
		{
			retval = retval.slice(0, loc);
		}
	}
	return(retval);
}

// ===========================================================================================
function urlParamValGet( param )
{
  param = param.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]" + param + "=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

// ===========================================================================================
function TextWriteConst() {	document.write('gribble ');	document.write('grobble '); }

function echo(mytext)
{
	document.write(mytext);
}

function TextWrite(mytext) {
	echo("zibble ");
	echo(mytext);
	echo("zobble ");
}

function imgWrite(myimg, w, h) {
	return('<img width="' + w + '" height="' + h + '" src="' + myimg + '" />');
}

function lynkWrite(mylink, mytext) {
	echo('<a href="http://' + mylink + '">');
	echo(mytext);
	echo('</a>' + "\n");
}

function lynkNative(mytext) {
	lynkWrite(mytext, mytext);
}

/*
var audiophyle = "";

function AudioWidget(audiophyle) {
  detectBrowser();
  if(windowsInternetExplorer)
   {
     document.write('<object id="id14" classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"
	 codebase="http://www.apple.com/qtactivex/qtplugin.cab" width="118" height="16">
	 <param name="src" value=audiophyle /><param name="controller" value="true" />
	 <param name="autoplay" value="false" /><param name="scale" value="tofit" />
	 <param name="volume" value="100" /><param name="loop" value="false" /></object>');
   }
  else if(isiPhone)
   {
     document.write('<object id="id14" type="video/quicktime" width="118" height="16">
	 <param name="src" value=audiophyle />
	 <param name="controller" value="true"/>
	 <param name="scale" value="tofit"/>
	 </object>');
   }
  else
   {
     document.write('<object id="id14" type="video/quicktime" width="118" height="16" data=audiophyle>
	 <param name="src" value=audiophyle />
	 <param name="controller" value="true"/>
	 <param name="autoplay" value="false"/>
	 <param name="scale" value="tofit"/>
	 <param name="volume" value="100"/>
	 <param name="loop" value="false"/>
	 </object>');
   }

}
*/

function kaki(str)
{
	document.write(str + "\n");
}

function rndInt(min, max)
{
	if (min > max)
		return min;

	max = max + 1 - min;
	// Return random value between min and max, inclusive
	return Math.floor(Math.random()* max) + min; 
}

function randomInt(min, max)
{
	if (min > max)
		return min;

	max = max + 1 - min;
	// Return random value between min and max, inclusive
	return Math.floor(Math.random()* max) + min; 
}

function attrGet(yd, nom)
{
	var ahh      = document.getElementById(yd);
	var attrRoot = ahh.attributes;
	var num      = ahh.attributes.length;
    for (var i = 0; i < num; i++)
	{
		var oAttr = attrRoot.item(i);
		if (nom == oAttr.name)
			return oAttr.value;
	}
	return null;
}

function attrSet(yd, nom, val)
{
	var ahh      = document.getElementById(yd);
	var attrRoot = ahh.attributes;
	var num      = ahh.attributes.length;
    for (var i = 0; i < num; i++)
	{
		var oAttr = attrRoot.item(i);
		if (nom == oAttr.name)
		{
			oAttr.value = val;
			return oAttr.value;
		}
	}
	return null;
}

function attrDumpText(yd, sep)
{
	var ahh      = document.getElementById(yd);
	var attrRoot = ahh.attributes;
	var num      = ahh.attributes.length;
	document.write("'" + yd + "' has " + num + " attributes<br/>\n");
    for (var i = 0; i < num; i++)
	{
		var oAttr = attrRoot.item(i);
		document.write(oAttr.name + "=" + oAttr.value + sep);
	}
}

function arbiButton(str)
{
	var yd = "arbiButton" + rndInt(1,1000);
	with(document)
	{
		write("<div>\n");
		write("<table border=\"5px\" bordercolor=\"#000066\" height=\"50\"><tr>\n");
		write("<td id=\"" + yd + "\" border=\"2px\" bgcolor='#006600'");
		write("onmouseover=\"attrSet('"+yd+"', 'bgcolor', '#660000');\" ");
		write("onmouseout=\"attrSet('"+yd+"', 'bgcolor', '#0000cc');\">\n");
		write(str + "\n</td>\n");
		write("</tr></table>\n");
		write("</div>\n");
	}
}

var newWinOffset = 0 // Position of first pop-up

function PlayerOpen(soundfiledesc,soundfile) { 
alert("Eeeek");
PlayWin = window.open('',314,'width=320,height=190,top=0,left=0,resizable=0,scrollbars=0,titlebar=0,toolbar=0,menubar=0,status=0,directories=0,personalbar=0');
// PlayWin.focus(); 

var myphyle = 'AudioClips/' + soundfile + '.mp3';
var winContent = "<HTML><HEAD><TITLE>" + soundfiledesc + "</TITLE></HEAD><BODY bgcolor='#C5B4DB'>"; 
winContent += "<B style='color: #5E2A8D;font-size:18px;font-family:Times,sans-serif;line-height:1.5'>" + soundfiledesc + "</B>";

winContent += "<OBJECT width='300' height='42'>"; 
winContent += "<param name='SRC' value='" + soundfile + "'>";
winContent += "<param name='AUTOPLAY' VALUE='true'>"; 
winContent += "<param name='CONTROLLER' VALUE='true'>";
winContent += "<param name='BGCOLOR' VALUE='#C5B4DB'>"; 
winContent += "<EMBED SRC='" + soundfile + "' AUTOSTART='TRUE' LOOP='FALSE' WIDTH='300' HEIGHT='42' CONTROLLER='TRUE' BGCOLOR='#C5B4DB'></EMBED>";
winContent += "</OBJECT>"; 

winContent += "<p style='color: #5E2A8D;font-size:12px;font-family:Verdana,sans-serif;text-align:center'><a href='" + soundfile + "' style='color: #5E2A8D;'>Download this file</a> <SPAN style='font-size:10px'>(right-click or Option-click)</SPAN></p>";
winContent += "<FORM><DIV align='center'><INPUT type='button' value='Close this window' onclick='javascript:window.close();'></DIV></FORM>"; 
winContent += "</BODY></HTML>"; 

PlayWin.document.write(winContent); 
PlayWin.document.close(); // "Finalizes" new window 
}
