// Open a window at the center of the screen
function openCenteredWindow(url, name, width, height, extra) {
	var x=0, y = 0;
	if (screen) {
		x = Math.floor((screen.availWidth-width)/2);
		y = Math.floor((screen.availHeight-height)/2);
		//alert('x' + x + ' y ' +y);
	}

	if (typeof(extra) == 'undefined') {
		extra = 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0	';	
	}
	window.open(url, '', 'height='+height+',width='+width+',top='+y+',left='+x+', '+extra);
	
}

function open_window(page,w,h) {
		var ww = (screen.width-w)/2;
		window.open(page, "CtrlWindow", "top=200,left="+ww+",width="+w+",height="+h+",buttons=no,scrollbars=yes,location=no,menubar=no,resizable=yes,status=no,directories=no,toolbar=no");
}

///////////////
//////////////				
/////////////

function initScrollZone()
{
	modAcordViewport('height','scrollContainer','scrollContRefText',-1,['modifyClip']);
	snapToOther2('width','scrollContainer','scrollContRefZone',-61,['modifyClip']);
	snapToOther2('top','scrollContainer','scrollContRefText',0);
	//snapToOther('scrollContainer','scrollContRefText',1,1)
	
	snapToOther('rd1ProtZoneRightShadow','scrollContRefZone',findObj('scrollContRefZone').offsetWidth-117,-14,1);
	snapToOther2('topRightCorner','scrollIconsTop','scrollContainer',[18,0],['display']);
	snapToOther2('bottomRightCorner','scrollIconsBottom','scrollContainer',[18,-24],['display']);
	scrollObj.init('scrollContainer','scrollText');
	/*
	*/
}

function setCornersShadow()
{
	snapToOther2('topRightCorner','rd1ProtZoneRightShadow','scrollContRefZone',[0,-12],['display'])
	snapToOther2('width','rd1ProtZoneTopShadow','scrollContRefZone',0,['display'])
}

function tagBody_onLoad()
{
	initScrollZone()
	preloadImages_All();
}

function tagBody_onResize()
{
	initScrollZone()
}

///////////////
//////////////				common
/////////////

function findObj(id,frame) {
	var d; if (frame) d = typeof(frame)=='object' ? frame.document : eval(frame+'.document')
	else d = window.document
	if (document.getElementById) return d.getElementById(id)
	if (document.all) return d.all(id)
	return false
}


function arrSearch(arr,value)
{
	for (var i=0; i<arr.length; i++)
	{
		if (arr[i]==value) return true
	}
	return false
}

///////////////
//////////////				support PNG for IE
/////////////

var pngImages = new Array()

function setPNGforIE_img_class(obj) {
	this.obj = obj
	this.spacer = "images/spacer.gif"
	this.realSrc = this.obj.src
	this.support = (document.getElementById && this.obj.runtimeStyle) ? 1 : 0
	this.init = function() {
		if (this.support) {
			this.obj.src = this.spacer
			this.obj.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.realSrc + "',sizingMethod='scale')"
		}
	}
}

////
//		<img onload="setPNGforIE_img(this)">
////
function setPNGforIE_img(obj) {
	for (var i=0; i<pngImages.length; i++) {
		if (pngImages[i].obj == obj) return
	}
	pngImages[pngImages.length] = new setPNGforIE_img_class(obj)
	pngImages[pngImages.length-1].init()
}


function setPNGforIE_bg(idStr,imgUrl) {
	if (document.getElementById) var obj = document.getElementById(idStr)
	else return
	if (!obj.runtimeStyle) return		// is the browser capable of managing runtimeStyle - tai palaiko tik IE?
	obj.style.backgroundImage = 'none'
	obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+imgUrl+"',sizingMethod='crop')"
	//'scale'
}

///////////////
//////////////				layout
/////////////

function findPosX(obj) {
	var curleft = 0;
	if (document.getElementById || document.all)
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	return curleft;
}


function findPosY(obj) {
	var curtop = 0;
	if (document.getElementById || document.all)
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	return curtop;
}


//obj					<-	pozicionuojamas objektas
//objRef				<-	objektas - atskaitos taskas
//offset_left			<-	pozicionuojamo objekto poslinkis objekto - atskaitos tasko - atzvilgiu 
//show (boolean)		<-	true - supozicionavus objektui priskiriama - style.display = 'block'
function snapToOther(objMove,objRef,offset_left,offset_top,show) {
	var obj_target = findObj(objRef), obj = findObj(objMove)
	if (offset_left !== '') {
		var pos_left = findPosX(obj_target)
		//alert('pos_left = '+pos_left)
		obj.style.left = pos_left + offset_left + 'px'
	}
	if (offset_top !== '') {
		var pos_top = findPosY(obj_target)
		//alert('pos_top = '+pos_top)
		obj.style.top = pos_top + offset_top + 'px'
	}
	if (show) obj.style.display = 'block'
}

