﻿var currentBook;
var appendHtml = '';


function model_onload() {
    currentBook = $('#hiddenbooks ul:eq(0)').attr('id').substr(1);
    LoadBook(currentBook);

    if ($(".link-comp").length == 1) {
        var compid = $(".link-comp").attr("id").substr(2);
        $(".link-comp").colorbox({ transition: "fade", speed: 500, title: "<a href=\"/pdfcomposit/" + compid + "\">print composit (pdf)</a>" });
    }
}


function LoadBook(idbook) {
    $('.imgcontainer').remove();
    var i = $("<img/>");

    var cover = $("#b" + idbook + " > li:eq(0)");
    var cover_href = $(cover).find("a:eq(0)").attr('href');
    var ch = $(cover).find("span[title=h]").text();
    var cw = $(cover).find("span[title=w]").text();
    var mt = 0;
    if (Number(ch) < 462) {
        mt = Math.floor((462 - ch) / 2);
    }
    $('#cover').append('<img style="margin-top:' + mt + 'px" alt="" width="' + $(cover).find('a > span[title=w]').text() + '" height="' + $(cover).find('a > span[title=h]').text() + '" src="' + $(cover).find('a').attr('href') + '">');
    $('#cover').attr("rel", "book" + idbook);
    $('#cover').attr('href',cover_href);

    $('#b' + idbook + ' li:gt(0)').each(function (index) {
        ch = $(this).find("a > span[title=h]").text();
        mt = 0;
        if (Number(ch) < 462) {
            mt = Math.floor((462 - ch) / 2);
        }
        if (((index + 1) % 2) == 0)
            $('#model-detail').append('<li class="imgcontainer"><a title="" class="colorboxgallery" rel="book' + idbook + '" href="' + $(this).find('a').attr('href') + '"><img style="margin-top:' + mt + 'px" alt="" width="' + $(this).find('a > span[title=w]').text() + '" height="' + $(this).find('a > span[title=h]').text() + '" src="' + $(this).find('a').attr('href') + '"></a></li>');
        else
            $('#model-detail').append('<li class="left imgcontainer"><a title="" class="colorboxgallery" rel="book' + idbook + '" href="' + $(this).find('a').attr('href') + '"><img style="margin-top:' + mt + 'px" alt="" width="' + $(this).find('a > span[title=w]').text() + '" height="' + $(this).find('a > span[title=h]').text() + '" src="' + $(this).find('a').attr('href') + '"></a></li>');
    });
    $('a.colorboxgallery').colorbox({ transition: 'fade', speed: 500 });
    $('#printbook').attr('href', '/pdf/' + idbook);
}


function Search() {
    document.location.href = '/models/list?src=' + escape($('#searchWord').val());
}

function SubmitEnter(myfield, e) {
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;

    if (keycode == 13) {
        Search();
        return false;
    }
    else
        return true;
}
