var ie5 = null;
var ns6 = null;
var ie7 = null;
var blnFirstRun = true;




var pageSize = null;
var pageHeight = null;
function positionThings()
{
	pageSize = getPageSize();
	pageWidth  = (ie5 ? pageSize[0] : pageSize[2]);
	pageHeight = (ie5 ? pageSize[1] : pageSize[3]);
	setup_global();
	blnFirstRun = false;
}

function g(v) {
	return document.getElementById(v);
}
function p(v) {
	return parseInt(v);
}

function rxpos(v) {
	var obj = g(v);
	var cx = 0;
	if (obj.offsetParent) {
		cx = obj.offsetLeft
		while (obj = obj.offsetParent) {
			cx += obj.offsetLeft
		}
	}
	return cx;
}

function rypos(v) {
	var obj = g(v);
	var cy = 0;
	if (obj.offsetParent) {
		cy = obj.offsetTop
		while (obj = obj.offsetParent) {
			cy += obj.offsetTop
		}
	}
	return cy;
}

function rxposObj(v) {
	var obj = v;
	var cx = 0;
	if (obj.offsetParent) {
		cx = obj.offsetLeft
		while (obj = obj.offsetParent) {
			cx += obj.offsetLeft
		}
	}
	return cx;
}

function ryposObj(v) {
	var obj = v;
	var cy = 0;
	if (obj.offsetParent) {
		cy = obj.offsetTop
		while (obj = obj.offsetParent) {
			cy += obj.offsetTop
		}
	}
	return cy;
}

function startup()
{
	ie5=document.all&&document.getElementById&&!window.XMLHttpRequest;
	ns6=document.getElementById&&!document.all;
	ie7=document.all&&document.getElementById&&window.XMLHttpRequest;
}

function delayPositionThings() {
		setTimeout("positionThings()", 500);
}

var intCurrImg = -1;

function nextImage() {
	if(typeof(arrPageImages)!="undefined" && g("imgRight")) {
		intCurrImg += 1;
		if(intCurrImg>=arrPageImages.length)
		{
			intCurrImg = 0;
		}
		g('imgRight').src = arrPageImages[intCurrImg];
		if (arrPageImagesNotes[intCurrImg] != undefined) g('divImgRightDescription').childNodes[0].nodeValue = "" + arrPageImagesNotes[intCurrImg];
		g('divImgRightCounter').childNodes[0].nodeValue = (intCurrImg + 1) + ' / ' + (arrPageImages.length);
	}
}

function prevImage() {
	if(typeof(arrPageImages)!="undefined" && g("imgRight")) {
		intCurrImg -= 1;
		if(intCurrImg<0) {
			intCurrImg = arrPageImages.length - 1;
		}
		g('imgRight').src = arrPageImages[intCurrImg];
		if (arrPageImagesNotes[intCurrImg] != undefined) g('divImgRightDescription').childNodes[0].nodeValue = "" + arrPageImagesNotes[intCurrImg];
		g('divImgRightCounter').childNodes[0].nodeValue = (intCurrImg + 1) + ' / ' + (arrPageImages.length);
	}
}

function getPageSize()
{	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function printThis() { 
	document.forms.printform.printwhat.value = document.getElementById("divDocumentContainer1").innerHTML;
	document.forms.printform.submit();
}

function printerfocus()
{
	printframe.focus();
	printframe.print();
}


function setFocusInput(obj) { obj = (typeof(obj) == "string")?g(obj):obj; if (obj != null) obj.focus(); }

function left(obj) {  obj = (typeof(obj) == "string")?g(obj):obj; return rxpos(obj.id); }

function right(obj) {  obj = (typeof(obj) == "string")?g(obj):obj; return rxpos(obj.id) + obj.offsetWidth; }

function top(obj) { obj = (typeof(obj) == "string")?g(obj):obj; return rypos(obj.id) }

function bottom(obj) { obj = (typeof(obj) == "string")?g(obj):obj; return rypos(obj.id) + obj.offsetHeight; }

function centerVert(objToCenter, objCenterTo)
{
	objCenterTo = (typeof(objCenterTo) == "string")?g(objCenterTo):objCenterTo;
	objToCenter = (typeof(objToCenter) == "string")?g(objToCenter):objToCenter;
	return (rypos(objCenterTo.id) + objCenterTo.offsetHeight / 2) - (objToCenter.offsetHeight /2);
}

function centerHori(objToCenter,objCenterTo)
{
	objCenterTo = (typeof(objCenterTo) == "string")?g(objCenterTo):objCenterTo;
	objToCenter = (typeof(objToCenter) == "string")?g(objToCenter):objToCenter;
	return (rypos(objCenterTo.id) + objCenterTo.offsetWidth / 2) - (objToCenter.offsetWidht /2);
}

function locate(obj,do_not_replace_existing_locations)
{
	if (typeof(obj) != "string")
	{
		alert("Please send ID as string and not object.")
		return;
	}

	objID = obj;
	obj = g(obj);

	if (!obj)
	{
		alert("No luck finding object with ID: " + objID);
	} else {
		show_cross   = 0;
		show_box     = 1;
		flash_border = 1;
		flash_speed  = 500;

		cross_x_size = 2;
		cross_y_size = 2;
		box_x_size   = 2;
		box_y_size   = 2;

		x = rxposObj(obj);
		y = ryposObj(obj);
		document.title = "Last coordinates - x: " + x + ", y: " + y;

		
		ranX1 = ranY1 = ranX2 = ranY2 = "";
		if (do_not_replace_existing_locations)
		{
			ranX1 = "_" + ("" + Math.random()).substring(2);
			ranY1 = "_" + ("" + Math.random()).substring(2);
			ranX2 = "_" + ("" + Math.random()).substring(2);
			ranY2 = "_" + ("" + Math.random()).substring(2);
		}

		if (show_cross == 1)
		{
			createLine( rxposObj(obj) + obj.offsetWidth/2,0,cross_x_size,screen.availHeight,"ff0000",999,"locate-x-line" + ranX1);
			createLine(0,ryposObj(obj) + obj.offsetHeight/2,screen.availWidth,cross_y_size,"ff0000",999,"locate-y-line"  + ranY1);
		}


		if (show_box == 1)
		{
			var left   = rxpos(objID);
			var right  = left + g(objID).offsetWidth;
			var top    = rypos(objID);
			var bottom = top + g(objID).offsetHeight;

			/*
			if (left - right < 5)
			{
				left += - 5;
				right -= - 5;
			}

			if (bottom - top < 5)
			{
				bottom += 5;
				top -= 5;
			}
			*/

			createLine(left,top,box_y_size,bottom-top,"ff0000",999,"x1"    +ranX1);
			createLine(left,top,right-left,box_x_size,"ff0000",999,"y1"    +ranY1);
			createLine(right,top,box_y_size,bottom-top,"ff0000",999,"x2"   +ranX2);
			createLine(left,bottom,right-left+2,box_x_size,"ff0000",999,"y2" +ranY2);
		}

		if (show_box == 1 & flash_border == 1)
		{
			setInterval('g("x1' + ranX1 + '").style.display = (g("x1' + ranX1 + '").style.display == "none")?"block":"none"; g("y1' + ranY1 + '").style.display = g("x2' + ranX2 + '").style.display = g("y2' + ranY2 + '").style.display = g("x1' + ranX1 + '").style.display;', flash_speed + (Math.random()*flash_speed));
		}

	}
	
}