﻿window.addEvent('domready', function() {
    if ($E('td.inputButtonFileTd')) Uploader.start();
    if ($('ColorsList')) SetLiSelect.start('ColorsList');
    if ($('DistancesList')) SetLiSelect.start('DistancesList');
});

var Uploader = {
    start: function() {
        Uploader.inputs = $ES('input[type=file]');
        Uploader.parse();
    },
    parse: function() {
        Uploader.inputs.each(function(input, o) {
            input.addEvent('change', function() {
                var name = input.name + '_text';
                var Text = $(name);
                Text.value = input.value;
            });
        });
    }
}

var SetLiSelect = {
    start: function(Id) {
        var List = $(Id);
        if (List == null)
            return;
        var ListA = List.getElements('a');
        ListA.each(function(el) {
            el.addEvent('click', function() {
                var Selected = List.getElement('a.selected');
                if (Selected != null)
                    Selected.removeClass('selected');
                el.addClass('selected');
                SetAction('PortfolioEditContentForm', 'color_and_distance');
                SubmitPortfolioEditContentForm();
            });
        });
    }
}

var ModalBlock;
var ModalBlock2;
var IsModalBlockShow;

window.onresize = function() {
    var Overlay = document.getElementById('overlay');
    if (Overlay.style.display == 'block') {
        var Layout = document.getElementById('layout');
        var Measurer = document.getElementById('measurer');
        Overlay.style.width = Measurer.offsetWidth + 'px';
        Overlay.style.height = Layout.offsetHeight + 'px';
        if (IsModalBlockShow) {
            if (ModalBlock != null)
                ModalBlock.style.left = (Measurer.offsetWidth > ModalBlock.offsetWidth ? (Measurer.offsetWidth - ModalBlock.offsetWidth) / 2 : 0) + 'px';
            if (ModalBlock2 != null)
                ModalBlock2.style.left = (Measurer.offsetWidth > ModalBlock2.offsetWidth ? (Measurer.offsetWidth - ModalBlock2.offsetWidth) / 2 : 0) + 'px';
        }
    }
}

var detect = navigator.userAgent.toLowerCase();

function checkIt(string) {
    place = detect.indexOf(string) + 1;
    thestring = string;
    return place;
}

function Login() {
    var LoginBlock = document.getElementById('login_block');
    var RightBlock = document.getElementById('right_block');
    var LoginLink = document.getElementById('login_link');
    if (LoginBlock != null && RightBlock != null && LoginLink != null) {
        if (LoginBlock.style.display == 'none') {
            LoginLink.className = 'link_gray';
            RightBlock.className = 'no_top_padding'
            LoginBlock.style.display = 'block';
        }
        else {
            LoginLink.className = '';
            RightBlock.className = ''
            LoginBlock.style.display = 'none';
        }
    }
}

function ShowMenu(Id1, Id2) {
    var Block1 = document.getElementById(Id1);
    var Block2 = document.getElementById(Id2);

    if (Block1 != null) {
        if (Block1.style.display == 'none')
            Block1.style.display = 'block';
        else
            Block1.style.display = 'none';
    }

    if (Block2 != null)
        Block2.style.display = 'none';
}

function ShowBlock(Id) {
    var Block = document.getElementById(Id);
    Block.style.display = 'block';
}

function HideBlock(Id) {
    var Block = document.getElementById(Id);
    Block.style.display = 'none';
}

function ShowOrHideBlock(Id) {
    var Block = document.getElementById(Id);
    if (Block.style.display == 'block')
        Block.style.display = 'none';
    else
        Block.style.display = 'block';
}

function VisibleSelects(visibility, type) {
    //    $(document.body).getElements('select');
    if (type == 'all') {
        $(document.body).getElements('select').each(function(el) {
            el.style.visibility = visibility;
        });
    }
    if (ModalBlock != null) {
        ModalBlock.getElements('select').each(function(el) {
            el.style.visibility = 'visible';
        });
    }
}

function getScrollY() {
    var scrOfY = 0;
    if (typeof (window.pageYOffset) == 'number') {
        //Netscape compliant
        scrOfY = window.pageYOffset;
    } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
    } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
    }
    return scrOfY;
}

//function getClientWidth() {
//    return document.compatMode == 'CSS1Compat' && !window.opera ? document.documentElement.clientWidth : document.body.clientWidth;
//}

