﻿
var currentPage = 1;
var pagesTotal = 1;

function refreshArtists(mode, param)
{
    var container = document.getElementById('artists_photos_container');
    var params = '';

    while (container.childNodes.length > 0) {
        container.removeChild(container.firstChild);
    }

    if ('page' == mode) {
        params = 'mode=page&page=' + parseInt(param);
    } else if ('abc' == mode) {
        params = 'mode=abc&code=' + parseInt(param);
    } else if ('all' == mode) {
        params = 'mode=all';
	/* next lines added by Vladimir Scherbina 25-12-08 BEGIN */
    } else if ('dig' == mode) {
        params = 'mode=dig'
    }
	/* END */

    xmlHttpRequest.open('get', '/AjaxArtists.aspx?' + params, false);
    xmlHttpRequest.onreadystatechange = function () { return null; }
    xmlHttpRequest.send(null);

    

    if (null != xmlHttpRequest.responseXML && null != xmlHttpRequest.responseXML.documentElement) {
        var xmldoc = xmlHttpRequest.responseXML.documentElement;

        var artistElements = xmldoc.getElementsByTagName('artist');
        var pageElements = xmldoc.getElementsByTagName('page');

        for (var i = 0; i < artistElements.length; ++i) {
            var elem = artistElements.item(i);
            var id = elem.getAttribute('id');
            var title = elem.getAttribute('title');
            var image = elem.getAttribute('image');

            var artistDiv = document.createElement('div');
            artistDiv.className = 'fl FotoReportFoto';
            artistDiv.style.borderColor = 'rgb(50, 117, 186)';
            var getThis = function () { return artistDiv; }
            artistDiv.onmouseover = new Function("this.style.borderColor = 'rgb(255, 0, 0)'");
            artistDiv.onmouseout = new Function("this.style.borderColor = 'rgb(50, 117, 186)'");

            var artistA = document.createElement('a');
            artistA.className = 'fl ArtistsFoto';
            artistA.style.backgroundColor = '' == image ? 'white' : 'transparent';
            artistA.style.backgroundAttachment = 'scroll';
            artistA.style.backgroundPosition = 'center';
            artistA.style.backgroundImage = 'url("/upload/contents/383/' + image + '")';
            artistA.setAttribute('href', '/artists/all/' + id + '/');
            artistA.setAttribute('title', title);

            artistDiv.appendChild(artistA);
            container.appendChild(artistDiv);
        }

    }	
}
function refreshArtistsPaging(mode, param)
{
    var container = document.getElementById('artists_photos_container');
    var params = '';

    while (container.childNodes.length > 0) {
        container.removeChild(container.firstChild);
    }

    if ('pageall' == mode) {
        params = 'mode=pageall&page=' + parseInt(param);
    } 

    xmlHttpRequest.open('get', '/AjaxArtists.aspx?' + params, false);
    xmlHttpRequest.onreadystatechange = function () { return null; }
    xmlHttpRequest.send(null);

    

    if (null != xmlHttpRequest.responseXML && null != xmlHttpRequest.responseXML.documentElement) {
        var xmldoc = xmlHttpRequest.responseXML.documentElement;

        var artistElements = xmldoc.getElementsByTagName('artist');
        var pageElements = xmldoc.getElementsByTagName('page');

        for (var i = 0; i < artistElements.length; ++i) {
            var elem = artistElements.item(i);
            var id = elem.getAttribute('id');
            var title = elem.getAttribute('title');
            var image = elem.getAttribute('image');

            var artistDiv = document.createElement('div');
            artistDiv.className = 'fl FotoReportFoto';
            artistDiv.style.borderColor = 'rgb(50, 117, 186)';
            var getThis = function () { return artistDiv; }
            artistDiv.onmouseover = new Function("this.style.borderColor = 'rgb(255, 0, 0)'");
            artistDiv.onmouseout = new Function("this.style.borderColor = 'rgb(50, 117, 186)'");

            var artistA = document.createElement('a');
            artistA.className = 'fl ArtistsFoto';
            artistA.style.backgroundColor = '' == image ? 'white' : 'transparent';
            artistA.style.backgroundAttachment = 'scroll';
            artistA.style.backgroundPosition = 'center';
            artistA.style.backgroundImage = 'url("/upload/contents/383/' + image + '")';
            artistA.setAttribute('href', '/artists/all/' + id + '/');
            artistA.setAttribute('title', title);

            artistDiv.appendChild(artistA);
            container.appendChild(artistDiv);
        }

    }	
}


function refreshArtists3(mode, param)
{
    var container = document.getElementById('artist_list_container');
    var params = '';

    if ('page' == mode) {
        params = 'mode=page&page=' + parseInt(param);
    } else if ('abc' == mode) {
        params = 'mode=abc&code=' + parseInt(param);
    } else if ('all' == mode) {
        params = 'mode=all';
    }

    xmlHttpRequest.open('get', '/AjaxArtists.aspx?' + params, false);
    xmlHttpRequest.onreadystatechange = function () { return null; }
    xmlHttpRequest.send(null);

    if (null != xmlHttpRequest.responseXML && null != xmlHttpRequest.responseXML.documentElement) {
        var xmldoc = xmlHttpRequest.responseXML.documentElement;

        var artistElements = xmldoc.getElementsByTagName('artist');
        var middleIndex = parseInt(artistElements.length / 2) + artistElements.length % 2;
        var leftCol = document.createElement('div');
        var rightCol = document.createElement('div');
        leftCol.className = 'ArtistsList fl';
        leftCol.style.width = '46%';
        rightCol.className = 'ArtistsList fl';
        rightCol.style.width = '46%';
        var leftList = document.createElement('ul');
        var rightList = document.createElement('ul');

        for (var i = 0; i < artistElements.length; ++i) {
            var elem = artistElements.item(i);
            var id = elem.getAttribute('id');
            var title = elem.getAttribute('title');
            var listItem = document.createElement('li');
            var listLink = document.createElement('a');
            listLink.setAttribute('href', '/artists/all/' + id + '/');
            listLink.appendChild(document.createTextNode(title));
            listItem.appendChild(listLink);
            
            if (i < middleIndex) {
                leftList.appendChild(listItem);
            } else {
                rightList.appendChild(listItem);
            }
        }
        
        leftCol.appendChild(leftList);
        rightCol.appendChild(rightList);
    }
    
    while (container.childNodes.length > 0) {
        container.removeChild(container.firstChild);
    }

    container.appendChild(leftCol);
    container.appendChild(rightCol);
}

