var NavigatorInfo = 
{
	"FF":false,
	"WebKit":false,
	"IE6":false,
	"init":function(){
		if(navigator.userAgent.search(/Firefox/)>-1) NavigatorInfo.FF = true;
		else if(navigator.userAgent.search(/WebKit/)>-1){ NavigatorInfo.WebKit = true;}
		else if(navigator.userAgent.search(/MSIE 6/)>-1){NavigatorInfo.IE6 = true;}
	}
}
NavigatorInfo.init();


function showPage(page, hash)
{
	var src = "/JCMD/exec/open/BookImage.aspx?cmd=showBig&ty=preview&pa=" + page+"&ch="+hash;
	var div = document.getElementById("previewDiv");
	var img = document.getElementById("previewPicture");

	div.style.display = "block";
	img.src = src;
	
	div.style.left = ((document.body.clientWidth / 2) - img.width / 2) + "px";
}

function hidePage()
{
		var div = document.getElementById("previewDiv");
		if(div){ 
			div.style.display="none";
		}
}
function showHits(page)
{
	var id = "P" + page;
	for(i = 0 ; i < document.anchors.length; i++){
		if(document.anchors[i].name == id){
			location.hash = id;
			return;
		}
	}
}

function myToggle(id)
{
	Effect.toggle(id, 'blind');
	if(document.cookie.match(/toggled=true/))
	{
		document.cookie = 'toggled=false';
	}
	else {
		document.cookie = 'toggled=true';
	}
}

function handleCookie()
{
	if(document.cookie.match(/toggled=true/))
	{
		document.write('<div id="area" style="display:none;">');
	}
	else 
	{
		document.write('<div id="area" style="">');
	}
}

function setElementHeight(elementId, heightToSet)
{
	if(this.document.getElementById(elementId))
	{
		var y = getScreenHeight();
		this.document.getElementById(elementId).style.height=(y/100)*heightToSet + 'px';
	}
	else
	{
		//Element existiert nicht!
	}
}

function getScreenHeight()
{
	if (self.innerHeight) // all except Explorer
	{
		return self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)	// Explorer 6 Strict Mode
	{
		return document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		return document.body.clientHeight;
	}
	else 
	{
		return 0;
	}
}

function getScreenWidth()
{
	if (self.innerWidth) // all except Explorer
	{
		return self.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientWidth)	// Explorer 6 Strict Mode
	{
		return document.documentElement.clientWidth;
	}
	else if (document.body) // other Explorers
	{
		return document.body.clientWidth;
	}
	else 
	{
		return 0;
	}
}


function tryWindowClose()
{
	if(this.document.all)
	{
		this.window.close();
	}
	else
	{
		alert("Bitte schließen sie das Fenster über den 'Schließen-Button' des Browsers!");	
	}
}

