
/*
***************************************************************************************************

	Webmoney Library Framework version 1.5
	2008 © Webmoney
	Supported Browsers: IE 6.0+, Mozilla based, Opera, Safari (Mac/Win)
	NOT FOR COMERCICAL USE
	
	http://webmoney.ru

***************************************************************************************************
*/

function $(elem)
{
	return WM.DOM.get(elem);
}

function $ID(name, elem)
{
	return WM.DOM.getElementsById(name, elem);
}

function $T(name, elem)
{
	return WM.DOM.getTags(name, elem);
}

function $C(name, elem)
{
    return WM.DOM.getElementsByClass(name, elem);
}


var WM =
{
	version :	'1.6',
	name :		'Webmoney Library Framework',
	author :	'Pavel "pasha" Vladimiroff',

	init : function(options)
	{
        if (options.isMinWidth)
        {
	        var width = options.width ? options.width : 1000;
	        var elem = options.element ? options.element : window;

	        var minWidth = function()
	        {
		        $T('BODY', null)[0].style.width = document.documentElement.clientWidth < width ? width + 'px' : '100%';
	        };

	        this.Events.add(window, 'resize', minWidth);
	        this.Events.add(window, 'load', minWidth);
        }
	},

	extend : function(namespace, obj)
	{
		if (typeof obj === 'object')
			for (var i in obj)
				namespace[i] = obj[i];

		return namespace;
	}
};



// Cache namespace
WM.extend(WM,
{
	Cache :
	{
		storage : [],
		remove : function(elem)
		{
			if (typeof elem === 'string')
				this.storage[elem] = null;
		},
		clear: function()
		{
			this.storage = [];
		}
	}
});

// DOM namespace
WM.extend(WM,
{
	DOM :
	{
		get : function(elem)
		{
			if (typeof elem === 'string')
			{
				if (WM.Cache.storage[elem])
					return WM.Cache.storage[elem];
				else
					return WM.Cache.storage[elem] = document.getElementById(elem);
			}

			return elem;
		},
		getTags : function(name, elem)
		{
			elem = elem || document;
			return elem.getElementsByTagName(name);
		},
		add : function(elem, dest)
		{
			var elem = $(elem);
			var dest = $(dest);
			dest.appendChild(elem);
		},
        addTag : function(tag, dest, options, replace)
        {
            var elem = $(options.id);

            if (elem)
            {
                if (!replace)
                    return elem;
                this.remove(elem);
            }

            var node = document.createElement(tag);
            for (var i in options)
                node[i] = options[i];

            this.add(node, dest);
            
            return $(options.id);
        },
		remove : function(elem)
		{
			var elem = $(elem);
			elem.parentNode.removeChild(elem);

			if (WM.Cache.storage[elem])
				WM.Cache.storage[elem] = null;
		},
		toggle:
		{
			switcher: function(elem)
			{
                if (window.getComputedStyle)
                {
                    if (window.getComputedStyle($(elem), null).display == '' || window.getComputedStyle($(elem), null).display == 'none')
                        return this.show(elem);
                    return this.hide(elem);
                }
                else if ($(elem).currentStyle)
                {
                    if ($(elem).currentStyle.display == '' || $(elem).currentStyle.display == 'none')
                        return this.show(elem);
                    return this.hide(elem);
                }
                else
                    return false;
			},
			show: function(elem)
			{
				$(elem).style.display = 'block';
				return false;
			},
			hide: function(elem)
			{
				$(elem).style.display = 'none';
				return false;
			}
		},
		confirm: function(text, callback)
		{
            if (confirm(text))
            {
                if (typeof callback === 'function')
                    callback();
                return true;
            }
            return false;
		},
		getElementsByClass : function(name, elem)
		{
			return this._find(name, $T('*', elem), 'className', false);
		},
		getElementsById : function(name, elem)
		{
			return this._find(name, $T('*', elem), 'id', true);
		},
		
		// private method
		_find : function(name, elems, attribute, global)
		{
			var retArray = [];
            var pattern = global ? new RegExp(name, 'i') : new RegExp('(^|\\s)' + name + '(\\s|$)', 'i');

			for (var i = 0, j = elems.length; i < j; i++)
				if (pattern.test(elems[i][attribute]))
					retArray.push(elems[i]);

			return retArray;
		}
	}
});

