﻿//Copyright 2009: Thomson Reuters Global Resources. All Rights Reserved. Proprietary and Confidential information of TRGR. Disclosure, Use or Reproduction without the written authorization of TRGR is prohibited.
/// <reference path="~/shared/js/namespaces.js"/>
/// <reference name="MicrosoftAjax.js" />  

/***********************************************************************
*	This file contains Westlaw-specific extensions to the Microsoft
*	ASP.NET AJAX library.
************************************************************************/

Type.prototype._createGetter = function(propName) {
	if ( typeof(this.prototype['get_' + propName]) != 'function' ){
		this.prototype['get_' + propName] = function(){ return this['_' + propName] }
	}
}

Type.prototype._createSetter = function(propName) {
	if ( typeof(this.prototype['set_' + propName]) != 'function' ){
		this.prototype['set_' + propName] = function(value){ this['_' + propName] = value; }
	}
}

Type.prototype.createProperty = function(propName) {
	///<summary>Creates a "private" property on a class, with getter and setter methods</summary>
	///<param name="propName" type="String">The name of the property to be created</param>	
	this._createGetter(propName);
	this._createSetter(propName);
}

Type.prototype._createAddHandler = function(eventName) {
	return function(handler) {
		this.get_events().addHandler(eventName, handler);
	}
}

Type.prototype._createRemoveHandler = function(eventName) {
	return function(handler) {
		this.get_events().removeHandler(eventName, handler);
	}
}

Type.prototype.createEvent = function(eventName) {
	///<summary>Create a custom event</summary>
	///<param name="eventName" type="String">The name of the event to be created</param>

	var addHandler = this._createAddHandler(eventName);
	var removeHandler = this._createRemoveHandler(eventName);

	this.prototype['add_' + eventName] = addHandler;
	this.prototype['remove_' + eventName] = removeHandler;

	if(!this.__events) {
		if(!this.inheritsFrom(Sys.Component)) {
			this.prototype.get_events = function() {
				if (!this._events) {
					this._events = new Sys.EventHandlerList();
				}
				return this._events;
			}
		}
	
		this.prototype._raiseEvent = function(eventName, eventArgs) {
			var handler = this.get_events().getHandler(eventName);
			if (handler) {
				if (!eventArgs) {
					eventArgs = Sys.EventArgs.Empty;
				}
				handler(this, eventArgs);
			}
		}
		
		this.__events = true;
	}
}

Sys.UI.DomEvent._addHandler = Sys.UI.DomEvent.addHandler;
$addHandler = Sys.UI.DomEvent.addHandler = function(element, eventName, handler) {
	///<summary>Add Event Handler</summary>
	///<param name="element">The element that exposes the event</param>
	///<param name="eventName" type="String">The name of the event</param>
	///<param name="handler" type="Function">The client function that is called when the event occurs</param>
	if(eventName.toLowerCase() == 'mousewheel') {
		var _handler = function(e) {
			if(e.rawEvent.wheelDelta) {
				e.wheelDelta = e.rawEvent.wheelDelta / 120;
			}
			else if(e.rawEvent.detail) {
				e.wheelDelta = -e.rawEvent.detail / 3;
			}
			// FF bug: http://extjs.com/forum/archive/index.php/t-200298.html
			if(Sys.Browser.agent == Sys.Browser.Firefox) {
				e.clientX = e.screenX;
				e.clientY = e.screenY;
			}
			handler(e);
		}
		if(Sys.Browser.agent == Sys.Browser.Firefox) {
			return Sys.UI.DomEvent._addHandler(element, 'DOMMouseScroll', _handler);			
		}
		else {
			return Sys.UI.DomEvent._addHandler(element, 'mousewheel', _handler);
		}
	}
	else {
		return Sys.UI.DomEvent._addHandler(element, eventName, handler);
	}
}


Sys.UI.DomEvent._removeHandler = Sys.UI.DomEvent.removeHandler;
$removeHandler = Sys.UI.DomEvent.removeHandler = function(element, eventName, handler) {
	///<summary>Remove Event Handler</summary>
	///<param name="element">The element that exposes the event</param>
	///<param name="eventName" type="String">The name of the event</param>
	///<param name="handler" type="Function">The client function that is called when the event occurs</param>
	if(eventName.toLowerCase() == 'mousewheel') {
		if(Sys.Browser.agent == Sys.Browser.Firefox) {
			Sys.UI.DomEvent._removeHandler(element, 'DOMMouseScroll', handler);
		}
		else {
			Sys.UI.DomEvent._removeHandler(element, 'mousewheel', handler);
		}
	}
	else {
		return Sys.UI.DomEvent._removeHandler(element, eventName, handler);
	}
}

//Override the _changePanes method on the accordion if the accordion is defined and we're in IE
//We're doing this because the accordion implementation in IE is a little buggy with the animation script
// - see below comments
Sys.Application.add_load(function() {
	if(typeof(AjaxControlToolkit) !== "undefined" && AjaxControlToolkit.AccordionBehavior && Sys.Browser.agent == Sys.Browser.InternetExplorer) {
		try{
			AjaxControlToolkit.AccordionBehavior.prototype._changePanes = function(lastIndex) {
				if (!this.get_isInitialized()) return;
				
				var open = null;
				var close = null;
				for (var i = 0;i < this._panes.length;i++) {
					var pane = this._panes[i];
					var animation = this._getAnimation(pane);
					if (animation.get_isPlaying()) {
						animation.stop();
					}
					if (i == this._selectedIndex) {
						animation._opening = true;
						open = animation;
					} else if (i == lastIndex) {
						animation._opening = false;
						close = animation;
					} else {
						continue;
					}
					this._startPaneChange(pane, animation._opening);
					if (this._fadeTransitions) {
						animation._fade.set_effect(animation._opening ? AjaxControlToolkit.Animation.FadeEffect.FadeIn : AjaxControlToolkit.Animation.FadeEffect.FadeOut );
					}
					if (this._autoSize === AjaxControlToolkit.AutoSize.Fill) {
						animation.set_target(pane.content._original);				
						//this next line has been changed - the bug occurred because the getContentSize method call returns the
						//full size of the control - even though the client height is 0. We force 0 in the start value if the animation is
						//opening
						animation._length.set_startValue(animation._opening ? 0 : $common.getContentSize(pane.content._original).height);
						animation._length.set_endValue(animation._opening ? this._getRemainingHeight(true) : 0);
					} else {
						animation.set_target(pane.content);
						animation._length.set_startValue(animation._opening ? 0 : pane.content.offsetHeight);
						animation._length.set_endValue(animation._opening ? this._getRemainingHeight(false) : 0);
					}
				}
				if (close) {
					close.play();
				}
				if (open) {
					open.play();
				}
			};		
		}
		catch(err) {
			
		}
	}
});

function GetWindowDimensions() {
	///<summary>Returns the width and height of the current window</summary>
	var height = window.innerHeight != null? window.innerHeight: document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight:document.body != null? document.body.clientHeight:null;
	var width = window.innerWidth != null ? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body ? document.body.clientWidth:null;
	
	return new Sys.UI.Point(width, height);
}

if(typeof(Sys) !== "undefined") {
	Sys.Application.notifyScriptLoaded();
}