﻿/************************************************************
 *     Модуль манипуляции свойствами объекта
 *
 *     Автор:                     Агроник А.Ю.
 *     Дата создания:             08.06.2007
 *
 ************************************************************/
 
 var $global = {
    ShowText:"Подробнее",
    HideText:"Скрыть",
    Mouse:{
        X:"0",
        Y:"0"
    },
    GetConst:{
        Up:"up",
        First:"first",
        Last:"last"
    },
    CurrentMenu:null,
    SwapClasses:function(class1, class2, obj)
    {
        if(obj)
        {
            obj.className = obj.className == class1 ? class2 : class1;
        }
    },
    SwapText:function(obj, Text1, Text2)
    {
        if(obj)
        {
            var tmp = this.GetText(obj);
            this.SetText(obj, (tmp == Text1 ? Text2 : Text1));
        }
    },
    SwapVisibility:function(obj)
    {
        if(obj)
        {
            obj.style.display = obj.style.display == "none" ? "block" : "none";
        }
    },
    GetParent:function(obj, depth)
    {
        var res = null;
        if(obj)
        {        
            res = obj;
            var i = 1;
            while(depth >= i)
            {
                res = res.parentNode != null ? res.parentNode : res;
                i++;
            }
        }
        return res;
    },
    GetText:function(obj)
    {
        var result = "";
        if(obj)
            result = (obj.innerText) ? obj.innerText : ((obj.textContent) ? obj.textContent : "");
        return result;
    },
    SetText:function(obj, text)
    {
        if(obj)
        {
            if(obj.firstChild)
            {
                obj.removeChild(obj.firstChild);
            }
            
            obj.appendChild(document.createTextNode(text));
        }
    },
    SetError:function(obj)
    {
        if(obj)
            obj.className = "Error";
    },
    ClearError:function(obj)
    {
        if(obj)
            obj.className = "";
    },
    StepToNumber:function(str)
    {
        var res = str;
        while(res.indexOf("k") != -1)
        {
           res = res.replace("k", "000");
        }
        while(res.indexOf("m") != -1)
        {
           res = res.replace("m", "000000");
        }
        return res;
    },
    FindPosition:function(obj) 
    {
	    var curleft = curtop = 0;
	    if (obj.offsetParent) {
		    curleft = obj.offsetLeft
		    curtop = obj.offsetTop
		    while (obj = obj.offsetParent) {
			    curleft += obj.offsetLeft
			    curtop += obj.offsetTop
		    }
	    }
	    return [curleft,curtop];
    },
    ShowUnderObject:function(obj, target)
    {
        if(obj && target)
        {
            var h = target.offsetHeight;
            var position = $global.FindPosition(target);
            obj.style.position = "absolute";
            obj.style.top = Number(position[1]+h)+"px";
            obj.style.left = position[0]+"px";
            obj.style.display = "block";
        }
    },
    ShowAtPosition:function(obj)
    {
        if(obj)
        {
            obj.style.position = "absolute";
            obj.style.display = "block";
            obj.style.top = "0px";
            obj.style.left = "0px";
            var pos = $global.FindPosition(obj);
            obj.style.top = Number($global.Mouse.Y-pos[1])+"px";
            obj.style.left = Number($global.Mouse.X-pos[0])+"px";
        }
    },
    TrackMousePosition:function(e) 
    {
	    var posx = 0;
	    var posy = 0;
	    if (!e) var e = window.event;
	    if (e.pageX || e.pageY) 	{
		    posx = e.pageX;
		    posy = e.pageY;
	    }
	    else if (e.clientX || e.clientY) 	{
		    posx = e.clientX + document.body.scrollLeft
			    + document.documentElement.scrollLeft;
		    posy = e.clientY + document.body.scrollTop
			    + document.documentElement.scrollTop;
	    }
	    $global.Mouse.X = posx;
	    $global.Mouse.Y = posy;
	},
	InitEvents:function(e)
	{
	    document.onmousemove = $global.TrackMousePosition;	    
	},
	SwitchToggleGroupByClass:function(obj, class1, class2, formal)
	{
	    if(obj)
	    {
	        var objs = obj.parentNode.getElementsByTagName("div");
	        for(var i = 0;i<objs.length;i++)
	            if(objs[i] != obj)
	                if(objs[i].className.indexOf(formal) != -1)
	                    objs[i].className = class1[i];	               
	        obj.className = class2;
	    }
	},
	GetElementsByTagName:function(obj, tag)
	{
	    var result = new Array();
	    if(obj)
	    {
	        if(tag)
	        {
	            var tmp = obj.getElementsByTagName(tag);
	            if(tmp != null)
	            {
	                for(var i = 0;i<tmp.length;i++)
	                {
	                    if(tmp[i].parentNode == obj)
	                        result.push(tmp[i]);
	                }
	            }
	        }
	    }
	    return result;
	},
	GetElementByTagName:function(obj, tag, index)
	{
	    var result = null;
	    var array = $global.GetElementsByTagName(obj, tag);
	    if(array.length > 0)
	    {
	        if(index != null)
	        {
	            var ind = index == $global.GetConst.First ? 0 : index == $global.GetConst.Last ? (array.length-1) : Number(index);
	            if(ind < array.length)
	            {
	                result = array[ind];
	            } else { result = array[0]; }
	        } else { result = array[0]; }
	    }
	    return result;
	},
	GetElementsByClass:function(obj, className)
	{
	    //TODO: Дописать функцию получения массива элементов имени CSS класса
	},
	GET:function(obj, path)
	{
	    var result = obj;
	    var str = new String();
	    str = path;
	    if(obj)
	    {
	        var strArray = str.split(',');
	        for(var i = 0;i<strArray.length;i++)
	        {
	            var pair = strArray[i].split(':');
	            if(pair[0] == $global.GetConst.Up)
	                result = $global.GetParent(result, Number(pair[1]));
	            else
	                result = $global.GetElementByTagName(result, pair[0], pair[1]);
	        }
	    }
	    return result;
	},
	AddEvent:function(ev, func)
	{
	    var old = ev ? ev : function(){};
	    ev = function(){ old(); func() };
	}
 }
 window.onload = $global.InitEvents;
 
