catalog_ppip_cw = 0;
catalog_ppip_ch = 0;
catalog_pgis = null;
$(function () {
    //fotogaleria
    catalog_pgis = $('#ctlgProductPgImages div.pgi');
    catalog_ppip_cw = $('#ctlgProductPgPreview').width();
    catalog_ppip_ch = $('#ctlgProductPgPreview').height();
    catalog_pgis.hover(function () {
        catalog_ppgShowPreview(this);
    }, function () {
        ;
    });
    //zobrazenie prveho obrazku
    catalog_ppgShowPreview($('#ctlgProductPgImages div.pgi:eq(0)'));
    //accessories
    $('#ctlgProductAccessories dt a').lightBox({
        imageLoading: '/public/themes/lightbox/images/lightbox-ico-loading.gif',
        imageBtnClose: '/public/themes/lightbox/images/lightbox-btn-close.gif',
        imageBtnPrev: '/public/themes/lightbox/images/lightbox-btn-prev.gif',
        imageBtnNext: '/public/themes/lightbox/images/lightbox-btn-next.gif',
        imageBlank: '/public/themes/lightbox/images/lightbox-blank.gif',
        txtImage: '',
        txtOf: ' / '
    });
    //details
    $('#ctlgProductDetails dt a').lightBox({
        imageLoading: '/public/themes/lightbox/images/lightbox-ico-loading.gif',
        imageBtnClose: '/public/themes/lightbox/images/lightbox-btn-close.gif',
        imageBtnPrev: '/public/themes/lightbox/images/lightbox-btn-prev.gif',
        imageBtnNext: '/public/themes/lightbox/images/lightbox-btn-next.gif',
        imageBlank: '/public/themes/lightbox/images/lightbox-blank.gif',
        txtImage: '',
        txtOf: ' / '
    });
});

function catalog_ppgShowPreview(pgi1) {
    catalog_pgis.removeClass('pgi-active');
    var pgi = $(pgi1);
    
    var previewImgAnchor = $('a',pgi);
    var aid = previewImgAnchor.attr('id');
    var previewUrl = $('img',previewImgAnchor).attr('longdesc');
    var descr = previewImgAnchor.attr('title');
    
    pgi.addClass('pgi-active');
    
    var pc = $('#ctlgProductPgPreview');
    var iid = aid + '_preview';
    var did = aid + '_descrOverlay';
    var ddid = aid + '_descr';
    $('img',pc).hide();
    $('div',pc).hide();
    var img = $('#' + iid);
    if (img.length) {
        catalog_ppgImgOnLoad(img);
        $('#' + did).show();
        $('#' + ddid).show();
    } else {
        var img = $('<img src="' + previewUrl + '" alt="" id="' + iid + '" />');
        img.hide();
        pc.append(img);
        catalog_ppgImgOnLoad(img);
        //vytvorime description
        if (descr) {
            var dd = $('<div></div>')
                .addClass('imgdescr-overlay')
                .attr('id',ddid);
            var d = $('<div></div>')
                .text(descr)
                .addClass('imgdescr')
                .attr('id',did);
            pc.append(dd).append(d);
            dd.height(d.height());
        }
    }
}

var catalog_ppgShowPreview_imgLoadTimer = null;
function catalog_ppgImgOnLoad(img) {
    if (catalog_ppgCheckImgLoad(img)) {
        window.clearTimeout(catalog_ppgShowPreview_imgLoadTimer);
        catalog_ppgShowPreview_imgLoadTimer = null;
        //ulozime obrazok na vysku
        var i = $(img);
        var iParent = $(img).parent();
        i.css({
            position:'absolute',
            top: Math.floor(((catalog_ppip_ch - i.height()) / 2)) + 'px',
            left: Math.floor(((catalog_ppip_cw - i.width()) / 2)) + 'px'
        });
        i.show();
    } else {
        catalog_ppgShowPreview_imgLoadTimer = window.setTimeout(function () {
            catalog_ppgImgOnLoad(img);
        },100);
    }
}

function catalog_ppgCheckImgLoad(img) {
    return $(img)[0].complete;
}
