/* 	BLRollOver and BLPopup
	Author: Mikael Ramirez

	This library is meant to supersede the DreamWeaver mouse over library and works with
	PNG fixes applied for IE 6.

 	Credits: 
	http://events in IE 5
*/

if (typeof Array.prototype.push != "function") {
	Array.prototype.push = ArrayPush;
	function ArrayPush(value) {
		this[this.length] = value;
	}
}

if (typeof blurri == "undefined")
	var blurri = {};
if (typeof blurri.blRollOver == "undefined")
	blurri.blRollOver = {};
if (typeof blurri.blPopup == "undefined")
	blurri.blPopup = {};
if (typeof blurri.lib == "undefined")
	blurri.lib = {};

blurri.timerID  = 0;
blurri.TIME_OUT_LEN = 800;
blurri.offX = 0;
blurri.offY = 0;
blurri.ieFix = false;
blurri.callbackData = null;
blurri.callbackClear = null;

if (!window.opera) {
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
	 var iever = new Number(RegExp.$1) // capture x.x portion and store as a number
	 if (iever >= 6)
		blurri.ieFix = true;
	}
}
/* START POPUP CODE */ 
blurri.blPopup = function(_cs, _id, _callbackData, _callbackClear) {
 	if (typeof _callbackData != 'undefined')
		blurri.callbackData = _callbackData;
	if (typeof _callbackClear != 'undefined')
		blurri.callbackClear = _callbackClear;
	blurri.blPopup.s = {classname: _cs, popup: _id};
	blurri.lib.addEvent(window, "load", blurri.lib.setupPopup);
}
blurri.lib.setupPopup = function() {
	var cs = blurri.blPopup.s.classname;
	if (!cs.length)
		return;
	if (!(blurri.blPopup.popup = document.getElementById(blurri.blPopup.s['popup'])))
		return;

	/* 	in ie6 and 5.5 the popup will not cover form controls even if the z-index is higher than them
		so the only fix is to use an iframe underneath it in these browsers */
	if (blurri.ieFix) {
		var shim = document.createElement('iframe');
		shim.id = 'popupShim';
		shim.name = 'popupShim';
		shim.style.position = 'absolute';
		shim.style.visibility = 'hidden';
		shim.style.left = 0;
		shim.style.top = 0;
		shim.width = 2;
		shim.height = 2;
		shim.src = 'about:blank';
		shim.frameBorder = 0;
		shim.scrolling = 'no';
		blurri.blPopup.shim = shim;
		var bodyRef = document.getElementsByTagName("body").item(0);
		bodyRef.appendChild(shim);
	}

	var b = blurri.lib.getElementsByClassName(null, "a", cs);
	if (b.length) {
		for (var i=0, len=b.length; i<len; i++) {
			blurri.lib.addEvent(b[i], 'mouseover', blurri.lib.popupOver);
			blurri.lib.addEvent(b[i], 'mouseout', blurri.lib.popupOut);
		}
	}
	delete blurri.blPopup.s;
}
blurri.lib.popupShow = function(sh) {
	var popup = blurri.blPopup.popup;
	var shim = blurri.blPopup.shim;
	popup.style.visibility = sh;

	if (blurri.ieFix && (typeof shim != undefined)) {
		shim.style.visibility = sh;
		shim.style.left  = popup.style.left;
		shim.style.top = popup.style.top;
		shim.style.width = popup.clientWidth;	
		shim.style.height = popup.clientHeight;
	}
}
blurri.lib.popupOver = function() {
	blurri.timerID = window.setTimeout ("blurri.lib.popupShow('visible')", blurri.TIME_OUT_LEN);
	blurri.lib.addEvent(this, 'mousemove', blurri.lib.popupMove);
	if (blurri.callbackData)
		blurri.callbackData(blurri.blPopup.popup, this.id);
}
blurri.lib.popupOut = function() {
	if (blurri.timerID) {
		window.clearTimeout (blurri.timerID);
		blurri.timerID = 0;
	}
	blurri.lib.popupShow('hidden');
	blurri.lib.removeEvent(this, 'mousemove', blurri.lib.popupMove);
	if (blurri.callbackClear)
		blurri.callbackClear(blurri.blPopup.popup);
}
blurri.lib.popupMove = function(e) {
	blurri.lib.movePopup(blurri.blPopup.popup, e);
}
blurri.lib.movePopup = function(obj, e) {
	var p = blurri.lib;
	var shim = blurri.blPopup.shim;
	var off = 10;
	var objx = parseInt(p.mx(e))+off;
	var objy = parseInt(p.my(e))+off;

	if (objx + obj.clientWidth >= (p.getViewportWidth()-15))
		objx -= (obj.clientWidth+(off*2));
	if (objy + obj.clientHeight >= (p.getViewportHeight()+p.getVerticalScroll()-15))
		objy -= (obj.clientHeight+(off*2));

	obj.style.left = (objx.toString() + 'px');
	obj.style.top = (objy.toString() + 'px');

	if (blurri.ieFix && (typeof shim != undefined)) {
		shim.style.left  = obj.style.left;
		shim.style.top = obj.style.top;
	}
	
}
/* END POPUP CODE */

