/* 
SOUND/MUSIC PLAYER
This routine will rewrite a layer (div or span) with ID="SOUND"
to include a call to play the referenced midi file.
The only required argument is the name of the midi file.
While the older (playsound) "popup" music routine remains in use, 
the call to "legacy" will make sure that the popups are closed.  

USAGE
In the header, place a style sheet for SOUND to control style
In the body, place a layer (div or span) with ID="SOUND"
After the layer is defined, include this file as an external javascript
After the include, call the subroutine

FUNCTION CALL & ARGUMENTS
PlayMusic("file.name"[,"title"[,volume 1-100[,# of repetitions]]])
Note that "repetitions" has little practical meaning other than 1 (single play) versus a large number (loop). However, it may be also used as a flag to initiate other functions
(i.e., 998 mean include animated picture, as in playsound).

ADDED FEATURE
execute this code someplace to turn off sound with a variable stored in a cookie:
var expire = new Date ();
expire.setTime (expire.getTime() + (24 * 3600000)); //expire in 24hrs
var WholeCookie = expire + '`' + 'no_sound';
document.cookie = "GMO_globals="+escape (WholeCookie)+";expires="+expire.toGMTString();

EXAMPLE
<head>
<style type="text/css">
SOUND {  font-family: "Amerigo BT"; font-size: 10px; color: #000000}
</style>
</head>
<body>
<div id="SOUND" style="position:absolute; width:200;top:10px; left:10px; z-index:2"> 
No Background Music
</div>
<SCRIPT LANGUAGE="JavaScript" src="music.js"></SCRIPT>
<SCRIPT LANGUAGE="JavaScript">
PlayMusic("musicfile.mid","Music Name",75,999);
</SCRIPT>
</body> 

AUTHOR
Written by Beach Bum for use on www.gay-males.org
If you would like to use it, please feel free to do so.
I would appreciate a link to my site.  Thank you.
*/


/////////////////////////////////////////////////////////////////
// BANDWIDTH CONSERVATION
// create a global variable for the alternate server - end with "/"
var ALTURL="";
//var ALTURL="http://webspace.ccsisp.com/group_a/images/";
// set other locations
if(ALTURL == ""){
	var ALTURLG="";
	var ALTURLU="";
	}else{
	var ALTURLG=ALTURL+"gallery/";
	var ALTURLU=ALTURL+"usa/";
	}
/////////////////////////////////////////////////////////////////
function nul(){ }
store_midi="";
store_text="";
store_V=75;
store_reps=999;

function PlayMusic(midi,text,V,reps){

// open and close old popup music window ****
//legacy();

// set flags for browser type and stds conformance
ie4 = ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4 )) ;
ie5 = ((navigator.appName == "Microsoft Internet Explorer") && ((navigator.appVersion.indexOf("5.") > 0 )| (navigator.appVersion.indexOf("6.") > 0 )) );
ns4 = ((navigator.appName == "Netscape") && (parseInt(navigator.appVersion) >= 4 )) ;
agt = navigator.userAgent.toLowerCase()  ;
ns6 =  (agt.indexOf("netscape/7") != -1 )  ;
w3c = (agt.indexOf("gecko") != -1 ) ;

// set style strings for movement
if (ie4) {
    var layerRef='document.all["';
    var styleRef='"].style';
} 
if (ns4) {
    var layerRef='document.layers["';
    var styleRef='"]';
} 
if (w3c){
    var layerRef='document.getElementById("';
    var styleRef='").style';
}
if (ie5){
    var layerRef='document.getElementById("';
    var styleRef='").style';
}

// repace text if necessary **********************
if(!text || text==""){
	start_t=midi.indexOf("\/");
	end_t=midi.indexOf(".");
	text=midi.substring(start_t,end_t);
	text=text.substr(0,1).toUpperCase()+text.substr(1).toLowerCase();
	}
// set volume *************************************
if(!V || V==""){V=75};

// check for cookie to turn off sound
TheCookieName='GMO_globals';
TheCookieValue=GetCookie (TheCookieName);
//TheExpDate=parseCookie(TheCookieValue,0);
TheSoundOption=parseCookie(TheCookieValue,1);
//if(TheSoundOption == 'no_sound'){V=0}

VV= 10*(V-100);
// make sure reps are covered ******************
if(!reps || reps==""){reps=999}

// store variables ********************************
store_midi=midi;
store_text=text;
store_V=V;
store_reps=reps;

// add possibility of second sound source
layerid="SOUND"
if (reps==997){reps=1;layerid="ANIMATE"}