// Cookies
WM.extend(WM,
{
	Cookies :
	{
		get : function(name)
		{
			name = name + '=';
			var arr = document.cookie.split(';');

			for(var i = 0, j = arr.length; i < j; i++)
			{
				var c = arr[i];
				while(c.charAt(0) == ' ')
					c = c.substring(1, c.length);
				if (c.indexOf(name) == 0)
					return c.substring(name.length, c.length);
			}

			return null;
		},
		set : function(name, value, seconds)
		{
			var expires = '';

			if (typeof seconds != 'undefined')
			{
				var date = new Date();
				date.setTime(date.getTime() + (seconds * 1000));
				var expires = '; expires=' + date.toGMTString();
			}

			document.cookie = name + '=' + value + expires + '; path=/';
		},
		remove : function(name)
		{
			this.set(name, '', -1);
		}
	}
});


// Keeper namespace
WM.extend(WM,
{
    Keeper :
    {
        msgs :       {},
        flag :       false,
        WMID :       '',
        checkLogin : function(f, m)
        {
            var msgs = m || this.msgs;
	        var sign = $('SignStr');
	        var wmid = $('WMID');
	        var obj = $('AcceptWM');
	        var plstr = $('PlainStr');
	        if (!sign || !wmid || !obj || !plstr)
		        return false;

	        var bAcceptWMReady = (obj.readyState == 4);
	        if (!bAcceptWMReady)
	        {
		        if (msgs.loadError)
				        alert(msgs.loadError);
		        return false;
	        }

	        sign.value = obj.SignString(unescape(plstr.value));
	        wmid.value = obj.strLoginName;
	        this.WMID = wmid.value;
	        if (wmid.value.indexOf('err') != -1)
	        {
                if (msgs.startKeeper)
    		        if (window.confirm(msgs.startKeeper))
	    		        document.location = 'wmk:start';
		        return false;
	        }
	        else
	            if (sign.value)
	            {
	                if (this.flag)
		                return f.submit();
		            else
		                return true;
                }

            return false;
        },
        wmLogin : function(f, callback)
        {
            if (typeof func === 'function')
                callback();
            return this.checkLogin(f, null);
        }
    }
});


// Browser namespace
WM.extend(WM,
{
	Browser :
	{
		userAgent : navigator.userAgent,
		isIE : (navigator.userAgent.toLowerCase().indexOf('msie') != -1 && navigator.userAgent.toLowerCase().indexOf('opera') == -1 && navigator.userAgent.toLowerCase().indexOf('webtv') == -1),
		isOpera : !!window.opera,
		isGecko : navigator.userAgent.toLowerCase().indexOf('gecko') > -1 && navigator.userAgent.toLowerCase().indexOf('khtml') == -1,
		isWebKit: navigator.userAgent.toLowerCase().indexOf('AppleWebKit/') > -1,
		version : (navigator.userAgent.toLowerCase().match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/) || [])[1]
	}
});

// Events namespace
WM.extend(WM,
{
	Events :
	{
		aObservers : [],

		add : function(elem, type, func)
		{
			if (elem.addEventListener)
        			elem.addEventListener(type, func, false);
	        	else if (elem.attachEvent)
			{
				if (!this.aObservers.length)
				{
					this.aObservers.push([elem, type, func]);

					var self = this;

					this.add(window, 'unload', function()
					{
						self.detachObservers();
					});
				}
				else
					this.aObservers.push([elem, type, func]);

				elem.attachEvent('on' + type, func);
			}
        		else
				elem['on' + type] = func;
		},
		remove : function(elem, type, func)
		{
			if (elem.removeEventListener)
				elem.removeEventListener(type, func, false);
			else if (elem.detachEvent)
				elem.detachEvent('on' + type, func);
		},
		detachObservers : function()
		{
			for (var i = 0, j = this.aObservers.length; i < j; i++)
			{
				this.remove(this.aObservers[i][0], this.aObservers[i][1], this.aObservers[i][2]);
				this.aObservers[i][0] = null;
			}

			this.aObservers.length = 0;
		},
		cancel : function(e)
		{
			e = e || window.event;

			e.cancelBubble = true;
			e.returnValue = false;

			if(e.cancelable)
			{
				e.preventDefault();
				e.stopPropagation();
			}

			return false;
		},
		addLoadEvent : function(func)
		{
			var old = window.onload;
			if (typeof window.onload != 'function')
				window.onload = func;
			else
				window.onload = function()
				{
					old();
					func();
				}
		}
	}
});