function refreshArtists2(page, text)
{
    if ((null == text || '' == text) && (null != blockartistsText && '' != blockartistsText)) {
        text = blockartistsText;
    }

    var container = document.getElementById('artists_list_container');
    var artistsList = document.createElement('ul');
    var addParams = '';
    !!text && (addParams = '&text=' + text);

    xmlHttpRequest.open('get', '/AjaxArtists.aspx?mode=page&page=' + page + addParams, false);
    xmlHttpRequest.onreadystatechange = function () {};
    xmlHttpRequest.send(null);

    if (null != xmlHttpRequest.responseXML && null != xmlHttpRequest.responseXML.documentElement) {
        while (container.childNodes.length > 0) {
            container.removeChild(container.firstChild);
        }

        var xmldoc = xmlHttpRequest.responseXML.documentElement;

        var artists = xmldoc.getElementsByTagName('artist');

        for (var i = 0; i < artists.length; ++i) {
            var id = artists.item(i).getAttribute('id');
            var title = artists.item(i).getAttribute('title');

            var artistListItem = document.createElement('li');
            var artistLink = document.createElement('a');
            artistLink.setAttribute('href', '/artists/all/' + id + '/');
            artistLink.appendChild(document.createTextNode(title));
            artistListItem.appendChild(artistLink);

            artistsList.appendChild(artistListItem);
        }
        
        container.appendChild(artistsList);
    }
}

function initPages(text)
{
    if ((null == text || '' == text) && (null != blockartistsText && '' != blockartistsText)) {
        text = blockartistsText;
    }

    var prevElem = document.getElementById('artists_list_prev');
    var nextElem = document.getElementById('artists_list_next');
    var curPageElem = document.getElementById('artists_cur_page');
    var totalElem = document.getElementById('artists_pages_count');

    pagesTotal = getPagesCount(text);
    
    if (pagesTotal > 1) {
        document.getElementById('pager_container').style.display = 'block';
        totalElem.removeChild(totalElem.firstChild);
        totalElem.appendChild(document.createTextNode('' + pagesTotal));
    } else {
        document.getElementById('pager_container').style.display = 'none';
    }
    
    /* patch */
    if ('a' == nextElem.parentNode.tagName.toLowerCase()) {
        nextElem.parentNode.parentNode.replaceChild(nextElem, nextElem.parentNode);
        nextElem.style.cursor = 'pointer';
    }

    curPageElem.clearChildren = function () {
        while (curPageElem.childNodes.length > 0) {
            curPageElem.removeChild(curPageElem.firstChild);
        }
    }

    prevElem.onclick = function () {
        if (currentPage > 1) {
            refreshArtists2(--currentPage, text);
            curPageElem.clearChildren();
            curPageElem.appendChild(document.createTextNode(currentPage));
        }
        
        if (1 == currentPage) {
            prevElem.style.cursor = 'default';
        } else {
            prevElem.style.cursor = 'pointer';
        }
        
        if (pagesTotal == currentPage) {
            nextElem.style.cursor = 'default';
        } else {
            nextElem.style.cursor = 'pointer';
        }
    }
    nextElem.onclick = function () {
        if (currentPage < pagesTotal) {
            refreshArtists2(++currentPage, text);
            curPageElem.clearChildren();
            curPageElem.appendChild(document.createTextNode(currentPage));
        }
        
        if (1 == currentPage) {
            prevElem.style.cursor = 'default';
        } else {
            prevElem.style.cursor = 'pointer';
        }
        
        if (pagesTotal == currentPage) {
            nextElem.style.cursor = 'default';
        } else {
            nextElem.style.cursor = 'pointer';
        }
    }
}

function getPagesCount(text)
{
    var addParams = '';
    !!text && (addParams = '&text=' + text);
    xmlHttpRequest.open('get', '/AjaxArtists.aspx?mode=count' + addParams, false);
    xmlHttpRequest.onreadystatechange = function () {};
    xmlHttpRequest.send(null);

    if (null != xmlHttpRequest.responseXML && null != xmlHttpRequest.responseXML.documentElement) {
        var xmldoc = xmlHttpRequest.responseXML.documentElement;

        if (xmldoc.getElementsByTagName('count').length > 0) {
            try {
                var count = xmldoc.getElementsByTagName('count').item(0).getAttribute('value');

                if (parseInt(count) > 16) {
                    return parseInt(count / 16) + (0 == count % 16 ? 0 : 1);
                }
            } catch (e) {}
        }
    }

    return 1;
}

function searchArtists()
{
    var text = '';
    
    try {
        text = document.getElementById('blockartists_text').value;
        text = escape(text);
    } catch (e) {}
    
    refreshArtists2(1, text);
    initPages(text);
}