var _BTN = '_BTN';
var _CAL = '_CAL';
var _DDL = '_DDL';
var _HDN = '_HDN';
var _IMG = '_IMG';
var	_LBL = '_LBL';
var	_LEN = '_LEN';
var _LNK = '_LNK';
var _PNL = '_PNL';
var _REQ = '_REQ';
var _PFX = 'ctl00_CA_';
var _MININT = '-2147483648';

var DDI = 
function() {

    return {

        /**
         * Yahoo presentation platform utils namespace
         */
        Web: {},

        /**
         * Returns the namespace specified and creates it if it doesn't exist
         *
         * DDI.namespace("property.package");
         * DDI.namespace("DDI.property.package");
         *
         * Either of the above would create DDI.property, then
         * DDI.property.package
         *
         * @param  {String} sNameSpace String representation of the desired
         *                             namespace
         * @return {Object}            A reference to the namespace object
         */
        namespace: function( sNameSpace ) {

            if (!sNameSpace || !sNameSpace.length) {
                return null;
            }

            var levels = sNameSpace.split(".");

            var currentNS = DDI;

            // DDI is implied, so it is ignored if it is included
            for (var i=(levels[0] == "DDI") ? 1 : 0; i<levels.length; ++i) {
                currentNS[levels[i]] = currentNS[levels[i]] || {};
                currentNS = currentNS[levels[i]];
            }

            return currentNS;

        }
    };

} ();


DDI.Web.Bookmark =
	function()
	{
		var title = document.title;
		var url = location.href;
		if (window.sidebar) 
		{
			window.sidebar.addPanel(title, url, '');
		}
		else if (window.opera && window.print)
		{
			var elem = document.createElement('a');
			elem.setAttribute('href',url);
			elem.setAttribute('title',title);
			elem.setAttribute('rel','sidebar');
			elem.click();
		} 
		else if (document.all)
		{
			window.external.AddFavorite(url, title);
		}
	}

DDI.Web.Boolean = 
	function(s1)
	{

		if (isNaN(parseInt(s1)))
		{
			if (s1.toLowerCase().substring(0,1) == 't')
				return true;
		}
		else
		{
			if (parseInt(s1) != 0)
				return true;
		}
		
		return false;
		
	}

DDI.Web.CenterInlinePopup = 
	function(popup, altWidth, altHeight)
	{
		var popupTop = popup.offsetHeight;
		if (popupTop == 0) popupTop = altHeight;
	
		var popupLeft = popup.offsetWidth;
		if (popupLeft == 0) popupLeft = altWidth;
	
		popupTop = parseInt((YAHOO.util.Dom.getViewportHeight() - popupTop) / 2);
		popupLeft = parseInt((YAHOO.util.Dom.getViewportWidth() - popupLeft) / 2);

		YAHOO.util.Dom.setStyle(popup, 'top', popupTop, 'px');
		YAHOO.util.Dom.setStyle(popup, 'left', popupLeft, 'px');
	}

DDI.Web.DropDownListSelectedIndex = 
	function(el,value)
	{
		var returnValue = -1;
		for (i=0; i<el.options.length; i++)
		{
			if (el.options[i].value == value) 
			{
				returnValue = i;
				break;
			}
		}
		return returnValue;
	}
	
DDI.Web.ErrorAlert = 
	function (el, Message)
	{
		alert(Message);
		el.focus();
		return true;
	}
	
DDI.Web.GetAttribute = 
	function(type, el, attributeName, defaultValue)
	{
		var finalValue = defaultValue;
		var tagOverride = el.getAttribute(attributeName);

		switch (type)
		{
			case 'boolean':
				if (DDI.Web.HasValue(tagOverride)) finalValue = DDI.Web.Boolean(tagOverride);
				if (tagOverride == attributeName) finalValue = true; // added as bug for Opera where 'attribute=1' appears as 'attribute-attribute'
				break;
			case 'int':
				if (DDI.Web.HasValue(tagOverride) && isFinite(parseInt(tagOverride))) finalValue = parseInt(tagOverride);
				break;
			case 'string':
				if (DDI.Web.HasValue(tagOverride)) finalValue = tagOverride;
				break;
		}
		
		return finalValue;
	}
	
