// JavaScript Document



/**
 * Iterate the document and remove all window.open tags and replace with target="_blank"
 * This is a HACK to get IE to register the referrer with IE
 * 
 * 
 */
function linksExternal()
{
    if (document.getElementsByTagName) {
        var anchors = document.getElementsByTagName("a");
        for ( var i = 0; i < anchors.length; i++) {
            var anchor = anchors[i];
            // Add the target here using some condidtion, could check for
            // window.open() insert target and remove onclick
            if (anchor.attributes['onclick'] && anchor.attributes['onclick'].nodeValue == "window.open(this.href);return false;") {
                //alert(anchor.attributes['onclick'].nodeValue);
                anchor.target = "_blank";
                anchor.attributes['onclick'].nodeValue = '';
                anchor.removeAttribute('onclick');
            }
        }
    }
}


// Listing Gallery Scripts
// TODO: Refine and reduce this code....
function gallery(i)
{
    if (document.getElementById('galleryImage')) {
        document.getElementById('galleryImage').src = galleryImages[i];
    }
    document.getElementById('index-info').innerHTML = 'Image ' + (i + 1)
            + ' of ' + galleryImages.length;
    if (document.getElementById('imgTitle')) {
        document.getElementById('imgTitle').innerHTML = galleryTitles[i];
    }
    setSelectedThumb(i);
    gImage = i;
}
function galleryMove(i)
{
    gallery(i < 0 ? (gImage == 0 ? galleryImages.length - 1 : gImage - 1)
            : (gImage == galleryImages.length - 1 ? 0 : gImage + 1));
}


function video(i)
{
    $('#index-info').html('Video ' + (i + 1) + ' of ' + galleryImages.length);
    $('#index-info').html(galleryTitles[i]);
    
    setSelectedThumb(i);
    if (gImage == i) {
        return;
    }
    gImage = i;
    var url = galleryImages[i].replace(/\&amp;/g, '&');
    $.get(url, function(data) {
        $('#galleryImage').html(data);
    });
}
function videoMove(i)
{
    video(i < 0 ? (gImage == 0 ? galleryImages.length - 1 : gImage - 1)
            : (gImage == galleryImages.length - 1 ? 0 : gImage + 1));
}

function setSelectedThumb(i)
{
    for (j = 0; j < galleryImages.length; j++) {
        if (document.getElementById('thumb_' + j)) {
            document.getElementById('thumb_' + j).className = '';
        }
    }
    if (document.getElementById('thumb_' + i)) {
        document.getElementById('thumb_' + i).className = 'selected';
    }

}

