/**
 *
 *	This functions add dynamic to the site of HS-Art Digital Service GmbH.
 *
*/


var lastClickedImage = "";	// used to remember last clicked image for magnifiction
var defaultAltText = "magnification of: ";	// default alternative text for the enlarged image (will be concatenated with the id of the image)

// element id's for enlarged image containers
var enlargedBackgroundId = "enlargedBackground";
var enlargedContentBackgroundId = "enlargedContentBackground";
var enlargedImageAreaId = "enlargedImageArea";
var enlargedImageId = "enlargedImage";
var enlargedTitleId = "enlargedTitle";
var closeButtonId = "closeButton";




/**
 *
 *	This function avoids display errors in the Internet Explorer (IE).
 *	Sometimes the IE interprets and renders the content and css not in a correct order.
 *	In this cases it can occur that the IE does not render and display the css layouted content correct.
 *	This function currently avoids a single and specific problem with the div-element with the id 'content',
 *	that occurs in documents where a meta menu is present (products). But to ensure that the problem does not happen anywhere,
 *	the function will be called in each document after it completly loaded via the universal onload-handler in the body-element.
 *
*/
function avoidIEDisplayErrors() {

	try {
		window.document.getElementById('content').style.display = 'none';
		window.document.getElementById('content').style.display = 'block';
	
	} catch(error) {}	// end try catch

	try {
		window.document.getElementById('enlargedImageArea').style.display = 'none';
		window.document.getElementById('enlargedImageArea').style.display = 'block';
	
	} catch(error) {}	// end try catch

} // end function

/**
 *
 *	Sets up the enlarged contents for flash content.
 *	This function expects that the enlargedImageArea and its surrounding elements are created.
 *	MEANS: The call of this function must happen after insertion of the surrounding elements!
 *
*/
function setupSwf(url) {
	
	var enlargedImageAreaNode = window.document.getElementById(enlargedImageAreaId);
	
	var objectNode = window.document.createElement('object');
	var embedNode = window.document.createElement('embed');
	
	var paramNode1 = window.document.createElement('param');
	var paramNode2 = window.document.createElement('param');
	
	
	// set up and insert object element
	objectNode.setAttribute('codebase', 'http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=8,5,0,0');
	// styles
	objectNode.style.width = '720px';
	objectNode.style.height = '576px';
	objectNode.style.position = 'absolute';
	objectNode.style.top = '50%';
	objectNode.style.left = '50%';
	objectNode.style.marginLeft = '-360px';
	objectNode.style.marginTop = '-288px';
	objectNode.style.border = '1px solid rgb(153, 153, 153)';
	
	
	
	// set up param nodes for the object element
	paramNode1.setAttribute('name', 'src');
	paramNode1.setAttribute('value', url);
	
	objectNode.appendChild(paramNode1);
	
	
	// set up embed node for the object element
	embedNode.setAttribute('src', url);
	embedNode.setAttribute('type', 'application/x-shockwave-flash');
	embedNode.setAttribute('pluginspage', 'http://www.macromedia.com/shockwave/download/');
	// styles
	embedNode.style.width = '720px';
	embedNode.style.height = '576px';
	
	if(navigator.appName != "Microsoft Internet Explorer")
		objectNode.appendChild(embedNode);
	
	else
		objectNode.setAttribute('type', 'application/x-shockwave-flash');
	
	return objectNode;
	
	// insert object element into the image area element
	
	//window.document.getElementById(enlargedImageAreaId).appendChild(objectNode);
	
	
} // end function