// create string ***********************************
if(TheSoundOption == 'no_sound'){
	PlayString="<center><font size='1'>Music Currently Off :</font>\n";
	PlayString=PlayString+" <a href='javascript:nul()' onclick='SoundOn()'>TURN ON</a>";
}else{
	PlayString="<center><font size='1'>Now Playing: </font>\n";
	PlayString=PlayString+"<font size='3'>"+text+"</font> \n";
	//PlayString=PlayString+"<font size='1'> "+timem+" min "+times+" sec - Vol "+V+"%</font>";
	PlayString=PlayString+" <a href='javascript:nul()' onClick='SoundOff()'><font size='1'>Off</font></a>";
	PlayString=PlayString+"<br>";
	if(ie4||ie5){
	PlayString=PlayString+"<BGSOUND SRC='"+midi+"' autostart='true' loop='"+reps+"' width='0' height='0'  volume='"+VV+"'>\n";
	}else{
	PlayString=PlayString+"<EMBED SRC='"+midi+"' autostart='true' loop='"+reps+"' width='0' 	height='0' volume='"+V+"'>\n";
	}
}
//rewrite sound call
if (w3c) {
	//confirmed
	eval("document.getElementById('"+layerid+"').innerHTML=PlayString") ;
	} else {
if (ns4) {
	//confirmed
	eval("document.layers['"+layerid+"'].document.open()");
	eval("document.layers['"+layerid+"'].document.write(PlayString)");
	eval("document.layers['"+layerid+"'].document.close()");
	} else {
if(ie5){
	//confirmed 
	eval("document.getElementById('"+layerid+"').innerHTML=PlayString") ;
} else {
if(ie4){
	//confirmed
	eval("document.all['"+layerid+"'].innerHTML=PlayString") ;
}}}}

// add code for ANIMATE Addition
if (reps == 998){
//CANT GET TRANSPARENT BACKGOUND IN NS
	if(ie4||ie5||ns6){
	animatelayer();
	}else{
	// NS alternative
	animatelayer_NS();
	}
}

}
// end of PlayMusic


////////////////////////////////////////////////////////////
//  LEGACY: open and close old popup
function delay(milliseconds){
mark1 = new Date();
milli1=mark1.getTime();
do {
	mark2= new Date();
	milli2=mark2.getTime();
	deltatime=milli2-milli1;
	} while (deltatime<milliseconds) ;
}
function legacy(){
// Must open first to be sure it exists.   Give time to open and then close it.
popmp = window.open("","ran_mus","height=100,width=220,screenX=450,screenY=0") ;
delay(100);
popmp.close() ;
}

////////////////////////////////////////////////////////////
// ANIMATE CODE
function animatelayer(){
// New - rewrite ANIMATE layer.  Note IE and NS variations  
AnimString="";
if(ie4||ie5){
AnimString=AnimString+'<OBJECT CLASSID="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ';
AnimString=AnimString+'CODEBASE="http://active.macromedia.com/flash2/cabs/swflash.cab#version=4,0,0,0" ';
AnimString=AnimString+'ID="test" WIDTH="320" HEIGHT="240">\n ';
AnimString=AnimString+'<PARAM NAME="movie" VALUE="'+ALTURL+'guy.swf">\n';
AnimString=AnimString+'<PARAM NAME="quality" VALUE="high">\n';
AnimString=AnimString+'<PARAM NAME="wmode" value="transparent">\n';
AnimString=AnimString+'<PARAM NAME="bgcolor" VALUE="#0000ff">\n';
AnimString=AnimString+'<PARAM NAME="play" VALUE="1">\n';
AnimString=AnimString+'</OBJECT>\n';
}else{
AnimString=AnimString+'<EMBED SRC="'+ALTURL+'guy.swf"  QUALITY="high" ';
AnimString=AnimString+' wmode="transparent" bgcolor="#000000" background="curtain.gif" ';
AnimString=AnimString+'WIDTH="320" HEIGHT="240" TYPE="application/x-shockwave-flash" ';
AnimString=AnimString+'PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash">\n';
}

AnimString=AnimString+'<div style="position:absolute; top:215px; width:200px;left:70px">\n';
AnimString=AnimString+'<font size="1" color="#000000">Animation by </font>';
AnimString=AnimString+'<a href="mailto:ctoons@excite.com"><font size="2" color="#000000">Cocktoons</font></a><div>';

//rewrite animation division
if (w3c) {
	//confirmed
	eval("document.getElementById('ANIMATE').innerHTML=AnimString") ;
	} else {
if (ns4) {
	//confirmed
	eval("document.layers['ANIMATE'].document.open()");
	eval("document.layers['ANIMATE'].document.write(AnimString)");
	eval("document.layers['ANIMATE'].document.close()");
	} else {
if(ie5){
	//confirmed 
	eval("document.getElementById('ANIMATE').innerHTML=AnimString") ;
} else {
if(ie4){
	//confirmed
	eval("layerRef['ANIMATE'].innerHTML=AnimString") ;
}}}}
// relocate animation division
ypos=screen.height-350;
xpos=-60;
eval(layerRef + 'ANIMATE' + styleRef + '.top = ypos');
eval(layerRef + 'ANIMATE' + styleRef + '.left = xpos');

	} 

