﻿

function ImageScroll(){
            var MyElements = document.getElementsByClassName('HorScroll');
            for( var i=0; i<MyElements.length; i++){
                var imgSize = 100;
                var img = MyElements[i].getElementsByTagName('img').length;
                var imgTotal = img * imgSize;
                imgTotal += 100;
                MyElements[i].style.width = imgTotal + "px";
            }
        }

function MainImage(Id, Description, Title, Type){
    var Path = "";
    var img = document.getElementById(Type + "imgMain");
    img.removeAttribute('height');
    img.removeAttribute('width');
    
    if (Id.indexOf('/') != -1) {
        Path = Id;
        img.src = Path;
        document.getElementById(Type + "aViewImage").href = Path;
    }
    else {
        Path = "Handlers/File.ashx?FileId=" + Id + "&Thumb=";
        img.src = Path + "3"; // Set to the third index of the thumbs
        document.getElementById(Type + "aViewImage").href = Path + "O";
    }
        
    document.getElementById(Type + "Description").innerHTML = Description;
    document.getElementById(Type + "Title").innerHTML = Title;
    
}

window.onload = function(){
    if (document.getElementById) {
	    var t, i, rows;
	    var ts = document.getElementsByTagName("div");
	    for (t in ts) {
	        if (ts[t].className == "PhotoThumbs PG_ThumbNails") {
	            rows = ts[t].getElementsByTagName("ul");
	            if (rows.length > 0) { 
	                var b = rows[0].getElementsByTagName("a");
                    if (b.length > 0){
                        var imageId = b[0].id.split('_')[1];
                        MainImage(imageId, $('hd_' + imageId).value, $('ht_' + imageId).value, $('hpt_' + imageId).value);
                        //b[0].click();
                    }
                    else{
                        //rows[0].getElementById("imgMain").style.display = "none";
                        //rows[0].getElementById("aViewImage").style.display = "none";
                        //rows[0].getElementById("Description").innerHTML = "No Images available in this gallery.";
                    }
	             }
	        }
	    }
	}
}


function CheckImage(Image){

    // Walk up the DOM tree until we find it's containing DIV tag
    try {
        var parents = Element.ancestors(Image);
        var found = false;
        var i = 0;
        while((!found) && (i<parents.length)) {
            if (parents[i].tagName == "DIV") {
                found = true;
                var dimensions = parents[i].getDimensions();
                if (Image.height > dimensions.height) {
                    Image.height = 247;
                    if (Image.width > dimensions.width) {
                        Image.width = 330;
                    }
                }
            }
            i++;
        }
    } catch(e) {
        //alert(e);
    }
    
}