// JavaScript Document
window.onload=init;
var w3c = (document.getElementById) ? 1:0
var ns4 = (document.layers) ? 1:0
var ie4 = (document.all) ? 1:0

var range = "";
var cap = "";
var mutex = 0;
var yplace = 0;
var ymax = 0;
var ymin = 0;
var xplace = 0;
var newsHeight = 0;
var initial = false;

/** The only code you should ever need to change here are the following 3 vars **/
var speed = 7;                         //speed at which the news scrolls
var newsId = "news";                   //name of the overall news div
var newsClipId = "newsClipping";       //name of the news clipping div


function gE(id){
	return document.getElementById(id);
}
function gEbC(objClass, objTag){
	var objList = document.getElementsByTagName(objTag);
	var objList2 = new Array;
	n=0;
	for (i = 0; i < objList.length; i++){
		if(objList[i].className == objClass){
			objList2[n]=objList[i];
			n++;
		}
	}
	return objList2;
}

var objShake, animObj;
var animIndex=0, scrollIndex=0, direct, maxLeft, doscroll, doscrollIndex, dodirect;
var timer, scrollTimer, doscrollTimer;
var scrollObj,scrollI, maxScroll;
var scrollTimerBot,scrollTimerTop, scrollTopPoint, scrollBotPoint;
function init(){
	var uA=navigator.userAgent;
     if(uA.indexOf("Firefox/3")>-1){
          if(gE('plume'))gE('plume').style.left='707px';
          if(gE('bigFlower'))gE('bigFlower').style.left='645px';
          if(gE('bigCoffee'))gE('bigCoffee').style.left='682px';
     }
     if(uA.indexOf("Safari")>-1 || uA.indexOf("Opera")>-1){
          if(gE('plume'))gE('plume').style.left='709px';
          if(gE('bigFlower'))gE('bigFlower').style.left='647px';
          if(gE('bigCoffee'))gE('bigCoffee').style.left='684px';
     }
	if(uA.indexOf("Opera")>-1){
		if(gE('plume'))gE('plume').style.left='710px';
		if(gE('bigFlower'))gE('bigFlower').style.left='647px';
		if(gE('bigCoffee'))gE('bigCoffee').style.left='684px';
	}
	if(gE('menuBox')){
		var menu=gE('menuBox').getElementsByTagName('a');
		for(var i=0; i<menu.length; i++){
			var img=new Image;
			img.src=menu[i].firstChild.longDesc;
		}
		var flag=false;
		var src;
		for(var i=0; i<menu.length; i++){
			menu[i].onmouseover=function(){
				flag=true;
				//this.firstChild.src=this.firstChild.src.substring(0,this.firstChild.src.length-4)+'a.gif';

				src = this.firstChild.src;
				this.firstChild.src=this.firstChild.longDesc;

			}
			menu[i].onmouseout=function(){
				if(flag){

					this.firstChild.src=src;

					//this.firstChild.src=this.firstChild.src.substring(0,this.firstChild.src.length-5)+'.gif';
				}
			}
		}
	}
	if(gE('phone')){
		gE('phone').onmouseover = function() {
			animIndex2=0;
			objShake=this;
			objShake.nx=this.offsetLeft;
			objShake.ny=this.offsetTop;
			timer=setInterval('shake()',10);
		}
		gE('phone').onmouseout = function() {
			clearInterval(timer);
			this.style.left=this.nx+'px';
			this.style.top=this.ny+'px';

		}
	}
	if(gE('listCont')){
	var previews=gE('listCont').getElementsByTagName('a');
		gE('listCont').style.width=previews.length*270+'px';
		for(var i=0; i<previews.length; i++){
			previews[i].onmouseover=function(){
				animIndex=40;
				animObj=this.previousSibling;
				animObj.style.display='block';
				timer=setInterval('goldenWings()',10);
			}
			previews[i].onmouseout=function(){
				clearInterval(timer);
				animObj.style.display='none';
				animObj.style.top='40px';
			}
		}
	}
	if(gE('leftPoint') && gE('rightPoint')){
		scrollObj=gE('listCont');
		maxLeft=810-scrollObj.offsetWidth;
		gE('leftPoint').onmouseover=function(){
			if(scrollIndex<0){
				this.style.opacity = '1';
				this.style.filter = 'alpha(opacity=100)';
				clearInterval(scrollTimer);
				clearInterval(doscrollTimer);
				direct=1;
				scrollTimer=setInterval('scrolling()',10);
			}
		}
		gE('leftPoint').onmouseout=function(){
			this.style.opacity = '0.5';
			this.style.filter = 'alpha(opacity=50)';
			clearInterval(scrollTimer);
			if (scrollIndex%270!=0){
				if(scrollIndex%270<-135){
					doscroll=scrollIndex-(270+scrollIndex%270);
					dodirect=-1;
				}else{
					doscroll=scrollIndex-scrollIndex%270;
					dodirect=1;
				}
				doscrollTimer=setInterval('doscrolling()',10);
			}
		}
		gE('rightPoint').onmouseover=function(){
			if(scrollIndex>maxLeft){
				this.style.opacity = '1';
				this.style.filter = 'alpha(opacity=100)';
				clearInterval(scrollTimer);
				clearInterval(doscrollTimer);
				direct=-1;
				scrollTimer=setInterval('scrolling()',10);
			}

		}
		gE('rightPoint').onmouseout=function(){
			this.style.opacity = '0.5';
			this.style.filter = 'alpha(opacity=50)';
			clearInterval(scrollTimer);
			if (scrollIndex%270!=0){
				if(scrollIndex%270<-135){
					doscroll=scrollIndex-(270+scrollIndex%270);
					dodirect=-1;
				}else{
					doscroll=scrollIndex-scrollIndex%270;
					dodirect=1;
				}
				doscrollTimer=setInterval('doscrolling()',10);
			}
		}
	}
	if(gE('name')){
		gE('name').onfocus=function(){
			this.value2=this.value;
			this.value='';
		}
		gE('name').onblur=function(){
			if (this.value=='')
				this.value=this.value2;
		}
	}
	if(gE('mail')){
		gE('mail').onfocus=function(){
			this.value2=this.value;
			this.value='';
		}
		gE('mail').onblur=function(){
			if (this.value=='')
				this.value=this.value2;
		}
	}
	if(gE('text')){
		gE('text').onfocus=function(){
			this.inner=this.innerHTML;
			this.innerHTML='';
		}
		gE('text').onblur=function(){
			if (this.innerHTML=='')
				this.innerHTML=this.inner;
		}
	}
	var quests=gEbC('questList','div');
	if(quests.length){
		var answer=quests[0].getElementsByTagName('p')[0];
		var quest=quests[0].getElementsByTagName('a')[0];
		for(var i=0; i<quests.length; i++){
			quests[i].getElementsByTagName('a')[0].onclick=function(){
				this.blur();
				answer.style.display='none';
				answer=this.parentNode.getElementsByTagName('p')[0];
				answer.style.display='block';
				quest.style.color='#600235';
				quest.style.background='url(images/purpBg.gif) repeat-x bottom';
				quest=this.parentNode.getElementsByTagName('a')[0];
				quest.style.color='#837167';
				quest.style.background='none';
				return false;
			}
		}
	}
	if(gE('scrollCont')){
		scrollObj=gE('scrollCont');
		scrollI=0;
		scrollObj.style.top='0';
		maxScroll=360-scrollObj.offsetHeight;
		if(maxScroll>0) gE('hideStr').style.display='block';
		scrollTopPoint=gE('scrollTop');
		scrollBotPoint=gE('scrollBot');
		scrollTopPoint.onmouseover = function() {
			scrollBotPoint.style.visibility='visible';
			scrollTimerTop=setInterval('scrollTop()',10);
		}
		scrollTopPoint.onmouseout = function() {
			clearInterval(scrollTimerTop);
		}
		scrollBotPoint.onmouseover = function() {
			scrollTopPoint.style.visibility='visible';
			scrollTimerBot=setInterval('scrollBot()',10);
		}
		scrollBotPoint.onmouseout = function() {
			clearInterval(scrollTimerBot);
		}
	}

	if(gE('areas')){
		var areas=gE('areas').getElementsByTagName('area');
		for(var i=0; i<areas.length; i++){
			areas[i].onmouseover=function(){
				if( this.id)
					var num=this.id-1;
				else
					var num=this.href.substring(this.href.length-2,this.href.length)-1;
				if(gE('infos').getElementsByTagName('div')[num].innerHTML=='sold'){
					return false;
				}
				gE('infoCont').innerHTML=gE('infos').getElementsByTagName('div')[num].innerHTML;
				var fix=new Array;
				var j=0;
				for(var i=0; i<this.coords.length; i++){
					if(this.coords.substring(i,i+1)==','){
						fix[j]=i;
						j++;
					}
				}
				var x1=parseInt(this.coords.substring(0,fix[0]));
				var y1=parseInt(this.coords.substring(fix[0]+1,fix[1]));
				var x2=parseInt(this.coords.substring(fix[1]+1,fix[2]));
				var y2=parseInt(this.coords.substring(fix[2]+1,this.coords.length));
				gE('infoBox').style.top=((y1+y2)/2-gE('infoBox').offsetHeight-10)+'px';
				gE('infoBox').style.left=((x1+x2)/2+15)+'px';
				gE('infoBox').style.visibility='visible';
			}
			areas[i].onmouseout=function(){
				gE('infoBox').style.visibility='hidden';
			}
			areas[i].onclick=function(){
				return false;
			}
		}
	}
	if(gE('close1'))
		gE('close1').onclick=function(){
			self.close();
		}
	if(gE('close2'))
		gE('close2').onclick=function(){
			self.close();
		}
	if(gE('id253'))
		gE('id253').onclick=function(){
			var planWin=openWindow('/projects/duna/gen-plan/', '1000', '700');
			return false;
		}
	if(gE('id254'))
		gE('id254').onclick=function(){
			var planWin=openWindow('/projects/wood-rainbow/gen-plan/', '1000', '700');
			return false;
		}
	if(gE('id142'))
		gE('id142').onclick=function(){
			var planWin=openWindow('/projects/zubrvil/gen-plan/', '1000', '700');
			return false;
		}
}

