if (window.$ === window.jQuery) {
	try {
    		delete window.$;
	} catch(e) {
		window["$"] = undefined; // SBTWO-4605
	}
}

(function($){

window.SitebuilderInfo = $.extend({}, {
	url: null,
	lastContentUpdated: null,
	setupHeight: jQuery.noop
}, window.SitebuilderInfo);

window.is_ie = !!(window.attachEvent && !window.opera);

// IE 11
if (!String.prototype.startsWith) {
	String.prototype.startsWith = function(pattern) {
		return this.lastIndexOf(pattern, 0) === 0;
	};
}

// IE 11
if (!String.prototype.endsWith) {
	String.prototype.endsWith = function(pattern) {
		var d = this.length - pattern.length;
	    return d >= 0 && this.indexOf(pattern, d) === d;
	};
}

window.redirectToGo = function(keyword, search) {
	var target = '/go/' + keyword;
	target = target + '?goSearchReferer=' + encodeURIComponent(window.location);
	if (search) {
		target = target + '&goSearchQuery=' + search;
	}
	window.location = target;
}

var WRollback = function(element, mouseOverFunction, mouseOutFunction) {
	this.element = element;
	this.over = mouseOverFunction;
	this.out = mouseOutFunction;
	this.element.onmouseover = mouseOverFunction;
	this.element.onmouseout = mouseOutFunction;
};
WRollback.prototype.disable = function() {
	this.element.onmouseover = null;
	this.element.onmouseout = null;
};
WRollback.prototype.enable = function() {
	this.element.onmouseover = this.over;
	this.element.onmouseout = this.out;
};

window.WRollback = WRollback;

/**
 * Make a button show and hide a div. the div also hides when you click outside
 * of it if closeOnDocumentClick is true.
 *
 * ex: new WTogglePopup('theButtonId','theContentDivId',true);
 */
var WTogglePopup = function(button, div, closeOnDocumentClick, callback) {
  this.$button = $('#' + button);
  this.$div = $('#' + div);

  this.duration = 0.2;
  this.closeOnDocumentClick = closeOnDocumentClick || false;

  this.callback = callback || function(){};
  this.$button.click($.proxy(this.toggle, this));
}

WTogglePopup.prototype.toggle = function(e) {
	if (e) e.preventDefault();

	var afterFinish;

	if (this.closeOnDocumentClick) {
		afterFinish = $.proxy(function() {
			if (this.$div.is(":visible")) {
				$(document).click($.proxy(this.hideIfVisible, this));
				this.callback(true);
			} else {
				$(document).unbind('click', $.proxy(this.hideIfVisible, this));
				this.callback(false);
			}
		}, this);
	} else {
		afterFinish = $.proxy(function() {
			if (this.$div.is(":visible")) {
				this.callback(true);
			} else {
				this.callback(false);
			}
		}, this);
	}

	this.$div.toggle(this.duration, afterFinish);
	return false;
};

WTogglePopup.prototype.hideIfVisible = function(e) {
	if (this.$div.is(":visible")) {
		this.toggle(e);
	}
	return true;
};
window.WTogglePopup = WTogglePopup;

/*
 * WCookie(name) - load a cookie with this name
 * WCookie(name,value,[hours],[path]) - create and save a cookie with this
 * name/value, optionally specifying the hours to expiry, and a path (default
 * '/')
 */
var WCookie = function(name, value, hours, path) {
  this.name = name;

  //set some defaults
  this.expires = '';
  this.path = '/';
  this.value = '';

  if (value) {
	  this.value = value;
	  if (hours) {
	    this.hours = hours;
	    this.expires = '; expires='+WCookie._getGMTStringForHoursAhead(hours);
	  } else {
	    this.expires = '';
	  }
	  if (path) {
	    this.path = path;
	  } else {
	    this.path = '/';
	  }
	  this.save();
  } else {
      this.value = this.load();
  }
};

WCookie._getGMTStringForHoursAhead = function(hours) {
  var date = new Date();
  date.setTime(date.getTime() + (hours*60*60*1000));
  return date.toGMTString();
};

WCookie.prototype.load = function() {
  var nameEQ = this.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;
};

WCookie.prototype.save = function() {
   document.cookie = this.name+'='+this.value+this.expires+'; path='+this.path;
};

WCookie.prototype.erase = function() {
   this.value = '';
   this.hours = -1;
   this.expires = '; expires='+WCookie._getGMTStringForHoursAhead(this.hours);
   this.save();
};
window.WCookie = WCookie;

/**
	StringBuilder class. When appending strings repeatedly, this
	is MUCH faster than using strings directly, ESPECIALLY in
	Internet Explorer.
*/
// Initializes a new instance of the StringBuilder class
// and appends the given value if supplied
if (typeof(window.StringBuilder)=="undefined")
{
var StringBuilder = function(value)
{
    this.strings = new Array("");
    this.append(value);
}
// Appends the given value to the end of this instance.
StringBuilder.prototype.append = function (value)
{
    if (value) this.strings.push(value);
}
// Clears the string buffer
StringBuilder.prototype.clear = function ()
{
    this.strings.length = 1;
}
// Converts this instance to a String.
StringBuilder.prototype.toString = function ()
{
    return this.strings.join("");
}
window.StringBuilder = StringBuilder;
}


/*
 * Use instead of encodeURIComponent() on its own because they do not
 * take into account multi-byte characters (such as Chinese symbols)
 */
String.prototype.postEncode = function() {
	var output = new StringBuilder();
	var size = this.length;
	for ( var i = 0; i < size; i++) {
		var c = this.charCodeAt(i).toString();
		if (c > 127) {
			output.append('%26%23');
			output.append(c);
			output.append('%3B');
		} else {
			output.append(encodeURIComponent(this.substr(i, 1)));
		}
	}
	return output.toString();
};

/*
 * Similar to postEncode but creates plain HTML entities, rather than URL
 * encoded HTML entities.
 */
String.prototype.characterEscape = function() {
	var output = new StringBuilder();
	var size = this.length;
	for ( var i = 0; i < size; i++) {
		var c = this.charCodeAt(i).toString();
		if (c > 127) {
			output.append('&#');
			output.append(c);
			output.append(';');
		} else {
			output.append(this.substr(i, 1));
		}
	}
	return output.toString();
};

window.WForm = {
	postEncode : function(form) {
		return $(form).serializeArray().map(
				function(i, val) {
					return val == null ? null : jQuery.isArray(val) ? jQuery
							.map(val, function(elem, i) {
								return {
									name : elem.name,
									value : WForm.Element.postEncode(elem.value)
								};
							}) : {
						name : val.name,
						value : WForm.Element.postEncode(val.value)
					}
				}).join('&');
	},

	Element : {
		postEncode : function(element) {
			var val = $(element).val();
			return val == null ? null : jQuery.isArray(val) ? jQuery
					.map(val, function(val, i) {
						return {
							name : element.name,
							value : WForm.Element.postEncode(val)
						};
					}) : {
				name : element.name,
				value : WForm.Element.postEncode(val)
			}
		}
	}
};

// Backward compatibility
window.addEvent = function(obj, type, fn) {
	$(obj).bind(type,fn);
};

window.cancelDefaultEvents = function(keyEvent) {
	if (keyEvent.preventDefault) keyEvent.preventDefault();
	keyEvent.returnValue = false;
};

window.sbrToAbsoluteUrl = function(relativeUrl) {
	var absoluteUrl = relativeUrl;
	if (absoluteUrl.indexOf(':') < 0) {
		var path = '' + window.location.href;
		var bases = document.getElementsByTagName('base');
		if (bases.length > 0) {
			path = bases[0].href;
		}

		if (absoluteUrl.startsWith('//')) {
			absoluteUrl = path.substring(0, path.indexOf(':') + 1)
					+ absoluteUrl;
		} else {
			if (absoluteUrl.indexOf('/') === 0) {
				absoluteUrl = absoluteUrl.substring(1);
				path = path.substring(0, path.indexOf('/', 7));
			}
			if (path.charAt(path.length - 1) !== '/') {
				path += '/';
			}
			absoluteUrl = path + absoluteUrl;
		}
	}
	return absoluteUrl;
};

if (!window.Url) window.Url = {};
window.Url.unparam = function(value, separator) {
	if (typeof(value) == 'undefined' || value == null) return { };

	var match = value.trim().match(/([^?#]*)(#.*)?$/);
    if (!match) return { };

    var params = {};
    var pieces = match[1].split(separator || '&');

    for (var i = 0; i < pieces.length; i++) {
    	var pair = pieces[i].split('=', 2);
    	var key = decodeURIComponent(pair[0]);
      params[key] = pair.length === 2 ? decodeURIComponent(pair[1]) : null;
    }

    return params;
};

if (typeof window.Event == 'undefined') {
  window.Event = {};
}

window.Event.onDOMReady = $.proxy($(document).ready, $(document));

})(jQuery);
