﻿///<reference path="global.js" />
var $M = {
    HideAllPages: function(ContentBlockId, ContentObjType, NavigationBlockId, NavigationObjType, NavigationClearCssClass) {
        var _container = $G.Get.ById(ContentBlockId);
        if (_container) {
            var _arr = $global.GetElementsByTagName(_container, ContentObjType);
            for (var i = 0; i < _arr.length; i++)
                $global.Display.Hide(_arr[i]);
        }
        var _cont = $G.Get.ById(NavigationBlockId);
        if (_cont) {
            var _arr = $G.Get.ByTag(_cont, NavigationObjType);
            for (var i = 0; i < _arr.length; i++)
                _arr[i].className = NavigationClearCssClass;
        }
    },
    Pager: function(link, DisplayedId) {
        $m.HideAllPages($G.GET(link, 'up:2,div:1').id, 'div', 'NavigationLinks', 'a', 'pager_all_def');
        $G.Display.Block($G.Get.ById(DisplayedId));
        link.className = "pager_all";
    },
    changeClassName: function(link, group_id) {
        var _arr = $global.GetElementsByTagName(link.parentNode, "a");
        for (var i = 0; i < _arr.length; i++)
            _arr[i].className = (_arr[i] == link ? (group_id == 1 ? "menu_a" : "news_a") : "");
    },
    AlignToCenter: function(obj) {
        if (obj) {
            var _deltaX = (obj.offsetWidth / 2);
            var _centerW = ($M.GetSize("x") / 2) + document.body.scrollLeft;
        }
    },
    GetSize: function(axis) {
        var x, y;
        if (self.innerHeight) {
            x = self.innerWidth;
            y = self.innerHeight;
        }
        else if (document.documentElement && document.documentElement.clientHeight) {
            x = document.documentElement.clientWidth;
            y = document.documentElement.clientHeight;
        }
        else if (document.body) {
            x = document.body.clientWidth;
            y = document.body.clientHeight;
        }
        return (axis == "x") ? x : (axis == "y") ? y : 0;
    },
    PC:
    {
        Close: function() {
            $G.Display.Hide($G.Get.ById('PhotoContainer'));
        },
        Open: function(id) {
            var url = "GetImages.ashx?id=" + id + "&rnd=" + Math.random();
            var request = new Request();
            request.Send(url, function callback(req) {
                var block = $G.Get.ById('PhotoContainer');
                block.innerHTML = req.responseText;
                $M.AlignToCenter(block);
                $G.Display.Block(block);
            } .bind(request), function() { } .bind(request)
        );
        }
    },
    Trim: function(str) {
        return str.replace(/( )+/gi, " ").replace(/^ /gi, "").replace(/ $/gi, "");
    },
    InitMenu: function() {

        var obj = $G.Get.ById('MainMenu');
        var arr = $G.Get.ByTag(obj, 'div');
        for (var i = 0; i < arr.length; i++) {
            var a_width = $G.GET(arr[i], 'a:0').offsetWidth;
            var _div = $G.GET(arr[i], 'div:0,div:0,div:1,div:0');
            if (_div)
                _div.style.width = (a_width - 45) + 'px';
            _div = $G.GET(arr[i], 'div:0,div:0,div:1');
            if (_div)
                _div.style.width = (a_width - 45) + 'px';

        }
    },
    PM: {
        ExpandNode: function(id) {
            if ($G.Get.ById(id) != null)
                $G.Get.ById(id).style.display = "block";
        },
        CollapseNode: function(id) {
            if ($G.Get.ById(id) != null) {
                $G.Get.ById(id).style.display = "none";
            }
        },
        ColapseAll: function(obj) {
            var root = $G.GET(obj, "up:3");
            var arr = $G.Get.ByTag(root, "ul");
            for (var i = 1; i < arr.length; i++) {
                var cur_obj = $G.GET(arr[i], "li:0,div:0");
                if (cur_obj)
                    cur_obj.style.display = "none";
            }
        },
        TreeNodeClick: function(id, Expanded, Collapsed, obj) {

            var trueId = id.indexOf("_el") > 0 ? id.substring(0, id.length - 3) : id;
            if ($G.Get.ById(id + "_link").href == "javascript:void(0)") {
                if (obj)
                    $M.PM.ColapseAll(obj);
                if ($G.Get.ById(trueId).style.display == "none") {
                    if (obj)
                        obj.className = "Active";
                    $M.PM.ExpandNode(id);
                }
                else {
                    $M.PM.CollapseNode(id);
                }
            }
        }
    }
}
var $m = $M;

var $in = {};
function menu_over(obj, id) {
    obj.className = 'main_menu_a2';
    if (obj) {
        $in[id] = true;
        var menu = $G.Next(obj, 2);
        $G.Display.Block(menu);
        menu.onmouseover = function() {
            $in[this.id] = true;
        } .bind({ obj: obj, id: id, menu: menu });
        menu.onmouseout = function() {
            $in[this.id] = false;
            setTimeout(function() {
                menu_out(this.id);
            } .bind(this), 100);
        } .bind({ obj: obj, id: id, menu: menu });
        obj.onmouseout = function() {
            $in[this.id] = false;
            setTimeout(function() {
                menu_out(this.id);
            } .bind(this), 100);
        } .bind({ obj: obj, id: id, menu: menu });
    }
}
function menu_out(id) {
    if (!$in[id]) {
        $G.Display.Hide($G.Get.ById(id));
        document.getElementById('a_' + id).className = 'main_menu_a';
    }
}
var wo = window.onload;
window.onload = function() {
    if (wo)
        wo();
    $M.InitMenu();
}
