// Llightbox2 and Highslide integration

/* *********
# Enable the highslide gallery globally across all products
# Use true to enable and false to disable
#
# To enable the gallery for an individual product, copy and paste
# the following code somewhere into the product description
# NOT descriptionShort

<!-- Highslide gallery enabler for product -->
<script type="text/javascript">
<![CDATA[
enable_Highslide_Gallery_For_Products = true;
]]>
</script>
<!-- END Highslide gallery enabler -->

********** */

var enable_Highslide_Gallery_For_Products = false;  // Set to true to enable HighSlide gallery for product images globally

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('\u0077\u0061\u0069\u0074\u0073\u0070\u0061\u0063\u0065\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;
    }
}
//function for individual elements

/********************* addHSLink function **********************
    hsOptions[object]
    hsOption provides the parameters for this function through use of an object with the following instance variables (with quotes):
    "element", "url", "type", "title", "width" (in pixels), and "height"
    The only ones that are required are "element" and "url".  It will use "image" for "type" by default, nothing for "title" and 800x600 for "width" and "height"
    An example call for this function would be:

addHSLink({
    "element" : "#my_product_image",
    "url" : "http://someurl.com/myvideo.swf",
    "type" : "flash",
    "title" : "A great video about California",
    "width" : "650",
    "height" : "420"
});

    "element" will be the string passed to jQuery and uses the $('selector') element selection system
    "url" is obviously the url to the Youtube video, other SWF file, or image
    "type" can be "image", "flash", and later on, "html", or "ajax"
    "width" & "height" are integers for the width and height in pixels for a video

    If you simply want to use an image without a title, and not a movie, you can simply use:

add HSLink({
    "element" : "#my_product_image",
    "url" : "http://someurl.com/myimage.jpg"
});

*/

function addHSLink (hsOptions) {
    var jQElem = $(hsOptions["element"]);
    if (typeof hsOptions["url"] !== 'string') {
        hsOptions["url"] = '';
    }
    if (typeof hsOptions["title"] !== 'string') {
        hsOptions["title"] = '';
    }
    if (typeof hsOptions["width"] !== 'number') {
        if (typeof hsOptions["width"] === 'string') {
            if (isNaN(parseFloat(hsOptions["width"]))) {
                hsOptions["width"] = 800;
            }
        }
        else {
            hsOptions["width"] = 800;
        }
    }
    if (typeof hsOptions["height"] !== 'number') {
        if (typeof hsOptions["height"] === 'string') {
            if (isNaN(parseFloat(hsOptions["height"]))) {
                hsOptions["height"] = 800;
            }
        }
        else {
            hsOptions["height"] = 600;
        }
    }
    if (typeof hsOptions["type"] === 'string' && hsOptions["type"] === 'image') {
        jQElem.each(function () {
            this.className = 'highslide';
            this.title = hsOptions["title"];
            this.onclick = function () {
                return hs.expand(this, {
                    transitions : ['expand']
                });
            };
        });
    }
    else if (typeof hsOptions["type"] === 'string' && hsOptions["type"] === 'flash') {
        jQElem.each(function () {
            this.className = 'highslide';
            this.title = hsOptions["title"];
            this.href = hsOptions["url"];
            this.onclick = function () {
                return hs.htmlExpand(this, {
                    objectType:'swf',
                    objectWidth: parseFloat(hsOptions["width"]),
                    objectHeight: parseFloat(hsOptions["height"]),
                    allowSizeReduction: false,
                    swfOptions: {
                        flashvars: {
                        },
                        params: {
                            allowFullScreen: 'true',
                            allowScriptAccess: 'always',
                            play: 'true'
                        }
                    }
                });
            };
        });
    }
}
//End Lightbox and Highslide Integration