/* START ROLLOVER CODE */
blurri.blRollOver = function (_array) {
	blurri.blRollOver.s = _array;
	blurri.lib.addEvent(window, "load", blurri.lib.setupButtons);
}
blurri.lib.setupButtons = function() {
	var da = blurri.blRollOver.s;
	if (!da)
		return;
	for (var c in da) {
		var pi1 = new Image();
		var pi2 = new Image();
		pi1.src = da[c][0];
		pi2.src = da[c][1];
		var b = blurri.lib.getElementsByClassName(null, "a", c);
		if (!b.length || da[c].length != 2)
			continue;
		for (var i=0, len=b.length; i<len; i++) {
			var img = b[i].getElementsByTagName("img");
			if (img.length) {
				b[i].outSrc = da[c][0];
				b[i].overSrc = da[c][1];
				blurri.lib.addEvent(b[i], 'mouseover', blurri.lib.buttonOver);
				blurri.lib.addEvent(b[i], 'mouseout', blurri.lib.buttonOut)
			}
		}
	}
	delete blurri.blRollOver.s;
}
blurri.lib.buttonOver = function() {
	var img = this.getElementsByTagName("img");
	img[0].src = this.overSrc;
}
blurri.lib.buttonOut = function() {
	var img = this.getElementsByTagName("img");
	img[0].src = this.outSrc;
}
/* END ROLLOVER CODE */

/* COMMON LIBRARY CODE */
blurri.lib.addEvent = function(obj, type, fn) {
	if (obj.attachEvent) {
		obj['e'+type+fn] = fn;
		obj[type+fn] = function() { obj['e'+type+fn](window.event); }
		obj.attachEvent('on'+type, obj[type+fn]);
	}
	else
		obj.addEventListener(type, fn, false);
}
blurri.lib.removeEvent = function(obj, type, fn) {
	if (obj.detachEvent) {
		obj.detachEvent( 'on'+type, obj[type+fn] );
		obj[type+fn] = null;
	}
	else
		obj.removeEventListener(type, fn, false);
}
blurri.lib.getElementsByClassName = function (oElm, strTagName, strClassName) {
	if (oElm == null)
		oElm = document;
	else
		oElm = document.getElementById(oElm);
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++) {
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className))
			arrReturnElements.push(oElement);
	}
	return (arrReturnElements);
}
// get the mouse x position
blurri.lib.mx = function(e) { 
	if (!e)
		var e = window.event;
	if (e && e.pageX)
		return e.pageX;
	else if (e && e.clientX) {
		return e.clientX + (
			document.documentElement.scrollLeft ?
			document.documentElement.scrollLeft :
			document.body.scrollLeft);
	}
	else
		return 0;
}
// get the mouse y position
blurri.lib.my = function(e) {
	if (!e)
		var e = window.event;
	if (e && e.pageY)
		return e.pageY;
	else if (e && e.clientY) {
		return e.clientY + (
			document.documentElement.scrollTop ?
			document.documentElement.scrollTop :
			document.body.scrollTop);
	}
  	else
		return 0;
}
// initialize this lib
blurri.init = function () {
	var p = blurri.lib;
	// All browsers but IE
	if (window.innerWidth) { 
		p.getViewportWidth = function()		{ return window.innerWidth; };
		p.getViewportHeight = function()	{ return window.innerHeight; };
		p.getHorizontalScroll = function()	{ return window.pageXOffset; };
		p.getVerticalScroll = function()	{ return window.pageYOffset; };
	}
	else if (document.documentElement && document.documentElement.clientWidth) {
	    // These functions are for IE 6 when there is a DOCTYPE
		p.getViewportWidth = function()		{ return document.documentElement.clientWidth; };
		p.getViewportHeight = function()	{ return document.documentElement.clientHeight; };
		p.getHorizontalScroll = function()	{ return document.documentElement.scrollLeft; };
		p.getVerticalScroll = function() 	{ return document.documentElement.scrollTop; };
	}
	else if (document.body.clientWidth) {
	    // These are for IE4, IE5, and IE6 without a DOCTYPE
		p.getViewportWidth = function()		{ return document.body.clientWidth; };
		p.getViewportHeight = function()	{ return document.body.clientHeight; };
		p.getHorizontalScroll = function()	{ return document.body.scrollLeft; };
		p.getVerticalScroll = function()	{ return document.body.scrollTop; }
	}
	blurri.lib.addEvent (window, "load", blurri.init);
	delete blurri.init;
};
blurri.lib.addEvent (window, "load", blurri.init);

var BLRollOver = blurri.blRollOver;
var BLPopup = blurri.blPopup;