DDI.Web.HasValue = 
	function(s1)
	{

		if (s1 != undefined && s1 != null && s1 != '')
			return true;
			
		return false;
		
	}

DDI.Web.PositionInlinePopup = 
	function(anchor, popup, xOffset, yOffset)
	{
		var anchorTop = YAHOO.util.Dom.getY(anchor);
		var anchorLeft = YAHOO.util.Dom.getX(anchor);
		
		var popupTop = anchorTop + anchor.offsetHeight + 2 + yOffset;
		if (popupTop + popup.offsetHeight > YAHOO.util.Dom.getClientHeight() && anchorTop - popup.offsetHeight - 2 - yOffset > 0)
			popupTop = anchorTop - popup.offsetHeight - 2 - yOffset;
			
		var popupLeft = anchorLeft + xOffset;
		if (popupLeft + popup.offsetWidth > YAHOO.util.Dom.getClientWidth() && anchorLeft + anchor.offsetWidth - popup.offsetWidth + xOffset > 0)
			popupLeft = anchorLeft + anchor.offsetWidth - popup.offsetWidth + xOffset;
			
		YAHOO.util.Dom.setStyle(popup, 'top', popupTop, 'px');
		YAHOO.util.Dom.setStyle(popup, 'left', popupLeft, 'px');
	}
	
DDI.Web.Selection = 
	new function()
	{

		this.range = function()
		{
		
			if (window.getSelection)
			{
				return window.getSelection();
			}
			else if (document.getSelection)
			{
				return document.getSelection();
			}
			else if (document.selection)
			{
				return document.selection.createRange();
			}
			
			return null
		}
		
		this.text = function()
		{
			var r = this.range();
					
			if (window.getSelection)
			{
				return r.toString();
			}
			else if (document.getSelection)
			{
				return r.toString();
			}
			else if (document.selection)
			{
				return r.text;
			}
			
			return null;
			
		}
		
		this.clear = function(el)
		{

			var s = el.value;
			var t = this.text();
				  
			if (t.length > 0) 
			{
				s = s.replace(t, '');					
				el.value = s;
				//TODO: get ClearPartialSelection function working correctly
				el.value.focus;
			}
		}
		
	}

DDI.Web.SoapRequest =
	function(method, obj)
	{
		var returnValue = '<?xml version="1.0" encoding="utf-8"?>\n';
		returnValue += '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">\n';
		returnValue += '<soap:Body>\n';
		returnValue += '<' + method + ' xmlns="http://tempuri.org/">\n';
		for (var i in obj) 
		{
			returnValue += '<' + i + '>';
			returnValue += obj[i].toString();				
			returnValue += '</' + i + '>';
		}
		returnValue += '</' + method + '>\n';
		returnValue += '</soap:Body>\n';
		returnValue += '</soap:Envelope>\n';
		return returnValue;
	}
	
DDI.Web.SoapPostRequest =
	function(method, obj)
	{
		var returnValue = '';
		for (var i in obj) 
		{
			if (returnValue.length > 0) returnValue += '&';
			returnValue += i + '=' + obj[i].toString();				
		}
		return returnValue;
	}
	
DDI.Web.XmlDoc = 
	function()
	{
		var xdoc;
		if (window.ActiveXObject)
		{
			xdoc = new ActiveXObject("Microsoft.XMLDOM");
			xdoc.async = false;
			if (arguments.length > 0) xdoc.loadXML(arguments[0]);
		}
		else
		{
			xdoc = document.implementation.createDocument("","",null);
			if (arguments.length > 0)
			{
				var parser = new DOMParser();
				xdoc = parser.parseFromString(arguments[0],'text/xml');
			}
		}
		return xdoc;
	}

DDI.Web.ScrollHeight = 
	function() 
	{
		var scrOfY = 0;
		if( typeof( window.pageYOffset ) == 'number' ) {
			scrOfY = window.pageYOffset;
		} else if( document.body && document.body.scrollTop ) {
			scrOfY = document.body.scrollTop;
		} else if( document.documentElement && document.documentElement.scrollTop ) {
			scrOfY = document.documentElement.scrollTop;
		}
		return scrOfY;
	}
	
