window.rotatingGalleryBehavior = new function() {
		this.intPictureChangeInterval = 3000;
		this.oPictureRotationTimer = null;
		this.intCurrentPictureNumber = 0;
		this.intNumberOfPictures = 0;
		this.arrPictures = [];
		
		eval(estrada.namespace);
		
	tilt.attachEvent(document, "layout", function() {
		
		////////////////////////////////////////////
		// create layout
		////////////////////////////////////////////
		var layout = window.commonLayout.layout
						
		layout.bind("rotating-gallery").to(document);

	});
	this.rotateGallery = function() {
		rotatingGalleryBehavior.nextPicture();
		rotatingGalleryBehavior.startGalleryRotation();
	}
	this.nextPicture = function() {
		rotatingGalleryBehavior.adjustPictureNumber(1);
	}
	this.adjustPictureNumber = function(intAdjustment) {
		rotatingGalleryBehavior.stopGalleryRotation();
		rotatingGalleryBehavior.hideGallery();
		switch (intAdjustment) {
			case 1:
				if (++rotatingGalleryBehavior.intCurrentPictureNumber >=
						rotatingGalleryBehavior.intNumberOfPictures) {
					rotatingGalleryBehavior.intCurrentPictureNumber = 0;
				}
				break;
			default:
				break;
		}
		rotatingGalleryBehavior.showPicture(
			rotatingGalleryBehavior.intCurrentPictureNumber);
	}
	
	this.hideGallery = function() {
		foreach(map(rotatingGalleryBehavior.arrPictures), function(oPicture) {
			oPicture.style.display="none";
		});
	}
	this.startGalleryRotation = function() {
		if (!rotatingGalleryBehavior.oPictureRotationTimer) {
			rotatingGalleryBehavior.oPictureRotationTimer =
				setInterval(rotatingGalleryBehavior.rotateGallery,
				rotatingGalleryBehavior.intPictureChangeInterval);
		}
	}
	this.stopGalleryRotation = function() {
		if (rotatingGalleryBehavior.oPictureRotationTimer) {
			clearInterval(rotatingGalleryBehavior.oPictureRotationTimer);
			rotatingGalleryBehavior.oPictureRotationTimer = null;	
		}
	}
	this.showPicture = function(intPictureNumber) {
		var oPicture = rotatingGalleryBehavior.arrPictures[intPictureNumber];
		if (oPicture) {
			oPicture.style.display = "block";
		}
	}
}
///////////////////////////////////////////////////////////////////////
// check for and implement rotating gallery
///////////////////////////////////////////////////////////////////////
estrada.behavior("rotating-gallery").is({
	bind: function(node) {
		var oGallery = first(getElements(node, "dd", "gallery"));
		var oPanel = first(getElements(node, "div", "panel"));
		var oAlignGallery = first(getElements(node, "dd", "align-gallery"));
		var strLmargin = "0";
		var strRmargin = "10px";
		var strAlign = "left";
		if (oGallery) {
			if (oAlignGallery.innerHTML.indexOf("right") == 0) {
				strLmargin = "10px";
				strRmargin = "0";
				strAlign = "right";
			} else if (oAlignGallery.innerHTML.indexOf("middle") == 0) {
				strLmargin = strRmargin = "auto";
				strAlign = "middle";
			}
			var strGalleryDir = oGallery.innerHTML;
			var oXhr = window.XMLHttpRequest ? new XMLHttpRequest() : window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : null;
			if (oXhr) {
				oXhr.onreadystatechange = function() {
					if (oXhr.readyState == 4 && oXhr.status == 200) {
						var oDoc = document.createElement("div");
						oDoc.innerHTML = oXhr.responseText;
						var bolSkipFirst = true;
						var oImg, strValue, intPictureNumber = 0;
						var oGalleryHolder = document.createElement("div");
						oGalleryHolder.className = "l-gallery-holder";
						if (oPanel) {
							oPanel.parentNode.insertBefore(oGalleryHolder, oPanel);
						} else {
							oGallery.parentNode.insertBefore(oGalleryHolder, oGallery);
						}
						foreach(map(oDoc.getElementsByTagName("a")), function(oLink) {
							if (bolSkipFirst) {
								bolSkipFirst = false;
							} else {
								if (oLink.innerHTML.indexOf(".jpg") > 0) {
									oLink.parentNode.style.display = "none";
									oImg = document.createElement("img");
									oImg.className = "l-gallery-image";
									if (oLink.href) {
										oImg.setAttribute("src", oLink.href);
									}
									oImg.style.display = "none";
									oImg.align = strAlign;
									oImg.style.marginRight = strRmargin;
									oImg.style.marginLeft = strLmargin;
									oGalleryHolder.appendChild(oImg);
									rotatingGalleryBehavior.arrPictures.push(oImg);
								}
							}
						});
						rotatingGalleryBehavior.intNumberOfPictures =
							rotatingGalleryBehavior.arrPictures.length;
						if (rotatingGalleryBehavior.intNumberOfPictures > 0) {
							rotatingGalleryBehavior.showPicture(
								rotatingGalleryBehavior.intCurrentPictureNumber);
							if (rotatingGalleryBehavior.intNumberOfPictures > 1) {
								rotatingGalleryBehavior.startGalleryRotation();
							}
						}
					}
				}	
				oXhr.open("GET", strGalleryDir, true);
				oXhr.send(null);
			}			
		}
	}
});

function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}