window.onload = function(e){
	sequenceViewer('piclist', 'div');
}

// JavaScript Document
var itemList;
var myList;
var nextBtn;
var prevBtn;
var n = 0;
var globalContainer;
var topNodes = new Array();
var bIsIE;
//
function sequenceViewer(container, tagName) {
	bIsIE = (navigator.userAgent.indexOf("MSIE") != -1 );
	itemList = document.getElementById(container);
	myList = itemList.getElementsByTagName(tagName);
	nextBtn = createButton(container, "nextBtn", "nextBtn", "Next", "nextbutton", "nextPic();");
	prevBtn = createButton(container, "prevBtn", "prevBtn", "Previous", "prevbutton", "prevPic();");
	globalContainer = container;
	removeChildDivs(myList);
	for(var j = 0; j<topNodes.length; ++j) {
		resizeImage(topNodes[j].firstChild, 150);
	}
	hideAllButOne(0);
	itemList.appendChild(prevBtn);
	itemList.appendChild(nextBtn);
	
	/*
	for(var z = 0; z<topNodes.length; ++z){
		resizeImage(topNodes[z].firstChild, 150);
	}
	*/
}
function removeChildDivs(from) {
	for(var z = 0; z<from.length; ++z){
		if(from[z].parentNode.id == globalContainer){
			topNodes.push(from[z]);
		}
	}
}
function hideAllButOne(i) {
	for(var z = 0; z<topNodes.length; ++z){
		if ( z==i ){
			//resizeImage(topNodes[z], 150);
			bIsIE ? topNodes[z].className = "gallery_scroll_show" : topNodes[z].setAttribute("class", "gallery_scroll_show");
		} else {
			bIsIE ? topNodes[z].className = "gallery_scroll_hide" : topNodes[z].setAttribute("class", "gallery_scroll_hide");
		}
	}
}
function nextPic() {
	if(n < topNodes.length-1){
		prevBtn.disabled = false;
		n++;
		hideAllButOne(n);
	} else {
		nextBtn.disabled = true;
	}
}
function prevPic() {
	if(n > 0){
		nextBtn.disabled = false;
		n--;
		hideAllButOne(n);
	} else {
	   prevBtn.disabled = true;
	}
}

function createButton(content, id,  name, label, newclass, onClickFunction){
	var newElt = document.createElement('INPUT');
	newElt.type = 'button';
	newElt.name = name;
	newElt.id = id;
	newElt.className = newclass;
	newElt.value = label;
	newElt["onclick"] = new Function(onClickFunction);
	return newElt;
}
//
function editCSSClass(cssClass, cssAttribute, value) {
	var cssRules;
	if (window.ActiveXObject) { // Check for IE
		cssRules = 'rules';
	} else {
		cssRules = 'cssRules';
	}
	for (var z = 0; z < document.styleSheets.length; z++){
		for (var j = 0; j < document.styleSheets[z][cssRules].length; j++) {
			if (document.styleSheets[z][cssRules][j].selectorText == cssClass) {
				document.styleSheets[z][cssRules][j].style[cssAttribute] = value;
			}
		}
	}	
}
function resizeImage(t, lim) {
	var w = t.width;
	var h = t.height;
	var multiplier;
	if(h > lim || w > lim) {
		if(Math.max(w, h) == w){
		   t.setAttribute("width", lim);
		   multiplier = t.width / w;
		   t.setAttribute("height", h*multiplier);
		} else if (Math.max(w, h) == h){
			t.setAttribute("height", lim);
			multiplier = t.height / h;
			t.setAttribute("width", w*multiplier);
		}
	}
}