﻿$(function () {
    $('.inv-pics .inv-thumb').parent().wrap('<div class="inv-thumb-wrapper" />');
    $(".inv-thumb").each(function (idx, el) {
        var $this = $(this);
        var pic_path = $this.attr("src");
        var url_path = "";
        // Handle either old or new image code
        if (pic_path.indexOf('webservices.phdcon.com') == -1) {
            var t_start = pic_path.indexOf("_t");
            var t_end;
            if (t_start != -1) {
                t_end = pic_path.indexOf(".jpg", t_start);
            }
            if (t_end != -1) {
                url_path = pic_path.replace(pic_path.substring(t_start, t_end), "");
            } else {
                url_path = pic_path;
            }
        } else {
            $qs = $.query.load(pic_path);
            url_path = '/PHDInventory/Pictures/' + unescape($qs.get('source'));
        }
        $this.parent().attr("href", url_path).addClass("gallery").removeAttr("target");
    });
    $('.gallery').not('#popup .gallery').colorbox({ current: "{current} of {total}" });
    $('#popup .gallery').click(function (ev) {
        ev.preventDefault();
        ev.stopPropagation();
        $('.inv-popup-bigpicture img').attr('src', $(this).attr('href'));
    });
});