function shake(){
	if(animIndex2<1){
		animIndex2++;
		(Math.floor(Math.random()*2))?znak=1:znak=-1;
		objShake.style.left=(objShake.offsetLeft+(Math.floor(Math.random()*4)*znak))+'px';
		(Math.floor(Math.random()*2))?znak=1:znak=-1;
		objShake.style.top=(objShake.offsetTop+(Math.floor(Math.random()*4)*znak))+'px';
	}else{
		objShake.style.left=objShake.nx+'px';
		objShake.style.top=objShake.ny+'px';
		animIndex2=0;
	}
}

function goldenWings(){
	if(animIndex>0){
		animIndex=animIndex-5;
		animObj.style.top=animIndex+'px';
	}else{
		clearInterval(timer);
		animObj.style.top='5px';
	}
}

function scrolling(){
	if(scrollIndex<1 && scrollIndex>(maxLeft-1)){
		scrollIndex=scrollIndex+direct*10;
		if(scrollIndex>=0){
			scrollIndex=0;
			clearInterval(scrollTimer);
		}
		if(scrollIndex<=maxLeft){
			scrollIndex=maxLeft;
			clearInterval(scrollTimer);
		}
		scrollObj.style.left=scrollIndex+'px';
	}else{
		clearInterval(scrollTimer);
	}
}
function doscrolling(){
	if((dodirect>0 && scrollIndex<doscroll) || (dodirect<0 && scrollIndex>doscroll)){
		scrollIndex=scrollIndex+dodirect*10;
		if((dodirect>0 && scrollIndex>doscroll) || (dodirect<0 && scrollIndex<doscroll)) scrollIndex=doscroll;
		scrollObj.style.left=scrollIndex+'px';
	}else
		clearInterval(doscrollTimer);
}