////
//		type (of operation): 'height' 'width' 'topRightCorner'
//		offset:	perduodamų duomenų tipas priklauso nuo type paramo, gali būti: x, [x,y]
//		
//		type = 'topRightCorner'; offset = [8,99]
////
function snapToOther2(type,objMove,objRef,offset,otherActions)
{	
	var objRef = findObj(objRef)
	var objMove = findObj(objMove)
	if (type=='height') 
	{
		objMove.style.height = objRef.offsetHeight-85 + 'px'
		if (arrSearch(otherActions,'modifyClip')) cssModify_clip(objMove,'','',objRef.offsetHeight-85,'')
	}
	if (type=='width') 
	{
		objMove.style.width = objRef.offsetWidth + offset + 'px'
		if (arrSearch(otherActions,'modifyClip')) cssModify_clip(objMove,'',objRef.offsetWidth + offset,'','')
	}
	if (type=='topRightCorner') 
	{
		//dev_print('objRef.id = '+objRef.id);
		//dev_print('objRef.style.left = '+objRef.style.left);
		//var ref_right = parseInt(objRef.style.left) + parseInt(objRef.style.width)
		var ref_right = findPosX(objRef) + objRef.offsetWidth
		//alert(ref_right)
		objMove.style.left = ref_right + offset[0] + 'px'
		//objMove.style.left = ref_right + offset[0] + 'px'
		objMove.style.top = findPosY(objRef) + offset[1] + 'px'
		//objMove.style.top = parseInt(objRef.style.top) + offset[1] + 'px'
	}
	if (type=='bottomRightCorner')
	{
		//dev_print('objRef.id = '+objRef.id);
		//dev_print('objRef.style.left = '+objRef.style.left);
		var ref_right = parseInt(objRef.style.left) + parseInt(objRef.style.width)
		objMove.style.left = ref_right + offset[0] + 'px'
		objMove.style.top = parseInt(objRef.style.top) + objRef.offsetHeight + offset[1] + 'px'
	}
	
	if (type=='top')
	{
		objMove.style.top = findPosY(objRef) + offset + 'px'
	}
	  //
	 //	otherActions
	//
	if (otherActions)
	{
		if (arrSearch(otherActions,'visibility'))
		{
			objMove.style.visibility = 'visible'
		}
		if (arrSearch(otherActions,'display'))
		{
			objMove.style.display = 'block'
		}
	}
}


////
//		type: 'height', 'height_width'
////
function modAcordViewport(type,obj,objRef,offset,otherActions)
{	
	if (typeof(obj)=='string') obj = findObj(obj)
	if (typeof(objRef)=='string') objRef = findObj(objRef)
	var viewPort = viewportDimensions()
	if (type=='height') 
	{
		//alert(viewPort.height - findPosY(objRef) + offset)
		obj.style.height = viewPort.height - findPosY(objRef) + offset + 'px'
		if (arrSearch(otherActions,'modifyClip'))
		{
			cssModify_clip(obj,'','',viewPort.height - findPosY(objRef) + offset,'')
		}
	}
}


function cssModify_clip(obj,top,right,bottom,left)
{
	if (typeof(obj)=='string') obj = findObj(obj)
	var clipStr = obj.style.clip
	var clipNum = clipStr.match(/\((.+)\)/)
	var clipArr = clipNum[1].split(' ');
	if (top!=='')
	{}
	if (right!=='')
	{
		clipArr[1] = right+'px';
	}
	if (bottom!=='')
	{
		//newClip = newClip.replace(/\s\w+\s([\d]+)[px|%]\s/g,'999')
		//alert('clipNum = '+clipNum[1])
		clipArr[2] = bottom+'px';
		//rect(0px 237px 300px 0px)
	}
	if (left!=='')
	{}
	//alert('rect('+clipArr.join(' ')+')')
	obj.style.clip = 'rect('+clipArr.join(' ')+')'
}


function viewportDimensions()
{
	var res = new Object()
	if (self.innerHeight) // all except Explorer
	{
		res.width = self.innerWidth;
		res.height = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
	{
		// Explorer 6 Strict Mode
		res.width = document.documentElement.clientWidth;
		res.height = document.documentElement.clientHeight;
	}
	else if (document.body) 
	{
		// other Explorers
		res.width = document.body.clientWidth;
		res.height = document.body.clientHeight;
	}
	return res
}

///////////////
//////////////				scroll container
/////////////
	
function scrollCont_class(variableString_param)
{
	//	properties
	
	this.ready = false					//indikuoja, kad inincializuotas ir gali veikti
	this.variableString = variableString_param
	this.contCliped; this.contClipedCH; this.contMove; this.contMoveSH;
	this.repeatSpead = 30				//If you want it to move faster you can set this lower, it's the timeout:
	this.loop
	this.repeatingRef
	this.contMove_x = 0
	this.contMove_y = 0
	
	//	methods
	
	this.init = function(contCliped_param,contMove_param)
	{
		this.contCliped = findObj(contCliped_param)
		this.contClipedCH = this.contCliped.offsetHeight		// contClipedCH - Clip Height
		this.contMove = findObj(contMove_param)
		this.contMoveSH = this.contMove.offsetHeight				// contMoveSH - Scroll Height
		this.move(0,0)
		this.show()
		this.ready = true
	}
	
	this.show = function()
	{
		this.contCliped.style.visibility = "visible"
	}
	
	this.reset = function()
	{
		this.move(0,0)
		this.contMoveSH = this.contMove.offsetHeight
	}
	
	this.up =  function(step) 
	{
		if (this.contMove_y < 0) this.move(0,this.contMove_y - step)
		else clearInterval(this.repeatingRef)
	}
	
	this.down =  function(step) 
	{
		//dev_print('this.down()')
		if (this.contMove_y > this.contClipedCH - this.contMoveSH) this.move(0,this.contMove_y - step)
		else clearInterval(this.repeatingRef)
	}
	
	this.move =  function(x,y)
	{
		//dev_print('y = '+y)
		this.contMove_x = x; this.contMove_y = y
		this.contMove.style.left = this.contMove_x + 'px'
		this.contMove.style.top = this.contMove_y + 'px'
	}
	
	this.stop =  function()
	{
		clearInterval(this.repeatingRef)
	}
	
	this.scroll =  function(step)
	{
		if (this.ready)
		{
			clearInterval(this.repeatingRef)
			if (step>0) this.repeatingRef = setInterval(this.variableString+'.down('+step+')',this.repeatSpead)
			else this.repeatingRef = setInterval(this.variableString+'.up('+step+')',this.repeatSpead)
		}
	}
}

var scrollObj = new scrollCont_class('scrollObj')