function imageZoom(){
imgContainer = document.createElement('div');
if (jsVars && jsVars.imgArray != '' && jsVars.imgArray != undefined) {
    imgs = jsVars.imgArray;
}
var imgBank = document.createElement('div');
imgBank.setAttribute('id', 'image-row');


//Set the options that will be used in the href to control the zoom and gallery functions
var zoomOptions = {};
zoomOptions.useZoom = 'hero'; //id of the main/hero img
zoomOptions.adjustX = 10; //x-axis offset in px
zoomOptions.adjustY = -4; //y-axis offset in px
zoomOptions.zoomWidth = 'auto'; //width in px or 'auto'
zoomOptions.zoomHeight = 'auto'; //height in px or 'auto'
zoomOptions.tint = '#ccc'; //hex value or false
zoomOptions.position = 'right' //left, right, top, bottom
zoomOptions.showTitle = true; //show the title, true/false

//Dynamically set the class and rel on the main image. If we do it dynamically 
//it takes less template modifications.
document.getElementById(zoomOptions.useZoom).setAttribute('class', 'cloud-zoom generated');
document.getElementById(zoomOptions.useZoom).setAttribute('rel', getZoomOptions(zoomOptions));

function getImages() {
    for (var i in imgs) {
        var anchor = document.createElement('a');
        var img = document.createElement('img');
        img.src = imgs[i].thumbnail;
        img.id = imgs[i].id;
        img.title = imgs[i].title;
        img.setAttribute('class', 'additional-images generated');
        img.setAttribute('data-large', imgs[i].large);
        img.setAttribute('data-popup', imgs[i].popup);
        img.setAttribute('data-rank', imgs[i].rank);
        img.setAttribute('data-prod_id', imgs[i].prod_id);
        img.setAttribute('data-type', imgs[i].type);
        img.setAttribute('data-inactive', imgs[i].inactive);
        img.setAttribute('alt', imgs[i].title);
        img.setAttribute('data-attribute_id', imgs[i].attribute_id);
        anchor.href = (imgs[i].popup !='')? imgs[i].popup : imgs[i].large;
        anchor.setAttribute('class', 'cloud-zoom-gallery generated');
        var imgRel = 'useZoom: ' +'\'' +zoomOptions.useZoom +'\' , ' +'smallImage: ' +'\'' +imgs[i].large +'\'';
        anchor.setAttribute('rel', imgRel );
        anchor.appendChild(img);
        imgBank.appendChild(anchor);
    }
}
getImages();
document.getElementById('additional-images').innerHTML = '';
document.getElementById('additional-images').appendChild(imgBank);

function getZoomOptions(zoomOptions) {
    var params = '';
    for (var option in zoomOptions) {
        if (zoomOptions.hasOwnProperty(option) && option != 'useZoom') {
            params += option + ': ';
            if(typeof zoomOptions[option] === 'number')
             {params += zoomOptions[option]}
            else
             { params += '\'' + zoomOptions[option] + '\''};
            params += ' , ';
        }
       
    }
    return params.substring(0, (params.length - 2) ); //trim last comma
}
}
imageZoom();

function clickFull(){//Replace the built in fullscreen image handling with a better one.
var imgPop = '<!DOCTYPE html><head><title>Additional Product Images</title><style type="text/css"> #popup-image-window {padding:5px; width:600px;} #popup-image-window-left {float:left; padding:5px; width:20%;} #popup-image-window-main {float:right; width:75%;} img {border:1px solid #D7D7D7; display:block; margin:5px; padding:3px; } #popup-image-window-additional-images img {width:75px;} </style></head><body><div id="popup-image-window"><div id="popup-image-window-left"><p>Rollover images for alternate views.</p><div id="popup-image-window-additional-images"></div></div><div id="popup-image-window-main"><div id="popup-image-window-hero"></div><br style="clear:both"></div></div></body></html>';
imgWindow = window.open('', 'popupImageWindow','status=1,width=600,height=600,toolbar=no');
imgWindow.document.title = 'Additional Product Images.';
imgWindow.document.write(imgPop);
imgCopy = $('#image-row').html();
heroCopy = $('#hero').html();
//setTimeout("imgCopy = $('#image-row').html()",500);
//setTimeout("heroCopy = $('#hero').html()",500);
$('#popup-image-window-additional-images', window.imgWindow.document).html(imgCopy);
$('#popup-image-window-hero', window.imgWindow.document).html(heroCopy);
$('#hero-img', window.imgWindow.document).removeAttr('height');
$('#hero-img', window.imgWindow.document).removeAttr('width');
$('#hero-img', window.imgWindow.document).css('width','');
$('#hero-img', window.imgWindow.document).css('height','');
$('#hero-img', window.imgWindow.document).attr('src', $('.additional-images:first').attr('data-popup') );
$("#popup-image-window-additional-images a", window.imgWindow.document).click(function(){$("#hero-img", window.imgWindow.document).attr("src", $(this).attr("href"));return false;});
}

$(document).ready(function(){
 $('.click-fullscreen').click(function(){ setTimeout("clickFull()",1000); return false; });
});

//This is because there are some images with a pop-up and thumb only and
//we are using the popup as the large so we need to hard set the width.
$(window).load(function(){
 imgH = $('#hero-img').height();
 $('#hero img').css('height',imgH);
});
