IG_Image = function(url, title, id) {
    this.URL = url;
    title = unescape(title);
    this.Title = (title.length==0)? " ": title;
    this.ID = id;
}
function getIGId(idExpression) {
    var id = new String(idExpression);
    var segments = id.split("_");
    if (segments.length>1) {
        id = segments[1];
    }
    return id.toString();
}
function IGSliderScrollForward(eventSource) {
    var id = getIGId(eventSource.id);
    if (window["IG_"+id+"_PAGING_TYPE"]==0)
        scrollIGSlider(id, 1);
    else
        scrollIGSlider(id, window["IG_"+id+"_SCROLL_SIZE"]);
}
function IGSliderScrollBackward(eventSource) {
    var id = getIGId(eventSource.id);
    if (window["IG_"+id+"_PAGING_TYPE"]==0)
        scrollIGSlider(id, -1);
    else
        scrollIGSlider(id, -window["IG_"+id+"_SCROLL_SIZE"]);
}
function scrollIGSlider(id, shift) {
    var firstImageIndex = window["IG_"+id+"_firstImageIndex"];
    var lastImageIndex = window["IG_"+id+"_lastImageIndex"];
    firstImageIndex += shift;
    if (firstImageIndex<0)
        firstImageIndex = 0;
    window["IG_"+id+"_firstImageIndex"] = firstImageIndex;
    lastImageIndex = firstImageIndex + window["IG_"+id+"_SCROLL_SIZE"];
    if (lastImageIndex>window["IG_"+id+"_IMAGES"].length)
        lastImageIndex = window["IG_"+id+"_IMAGES"].length;
    window["IG_"+id+"_lastImageIndex"] = lastImageIndex;
    var item, label, image;
    var index = 0;
    window["IG_"+id+"_pageImages"] = new Array();
    for (var i=firstImageIndex; i<lastImageIndex; i++) {
        item = document.getElementById("IG_"+id+"_item"+index).firstChild;
        item.width = window["IG_"+id+"_PROGRESS_IMAGE_WIDTH"];
        
		item.height = window["IG_"+id+"_PROGRESS_IMAGE_HEIGHT"];
		item.src = window["IG_"+id+"_progressImageUrl"];
		item.style.display = "";
        window["IG_"+id+"_pageImages"][index] = new Image();
		window["IG_"+id+"_pageImages"][index].id = id;
		window["IG_"+id+"_pageImages"][index].index = index;
		window["IG_"+id+"_pageImages"][index].scrollIndex = i;
		window["IG_"+id+"_pageImages"][index].onload = function() {
			this.onload = null;
			var item = document.getElementById("IG_"+this.id+"_item"+this.index);
			item.firstChild.style.display = "";
			if (this.width > this.height) {
			    item.firstChild.height = this.height*window["IG_"+id+"_IMAGE_WIDTH"]/this.width;
			    item.firstChild.width = window["IG_"+id+"_IMAGE_WIDTH"];
			} else {
			    item.firstChild.width = this.width*window["IG_"+id+"_IMAGE_HEIGHT"]/this.height;
			    item.firstChild.height = window["IG_"+id+"_IMAGE_HEIGHT"];
			}
			item.index = this.index;
			item.firstChild.src = this.src;
			item.sliderIndex = this.scrollIndex;
		}
		window["IG_"+id+"_pageImages"][index].onerror = function() {
			this.onload = null;
			this.onerror = null;
			var item = document.getElementById("IG_"+this.id+"_item"+this.index);
			item.firstChild.style.display = "none";
		}
		window["IG_"+id+"_pageImages"][index].src = window["IG_"+id+"_THUMBNAILS_BASE_URL"] + getThumbnailImageUrl(window["IG_"+id+"_IMAGES"][i].URL);
		item = document.getElementById("IG_"+id+"_item"+index);
		label = document.getElementById("IG_"+id+"_label"+index);
		if (i == window["IG_"+id+"_selectedImage"]) {
            item.className = "IGSelectedItem";
            label.className = "IGSelectedLabel";
        } else {
            item.className = "IGItem";
            label.className = "IGLabel";
        }
        item.index = i;
        label.firstChild.firstChild.nodeValue = (i+1) + ". " + window["IG_"+id+"_IMAGES"][i].Title;
		index++;
    }
    for (var i=index; i<Number(window["IG_"+id+"_SCROLL_SIZE"]); i++) {
		item = document.getElementById("IG_"+id+"_item"+i);
		item.className = "IGItem";
        item.firstChild.style.display = "none";
		item.firstChild.src = "";
		label = document.getElementById("IG_"+id+"_label"+i);
        label.firstChild.firstChild.nodeValue = "";
		label.className = "IGLabel";
    }
    setControlButtons(id);
}
function setControlButtons(id) {
    var forwardButton = document.getElementById("IG_"+id+"_forwardButton");
    var backwardButton = document.getElementById("IG_"+id+"_backwardButton");
    if (window["IG_"+id+"_firstImageIndex"] == 0) {
        if (backwardButton.onclick != null) {
            backwardButton.src = window["IG_"+id+"_RESOURCE_PATH"] + "disabledbackwardbutton.gif";
            backwardButton.onclick = null;
            backwardButton.style.cursor = "default";
        }
    } else {
        if (backwardButton.onclick == null) {
            backwardButton.src = window["IG_"+id+"_RESOURCE_PATH"] + "backwardbutton.gif";
            backwardButton.onclick = function() { IGSliderScrollBackward(this); };
            backwardButton.style.cursor = "pointer";
        }
    }
    if (window["IG_"+id+"_lastImageIndex"] == window["IG_"+id+"_IMAGES"].length) {
        if (forwardButton.onclick != null) {
            forwardButton.src = window["IG_"+id+"_RESOURCE_PATH"] + "disabledforwardbutton.gif";
            forwardButton.onclick = null;
            forwardButton.style.cursor = "default";
        }
    } else {
        if (forwardButton.onclick == null) {
            forwardButton.src = window["IG_"+id+"_RESOURCE_PATH"] + "forwardbutton.gif";
            forwardButton.onclick = function() { IGSliderScrollForward(this); };
            forwardButton.style.cursor = "pointer";
        }
    }
}
function openImage(eventSource) {
    var id = getIGId(eventSource.id);
    var zoomImageHolder = document.getElementById("IG_"+id+"_zoomItem");
    var source = eventSource;
    if (source.index==null || source.index == window["IG_"+id+"_selectedImage"] && zoomImageHolder.lastChild.src.length>0)
        return;
    zoomImageHolder.lastChild.width = window["IG_"+id+"_PROGRESS_IMAGE_WIDTH"];
	zoomImageHolder.lastChild.src = window["IG_"+id+"_progressImageUrl"];
	zoomImageHolder.lastChild.style.display = "";
    window["IG_"+id+"_zoomImage"] = new Image();
    window["IG_"+id+"_zoomImage"].id = id;
	window["IG_"+id+"_zoomImage"].onload = function() {
		this.onload = null;
		item.style.display = "";
		var zoomImageHolder = document.getElementById("IG_"+this.id+"_zoomItem");
		if (this.width >  window["IG_"+id+"_MAX_WIDTH"]*0.9) {
		    zoomImageHolder.lastChild.width = window["IG_"+id+"_MAX_WIDTH"]*0.9;
		}
		else { 
		     zoomImageHolder.lastChild.width = this.width;
		}
		zoomImageHolder.lastChild.src = this.src;
	}
	window["IG_"+id+"_zoomImage"].onerror = function() {
		this.onload = null;
		this.onerror = null;
		var zoomImageHolder = document.getElementById("IG_"+this.id+"_zoomItem");
		zoomImageHolder.lastChild.style.display = "none";
	}
    var item, label;
    for (var i=0; i<Number(window["IG_"+id+"_SCROLL_SIZE"]); i++) {
        item = document.getElementById("IG_"+id+"_item"+i);
        if (item.index == window["IG_"+id+"_selectedImage"]) {
            item.className = "IGItem";
            label = document.getElementById("IG_"+id+"_label"+i);
            label.className = "IGLabel";
            break;
        }
    }
    
    window["IG_"+id+"_selectedImage"] = Number(eventSource.sliderIndex);
    eventSource.className = "IGSelectedItem";
    label = document.getElementById("IG_"+id+"_label"+source.index);
    label.className = "IGSelectedLabel";
    window["IG_"+id+"_zoomImage"].src = window["IG_"+id+"_BASE_URL"] + window["IG_"+id+"_IMAGES"][window["IG_"+id+"_selectedImage"]].URL;
    if (zoomImageHolder.firstChild.firstChild)
        zoomImageHolder.firstChild.firstChild.nodeValue = window["IG_"+id+"_IMAGES"][window["IG_"+id+"_selectedImage"]].Title;
    else
        zoomImageHolder.firstChild.nodeValue = window["IG_"+id+"_IMAGES"][window["IG_"+id+"_selectedImage"]].Title;
    if (window["IG_"+id+"_SHOW_DESCRIPTION"] == true)
    {
        var descriptionHolder = document.getElementById(window["IG_"+id+"_DESCRIPTION_HOLDER_ID"]);
        if (descriptionHolder!=null)
        {
            descriptionHolder.value = "";
        }
        var idHolder = document.getElementById(window["IG_"+id+"_ID_HOLDER"]);
        if (idHolder!=null)
        {
            idHolder.value = window["IG_"+id+"_IMAGES"][window["IG_"+id+"_selectedImage"]].ID;
            var getDescriptionButton = document.getElementById(window["IG_"+id+"_GET_DESCRIPTION_BUTTON_ID"]);
            if (getDescriptionButton!=null)
            {
                getDescriptionButton.click();
            }
        }
    }
}

function getThumbnailImageUrl(url)
{
    if(url.length==0)
        return "";
    var urlString = new String(url);
    urlString = unescape(urlString);
    var index = urlString.lastIndexOf(".");
    
    if(index==-1)
        return "";
    var extention = urlString.substring(index+1);
    var firstUrlPart = urlString.substring(0,index);
    url = firstUrlPart + "_" + extention + ".jpg";
    return url;
}
