// JavaScript Document
// sjhs.js

// Create empty object as sjhs global namespace
var sjhs = {};	

function isMember(element, classname) {
	var classes = element.className;
	if (!classes) {return false;}
	if (classes == classname) {return true;}
	
	var whitespace = /\s+/;
	if (!whitespace.test(classes)) {return false;}
	
	var c = classes.split(whitespace);
	for (var i=0; i < c.length; i++) {
		if (c[i] == classname) {return true;}
	}
	return false;
}
	
Spry.Utils.addLoadListener(function() {
	var elements = document.getElementsByTagName("a");
	for (var i=0; i < elements.length; i++) {
		if (isMember(elements[i], "ContactSJHS")) {
			var a = "mail";
			var b = "to:";
			var c = "ContactUs";
			var d = "@";
			var e = "StJohnHistoricalSociety.org";
			elements[i].href = a + b + c + d + e;
		}
	}	});

function LargerImage() {
// Fix to retrieve actual xlarge/@base value (html src="galleries/{dsGalleries::@base}{dsGallery::xlarge/@base}{@xlargepath}"	
//  Hardcoded value "XLarge"																							
	var image_url = "Galleries/" + dsGalleries.getCurrentRow()["@base"] + "XLarge/" + dsPhotos.getCurrentRow()["@xlpath"];
  return image_url;
}

function getTitle() {
  var curRow = dsPhotos.getCurrentRow();
	var image_title = "";
	if (curRow.title.length > 0) { image_title = image_title + "Title: " + curRow.title; }
	if (curRow.date.length > 0) { image_title = image_title + " Date: " + curRow.date; }
	if (curRow.creator.length > 0) { image_title = image_title + " Creator: " + curRow.creator; }
  return image_title;
}


msProgIDs = ["MSXML2.XMLHTTP.6.0", "MSXML2.XMLHTTP.3.0"];

CreateXMLHttpRequest = function()
{
	var req = null;
	try
	{
		// Try to use the ActiveX version of XMLHttpRequest. This will
		// allow developers to load file URLs in IE7 when running in the
		// local zone.

		if (window.ActiveXObject)
		{
			while (!req && msProgIDs.length)
			{
				try { req = new ActiveXObject(msProgIDs[0]); } catch (e1) { req = null; }
				if (!req) {
					msProgIDs.splice(0, 1);
				}
			}
		}

		// We're either running in a non-IE browser, or we failed to
		// create the ActiveX version of the XMLHttpRequest object.
		// Try to use the native version of XMLHttpRequest if it exists.

		if (!req && window.XMLHttpRequest)
			{ req = new XMLHttpRequest(); }
	}
	catch (e2) { req = null;	}

	return req;
};

function parseQueryString()
{
    var obj = {};
    var nvpairs = location.search.substring(1).split("&");

    for (var idx = 0; idx < nvpairs.length; idx++)
    {
        var tokens = nvpairs[idx].split("=");
        obj[unescape(tokens[0])] = (tokens.length == 2 ) ? unescape(tokens[1]) : undefined;
    }
    return obj;
} 

Spry.Effect.FadeBlind = function(element, options)
{
	Spry.Effect.Cluster.call(this, options);
	
	this.name = 'FadeBlind';
	var duration = 1000;
	var doToggle = false;
	var from = 100;
	var to = 0;

	if (options)
	{
		if (options.duration) { duration = options.duration; } 
		if (options.toggle) { doToggle = options.toggle; } 
		if (options.from) { from = options.from; } 
		if (options.to) { to = options.to; } 
	}

	var options = {duration: duration, from: from, to: to, toggle: doToggle};
	var fadeEffect = new Spry.Effect.Fade(element, options); 
	this.addParallelEffect(fadeEffect);

	var options = {duration: duration, from: from, to: to, toggle: doToggle};
	var blindEffect = new Spry.Effect.Blind(element, options); 
	this.addParallelEffect(blindEffect);

};

Spry.Effect.FadeBlind.prototype = new Spry.Effect.Cluster();
Spry.Effect.FadeBlind.prototype.constructor = Spry.Effect.FadeBlind; 