////////////////////////////////////////////////////////////
// ANIMATE CODE FOR NS that wont support transparency
function animatelayer_NS(){
// New - rewrite ANIMATE layer.  Note IE and NS variations  
AnimString="";
AnimString=AnimString+'<font size="1" color="#000000"><br> Animation by </font>';
AnimString=AnimString+'<a href="mailto:ctoons@excite.com"><font size="2" color="#000000">Cocktoons</font></a><br>';
AnimString=AnimString+'<EMBED SRC="'+ALTURL+'guy.swf"  QUALITY="high" ';
AnimString=AnimString+' wmode="transparent" bgcolor="#000000" background="curtain.gif" ';
AnimString=AnimString+'WIDTH="133" HEIGHT="100" TYPE="application/x-shockwave-flash" ';
AnimString=AnimString+'PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash">\n';

// relocate animation division
ypos=screen.height-255;
xpos=screen.width-125;
eval(layerRef + 'ANIMATE' + styleRef + '.top = ypos');
eval(layerRef + 'ANIMATE' + styleRef + '.left = xpos');

//rewrite animation division
if (w3c) {
	//confirmed
	eval("document.getElementById('ANIMATE').innerHTML=AnimString") ;
	} else {
if (ns4) {
	//confirmed
	eval("document.layers['ANIMATE'].document.open()");
	eval("document.layers['ANIMATE'].document.write(AnimString)");
	eval("document.layers['ANIMATE'].document.close()");
	} else {
if(ie5){
	//confirmed 
	eval("document.getElementById('ANIMATE').innerHTML=AnimString") ;
} else {
if(ie4){
	//confirmed
	eval("layerRef['ANIMATE'].innerHTML=AnimString") ;
}}}}
	} 

//Get the cookie for a list of possible cookies. Honest!
function GetCookie (CookieName) {
  var cname = CookieName + "=";
  var i = 0;
  while (i < document.cookie.length) {
    var j = i + cname.length;
    if (document.cookie.substring(i, j) == cname){
	var leng = document.cookie.indexOf (";", j);
	if (leng == -1) leng = document.cookie.length;
	return unescape(document.cookie.substring(j, leng));
    }
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; //thats -1 plus 1, duh.
  }
  return "*";
}

//Parse Cookie.
function parseCookie(cookieValue, citem) {
   var indx = 0, citemlen =0;
   if ( cookieValue == null ) return "*"//Data has expired or never entered.
   if ( cookieValue == "*"  ) return "*"//Data has expired or never entered.
   for(var i=0; i < citem; i++) {
       indx = ( citem==0 )?0:cookieValue.indexOf("`", indx + 1)+1;}
       citemlen=(cookieValue.indexOf("`",indx)>0)
	    ?cookieValue.indexOf("`", indx+1):cookieValue.length;
   return cookieValue.substring(indx, citemlen);
}

//Write Cookie to disable sound
function SoundOff(){
var expire = new Date ();
expire.setTime (expire.getTime() + (24 * 3600000)); //expire in 24hrs
var WholeCookie = expire + '`' + 'no_sound';
document.cookie = "GMO_globals="+escape (WholeCookie)+";expires="+expire.toGMTString();
PlayMusic(store_midi,store_text,store_V,store_reps);
}

//Write Cookie to enable sound
function SoundOn(){
var expire = new Date ();
expire.setTime (expire.getTime() + (24 * 3600000)); //expire in 24hrs
var WholeCookie = expire + '`' + 'yes_sound';
document.cookie = "GMO_globals="+escape (WholeCookie)+";expires="+expire.toGMTString();
PlayMusic(store_midi,store_text,store_V,store_reps);
}


