
//******************************************************************************// 	sbwidgets.js
//		Generic javascript utils
//		portions copyright 2001-2009 Clik2.com, SpottsBrothers.com, David Spotts
//
//******************************************************************************


var gDebug = false;

function debugStr(pStr){
	if (!gDebug){
		return;
	}
	document.writeln(pStr + '<br>');
}

function setPermanentCookie (name, value) {
	var sExpD = new Date();
	var sYear = sExpD.getFullYear();
	debugStr("sYear=" + sYear);
	sYear += 30;
	debugStr("sYear=" + sYear);
	sExpD.setFullYear(sYear);
	setCookie(name, value, sExpD);
}

function getCookieVal (offset) {  

	var endstr = document.cookie.indexOf (";", offset);  

	if (endstr == -1)    
		endstr = document.cookie.length;  
	return unescape(document.cookie.substring(offset, endstr));

}

function getCookie (name) {  

	var arg = name + "=";  
	var alen = arg.length;  
	var clen = document.cookie.length;  
	var i = 0;  

	while (i < clen) {    
		var j = i + alen;    
		if (document.cookie.substring(i, j) == arg)      
			return getCookieVal (j);    
		i = document.cookie.indexOf(" ", i) + 1;    
		if (i == 0) break;   
	}  
	return null;
}

