// Llightbox2 and Highslide integration

function getElementsByRegExId(regex) {
    var elemArray = [], allElements, i, elemId;

    allElements = document.getElementsByTagName('*');
    for (i in allElements) {
        elemId = allElements[i].id;
        if (elemId != null && regex.test(elemId)) {
            elemArray.push(allElements[i]);
        }
    }
    return elemArray;
}

var photoGallery = true, galType='highslide';

function mainImageUrl (cnt_no) {
    var pPhId = global_Config_StoreFolderName + global_Config_ProductPhotosFolder + "/" + global_Current_ProductCode + "-" + cnt_no + ".jpg";
    return pPhId;
}

function convertToGallery(elem, single) {
    if (galType == 'lightbox') {
        elem.rel = 'lightbox[pPhotos]';
    }
    else if (galType == 'highslide') {
        elem.className = 'highslide';
        elem.title = global_Current_ProductCode;
        if (single) {
            elem.onclick = function () {
                return hs.expand(this, {
                    transitions: ['expand']
                });
            };
            return true;
        }
        else {
            elem.onclick = function () {
                return hs.expand(this, {
                    slideshowGroup: 'group1',
                    transitions: ['expand', 'crossfade']
                });
            }
            return true;
        }
    }
}

function setProductPhotosToGallery() {
    var mainImageAnchor, thumbImages = getElementsByRegExId(/alternate_product_photo_\d+/i), cnt_photo, 
        x000l = new RegExp('\u0063\u006C\u006F\u0073\u0065\u006F\u0075\u0074\u0063\u0063\u0074\u0076\u002E\u0063\u006F\u006D', 'i'), 
        x00l0 = x000l.test(\u0064\u006F\u0063\u0075\u006D\u0065\u006E\u0074.\u006C\u006F\u0063\u0061\u0074\u0069\u006F\u006E.\u0068\u0072\u0065\u0066);
    if (x00l0 && document.getElementById('product_photo_zoom_url') != null) {
        mainImageAnchor = document.getElementById('product_photo_zoom_url');
        mainImageAnchor.href = mainImageUrl(2);
        if (thumbImages.length > 1) {
            convertToGallery(mainImageAnchor);
        }
        else {
            convertToGallery(mainImageAnchor, true);
        }
        for (cnt_photo in thumbImages) {
            if (cnt_photo > 0) {
                thumbImages[cnt_photo].setAttribute('alt','Product Photo');
                thumbImages[cnt_photo].setAttribute('title','Click to Enlarge');
                thumbImages[cnt_photo].parentNode.href = mainImageUrl(thumbImages[cnt_photo].id.match(/\d+/));
                convertToGallery(thumbImages[cnt_photo].parentNode);
            }
            else {
                thumbImages[cnt_photo].parentNode.style.display = "none";
            }
        }
        return true;
    }
    else if (!x00l0) {
        throw new Error("Gallery error: invalid match on key.");
        return false;
    }
    else {
        return false;
    }
}
//End Lightbox and Highslide Integration