/// dynamic tooltip
/////////////////////
var offsetxpoint = -80; //Customize x offset of tooltip
var offsetypoint = 20; //Customize y offset of tooltip
var enabletip=false;
var tipobj="";
var ie = false;
var ns6 = false;

function ietruebody(){
	return (document.compatMode && document.compatMode!="BackCompat") ? document.documentElement : document.body;
}

function positiontip(e){
	if (enabletip){
		var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
		var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;

		//Find out how close the mouse is to the corner of the window
		var rightedge=ie&&!window.opera ? ietruebody().clientWidth-event.clientX-offsetxpoint : window.innerWidth-e.clientX-offsetxpoint-20;
		var bottomedge=ie&&!window.opera ? ietruebody().clientHeight-event.clientY-offsetypoint : window.innerHeight-e.clientY-offsetypoint-20;
		var leftedge=(offsetxpoint<0)? offsetxpoint*(-1) : -1000;
		
		//if the horizontal distance isn't enough to accomodate the width of the context menu
		if (rightedge<tipobj.offsetWidth)
		//move the horizontal position of the menu to the left by it's width
			{ tipobj.style.left=ie? ietruebody().scrollLeft+event.clientX-tipobj.offsetWidth+"px" : window.pageXOffset+e.clientX-tipobj.offsetWidth+"px"; }
		else if (curX<leftedge)
			{ tipobj.style.left="5px"; }
		else
		//position the horizontal position of the menu where the mouse is positioned
			{ tipobj.style.left=curX+offsetxpoint+"px"; }
		
		//same concept with the vertical position
		if (bottomedge<tipobj.offsetHeight)
			{ tipobj.style.top=ie? ietruebody().scrollTop+event.clientY-tipobj.offsetHeight-offsetypoint+"px" : window.pageYOffset+e.clientY-tipobj.offsetHeight-offsetypoint+"px"; }
		else
			{ tipobj.style.top=curY+offsetypoint+"px"; }

		tipobj.style.visibility="visible";
	}
}

function InitToolTip() {
	ie=document.all;
	ns6=document.getElementById && !document.all;
	if (ie||ns6){ 
	tipobj=document.all? document.all.dhtmltooltip : document.getElementById ? document.getElementById("dhtmltooltip") : "" ; 
	}
	document.onmousemove=positiontip;
}

function ShowToolTip(thetext, thecolor, thewidth){
	if (ns6||ie){
		if ((typeof thetext === "undefined") || !(thetext) || (thetext == "") || (thetext === "undefined")) { return false; }
		if (typeof thewidth !== "undefined") { tipobj.style.width=thewidth+"px"; }
		if (typeof thecolor !== "undefined" && thecolor!="") { tipobj.style.backgroundColor=thecolor; }
		tipobj.innerHTML=thetext;
		enabletip=true;
	}
	return false;
}


function HideToolTip(){
	if (ns6||ie){
		enabletip = false;
		tipobj.style.visibility = "hidden";
		tipobj.style.left = "-1000px";
		tipobj.style.backgroundColor = '';
		tipobj.style.width = '';
	}
}

/*
/	Transform url name/value parameters to an associative array
*/

function getUrlVariables(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
}
sjhs.urlVariables =  getUrlVariables();

/*
/	consoleTrace class
/	22 Nov 2009
*/

/*
* Constructor
*/

var LOG = 0;
var INFO = 1;
var WARNING = 2;
var ERROR = 3;

function consoleTrace (traceLevel) {
	this._canLog = true;
	this.traceLevel = (typeof traceLevel == "number") ? traceLevel : ERROR;
	this.LOG = LOG;
	this.INFO = INFO;
	this.WARNING = WARNING;
	this.ERROR = ERROR;
}


/*
* Methods
*/
consoleTrace.prototype.assert = function(expression, msg) {
	window.console.assert.apply(window.console, expression, arguments);
};