// Ajax namespace
WM.extend(WM,
{
	Ajax :
	{
		// Ajax settings
		xmlobj :		null,
		postMethod :	'POST',
		asynchronous :	true,
		responseStatus :[],
		failed :		false,

		// Functions
		onLoading :		new Function(),
		onLoaded :		new Function(),
		onInteractive :	new Function(),
		onComplete :	new Function(),
		onError :		new Function(),
		onFail :		new Function(),

		init : function()
		{
			try {
				return this.xmlobj = new ActiveXObject('Msxml2.XMLHTTP'); }
			catch (e) {
				try {
					return this.xmlobj = new ActiveXObject('Microsoft.XMLHTTP'); }
				catch (ee) {
					if (typeof XMLHttpRequest != 'undefined')
						return this.xmlobj = new XMLHttpRequest();
					else
					{
						this.failed = true;
						return false;
					}
				}
			}
		},
		send : function(url, options)
		{
			if (!this.init())
				this.onFail();
			else
			{
				var xmlobj = this.xmlobj
				var method = options.method ? options.method.toLowerCase() : this.postMethod.toLowerCase();
				var asynchronous = options.asynchronous ? options.asynchronous : this.asynchronous;

				var params = options.params || '';
				if (params)
				{
					if (typeof params === 'object')
					{
						var p = '';
						for (var i in params)
							if (typeof params[i] != 'object' && typeof params[i] != 'function' && typeof params[i] != 'array')
								p += '&' + i + '=' + params[i];
						params = p.replace(/^\&/, '');
					}
					if (method == 'get' && params.length > 0)
						url += (url.match(/\?/) ? '&' : '?') + params;
				}

				try {
					xmlobj.open(method, url, asynchronous);
					xmlobj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

					if (method == 'post')
						xmlobj.send(params);
					else
						xmlobj.send(null);

					var self = this;
					xmlobj.onreadystatechange = function() {
						switch (xmlobj.readyState) {
							case 0:
								if (options.onError && typeof options.onError === 'function')
									options.onError();
								else
									self.onError();
								break;
							case 1:
								if (options.onLoading && typeof options.onLoading === 'function')
									options.onLoading();
								else
									self.onLoading();
								break;
							case 2:
								if (options.onLoaded && typeof options.onLoaded === 'function')
									options.onLoaded();
								else
									self.onLoaded();
								break;
							case 3:
								if (options.onInteractive && typeof options.onInteractive === 'function')
									options.onInteractive();
								else
									self.onInteractive();
								break;
							case 4:
								self.response = xmlobj.responseText;
								self.responseXML = xmlobj.responseXML;
								self.responseStatus[0] = xmlobj.status;
								self.responseStatus[1] = xmlobj.statusText;

								if (self.responseStatus[0] == '200')
								{
									if (options.onComplete && typeof options.onComplete === 'function')
										options.onComplete();
									else
										self.onComplete();
								}
								else
								{
									if (options.onError && typeof options.onError === 'function')
										options.onError();
									else
										self.onError();
								}
								break;
						}
					}
				}
				catch (e) {
					if (options.onError && typeof options.onError === 'function')
						options.onError();
					else
						self.onError();
				}
			}
		}
	}
});

// Plugins namespace
WM.extend(WM, { Plugins : { } });

