var currentView;
var currentStandardViewNumber = 0;

function showView(id) {
	var newView = document.getElementById(id);

	if (newView != null) {
		if (currentView == null) {
			// fist call -> simply fade in new view!
			opacity(newView.id, 0, 100, 500);
		} else if (newView.id != currentView.id) {
			if (currentView != null) {
				// fade out old view
				opacity(currentView.id, 100, 0, 500);
			}
			// fade in new view
			opacity(newView.id, 0, 100, 500);
		}
	}
}

function opacity(id, opacStart, opacEnd, millisec) { 
    //speed for each frame 
    var speed = Math.round(millisec / 100); 
    var timer = 0; 

    //determine the direction for the blending, if start and end are the same nothing happens 
    if(opacStart > opacEnd) { 
        for(i = opacStart; i >= opacEnd; i--) { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        }
		setTimeout("hideView('" + id + "')", (timer * speed));
    } else if(opacStart < opacEnd) { 
        for(i = opacStart; i <= opacEnd; i++) 
            { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        }
		setTimeout("storeView('" + id + "')", (timer * speed));
    } 
} 

//change the opacity for different browsers 
function changeOpac(opacity, id) { 
    var object = document.getElementById(id).style; 
	object.visibility = "visible";
    object.opacity = (opacity / 100); 
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")";
}

function storeView(id) {
	currentView = document.getElementById(id);
}

function hideView(id) {
	var view = document.getElementById(id);
	if (view != null) {
		view.style.visibility = "hidden";
		if (currentView == view) {
			currentView = null;
		}
	}
}

function showColor(number, contextPath, zoomImageURL) {
	showView('standardview'+number);
	currentStandardViewNumber = number;
	if (zoomImageURL && zoomImageURL.length > 0) {
		var zoomViewImage = document.getElementById("zoomimage");
		if (zoomViewImage) {
			zoomViewImage.src = contextPath + zoomImageURL;
		}
	}
}

/* dragging stuff for zoom view ***********************************/
function initZoomViewDrag() {
	var draggableImage = document.getElementById("zoomimage");
	var viewport = document.getElementById("zoomview");
	if (draggableImage != null && viewport != null) {
//		alert("Zoombild: "+draggableImage.offsetWidth+"px x "+draggableImage.offsetHeight+"px.\nFläche: "+viewport.offsetWidth+"px x " + viewport.offsetHeight+"px");
		if (draggableImage.offsetWidth > viewport.offsetWidth || draggableImage.offsetHeight > viewport.offsetHeight) {
			var draggingThing = new YAHOO.util.DD("zoomimage");
			var diffX = draggableImage.offsetWidth - viewport.offsetWidth;
			var diffY = draggableImage.offsetHeight - viewport.offsetHeight;
			var iDeltaX = 0;
			var iDeltaY = 0;
//			draggingThing.setXConstraint (diffX, 0);
//			draggingThing.setYConstraint(diffY, 0);
			if (diffX > 0) {
				draggableImage.style.left = "-" + (diffX/2) + "px";
//				iDeltaX = diffX/2*-1;
			}
			if (diffY > 0) {
				draggableImage.style.top = "-" + (diffY/2) + "px";
//				iDeltaY = diffY/2*-1;
			}
			draggingThing.setXConstraint (diffX/2, diffX/2);
			draggingThing.setYConstraint(diffY/2, diffY/2);
		}
	}
}

/* collapsible paragraph stuff ****************************/
function toggleCollapsibleParagraph(id) {
	var paragraph = document.getElementById(id);
	if (paragraph) {
		var currentClass = paragraph.getAttribute("class");
		if (!currentClass) {
			currentClass = paragraph.getAttribute("className");
		}
		if (currentClass == "closed") {
			paragraph.setAttribute("class", "open"); 
			paragraph.setAttribute("className", "open");
		} else {
			paragraph.setAttribute("class", "closed"); 
			paragraph.setAttribute("className", "closed");
		}
	}
}

function openCollapsibleParagraph(id) {
	var paragraph = document.getElementById(id);
	if (paragraph) {
		var currentClass = paragraph.getAttribute("class");
		if (!currentClass) {
			currentClass = paragraph.getAttribute("className");
		}
		if (currentClass == "closed") {
			paragraph.setAttribute("class", "open"); 
			paragraph.setAttribute("className", "open");
		}
		window.location.hash=id;
	}
}