consoleTrace.prototype.log = function(severity, msg) {
	if (!this._canLog) { return; }
	switch (severity) {
		case "LOG": if (this.LOG < this.traceLevel) { return; } else { break; }
		case "INFO": if (this.INFO < this.traceLevel) { return; } else { break; }
		case "WARNING": if (this.WARNING < this.traceLevel) { return; } else { break; }
		default: break;
	}
	
	try {
		// get the msg arguments
		var msgArgs = Array.prototype.slice.apply(arguments, [1]); 

		// Prepend timestamp (hh.mm.ss.mmm)
		var dt = new Date();
		var tag = dt.getHours()+":"+dt.getMinutes()+":"+dt.getSeconds()+"."+dt.getMilliseconds();
		msgArgs.unshift(tag + " - ");
		
		switch( severity ) {
			case "LOG":
				window.console.log.apply(window.console, msgArgs);
				break;
			case "INFO":
				window.console.info.apply(window.console, msgArgs);
				break;
			case "WARN":
				window.console.warn.apply(window.console, msgArgs);
				break;
			default:
				window.console.error.apply(window.console, msgArgs);
		}
	} catch (exc) {
		if ( !window.console ) {
			_canLog = false; // Permanently disable, when logging is not supported by the browser 
		}
	}
};


/*
/	consoleDebug class
/	8 Nov 2009
*/

/*
* Constructor
*/
function consoleDebug (debugLevel) {
	this._canLog = true;
	this.debugLevel = debugLevel || 3 ;
	this.LOG = 0;
	this.INFO = 1;
	this.WARNING = 2;
	this.ERROR = 3;
}
/*
* Methods
*/
consoleDebug.prototype.assert = function(expression, msg) {
	window.console.assert.apply(window.console, expression, arguments);
};
consoleDebug.prototype.errorMsg = function(msg) {
	if (!this._canLog) { return; }
	try {
		// Prepend timestamp
		var args = arguments;
		var dt = new Date();
		var tag = dt.getHours()+":"+dt.getMinutes()+":"+dt.getSeconds()+"."+dt.getMilliseconds();
		args[0] = tag + " - " + args[0];
		window.console.error.apply(window.console, args);
	} catch (exc) {
		if ( !window.console ) {
			_canLog = false; // Permanently disable, when logging is not supported by the browser 
		}
	}
};
consoleDebug.prototype.infoMsg = function(msg) {
	if (!this._canLog) { return; }
	if (this.debugLevel < this.INFO){ return; }
	try {
		// Prepend timestamp
		var args = arguments;
		var dt = new Date();
		var tag = dt.getHours()+":"+dt.getMinutes()+":"+dt.getSeconds()+"."+dt.getMilliseconds();
		args[0] = tag + " - " + args[0];
		window.console.info.apply(window.console, args);
	} catch (exc) {
		if ( !window.console ) {
			_canLog = false; // Permanently disable, when logging is not supported by the browser 
		}
	}
};
consoleDebug.prototype.logMsg = function(msg) {
	if (!this._canLog) { return; }
	if (this.debugLevel < this.LOG) { return; }
	try {
		// Prepend timestamp
		var args = arguments;
		var dt = new Date();
		var tag = dt.getHours()+":"+dt.getMinutes()+":"+dt.getSeconds()+"."+dt.getMilliseconds();
		args[0] = tag + " - " + args[0];
		window.console.log.apply(window.console, args);
		//window.console.log(msg);
	} catch (exc) {
		if ( !window.console ) {
			_canLog = false; // Permanently disable, when logging is not supported by the browser 
		}
	}
};
consoleDebug.prototype.warningMsg = function(msg) {
	if (!this._canLog) { return; }
	if (this.debugLevel < this.WARNING) { return; }
	try {
		// Prepend timestamp
		var args = arguments;
		var dt = new Date();
		var tag = dt.getHours()+":"+dt.getMinutes()+":"+dt.getSeconds()+"."+dt.getMilliseconds();
		args[0] = tag + " - " + args[0];
		window.console.warn.apply(window.console, args);
	} catch (exc) {
		if ( !window.console ) {
			_canLog = false; // Permanently disable, when logging is not supported by the browser 
		}
	}
};
