function Bottom (id, level, category)
{
    window.location.href=webroot+'/Admin/Site/Articles/Edit/Bottom/'+id+'/'+level+'/'+category+'.html';
}

function Right (id, level, category)
{
    window.location.href=webroot+'/Admin/Site/Articles/Edit/Right/'+id+'/'+level+'/'+category+'.html';
}

function Edit (id)
{
    window.location.href=webroot+'/Admin/Site/Articles/Edit/'+id+'.html';
}

function Delete (id)
{
    if (confirm ('Tikrai ištrinti?'))
    {
        window.location.href=webroot+'/Admin/Site/Articles/Delete/'+id+'.html';
    }
}

function MoveDown (id)
{
    window.location.href=webroot+'/Admin/Site/Articles/MoveDown/'+id+'.html';
}

function MoveUp (id)
{
    window.location.href=webroot+'/Admin/Site/Articles/MoveUp/'+id+'.html';
}

function MultiFile (target, max)
{
    this.target = target;
    this.count = 0;
    this.id = 0;

    if (max)
    {
        this.max = max;
    }
    else
    {
        this.max = -1;
    }

    this.add = function (element)
    {
        if (element.tagName == 'INPUT' && element.type == 'file')
        {
            element.multifile = this;

            if (this.max != -1 && this.count >= this.max)
            {
                element.disabled = 'disabled';
            }

            element.onchange = function ()
            {
                ext = element.value.substr(element.value.lastIndexOf('.')+1, (element.value.length-1)-element.value.lastIndexOf('.')+1);

                if (ext == 'jpg' || ext == 'gif' || ext == 'png' || ext == 'JPG' || ext == 'GIF' || ext == 'PNG')
                {
                    element.name = 'file_' + element.multifile.id++;

                    var new_element = document.createElement('input');
                    new_element.type = 'file';
                    new_element.setAttribute ('class', 'fileinput');

                    element.parentNode.insertBefore (new_element, element);
                    element.multifile.add(new_element);
                    element.multifile.updateList(element);
                    element.style.display = 'none';
                }
                else
                {
                    alert ('File is not an image!');
                    element.value = '';
                    return false;
                }
            }

            this.count++;
            this.current = element;
        }
        else
        {
            alert ('Element is not file type!');
        }
    }

    this.updateList = function (element)
    {
        var br = document.createElement('br');
        var new_row = document.createElement('div');
        var delB = document.createElement('div');
        delB.setAttribute('class', 'icoD');
        delB.setAttribute('style', 'float: left; height: 24px; width: 32px;');
        new_row.element = element;
        delB.onclick = function ()
        {
            this.parentNode.parentNode.removeChild(this.parentNode.nextSibling);
            this.parentNode.element.parentNode.removeChild(this.parentNode.element);
            this.parentNode.parentNode.removeChild(this.parentNode);
            this.parentNode.element.multifile.count--;
            this.parentNode.element.multifile.current.disabled = false;
            return false;
        }

        if (element.value.indexOf('\\') != -1)
        {
            fname = element.value.substr(element.value.lastIndexOf('\\')+1, (element.value.length-1)-element.value.lastIndexOf('\\')+1);
        }
        else
        {
            fname = element.value.substr(element.value.lastIndexOf('/')+1, (element.value.length-1)-element.value.lastIndexOf('/')+1);
        }

        var file = document.createElement('div');
        file.setAttribute('style', 'float: left; height: 13px; padding-top: 5px; padding-bottom: 6px;');
        var fileT = document.createTextNode(fname);
        file.appendChild(fileT);
        new_row.appendChild(delB);
        new_row.appendChild(file);
        this.target.appendChild(new_row);
        this.target.appendChild(br);
    }
}

var PhotoArray = new Array;
var Total = 0;

function BuildPhotoArray ()
{
    var div = document.getElementById('upload');

    for (i = 0; i < div.childNodes.length; i++)
    {
        if (div.childNodes[i].className == 'photoFrame')
        {
            var id = div.childNodes[i].id;
            var r_id = id.substr (6, id.length-5);
            PhotoArray['p'+r_id] = r_id;
            Total++;
        }
    }
}

function RemovePhoto (PhotoId, ArticleId)
{
    if (confirm ('Tikrai ištrinti?'))
    {
        var r = new Ajax ('/Admin/Site/Articles/RemovePhoto.html', 'PhotoId='+PhotoArray['p'+PhotoId]+'&ArticleId='+ArticleId, Loading, RPReady);
        r.makeRequest (1, PhotoId);
    }
}

function DeleteConsultantPhoto (ConsultantId)
{
    if (confirm ('Tikrai ištrinti?'))
    {
        var r = new Ajax ('/Admin/Site/Articles/DeleteConsultantPhoto.html', 'ConsultantId='+ConsultantId, Loading, RCPReady);
        r.makeRequest (1);
    }
}

function RCPReady(response) {
    var ldiv = document.getElementById('photo');
    ldiv.parentNode.removeChild(ldiv);
    Loading(0);
}

function RPReady (response, id)
{
    var pdiv = document.getElementById('photo_'+id);

    for (i = 1; i <= Total; i++)
    {
        if (i > id)
        {
            PhotoArray['p'+i] = PhotoArray['p'+i]-1;
        }
    }

    pdiv.parentNode.removeChild(pdiv);

    Loading(0);
}

function ShowPreview (id, num)
{
    if (num == '')
    {
        cid = document.getElementById('current_'+id);
        window.open(webroot+'/Articles/Image/'+id+'/'+cid.innerHTML+'.html');
    }
    else
    {
        window.open(webroot+'/Articles/Image/'+id+'/'+num+'.html');
    }
}

function DeleteConsultant (id)
{
    if (confirm ('Tikrai ištrinti?'))
    {
        window.location.href=webroot+'/Admin/Site/Articles/DeleteConsultant/'+id+'.html';
    }
}

function EditConsultant (id)
{
    window.location.href=webroot+'/Admin/Site/Articles/EditConsultant/'+id+'.html';
}




