/* Namespace */
var bnw = {};

/* Configuration */
bnw.config = {
	blankSrc: '/templates/abs/biblesearch/images/spacer.gif',
	brokenImage: '/templates/abs/biblesearch/images/spacer.gif',
	rollOverClass: 'bnwRollOver',
	dropdownClass: 'bnwDropdown',
	popUps: [
		{
			expr: '.bnwPopup',
			windowName: '_blank'
		},
		{
			expr: '.bnwPhotoPopup',
			windowName: 'bnwPhotoPopup',
			params: 'width=443, height=408',
			queryExtra: 'popup=true'
		},
		{
			expr: '.bnwBackLink',
			href: 'javascript: history.go(-1);'
		}
	]
};

/* Initialization */
$(document).ready(function() {
	bnw.display.assignImgFlips();
	bnw.display.assignSpecialLinks();
	if ( /MSIE ((5\.5)|[6])/.test(navigator.userAgent) && navigator.platform == 'Win32' ) {
		bnw.display.fixPngs();
	}
	bnw.display.fixBrokenImages();
	//bnw.site.toolTip();
});

/* Site */
bnw.site = {
	toolTip: function()	{
		$('a[title]').hover(showToolTip, hideToolTip);
		$('a[title]').focus(showToolTip);
		$('a[title]').blur(hideToolTip);
		
		function showToolTip()	{
			var $this = $(this);
			var title = $this.attr('title');
			$('body').append('<div id="tooltip"><div id="tooltipInner"></div></div>');
			$('#tooltipInner').text(title);
			$this.removeAttr('title');
			var offset = $this.offset();
			var width = $this.width();
			var left = parseInt(offset.left) + width - 50 + 'px';
			var top = parseInt(offset.top) - 55 + 'px';
			$('#tooltip').css({ top:top, left:left });
		}
		
		function hideToolTip()	{
			var title = $('#tooltipInner').text();
			$('#tooltip').remove();
			$(this).attr('title', title);
		}
	}
};
/* /Site */

/* Utilities */
bnw.util = {
	cookie: {
		set: function(name, value, expire) {
			var expires;

			if ( expire ) {
				var date = new Date();
				date.setTime(date.getTime() + expire);
				expires = '' + date.toGMTString();
			} else {
				var expires = '';
			}

			document.cookie = name + '=' + value + expires + '; path=/';
		},

		get: function(name) {
			var nameEQ = name + '=';
			var ca = document.cookie.split(';');

			for ( var i=0; i < ca.length; i++ ) {
				var c = ca[i];
				while ( c.charAt(0)==' ' ) c = c.substring(1, c.length);
				if ( c.indexOf(nameEQ) == 0 ) return c.substring(nameEQ.length,c.length);
			}
			return null;
		},

		del: function(name) {
			bnw.util.cookie.set(name, ';', -1);
		}
	},

	inArray: function (value, arr) {
		for ( var i in arr ) {
			if ( arr[i] == value ) {
				return i;
			}
		}
		return false;
	}
}; /* /Utilities */

/* DOM */
bnw.dom = {
	getPos: function(obj) {
		var curleft = curtop = 0;
		if (obj.offsetParent) {
			curleft = obj.offsetLeft
			curtop = obj.offsetTop
			while (obj = obj.offsetParent) {
				curleft += obj.offsetLeft
				curtop += obj.offsetTop
			}
		}
		return {x: curleft, y: curtop};
	}
}; /* /DOM */

