/*
 * Object Utils - v0.91 - 3/28/2009
 * http://benalman.com/
 * 
 * Copyright (c) 2009 "Cowboy" Ben Alman
 * Licensed under the MIT license
 * http://benalman.com/about/license/
 * 
 * Note: the setObject, getObject, and exists methods were inspired by Dojo,
 * which is Copyright (c) 2005-2009, The Dojo Foundation.
 */
(function($){$.setObject=function(){var f=b(arguments),h=f.parts.pop(),g=e(f.context,f.parts,true);return g&&typeof g==="object"&&h?(g[h]=f.remain):undefined};$.getObject=function(){var f,g=arguments;if(!g.length||typeof g[0]==="number"){return c(g[0])}else{f=b(g);return f.callback?a(f):e(f.context,f.parts,f.remain)}};var d=[];function c(g){var f=typeof g==="number"&&d[g-1];if(f&&f.timeout_id){clearTimeout(f.timeout_id);f.timeout_id=null;return true}return false}function a(f){var g,i,h={};return(function(){g=e(f.context,$.extend([],f.parts));if(g===undefined){h.timeout_id=setTimeout(arguments.callee,f.delay);return i=i||d.push(h)}else{h.timeout_id=null;f.callback.call(f.context,g);return}})()}$.exists=function(){var f=b(arguments);return e(f.context,f.parts)!==undefined};function e(i,h,f){var g;while(i&&h.length){g=h.shift();if(!i[g]&&f){i[g]={}}i=i[g]}return i}function b(g){var f={};g=Array.prototype.slice.call(g);f.callback=$.isFunction(g[0])?g.shift():null;f.delay=f.callback&&typeof g[0]==="number"?Math.max(g.shift(),0):50;f.context=typeof g[0]!=="string"?g.shift():window;f.parts=g.shift().split(".");f.remain=g.shift();return f}})(jQuery);

/*
 * GlobalCode
 */