function scrollTop(){
	if (scrollI<0){
		scrollI=scrollI+5;
		scrollObj.style.top=scrollI+'px';
	}else{
		clearInterval(scrollTimerTop);
		scrollTopPoint.style.visibility='hidden';
	}
}

function scrollBot(){
	if (scrollI>maxScroll){
		scrollI=scrollI-5;
		scrollObj.style.top=scrollI+'px';
	}else{
		clearInterval(scrollTimerBot);
		scrollBotPoint.style.visibility='hidden';
	}
}

function openWindow($url, windowWidth, windowHeight){
	if( (windowWidth > screen.width) || (windowHeight > screen.height) ){
		windowWidth = Math.floor(screen.width-10);
		windowHeight = Math.floor(screen.height-100);
		var xPos = 0;
		var yPos = 0;
	}else{
		var xPos = Math.floor(screen.width/2-windowWidth/2);
		var yPos = Math.floor(screen.height/2-windowHeight/2-40);
	}

	return window.open($url, '','height=' + windowHeight + ', width=' + windowWidth + ', scrollbars=yes, top=' + yPos + ', left=' + xPos );
}


function redrawScreen() {
  location.reload();
  return false
}

function shiftTo(obj, x, y) {
  if (w3c) {
    obj.style.left = x + "px";
    obj.style.top = y + "px";
  }
  else if (ns4) {
	 obj.moveTo(x,y);
  } 
  else if (ie4) {
    obj.style.pixelLeft = x;
	obj.style.pixelTop = y;
  }
}

