var books_raw = new Array(3702475, 4729529, 1572973, 4794286, 4317461, 2428977, 4182762,
                          4447459, 4051622, 4394520, 4627190, 3121754, 4309954, 3375865,
                          1471182, 4744070, 3641803, 3566267, 863856, 4308042,
                          5019132, 4984888, 4895768, 4778920, 4778927, 4319429);
var books_full = new Array();
var last_book = books_raw[books_raw.length -1];
var books_count = 0;

// get XML data
function get_ozon_books()
{
    books_raw.each(function(bookId, index) {
        books_full[bookId] = new Object();
        moojax(bookId);
        if (books_full[bookId].name != undefined) {
            display_book(bookId);
        } else {
            //console.info('book %d is undefined', bookId);
        }
    });
}

function display_book(book_index)
{
    var book = books_full[book_index];
    var books_table = document.getElementById('ozonBooks');
    var tbody = books_table.getElementsByTagName('tbody')[0];

    if (book.name) {
        books_count++;

        var book_pic   = document.createElement('img');
        book_pic.setAttribute('src', book.pic);
        book_pic.setAttribute('alt', book.name);

        var book_url   = document.createElement('a');
        book_url.setAttribute('target', '_blank');
        book_url.setAttribute('href', book.url + '?partner=rubashka-na-zakaz');
        book_url.className = 'ozonBookIcon';

        var book_name  = document.createElement('div');
        book_name.className = 'name_ozone';
        book_name.innerHTML = book.name;

        var book_price = document.createElement('div');
        book_price.className = 'price';
        book_price.innerHTML = book.price+' руб.';

        var book_td = document.createElement('td');
        book_td.className = 'product';

        book_url.appendChild(book_pic);
        book_td.appendChild(book_url);
        book_td.appendChild(book_price);
        book_td.appendChild(book_name);

        switch (books_count) {
            case 5:
                tbody.appendChild(book_tr);
                book_tr = document.createElement('tr');
                books_count = 1;
            break;
            case 1:
                book_tr = document.createElement('tr');
            break;
            default:
            break;
        }
        book_tr.appendChild(book_td);

        if (book_index == last_book) {
            tbody.appendChild(book_tr);
            return;
        }
    } else {
        if (books_raw[books_raw.length-1] == book_index) {
            tbody.appendChild(book_tr);
        }
    }
}

function moojax(book_id)
{
    var xhr_obj = new Request({
        url: customerServiceUrl,
        data: {action: 'getOzonBook', bookId: book_id},
        method: 'get',
        link: 'chain',
        async: false,
        onSuccess: function(raw, xml) {
            eval(raw); // здесь возвращаются две переменные с xml - descr & access
            if (customerServiceCode !== 0) {
                //console.info('service error: %s; %s', customerServiceCode, customerServiceErr);
                return false;
            }
            // good browsers
            if (window.DOMParser){
                parser    = new DOMParser();
                descrXml  = parser.parseFromString(descr, "text/xml");
                accessXml = parser.parseFromString(access, "text/xml");
            // Internet Explorer
            } else {
                descrXml = new ActiveXObject("Microsoft.XMLDOM");
                descrXml.async = "false";
                descrXml.loadXML(descr);

                accessXml = new ActiveXObject("Microsoft.XMLDOM");
                accessXml.async = "false";
                accessXml.loadXML(access);
            }

            var name = descrXml.getElementsByTagName('Name')[0]    ? descrXml.getElementsByTagName('Name')[0].childNodes[0].nodeValue : null;
            var pic  = descrXml.getElementsByTagName('Picture')[0] ? descrXml.getElementsByTagName('Picture')[0].childNodes[0].nodeValue : null;
            var url  = descrXml.getElementsByTagName('Url')[0]     ? descrXml.getElementsByTagName('Url')[0].childNodes[0].nodeValue: null;
            books_full[book_id].name = name;
            books_full[book_id].pic  = pic;
            books_full[book_id].url  = url;
            var price = accessXml.getElementsByTagName('Price')[0] ? parseInt(accessXml.getElementsByTagName('Price')[0].childNodes[0].nodeValue) : null;
            books_full[book_id].price = price;
        }
    }).send();
}