//function getClientHeight() {
//    return document.compatMode == 'CSS1Compat' && !window.opera ? document.documentElement.clientHeight : document.body.clientHeight;
//}

function ShowModalBlock(Id) {
    var Block = $(Id);
    var Overlay = $('overlay');
    var Layout = $('layout');
//    var Measurer = $('measurer');
    Layout.adopt(Block);
    Block.style.display = 'block';

    if (IsModalBlockShow == true) {
        ModalBlock2 = ModalBlock;
        ModalBlock2.style.zIndex = 150;
    }
    ModalBlock = Block;
    IsModalBlockShow = true;
    //Block.style.top = (Block.getStyle('top').toInt() + getScrollY()) + 'px';
    var Top = (window.getHeight() > Block.getHeight() ? ((window.getHeight() - Block.getHeight()) / 2).toInt() : 0);
    Block.style.top = (getScrollY() + Top) + 'px';
//    Block.style.left = (Measurer.offsetWidth > Block.offsetWidth ? (Measurer.offsetWidth - Block.offsetWidth) / 2 : 0) + 'px';
    Block.style.left = (window.getWidth() > Block.getWidth() ? ((window.getWidth() - Block.getWidth()) / 2).toInt() : 0) + 'px';
    Overlay.style.width = window.getWidth() + 'px';
    Overlay.style.height = Layout.offsetHeight + 'px';
    Overlay.style.display = 'block';
    if (checkIt('msie'))
        VisibleSelects('hidden', 'all');
}

function HideModalBlock(Id) {
    var Block = document.getElementById(Id);
    var Overlay = document.getElementById('overlay');
    Block.style.display = 'none';
    Block.style.top = '';

    if (ModalBlock2 != null) {
        ModalBlock = ModalBlock2;
        ModalBlock.style.zIndex = 300;
        ModalBlock2 = null;
        if (checkIt('msie'))
           VisibleSelects('visible', 'modal');
    }
    else {
        IsModalBlockShow = false;
        ModalBlock = null;
        Overlay.style.display = 'none';
        if (checkIt('msie'))
            VisibleSelects('visible', 'all');
    }
}

function SetIdAndSubmit(FormId, Id) {
    var formObj = document.getElementById(FormId);
    if (formObj != null) {
        formObj.id.value = Id;
        formObj.submit();
    }
}

function SetId(FormId, Id) {
	//alert("v:" + Id);
    var formObj = document.getElementById(FormId);
    
	//alert($(formObj).get('name'));

    if (formObj != null) {

        //formObj.id.value = Id;
        //alert($(formObj).getElement('input[name=id]').get('value'));
        $(formObj).getElement('input[name=id]').set('value', Id);
        //alert($(formObj).getElement('input[name=id]').get('value'));

        // var formobj2 = document.getelementbyid(formid);
        // alert('id: ' + $(formObj2).getElement('input[name=id]').get('value'));
        
    }
    
    
}

function SetActionAndSubmit(FormId, Value) {
    var formObj = document.getElementById(FormId);
    if (formObj != null) {
        formObj.action_type.value = Value;
        formObj.submit();
    }
}

function SetAction(FormId, Value) {
    var formObj = document.getElementById(FormId);
    if (formObj != null) {
        formObj.action_type.value = Value;
    }
}

function ShowAddResponse(Id) {
    $('AddResponse').injectAfter('UserBlock' + Id);
    
    $('AddResponseForm').reset(); //поменял местами
    SetId('AddResponseForm', Id); //поменял местами
    
    ShowBlock('AddResponse');
}

function ShowNewDiscussion() {
    var Obj = document.getElementById("NewDiscussion");
    if (Obj != null) {
        Obj.style.display = "block";
    }
}

function HideNewDiscussion() {
    var Obj = document.getElementById("NewDiscussion");
    if (Obj != null) {
        Obj.style.display = "none";
    }
}

function ShowMan(Id) {
    var Man = $('Man' + Id);
    var AuthorInfo = $('AuthorInfo' + Id);
    Man.addClass('opacity');
    AuthorInfo.style.display = 'block';
}

function HideMan(Id) {
    var Man = $('Man' + Id);
    var AuthorInfo = $('AuthorInfo' + Id);
    Man.removeClass('opacity');
    AuthorInfo.style.display = 'none';
}