function getObject(obj) {
	var theObj = eval("document." + range + obj + cap);
	return theObj;
} 

function scrollUp() {
  if (mutex == 1){
    var theObj = getObject(newsId);
    if (yplace < ymax) {
      yplace = yplace + speed;
      if (yplace > ymax) yplace = ymax;
      shiftTo(theObj, xplace, yplace);
      setTimeout("scrollUp()",25);
    }
  }
}
  
function scrollDown() {
  if (mutex == 2){
    var theObj = getObject(newsId);
    if (yplace > ymin) {
      yplace = yplace - speed;
      if (yplace < ymin) yplace = ymin;
      shiftTo(theObj, xplace, yplace);
      setTimeout("scrollDown()",25);
    }
  }
}

function scrollIt(msg, dir) {
  if (initial==false) {
  	initial = true;
  	initscroll();
  }
  window.status = msg; 
  mutex = dir;
  if (mutex == 1) scrollUp();
  else if (mutex == 2) scrollDown();
}

function initscroll() {
  if (w3c) {
    range = "getElementById(\"";
    cap = "\")";
    theObj = getObject(newsClipId);
    newsHeight = parseInt(theObj.offsetHeight);
    theObj = getObject(newsId);
    ymin = (parseInt(theObj.offsetHeight) - newsHeight) * -1;
  }
  else if (ns4) {
    window.captureEvents(Event.RESIZE);
    window.onresize = redrawScreen;
    theObj = getObject(newsClipId);
    newsHeight = theObj.clip.height;
    newsId = newsClipId + ".document." + newsId;
    theObj = getObject(newsId);
    ymin = (theObj.clip.height - newsHeight) * -1;
  }
  else if (ie4) {
    range = "all.";
    theObj = getObject(newsClipId);
    newsHeight = theObj.offsetHeight;
    theObj = getObject(newsId);
    ymin = (theObj.offsetHeight - newsHeight) * -1;
  }
}