// Plugins.Tabs namespace
WM.extend(WM.Plugins,
{
	Tabs :
	{
		options :
		{
			active :	1,
			tabclass :	'tab-container',
			activeclass :	'selected',
			navprefix :	'-nav',
			skipclass : 'skiped'
		},
		init : function(elem)
		{
		    var self = this;
		
		    var tabs = $C(this.options.tabclass, $(elem));
		    var links = function()
		    {
                var ret = [];
                var links = $T('LI', $(elem + self.options.navprefix));

                for (var i = 0, j = links.length; i < j; i++)
                    ret.push(links[i]);

                return ret;
		    }();

			show(this.options.active);
	
			for (var i = 0, j = links.length; i < j; i++)
			{
				var link = $T('A', links[i])[0];
				if (link && link.className != this.options.skipclass)
				{
					link.customindex = i + 1;
					link.onclick = function()
					{
						return show(this.customindex);
					};
				}
			}
			
                        function show(num)
                        {
                            for (var i = 0, j = tabs.length; i < j; i++)
	    		            {
		    		            $(tabs[i]).style.display = (i + 1) == num ? 'block' : 'none';
			    	            links[i].className = (i + 1) == num ? self.options.activeclass : '';
			                }
			                
			                return false;
                        }

		}
	}
});

// Plugins.ToolTip namespace
WM.extend(WM.Plugins,
{
	ToolTip :
	{
	    targetElem :
	    {
	        id :        'tooltip-shower',
	        css :       'tooltip-panel',
	        pattern :   /^help-/gi,
	        deltaX :   10,
	        deltaY :   10
	    },

	    show : function(e, callback)
	    {
            e = e || window.event;
            i = e.target || window.event.srcElement;

            var elem = WM.DOM.addTag('DIV', $T('BODY', null)[0],
            {
                id : this.targetElem.id,
                className : this.targetElem.css
            }, false);

            if (!elem)
                return false;
            
            var id = null;
            try
            {
                id = i.getAttribute('rel') || i.parentNode.getAttribute('rel');
            }
            catch(e) { }

            if (!id)
                return WM.DOM.toggle.hide(elem);

            if (id != this._id)
            {
                if (!this.targetElem.pattern.test(id))
                    return false;

                this._id = id;
                if (typeof callback === 'function')
                    callback(elem);
            }

            elem.style.left = e.clientX + this.targetElem.deltaX + 'px';
            elem.style.top = e.clientY + document.documentElement.scrollTop + this.targetElem.deltaY + 'px';

            WM.DOM.toggle.show(elem);
	    },

	    // private property
	    _id : null
	}
});

// Plugins namespace
WM.extend(WM, { Effects : { } });

// Effects.Animation namespace
WM.extend(WM.Effects,
{
	Animation :
    {
        options :
        {
            stop :      100,
            direction : 'up',
            delta :     10,
            duration :  10,
            callback :  new Function()
        },

        go : function(elem, params)
        {
			var stop = params.stop == undefined ? this.options.stop : params.stop;
			var direction = params.direction == undefined ? this.options.direction : params.direction;
			var delta = params.delta == undefined ? this.options.delta : params.delta;
			var duration = params.duration == undefined ? this.options.duration : params.duration;

			this._tmpParams =
			{
				stop :	    stop,
				direction :	direction,
				delta :     delta,
				callback :	typeof params.callback === 'function' ? params.callback : new Function()
			};

			var self = this; 

			var tId = setInterval(function()
			{
				var params = self._tmpParams;
				var obj = $(elem);

                switch (params.direction)
                {
                    case 'up': default:
                        if (obj.clientHeight < params.stop)
                        {
                            var d = obj.clientHeight + delta;
                            if (d > params.stop)
                                d = params.stop;
                            obj.style.height = d + 'px';
                        }
                        else
                            self.stop(params, tId);
                        break;
                    case 'down':
                        if (obj.clientHeight > params.stop)
                        {
                            var d = obj.clientHeight - delta;
                            if (d < params.stop)
                                d = params.stop
                            obj.style.height = d + 'px';
                        }
                        else
                            self.stop(params, tId);
                        break;
				}
			}, duration);
        },
        stop : function(params, timerid)
        {
            params.callback();
            clearInterval(timerid);
        },

		// private property
		_tmpParams :	null
    }
});

/*
***************************************************************************************************
	2008 © Webmoney
***************************************************************************************************
*/