function getViewScreen()
{
	var x,y;
	if (self.innerHeight) // all except Explorer
	{
		x = self.innerWidth;
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	return {X : x, Y : y};
}

function keyHandler(e)
{
	if(! e){
		e = window.event;
	}
	var elem;
	var keyCode = 0;
	
	if(e.which){ // FF,SAFARI
		keyCode = e.which; 
	}
	else if(e.keyCode){ //IE
		keyCode = e.keyCode; 
	}

	switch(keyCode)
	{
		case 37:
			elem = document.getElementById('leftArrow');
			e.cancelBubble = true;
			break;

		case 38:
			elem = document.getElementById('zoomImage');
			e.cancelBubble = true;
			break;

		case 39:
		case 32:
			elem = document.getElementById('rightArrow');
			e.cancelBubble = true;
			break;

		default:
			break;
	}
	
	if(elem)
	{
		document.location = elem.href;
	}
}

var imageIds = 0;
var imageTimers = new Array();
imageTimers[0] = new Object();

function insertEmptySide(templates)
{
	var currentId = "image_id" + imageIds++;
	document.write("<img id=\"" + currentId + "\" src=\""+templates+"/images/none.gif\">");
	imageTimers[0][currentId] = window.setInterval("resizeEmptyImage('" + currentId + "');", 200);
}

function resizeEmptyImage(imageId)
{
	if(document.images[0].complete) {
		var img = document.getElementById(imageId);
		if(img) {
			img.height = document.images[0].height;
			img.width = document.images[0].width;
		}
		window.clearInterval(imageTimers[0][imageId]);
	}
}

function getSearchChapters(targetElementID)
{
	var element = document.getElementById(targetElementID);
	element.value = window.chooser.document.dataDummy.SelectedNode.value;
}

function reduceElement(id)
{
	var element = document.getElementById(id);
	if(element)
	{
		var current = element.firstChild;
		var elements = 0;
		while(current)
		{
			if(current.nodeName != '#text' && current.nodeName != 'SCRIPT')
			{
				elements++;
			}
			current = current.nextSibling;
		}
		if(elements == 0)
		{
			element.style.height = "0px";
		}
	}
}

function getElementQuasiById(id)
{	
	obj = new Object();
	if(typeof(document.getElementById) == 'function' || typeof(document.getElementById) == 'object')
	{
		//IE7, FF
		//alert("IE7, FF");
		obj = document.getElementById(id);
	}
	else if (typeof(document.all) == 'object')
	{
		//IE ab 4-6, Opera ab 5
		//alert("IE6, Opera");
		obj = document.all(id);
	}
	else
	{
		//alert("Other Browser");
		obj = null;
	}
	return obj;
}

function changeComboColor()
{
	var obj = getElementQuasiById("pf_colorSchemeSelector");
	var option = document.forms[1].elements["pf_colorSchemeSelector"].options[obj.selectedIndex];
				
	obj.style.backgroundColor = option.style.backgroundColor;
	obj.style.color = option.style.color;
	
	obj = getElementQuasiById("symbolicTop");
	obj.style.backgroundColor = option.style.color;

	obj = getElementQuasiById("symbolicBottom");
	obj.style.backgroundColor = option.style.color;

	obj = getElementQuasiById("symbolicContent");
	obj.style.backgroundColor = option.style.backgroundColor;
}

function switchPageFlipTabContent(element)
{	
	a = getElementQuasiById("pageFlipTabInvisible");
	b = getElementQuasiById("pageFlipTabVisible");
	c = getElementQuasiById(element);
	
	if(c.checked)
	{
		a.className = "isInvisible";
		b.className = "isVisible";
	}
	else
	{
		a.className = "isVisible";
		b.className = "isInvisible";
	}
}

function hideAndShow(toCheck, toHide)
{
	a = getElementQuasiById(toCheck);
	b = getElementQuasiById(toHide);

	if(a != null && b != null)
	{
		b.style.display = (a.checked?'block':'none')
	}
}

function checkColorOnSubmit()
{
	var scheme = getElementQuasiById("colorModeSelector0");
	var manual = getElementQuasiById("colorModeSelector1");
	
	if(scheme.checked)
	{
		var obj1 = getElementQuasiById("background");
		var obj2 = getElementQuasiById("controls");
	
		if(obj1.value == "" || !checkColorValue(obj1))
		{
			obj1.value="#cbc8c5";
		}
		if(obj2.value == "" ||  !checkColorValue(obj2))
		{
			obj2.value="#c8c4c4";
		}
		return true;
	}
	
	if(manual.checked)
	{
		var ok = true;

		var obj1 = getElementQuasiById("background");
		var obj2 = getElementQuasiById("controls");

		if(!checkColorValue(obj1))
		{
			alert("Bitte korrigieren sie die Farbeingabe des Feldes 'Hintergrund'");
			return false;
		}

		if(!checkColorValue(obj2))
		{
			alert("Bitte korrigieren sie die Farbeingabe des Feldes 'Navigation'");
			return false;
		}
	}
}

function checkColorValue(element)
{
	if(element == null)
	{
		var obj = getElementQuasiById("background");
		if(obj != null)
		{
			checkColorValue(obj);
		}

		obj = getElementQuasiById("controls");
		if(obj != null)
		{
			checkColorValue(obj);
		}
	}
	else
	{
		var name = element.name;
		var valueOK = 1;
		
		var color = element.value.toLowerCase();
		
		if(color.length == 7)
		{	
			var first = color.indexOf("#");
			var last = color.lastIndexOf("#");
			
			if(first == 0 && last == 0)
			{			
				for(var i = 1; i < color.length; i++)
				{
					var c = color.charAt(i);
					if(!(c.match(/[0-9a-f]{1}/)))
					{
						valueOK = 0;
						break;
					}				
				}
			}
			else{valueOK = 0;}
		}
		else{valueOK = 0;}

		if(valueOK == 1)
		{
			updateColor(name, color);
			return true;
		}
		else
		{
			return false;;
		}
	}
}

function updateColor(name, color)
{	
	if(getElementQuasiById("colorModeSelector1").checked && (name == "background" || name == "controls"))
	{
		if(name == "background")
		{
			obj = getElementQuasiById("symbolicContent");
			obj.style.backgroundColor = color;

			return;
		}

		if(name == "controls")
		{
			obj = getElementQuasiById("symbolicTop");
			obj.style.backgroundColor = color;

			obj = getElementQuasiById("symbolicBottom");
			obj.style.backgroundColor = color;

			return;
		}
	}
	else
	{
		obj = getElementQuasiById(name);
		
		if(obj.name == "pf_titleColor")
		{
			obj.style.backgroundColor = color;
			setColorRelativeToBackground(obj);

			return;
		}

		if(obj.name == "pf_linkColor")
		{
			obj.style.backgroundColor = color;
			setColorRelativeToBackground(obj);
			
			return;
		}
	}
}

function setColorRelativeToBackground(coloredObject)
{
	if(coloredObject.style.backgroundColor != null && coloredObject.style.backgroundColor != "")
	{
		//Falls leer, initialisieren!
		if(coloredObject.style.color == null || coloredObject.style.color == "")
		{
			coloredObject.style.color = "#000000";
		}
		
		var white = true;

		for(var i = 1; i < coloredObject.value.length; i = i + 2)
		{			
			var charAtPos = coloredObject.value.charAt(i);
			
			if(charAtPos.match(/[a-fA-F]/))
			{
				white = false;
				break;
			}
		}

		if(white)
		{
			coloredObject.style.color = "#ffffff";
		}
		else
		{
			coloredObject.style.color = "#000000";
		}
	}
}

var ViewFormer = {
	"imageShown":false,
	"LEFT":1,
	"RIGHT":2,
	"TOP":3,
	"BOTTOM":4,
	"hidePage": function()
	{
		$("previewDiv").hide();
	},
	"showAtPosition" : function(hash, event, element, downlighted)
	{
		var src = "/JCMD/exec/open/BookImage.aspx?cmd=showBig&ty=preview&pa=0&ch="+hash;
		var div = $("previewDiv");
		var img = $("previewPicture");

		var size = getViewScreen();
		var image = $(element).down("IMG");
		var pos = $(image).viewportOffset();
		//alert(pos.left);
				
		var dimension = $(image).getDimensions();
		var imageDimension = div.getDimensions();		
		
		if(downlighted)
		{
			img.className = "downlighted";			
		}
		else 
		{
			img.className = "highlighted";
		}
		
		if(navigator.userAgent.search(/WebKit/)>-1){
			pos.left+=2;
		}

		//if(document.all) pos.top -= 300;		

		var thumbRel = ViewFormer.BOTTOM;
		var imageRel = ViewFormer.LEFT;

		if(pos.top + dimension.height + imageDimension.height > size.Y) {
			if(pos.top + imageDimension.height > size.Y){
				if(pos.top - imageDimension.height < 5)	{
					imageRel = ViewFormer.BOTTOM;
					if(pos.left + dimension.width + imageDimension.width > size.X) {
						thumbRel = ViewFormer.LEFT;
					}
					else {
						thumbRel = ViewFormer.RIGHT;
					}
				}
				else {
					thumbRel = ViewFormer.TOP;
					if(pos.left + dimension.width + imageDimension.width > size.X) {
						imageRel = ViewFormer.RIGHT;
					}
				}
			}
			else {
				imageRel = ViewFormer.TOP;
				if(pos.left + dimension.width + imageDimension.width > size.X) {
					thumbRel = ViewFormer.LEFT;
				}
				else {
					thumbRel = ViewFormer.RIGHT;
				}
			}
		}
		else if(pos.left + dimension.width + imageDimension.width > size.X) {
			imageRel = ViewFormer.RIGHT;
		}
		
		var result = ViewFormer.showAt(thumbRel, imageRel, imageDimension, dimension, pos);

		div.style.left = result.left + "px";
		div.style.top = result.top +ViewFormer.getPageOffset().y+ "px";							
				
		img.src = src;
		div.show();
	},
	"getPageOffset":function()
	{
		var result = new Object();
		if (self.pageYOffset) // all except Explorer
		{
			result.x = self.pageXOffset;
			result.y = self.pageYOffset;
		}
		else if (document.documentElement && document.documentElement.scrollTop) // Explorer 6 Strict
		{
			result.x = document.documentElement.scrollLeft;
			result.y = document.documentElement.scrollTop;
		}
		else if (document.body) // all other Explorers
		{
			result.x = document.body.scrollLeft;
			result.y = document.body.scrollTop;
		}
		
		return result;
	},
	"showAt":function(relativeThumb, relativeImage,imageDimension, thumpDimension, thumpPosition)
	{
		var result = new Object();
		result.left = 0;
		result.top = 0;
		var borderCorecture = 2;
		var spacer = 2;

		switch(relativeThumb)
		{
			case 1: //left
				switch(relativeImage)
				{
					case 3: //top
						result.left = thumpPosition.left - imageDimension.width - (2 * borderCorecture) - spacer;
						result.top = thumpPosition.top - borderCorecture;
						break;

					case 4: //bottom
						result.left = thumpPosition.left - imageDimension.width - (2 * borderCorecture) - spacer;
						result.top = thumpPosition.top + thumpDimension.height - imageDimension.height - borderCorecture;
						break;
				}
				break;

			case 2: //right
				switch(relativeImage)
				{
					case 3: //top
						result.left = thumpPosition.left + thumpDimension.width + borderCorecture;
						result.top = thumpPosition.top - borderCorecture;
						break;

					case 4: //bottom
						result.left = thumpPosition.left + thumpDimension.width + borderCorecture;
						result.top = thumpPosition.top + thumpDimension.height - imageDimension.height -borderCorecture;
						break;
				}
				break;
				
			case 3: //top
				switch(relativeImage)
				{
					case 1: //left
						result.left = thumpPosition.left - borderCorecture;
						result.top = thumpPosition.top - imageDimension.height - (2 * borderCorecture) - spacer;
						break;

					case 2: //right
						result.left = thumpPosition.left + thumpDimension.width - imageDimension.width - borderCorecture;
						result.top = thumpPosition.top - imageDimension.height - (2 * borderCorecture) - spacer;
						break;
				}
				break;
				
			case 4: //bottom
				switch(relativeImage)
				{
					case 1: //left
						result.left = thumpPosition.left - borderCorecture;
						result.top = thumpPosition.top + thumpDimension.height + spacer + borderCorecture;
						break;

					case 2: //right
						result.left = thumpPosition.left + thumpDimension.width - imageDimension.width - borderCorecture;
						result.top = thumpPosition.top + thumpDimension.height + spacer + borderCorecture;
						break;
				}
				break;
		}

		if(result.top < 0) result.top = 0;

		return result;
	},
	"setDisplay":function(options)
	{
		if(options)
		{
			for(var i =0; i < options.length; i++)
			{
				var elem = $(options[i].Name);
				var height;
				var correction = 5;
				
				if(options[i].Margin)
				{
					correction += parseInt(options[i].Margin);
				}
				
				if(elem)
				{
					if(options[i].Height)
					{						
						if(options[i].Height.endsWith('%'))
						{
							var val = options[i].Height.substring(0, options[i].Height.length-1);
							var screenHeight = getScreenHeight();
							height = parseInt(screenHeight / 100 * val);
						}
						else 
						{
							height = parseInt(options[i].Height);
						}
						elem.setStyle({'height':height+'px'});
					}
					else if(options[i].HeightSpare)
					{
					    var b = document.getElementById("resultArea");
					    if (options[i].HeightSpare[5] == "#scopeList"){
					        var t = $$(options[i].HeightSpare[5]);
					        
					        var h = Math.round(b.offsetHeight * 0.4);
					        if (t && t.length>0){
					            t[0].setStyle({'height':h+'px'});
					        }
					    }
					    
						for(var j = 0; j < options[i].HeightSpare.length; j++)
						{
							var a = $$(options[i].HeightSpare[j]);
							if(a && a.length>0)
							{
							    if (a[0].id == "scopeList")
								    correction += a.first().getHeight() + 5 ;								
								else
								    correction += a.first().getHeight();								
							}
						}
						height = getScreenHeight() - correction;
						elem.setStyle({'height':height+'px'});
					}
				}			
			}
		}
	}
}