/**
 *	
 *	This function enlarges the image with the id 'elemID'.
 *	The function uses an espacially for this case predefined restriction of naming image files for magnification.
 *	Image file names used for the magnification must have the postfix string "_small" and "_large" before the file extensions dot.
 *	The "_small" postfix will be replaced with "_large" and the resulting URI is set to the enlarged image element in html.
 *
 *	The restriction desires exactly the same directories and exactly the same file names (excluding the postfixes)
 *	and file types to make the magnification work properly.
 *	
 *	To avoid problems with the Internet Explorer the small preview in the page content will be made invisible.
 *	If you do not so, the whole layout jumps up.
 *	To solve this IE problem the id of the last clicked image will be stored and is used to make the small image visible again later.
 *
*/
function enlarge(elemID, title, url) {
	
	var elem = window.document.getElementById(elemID);
	var elemSrc;
	var elemSrcSplitted;
	
	var enlargedBackgroundNode = window.document.createElement('div');
	var enlargedContentBackgroundNode = window.document.createElement('div');
	var enlargedImageAreaNode = window.document.createElement('div');
	
	var enlargedTitleNode = window.document.createElement('h1');
	var titleTextNode = window.document.createTextNode(title);
	
	var enlargedImageNode = window.document.createElement('img');
	var closeButtonNode = window.document.createElement('a');
	
	
	lastClickedImage = elemID;
	
	if(elemID == undefined || elemID == '' || elem == undefined) {
	
		elemSrcSplitted = url.split(".");
	
	} else {
	
		elemSrc = elem.src;
		elemSrcSplitted = elemSrc.split("_small");
		
	}// end if
	
	// set up enlarged background (insertion happen in the last step
	enlargedBackgroundNode.setAttribute('id', enlargedBackgroundId);
	
	
	
	// set up and insert enlarged content background
	enlargedContentBackgroundNode.setAttribute('id', enlargedContentBackgroundId);
	enlargedBackgroundNode.appendChild(enlargedContentBackgroundNode);
	
	
	// set up and insert image area
	enlargedImageAreaNode.setAttribute('id', enlargedImageAreaId);
	enlargedContentBackgroundNode.appendChild(enlargedImageAreaNode);
	
	
	// set up and insert close button
	closeButtonNode.setAttribute('id', closeButtonId);
	//closeButtonNode.setAttribute('href', '#' + elemID);
	closeButtonNode.onclick = closeEnlarged;
	
	enlargedContentBackgroundNode.appendChild(closeButtonNode);
	
	
	// set up and insert enlarged title
	//alert(enlargedTitleNode.nodeName);
	enlargedTitleNode.setAttribute('id', enlargedTitleId);
	enlargedTitleNode.appendChild(titleTextNode);
	
	enlargedContentBackgroundNode.appendChild(enlargedTitleNode);
	
	
	// check for swf or image and insert the appropriate content
	if(elemSrcSplitted[elemSrcSplitted.length - 1] == "swf") {
	
		if(url != undefined && url != '') {
		
			var objectNode = setupSwf(url);
			
			enlargedImageAreaNode.appendChild(objectNode);
			window.document.getElementsByTagName('body')[0].appendChild(enlargedBackgroundNode);
			
			var scriptNode = window.document.createElement('script');
			scriptNode.setAttribute('type', 'text/javascript');
			scriptNode.setAttribute('src', 'http://www.hs-art.com/scripts/ieupdate.js');
			
			enlargedImageAreaNode.appendChild(scriptNode);
		
		} // end if
	
	} else {
	
		// set up and insert enlarged image
		enlargedImageNode.setAttribute('id', enlargedImageId);
		enlargedImageNode.setAttribute('src', elemSrcSplitted[0] + "_large" + elemSrcSplitted[1]);
		enlargedImageNode.setAttribute('alt', defaultAltText + title);
		// styles
		//enlargedImageNode.style.top = '50%';
		//enlargedImageNode.style.left = '50%';
		//enlargedImageNode.style.marginTop = (enlargedImageNode.height / -2) + 'px';
		//enlargedImageNode.style.marginLeft = (enlargedImageNode.width / -2) + 'px';
		
		enlargedImageAreaNode.appendChild(enlargedImageNode);
		window.document.getElementsByTagName('body')[0].appendChild(enlargedBackgroundNode);
	
	}
	
	// make the content's small preview invisible
	if(elem != undefined) {
	
		elem.style.display = "none";
		
	} // end if
	
	// avoid a display error in the firefox browser for mac
	window.document.getElementById("content").style.overflow = "hidden";
	
	avoidIEDisplayErrors();
	return false;
}


/**
 *
 *	This function makes the normal content of the page visible and hides the html elements used for image magnification.
 *
*/
function closeEnlarged() {

	if(lastClickedImage != undefined && lastClickedImage != "") {
		
		window.document.getElementById(lastClickedImage).style.display = "block";
		
	} // end if
	
	var elem = window.document.getElementById(enlargedBackgroundId);
	
	if(elem != null && elem != undefined) {
	
		var node = window.document.getElementsByTagName('body')[0].removeChild(elem);
	
	} // end if
	
	// avoid a display error in the firefox browser for mac
	window.document.getElementById("content").style.overflow = "auto";
	
	avoidIEDisplayErrors();

} // end function