GlobalCode = function(){
	
	var resizableItems = [];
	
	jQuery.fn.extend({
		"linkToWindow": function(){
			return this.each(function(){
				this.url = this.href
				this.href = "#"
				var windowOptions = ""
				
				if ($(this).attr("evl_content_width") != "undefined") {
					windowOptions += 'width=' + $(this).attr("evl_content_width") + ','
				}
				if ($(this).attr("evl_content_height") != "undefined") {
					windowOptions += 'height=' + $(this).attr("evl_content_height") + ','
				}
				$(this).click(function(){
					window.open(this.url,"LinkWindow",windowOptions);
				})
			})
		}
	})
	
	jQuery.fn.extend({
		"renderTemplate": function(data){
			var getData
			if (arguments.length == 0) {
				getData = function(path) {
					return $.getObject(path)
				}
			} else {
				getData = function(path) {
					return $.getObject(data,path)
				}
			}
			return this.each(function(){
				var element = this
				var jNode = $(this)
				var tContent = jNode.attr("t:content")
				var tAttributes = jNode.attr("t:attributes")
				var tReplace = jNode.attr("t:replace")
				if (tReplace) {			
					var value = getData(tReplace)
					if (typeof(value) == 'function') {
						jNode.replaceWith(value.apply(this))
					} else {
						jNode.replaceWith(value)
					}
					return
				}
				if (tContent) {			
					var value = getData(tContent)
					if (typeof(value) == 'function') {
						jNode.html(value.apply(this))
					} else {
						jNode.html(value)
					}
				}
				if (tAttributes) {
					tAttributes = tAttributes.split(/;+(; )*/)
					$.each(tAttributes,function(index,item){
						var tAttribute = item.split(/ +/)
						var attributeName = tAttribute[0]
						var attributeDataPath = tAttribute[1]
						value = getData(attributeDataPath)
						if (typeof(value) == 'function') {
							jNode.attr(attributeName,value.apply(element,[attributeName]))
						} else {
							jNode.attr(attributeName,value)
						}
						//jNode.attr(tAttribute,);
					})
				}
			})
		}
	})

	function setScrollHeight(event,item) {
				  		var windowHeight = 0;
							if (window.innerHeight) {
								windowHeight = window.innerHeight;
							} else {
								windowHeight = (document.documentElement.clientHeight > 0) ? document.documentElement.clientHeight : document.body.clientHeight; 
							}
				
			  			var scrollHeight = $.browser.msie ? 20 : 40
			  			if (windowHeight > scrollHeight) {
			  				scrollHeight = windowHeight - scrollHeight;
			  			}
			  			if (arguments.length > 1) {
			  				$(item).height(scrollHeight)
			  			} else {
				  			$.each(resizableItems,function(){
				  				$(this).height(scrollHeight)
				  			})
				  		}
	}

	jQuery.fn.extend({
		"autoResizeOnScroll": function(){
			return this.each(function(){
				resizableItems.push(this);
				setScrollHeight(null,this)
				$(window).unbind("resize",setScrollHeight);
				$(window).bind("resize",setScrollHeight);
			})
		}
	})
	
	var AddThis = function(){
		
		var _options = 'email, favorites, delicious, digg, facebook, myspace, google, live, reddit, stumbleupon, twitter, more'
		
		function options() {
			if (arguments.length == 0) {
				return _options
			} else if (arguments.length == 1) {
				_options = arguments[0]
			} else {
				_options = Array.prototype.slice.call(arguments).join(',')
			}
		}
		
		return {
			"options": options
		}
	}
	
	var GoogleAjaxApi = function(){
		
		var keyMap = {
			"evolveadmindev.stl.elsevier.com":"ABQIAAAAqPHg90RnmXCYOfiEdbGsHxSZfMid8f1kIM64GemIe6NJxxm6BBQVGL8itGl_Y2FXDE8jO841IdykLg",
			"evolvedev.stl.elsevier.com":"ABQIAAAAqPHg90RnmXCYOfiEdbGsHxRvJZZqW_IcBEEGe0Z1b4Hy5wts4hQ9ug_qKExSFr_jLSfP7AQArDV96Q",
			"evolveadminint.stl.elsevier.com":"ABQIAAAAqPHg90RnmXCYOfiEdbGsHxTp_G5gov8adT4yqUTPJiprZUbiQxTb0zPo7_tlhHFD2xorX-6NWRrSLA",
			"evolveint.stl.elsevier.com":"ABQIAAAAqPHg90RnmXCYOfiEdbGsHxT4iSEC7JZ_cHF9FZHPQk5-wiF78hTs7COlmd_WVd8gQDIInIPHmgPQ2A",
			"evolveadmincert.elsevier.com":"ABQIAAAAqPHg90RnmXCYOfiEdbGsHxR9VLZZ2e8JzibMyAdk69-B8hEYUBSt5zYklcujbu3FKMg3yIPB0C6JPw",
			"evolvecert.elsevier.com":"ABQIAAAAqPHg90RnmXCYOfiEdbGsHxSEfzqGUnkx9v99a-uejZI2oLZMdhQ2SEm17BKltwqa1I8c8l1aauYvfA",
			"evolveadmin.elsevier.com":"ABQIAAAAqPHg90RnmXCYOfiEdbGsHxRYnA8qMG7v508Wm0R_vcGKORk6-xQhDV2NswNl9VLS2pC_LnZkChF0kQ",
			"evolve.elsevier.com":"ABQIAAAAqPHg90RnmXCYOfiEdbGsHxQuxcmHStRB_YLY5GJGF-w_Pdq2NhQs7Box5cawLWrEkP0TGLeh4F3cjw"
		}
		if (document.location.host in keyMap) {
			document.write('<script src="https://www.google.com/jsapi?key=' + keyMap[document.location.host] + '" type="text/javascript"></script>')
		}
	}
	
	var deliveryHost = document.location.protocol + "//" + document.location.host;
	
	var DeliveryHost = function(host){
		if (arguments.length == 0) {
			return deliveryHost;
		} else {
			deliveryHost = host;
		}
	}
	
	var DeliveryUrl = function(url) {
		var url = $.trim(url)
		if (url.substring(0,deliveryHost.length) != deliveryHost) {
			if (url.indexOf("://") >= 0) {
				url = url.substring(url.indexOf("://")+"://".length)
				url = url.substring(url.indexOf("/"))
			}
			if ((document.location.protocol + "//" + document.location.host) != deliveryHost) {
				url = deliveryHost + url
			}
		}
		return url
	}
	
	var DeliveryUrlCallbackForAttribute = function(attributeName) {
		return function(){return GlobalCode.DeliveryUrl($(this).attr(attributeName))}
	}
	
	var ErrorResponseWrapper = function(error,errorMessage) {
		var wrapper = {}
		wrapper['success'] = false
		wrapper['error'] = error
		wrapper['error_message'] = errorMessage
		return wrapper
	}

	var SuccessResponseWrapper = function(result) {
		var wrapper = {}
		wrapper['result'] = null
		if (arguments.length > 0) {
			wrapper['result'] = result
		}
		wrapper['success'] = true
		return wrapper
	}
	
	var analyticsItems = {};
	
	var Analytics = function(){
		
		function addItem(itemKey,itemValue){
			if (!analyticsItems[itemKey]) {
				analyticsItems[itemKey] = [];
			}
			analyticsItems[itemKey].push(escape(itemValue));
		}
		
		function getItem(itemKey){
			if (!analyticsItems[itemKey]) {
				return null;
			}
			return analyticsItems[itemKey].join("|");
		}
		
		return {
			"AddItem":addItem,
			"GetItem":getItem
		}
	}
	
	var WelcomeMessage = function(name,audience){
		if (arguments.length < 2) {
			return "Welcome, Guest!";
		}
		if (name != null && name != "Guest") {
			return "Welcome back, " + name + "!";
		}
		return "Welcome, " + audience + "!";
	}
	
	$(function(){

	})

	return {
			"AddThis":AddThis(),
			"GoogleAjaxApi":GoogleAjaxApi,
			"DeliveryHost":DeliveryHost,
			"DeliveryUrl":DeliveryUrl,
			"DeliveryUrlCallbackForAttribute":DeliveryUrlCallbackForAttribute,
			"ErrorResponseWrapper":ErrorResponseWrapper,
			"SuccessResponseWrapper":SuccessResponseWrapper,
			"Analytics":Analytics(),
			"WelcomeMessage":WelcomeMessage
	}
}()




						