DDI.Web.ScrollWidth = 
	function() 
	{
		var scrOfX = 0;
		if( typeof( window.pageYOffset ) == 'number' ) {
			scrOfX = window.pageXOffset;
		} else if( document.body && document.body.scrollLeft ) {
			scrOfX = document.body.scrollLeft;
		} else if( document.documentElement && document.documentElement.scrollLeft ) {
			scrOfX = document.documentElement.scrollLeft;
		}
		return scrOfX;
	}
	
DDI.Web.DDList = 
	function(id, sGroup, config) 
	{

		DDI.Web.DDList.superclass.constructor.call(this, id, sGroup, config);

		var el = this.getDragEl();
		YAHOO.util.Dom.setStyle(el, "opacity", 0.67); // The proxy is slightly transparent

		this.goingUp = false;
		this.lastY = 0;
	};

YAHOO.extend(DDI.Web.DDList, YAHOO.util.DDProxy, 
{

    startDrag: 
		function(x, y) 
		{

			// make the proxy look like the source element
			var dragEl = this.getDragEl();
			var clickEl = this.getEl();
			YAHOO.util.Dom.setStyle(clickEl, "visibility", "hidden");

			dragEl.innerHTML = clickEl.innerHTML;

			YAHOO.util.Dom.setStyle(dragEl, "color", YAHOO.util.Dom.getStyle(clickEl, "color"));
			YAHOO.util.Dom.setStyle(dragEl, "backgroundColor", YAHOO.util.Dom.getStyle(clickEl, "backgroundColor"));
			YAHOO.util.Dom.setStyle(dragEl, "fontSize", YAHOO.util.Dom.getStyle(clickEl, "fontSize"));
			YAHOO.util.Dom.setStyle(dragEl, "fontWeight", YAHOO.util.Dom.getStyle(clickEl, "fontWeight"));
			YAHOO.util.Dom.setStyle(dragEl, "border", "1px solid #cc9900");
		},

    endDrag: 
		function(e) 
		{

			var srcEl = this.getEl();
			var proxy = this.getDragEl();

			// Show the proxy element and animate it to the src element's location
			YAHOO.util.Dom.setStyle(proxy, "visibility", "");
			var a = new YAHOO.util.Motion( 
				proxy, 
				{ 
					points: 
					{ 
						to: YAHOO.util.Dom.getXY(srcEl)
					}
				}, 
				0.2, 
				YAHOO.util.Easing.easeOut 
			)
			var proxyid = proxy.id;
			var thisid = this.id;

			// Hide the proxy and show the source element when finished with the animation
			a.onComplete.subscribe(function() {
					YAHOO.util.Dom.setStyle(proxyid, "visibility", "hidden");
					YAHOO.util.Dom.setStyle(thisid, "visibility", "");
				});
			a.animate();
		},

    onDragDrop: 
		function(e, id) {
		
			if (id == 'DragList')
			{
				var elUl = YAHOO.util.Dom.get(id);
				var elLi = elUl.getElementsByTagName('li');
				var newOrder = new Array();
				for (var i=0;i<elLi.length;i++) 
				{
					newOrder.push(DDI.Web.GetAttribute('string', YAHOO.util.Dom.get(elLi[i].id), 'key', ''));
				}
				var elOrder = YAHOO.util.Dom.get(_PFX + 'SortedValues');
				elOrder.value = newOrder.join(',');
			}
			
		},

    onDrag: 
		function(e) {

			// Keep track of the direction of the drag for use during onDragOver
			var y = YAHOO.util.Event.getPageY(e);

			if (y < this.lastY) {
				this.goingUp = true;
			} else if (y > this.lastY) {
				this.goingUp = false;
			}

			this.lastY = y;
		},

    onDragOver: 
		function(e, id) {
	    
			var srcEl = this.getEl();
			var destEl = YAHOO.util.Dom.get(id);

			// We are only concerned with list items, we ignore the dragover
			// notifications for the list.
			if (destEl.nodeName.toLowerCase() == "li") {
				var orig_p = srcEl.parentNode;
				var p = destEl.parentNode;

				if (this.goingUp) {
					p.insertBefore(srcEl, destEl); // insert above
				} else {
					p.insertBefore(srcEl, destEl.nextSibling); // insert below
				}

				YAHOO.util.DragDropMgr.refreshCache();
			}
		}
});