function setCookie (name, value) {  

	var argv = setCookie.arguments;  
	var argc = setCookie.arguments.length;  
	var expires = (argc > 2) ? argv[2] : null;  
	var path = (argc > 3) ? argv[3] : null;  
	var domain = (argc > 4) ? argv[4] : null;  
	var secure = (argc > 5) ? argv[5] : false;  

	document.cookie = name + "=" + escape (value) + 
			((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
			((path == null) ? "" : ("; path=" + path)) +  
			((domain == null) ? "" : ("; domain=" + domain)) +    
			((secure == true) ? "; secure" : "");

}


function deleteCookie (name) {  

	var exp = new Date();  

	exp.setTime (exp.getTime() - 1);  
	var cval = getCookie (name);  
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}



function maximizewindow() {
	window.moveTo(0,0);
	if (document.all) {
		window.resizeTo(screen.availWidth,screen.availHeight);
	}
	else if (document.layers) {
		if (window.outerHeight<screen.availHeight||window.outerWidth<screen.availWidth){
			window.outerHeight = screen.availHeight;
			window.outerWidth = screen.availWidth;
		}
	}
}

function openWindowCentered(thisURL, pWidth, pHeight){
	var sWidth = screen.availWidth;
	var sHeight = screen.availHeight;
	var sTop = (sHeight - pHeight) / 2;
	var sLeft = (sWidth - pWidth) / 2;
	var sParms = 'top=' + sTop + ',left=' + sLeft + ',width=' + pWidth + ',height=' + pHeight;
	openWindow(thisURL, '', sParms);
}

function openWindow(thisURL,thisName,thisArgs) {
	internalURL = thisURL;
	internalName = thisName;
	internalArgs = thisArgs;
	if (internalURL == '') {
		exit;
	}
	if (internalName == '') {
		internalName = 'popupWin';
	}
	if (internalArgs == '') {
		internalArgs = 'scrollbars,resizable,toolbar,width=640,height=480,left=50,top=50';
	}
	popupWin = window.open(internalURL,internalName,internalArgs);
 	popupWin.focus();
}

function openWindowFS(thisURL,screenw,screenh,options) {
	thisW = screenw - 10;
	thisH = screenh - 50;	
	internalArgs = 'resizable,left=0,top=0,width=' + thisW + ',height=' + thisH + options;
	openWindow(thisURL,'popupWinFS',internalArgs);
}

function openWindow640(thisURL) {
	openWindow(thisURL,'popupWin640','');
}

function openWindowWH(thisURL,thisW,thisH) {
	internalArgs = 'scrollbars,location,resizable,status,left=20,top=20,width=' + thisW + ',height=' + thisH;
	openWindow(thisURL,'popupWinWH',internalArgs);
}

function openWindowDemo(thisURL) {
	internalName = 'popupWinDemo';
	internalArgs = 'scrollbars,resizable,toolbar,left=50,top=50,width=475,height=570';
  openWindow(thisURL,internalName,internalArgs);
}

function toggleCheckBoxes(dowhat) {
	for (i=0; i<document.forms[0].elements.length; i++) {
		if (document.forms[0].elements[i].type == 'checkbox') {
			if (dowhat == 'on') {
				document.forms[0].elements[i].checked = true;
			} else {
				document.forms[0].elements[i].checked = false;
			}
		}
	}
}

function getUTCMins(){
	var d = new Date();
	var sCMS = d.getTime();
	var millisMin = 1000 * 60;
	var sMins = sCMS / millisMin;
	sMins = parseInt(sMins);
	return(sMins);
}

function getAudioTypeTag(filepath){
// Get Operating System 
var isWin = navigator.userAgent.toLowerCase().indexOf("windows") != -1
var isFireFox = navigator.userAgent.toLowerCase().indexOf("firefox") != -1

if (isWin) {
    // Use MIME type = "application/x-mplayer2";
	visitorOS="Windows";
} else {
    // Use MIME type = "audio/mpeg"; // or audio/x-wav or audio/x-ms-wma, etc.
	visitorOS="Other";
}

// Get the MIME type of the audio file from its extension (for non-Windows browsers)
var mimeType = "audio/mpeg"; // assume MP3/M3U
var objTypeTag = "application/x-mplayer2"; // The Windows MIME type to load the WMP plug-in in Firefox, etc.

var theExtension = filepath.substr(filepath.lastIndexOf('.')+1, 3); // truncates .aiff to aif
if (theExtension.toLowerCase() == "wav") { mimeType = "audio/x-wav"};
if (theExtension.toLowerCase() == "aif") { mimeType = "audio/x-aiff"}; 
if (theExtension.toLowerCase() == "wma") { mimeType = "audio/x-ms-wma"};
if (theExtension.toLowerCase() == "mid") { mimeType = "audio/mid"};
// Add additional MIME types as desired

if (visitorOS != "Windows") { 
objTypeTag = mimeType; // audio/mpeg, audio/x-wav, audio/x-ms-wma, etc.
};
	/*
	if (isFireFox) {
		objTypeTag = "audio/x-ms-wma";
	}
	*/
	return(objTypeTag);
}

// Pop-Up Embedder Script by David Battino, www.batmosphere.com
// Version 2006-05-31  
// OK to use if this notice is included
   
function BatmoAudioPop(filedesc,filepath,WindowNumber) 
{

// Get Operating System 
var isWin = navigator.userAgent.toLowerCase().indexOf("windows") != -1
if (isWin) {
    // Use MIME type = "application/x-mplayer2";
	visitorOS="Windows";
} else {
    // Use MIME type = "audio/mpeg"; // or audio/x-wav or audio/x-ms-wma, etc.
	visitorOS="Other";
}

// Get the MIME type of the audio file from its extension (for non-Windows browsers)
var mimeType = "audio/mpeg"; // assume MP3/M3U
var objTypeTag = "application/x-mplayer2"; // The Windows MIME type to load the WMP plug-in in Firefox, etc.

var theExtension = filepath.substr(filepath.lastIndexOf('.')+1, 3); // truncates .aiff to aif
if (theExtension.toLowerCase() == "wav") { mimeType = "audio/x-wav"};
if (theExtension.toLowerCase() == "aif") { mimeType = "audio/x-aiff"}; 
if (theExtension.toLowerCase() == "wma") { mimeType = "audio/x-ms-wma"};
if (theExtension.toLowerCase() == "mid") { mimeType = "audio/mid"};
// Add additional MIME types as desired

if (visitorOS != "Windows") { 
objTypeTag = mimeType; // audio/mpeg, audio/x-wav, audio/x-ms-wma, etc.
};

    PlayerWin = window.open('',WindowNumber,'width=320,height=217,top=0,left=0,screenX=0,screenY=0,resizable=0,scrollbars=0,titlebar=0,toolbar=0,menubar=0,status=0,directories=0');

    PlayerWin.focus();
    PlayerWin.document.writeln("<html><head><title>" + filedesc + "</title></head>");
    PlayerWin.document.writeln("<body bgcolor='#9999ff'>"); // specify background img if desired
    PlayerWin.document.writeln("<div align='center'>");
    PlayerWin.document.writeln("<b style ='font-size:18px;font-family:Lucida,sans-serif;line-height:1.6'>" + filedesc + "</b><br />");
    PlayerWin.document.writeln("<object width='280' height='69'>");
    PlayerWin.document.writeln("<param name='src' value='" +  filepath + "'>");
    PlayerWin.document.writeln("<param name='type' value='" + objTypeTag + "'>");
    PlayerWin.document.writeln("<param name='autostart' value='1'>");
    PlayerWin.document.writeln("<param name='showcontrols' value='1'>"); 
    PlayerWin.document.writeln("<param name='showstatusbar' value='1'>");
    PlayerWin.document.writeln("<embed src ='" + filepath + "' type='" + objTypeTag + "' autoplay='true' width='280' height='69' controller='1' showstatusbar='1' bgcolor='#9999ff' kioskmode='true'>");
    PlayerWin.document.writeln("</embed></object></div>");
    PlayerWin.document.writeln("<p style='font-size:12px;font-family:Lucida,sans-serif;text-align:center'><a href='" + filepath +"'>Download this file</a> <span style='font-size:10px'>(right-click or Control-click)</span></p>");
    PlayerWin.document.writeln("<form><div align='center'><input type='button' value='Close this window' onclick='javascript:window.close();'></div></form>");
    PlayerWin.document.writeln("</body></html>");

    PlayerWin.document.close(); // "Finalizes" new window
}


// Batmosphere Embedded Media Player, version 2006-05-31 
// Written by David Battino, www.batmosphere.com
// OK to use if this notice is included

// This function reads an MP3 URL and title from the referring page and generates embedding code to play back the audio file.
// Windows browsers (except for Internet Explorer) will play back the file with the Windows Media Player *plugin.* Internet Explorer will use Windows Media Player.
// Non-Windows browsers will play back the file with their standard audio handler for the MIME type audio/mpeg. On Macs, that handler will usually be QuickTime.

var audioFolder = ""; // If you have a default audio directory, e.g., http://www.your-media-hosting-site.com/sounds/, you can put it here to make links on the referring page shorter.

function embedPlayer(MP3title,MP3URL) { 

// Get Operating System 
var isWin = navigator.userAgent.toLowerCase().indexOf("windows") != -1
if (isWin) { // Use MIME type application/x-mplayer2
	visitorOS="Windows";
} else { // Use MIME type audio/mpeg, audio/x-wav, etc.
	visitorOS="Other";
}

var audioURL = audioFolder + MP3URL;

var objTypeTag = "application/x-mplayer2"; // The  MIME type to load the WMP plugin in non-IE browsers on Windows
if (visitorOS != "Windows") { objTypeTag = "audio/mpeg"}; // The MIME type for Macs and Linux 

document.writeln("<div>");
document.writeln("<strong style='font-size:18px; position:relative; top:-28px'>" + MP3title + "&nbsp;</strong>"); // Adjust font style to taste
document.writeln("<object width='280' height='69'>"); // Width is the WMP minimum. Height = 45(WMP controls) + 24 (WMP status bar) 
document.writeln("<param name='type' value='" + objTypeTag + "'>");
document.writeln("<param name='src' value='" + audioURL + "'>");
document.writeln("<param name='autostart' value='0'>");
document.writeln("<param name='showcontrols' value='1'>");
document.writeln("<param name='showstatusbar' value='1'>");
document.writeln("<embed src ='" + audioURL + "' type='" + objTypeTag + "' autoplay='false' autostart='0' width='280' height='69' controller='1' showstatusbar='1' bgcolor='#ffffff'></embed>"); // Firefox and Opera Win require both autostart and autoplay
document.writeln("</object>");
document.writeln("</div>");
document.close(); // Finalizes the document
}


// JavaScript Document
// Pop-Up Audio Embedder Script by David Battino, www.batmosphere.com; Object tag implementation by Mark Levitt, http://digitalmedia.oreilly.com
// OK to use if this notice is included
var UniqueID = 314 // Make each link open in a new window.
function AudioPop(soundfiledesc,soundfilepath) {

	var winHeight = 190;
	var winWidth = 320;
	
	     // center the window 
    var sWidth = screen.availWidth;
	var sHeight = screen.availHeight;
	var sTop = (sHeight - winHeight) / 2;
	var sLeft = (sWidth - winWidth) / 2;

PlayerWin = window.open('',UniqueID,'width=' + winWidth + ',height=' + winHeight +  ',top=' + sTop +  ',left=' + sLeft +  ',resizable=0,scrollbars=0,titlebar=0,toolbar=0,menubar=0,status=0,directories=0,personalbar=0');
PlayerWin.focus(); 
var winContent = "<html><head><title>" + soundfiledesc + "</title></head><body bgcolor='#9E9E9E'  >";
winContent += "<center><b style='font-size:18px;font-family:Verdana,sans-serif;line-height:1.5'>" + soundfiledesc + "</b>";

winContent += "<object width='240' height='42'>";

 // 090116 set object type for firefox windows w/o quicktime
 var objTypeTag = getAudioTypeTag(soundfilepath);
 winContent += "<param name='type' value='" + objTypeTag + "'>";

winContent += "<param name='src' value='" +  soundfilepath + "'>";
winContent += "<param name='autoplay' value='true'>";
winContent += "<param name='controller' value='true'>";
//winContent += "<param name='bgcolor' value='#9E9E9E'>";
winContent += "<embed src='" + soundfilepath + "' autostart='true' loop='false' width='240' height='42' controller='true' bgcolor='#9e9e9e'  ></embed>";
winContent += "</object>";

winContent += "<p style='font-size:12px;font-family:Verdana,sans-serif;text-align:center'><a href='"+soundfilepath+"'>Download this file</a> <span style='font-size:10px'>(right-click or Option-click)</span></p>";

winContent += '<font face="\'Trebuchet MS\',Trebuchet,Verdana,Sans-serif">';

winContent += '<center><font size="2"><a href="javascript:window.close();">Close This Window</a></font></center>';

winContent += "</font>";
winContent += "</body></html>";
PlayerWin.document.write(winContent);
PlayerWin.document.close(); // "Finalizes" new window
UniqueID = UniqueID + 1
}

///////////////////////////////////////////////////

// Pop-Up Photo Embedder by David Battino, www.batmosphere.com
// OK to use if this notice is included

var UniqueID = 314 // Identify pop-ups so subsequent ones don't replace current one; increment each time function runs. 
// Could pass this parameter to the function so clicking the link twice wouldn't open another copy....
var now = new Date

function PhotoPop(filedesc,filepath,origwidth,orighite,caption) {

var PopWin = window.open('',UniqueID,'top=0,left=0,position=0,width='+screen.availWidth+',height='+screen.availHeight+',resizable=1,scrollbars=1,titlebar=0,toolbar=0,menubar=0,status=0,directories=0'); 
PopWin.focus(); 

var winContent = "<html><head><title>" + filedesc + "</title>";
winContent += "<link href='sctstyle.css' rel='stylesheet' type='text/css'>";
winContent += "</head>";

winContent += "<body bgcolor='#ffffff'>";
winContent += "<div align='center'><form>";
winContent += "<h3><br>" + filedesc + "</h3>";
winContent += "<img src='" + filepath + "' id='image1' name='image1' alt='Loading " + filedesc + " image...' width='" + origwidth + "' height='" + orighite + "'title='" +filedesc + "'>";
winContent += "<p style='font-size:12px;font-family:Verdana,sans-serif;text-align:center'>" + caption + "</p>";

winContent += "<br><input type='button' value='Close this window' onClick='javascript:window.close()'><br><br>";
winContent += "</form></div>";
winContent += "</body></html>";
PopWin.document.write(winContent);
PopWin.document.close(); // "Finalizes" new window
UniqueID = UniqueID + 1 // gives subsequent pop-ups new ID
}

///////////////////////////////////////////////////

// Pop-Up Audio/Photo Embedder Script by David Battino, www.batmosphere.com
// v 2005-10-04
// OK to use if this notice is included

function EnhAudioPop(popuptitle,imgpath,imgwidth,imgheight,caption,soundpath,UniqueID) { // Add error handling?

     var winWidth = Number(imgwidth) + 100;
     var rawHeight = Number(imgheight) + 168 + caption.length/7; // calculate window height based on caption length
     var winHeight = Math.round(rawHeight * Math.pow(10,0))/Math.pow(10,0); // round to integer
     
     if (winWidth < 320) {
     	winWidth = 320;
     }
     if (winHeight < 320) {
     	winHeight = 320;
     }
     
     // center the window 
    var sWidth = screen.availWidth;
	var sHeight = screen.availHeight;
	var sTop = (sHeight - winHeight) / 2;
	var sLeft = (sWidth - winWidth) / 2;

     
     MediaWin = window.open('',UniqueID,'width=' + winWidth + ',height=' + winHeight + ',top=' + sTop +  ',left=' + sLeft + ',resizable=0,scrollbars=0,titlebar=0,toolbar=0,menubar=0,status=0,directories=0,personalbar=0');
     MediaWin.focus();
     var winContent = "<html><head><title>" + popuptitle + "</title></head>";
     winContent += "<body bgcolor='#9E9E9E' >"; // check image path
    
     winContent += "<div align='center'>";
     winContent += "<br /><br />"; // could use padding
     winContent += "<img src='" + imgpath + "' id='image1' border='2' alt='" + popuptitle + "' width='" + imgwidth + "' height='" + imgheight + " 'title='" + popuptitle + "' />";
     winContent += "<br />";
     winContent += "<object width='" + imgwidth + "' height='42' >"; // add 4 to width to align controller with img border?
	
	 // 090116 set object type for firefox windows w/o quicktime
     var objTypeTag = getAudioTypeTag(soundpath);
     winContent += "<param name='type' value='" + objTypeTag + "'>";

     winContent += "<param name='src' value='" + soundpath + "'>";
     winContent += "<param name='autoplay' value='true'>";
     winContent += "<param name='controller' value='true'>";
     winContent += "<embed src ='" + soundpath + "' autostart='true' loop='false' width='" + imgwidth + "' height='42' controller='true' bgcolor='#9e9e9e' >";
     winContent += "</embed></object>";

     winContent += "<div style='width: " + imgwidth + "px; margin: 0px; padding: 0px; text-align:left;'>"; // restrict caption width to image width
     winContent += "<p style='font-size:12px;font-family:Verdana,sans-serif'>" + caption + "</p>";
     winContent += "</div>";
     winContent += "<p style='font-size:12px;font-family:Verdana,sans-serif'><a href='" + soundpath +"'>Download audio file</a> <span style='font-size:10px'>(right-click or Option-click)</span>";
     winContent += "<br><a href='#' onClick='javascript:window.close();'>Close this window</a></p>";

     winContent += "</div>";
     winContent += "</body></html>";
     MediaWin.document.write(winContent); 
     MediaWin.document.close(); // "Finalizes" new window
}

function InfoPop(pTitle,pContent, pWidth, pHeight) {

	
	var winHeight = 160;
	var winWidth = 320;
	if (pWidth > 0) {
		winWidth=pWidth;
	}
	if (pHeight > 0) {
		winHeight = pHeight;
	}
	
	     // center the window 
    var sWidth = screen.availWidth;
	var sHeight = screen.availHeight;
	var sTop = (sHeight - winHeight) / 2;
	var sLeft = (sWidth - winWidth) / 2;

InfoWin = window.open('',UniqueID,'width=' + winWidth + ',height=' + winHeight +  ',top=' + sTop +  ',left=' + sLeft +  ',resizable=0,scrollbars=0,titlebar=0,toolbar=0,menubar=0,status=0,directories=0,personalbar=0');
InfoWin.focus(); 
var winContent = "<html><head><title>" + pTitle + "</title></head><body bgcolor='#9E9E9E' >";
winContent += '<font face="\'Trebuchet MS\',Trebuchet,Verdana,Sans-serif">';

winContent += pContent;
winContent += '<br><center><font size="2"><a href="javascript:window.close();">Close This Window</a></font></center>';

winContent += "</font></body></html>";

InfoWin.document.write(winContent);
InfoWin.document.close(); // "Finalizes" new window
UniqueID = UniqueID + 1

	return(InfoWin);
	
}


function nameFromImage(pImageURL){
	var sLastSlash = pImageURL.lastIndexOf("/");
	var sName = pImageURL.substring(sLastSlash+1, pImageURL.length);
	var sLastDot = sName.lastIndexOf(".");
	sName = sName.substring(0, sLastDot);
	return(sName);
}

function popPhoto(pImageURL, pImageWidth, pImageHeight, pWindowTitle){
	var winWidth = pImageWidth + 20;
	var winHeight = pImageHeight + 70;
	// center the window 
    var sWidth = screen.availWidth;
	var sHeight = screen.availHeight;
	var sTop = (sHeight - winHeight) / 2;
	var sLeft = (sWidth - winWidth) / 2;
	
	
	var winContent = "<html><head><title>" + pWindowTitle + "</title></head>";
    winContent += "<body><font face='Verdana'>"; 
    winContent += "<center>";
    
    winContent += "<script type='text/javascript' src='includes/norightclick.js'>";
    winContent += "</script>";
    

    winContent += '<img src="' + pImageURL + '" border="0"';
    winContent += ' width="' + pImageWidth + '"';
    winContent += ' height="' + pImageHeight + '"';
    winContent += '>';

    winContent += "<br><br><a href='javascript:window.close();'>Close this window</a>";
    
    winContent += "</center></font></body></html>";
    
    var sPhotoWin =  window.open('',UniqueID,'width=' + winWidth + ',height=' + winHeight + ',top=' + sTop +  ',left=' + sLeft + ',resizable=0,scrollbars=0,titlebar=0,toolbar=0,menubar=0,status=0,directories=0,personalbar=0');
    sPhotoWin.focus();
    
    sPhotoWin.document.write(winContent); 
    sPhotoWin.document.close(); // "Finalizes" new window

}

function escapeURL(pURL){
	var sNewURL = pURL;
	
	sNewURL = sNewURL.replace(' ','%20');
	sNewURL = sNewURL.replace('?','%3F');
	sNewURL = sNewURL.replace('&','%26');
	
	return(sNewURL);
}

var gPPDebug=false;

function payPalURL(pBusiness, pItemName, pItemNumber, pAmount, pSuccessURL, pCancelURL){
	var sURL = "https://www.paypal.com/xclick/business=";
	sURL += pBusiness;
	sURL += "&item_name=" + escape(pItemName);

	sURL += "&item_number=" + escape(pItemNumber);
	if (gPPDebug){
		sURL += "&amount=0.01";
	} else {
		sURL += "&amount=" + escape(pAmount);
	}
	sURL += "&return=" + escapeURL(pSuccessURL);
	sURL += "&cancel_return=" + escapeURL(pCancelURL);
	//sURL += "&notify_url=" + escape(pSuccessURL);
	return(sURL);
}

function doRedirect(pURL){
	window.location = pURL;
}



function enlargeGalleryItem(pImageBase, pName){
	pName += ".large.jpg";

	var pImageURL = "gallery/"+pName;
	var pImageWidth = 526;
	var pImageHeight = 338;

	var pTitle = "Enlargement";
	pImageURL = pImageBase + "/" + pImageURL;

	popPhoto(pImageURL, pImageWidth, pImageHeight, pTitle);
}