/* Display */
bnw.display = {
	imageFlips: new Array(),

	fixPngs: function() {
		var type, background_image, background_repeat;

		$('img, input[type=\'image\']').each(function() {
			type = this.tagName.toLowerCase();

			if ( type == 'input' || type == 'img' ) {
				if ( (type == 'input' && this.getAttribute('type') == 'image') || this.src.toLowerCase().search(/\.png$/) ) {
					bnw.display.fixPng(this);
					$(this).bind('propertychange', function() {
						bnw.display.fixPng(this);
					});
				}
			}

			background_image = $(this).css('background-image');

			if ( background_image.toLowerCase().search(/\.png"\)$/) < 0 ) return;

			background_image = background_image.substring(5, background_image.length-2);
			background_repeat = $(this).css('background-repeat');
			this.style.background = 'none';

			if ( background_repeat == 'no-repeat' ) {
				this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + background_image + "', sizingMethod='crop')";
			} else {
				this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + background_image + "', sizingMethod='scale')";
			}
		});
	},

	fixing: false,
	fixPng: function(obj) {
		if ( bnw.display.fixing ) return;

		bnw.display.fixing = true;

		var src = obj.src;

		if ( new RegExp(bnw.config.blankSrc).test(src) ) {
			bnw.display.fixing = false;
			return;
		}

		obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src  + "', sizingMethod='image')";

		$(obj).bind('beforeprint', function(imgObjSrc) {
			return function() {
				this.src = imgObjSrc;
			}
		}(obj.src));

		$(obj).bind('afterprint', function() {
			this.src = bnw.config.blankSrc;
		});

		obj.src = bnw.config.blankSrc;
		bnw.display.fixing = false;
	},

	assignImgFlips: function() {
		var img_obj, imageSource, hasRolls;

		var reg_match = /^(.*?)([^\/]+)_(on|off|down)\.(\w{3,4})$/;

		var rollOverClass = bnw.config.rollOverClass || 'rollOver';

		$('.' + rollOverClass).each(
			function() {
				if ( this.src ) {
					img_obj = this;
				} else {
					$('.' + rollOverClass + ' *').each(
						function() {
							if ( (this.tagName == 'IMG' || (this.tagName == 'INPUT' && this.getAttribute('type') == 'image')) && this.src.match(reg_match) ) {
								img_obj = this;
								return; //break
							}
						}
					);
				}

				imageSource = img_obj.src.match(reg_match);

				hasRolls = false;

				if ( $(this).is('.' + rollOverClass) && imageSource ) {
					if ( !bnw.display.imageFlips[imageSource[1] + imageSource[2]] ) bnw.display.imageFlips[imageSource[1] + imageSource[2]] = [];

					bnw.display.imageFlips[imageSource[1] + imageSource[2]]['on'] = new Image();
					bnw.display.imageFlips[imageSource[1] + imageSource[2]]['on'].src = imageSource[1] + imageSource[2] + '_on.' + imageSource[4];
					$(this)
					.bind('mouseover', function(imgObj, flipName) {
						return function() {
							imgObj.src = bnw.display.imageFlips[flipName]['on'].src;
						}
					}(img_obj, imageSource[1] + imageSource[2]))

					$(this)
					.parent()
						.focus(function(imgObj, flipName) {
							return function() {
								imgObj.src = bnw.display.imageFlips[flipName]['on'].src;
							}
						}(img_obj, imageSource[1] + imageSource[2]));
					hasRolls = true;
				}

				if ( hasRolls ) {
					bnw.display.imageFlips[imageSource[1] + imageSource[2]]['off'] = new Image();
					bnw.display.imageFlips[imageSource[1] + imageSource[2]]['off'].src = imageSource[1] + imageSource[2] + '_off.' + imageSource[4];
					$(this)
					.bind('mouseout', function(imgObj, flipName) {
						return function() {
							imgObj.src = bnw.display.imageFlips[flipName]['off'].src;
						}
					}(img_obj, imageSource[1] + imageSource[2]))

					$(this)
					.parent()
						.blur(function(imgObj, flipName) {
							return function() {
								imgObj.src = bnw.display.imageFlips[flipName]['off'].src;
							}
						}(img_obj, imageSource[1] + imageSource[2]));
				}
			}
		);
	},

	/* Unobtrusive popups */
	assignSpecialLinks: function() {
		var pop_obj;

		$(bnw.config.popUps).each(function() {
			pop_obj = this;
			$(this.expr).each(function() {
				if ( pop_obj.href ) {
					this.href = pop_obj;
				} else {
					$(this).bind('click', function(windowName, params, queryExtra) {
						return function() {
							var href = this.href;
							if ( queryExtra && href.indexOf('?') != -1 ) href = href + '&' + queryExtra;
							if ( params ) {
								window.open(href, windowName, params);
							} else {
								window.open(href, windowName);
							}
							return false;
						}
					}(pop_obj.windowName, pop_obj.params, pop_obj.queryExtra));
				}
			});
				
		});
	},

	/* Fix Broken Images */
	fixBrokenImages: function() {
		$('img').each(
			function() {
				this.onerror = function() {
					this.src = bnw.config.brokenImage;
				}
			}
		);
	}
}; /* /Display */