/////////////////////////////////////////////////////////////////////////////////
// Library 
/////////////////////////////////////////////////////////////////////////////////

function insertAfter(newElement,targetElement) {
  var parent = targetElement.parentNode;
  if (parent.lastChild == targetElement) {
    parent.appendChild(newElement);
  } else {
    parent.insertBefore(newElement,targetElement.nextSibling);
  }
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){
	var yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
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;
}

/////////////////////////////////////////////////////////////////////////////////
// Workaround Sprache und Presse 
/////////////////////////////////////////////////////////////////////////////////

var tmpStr = window.location.href;
if( tmpStr == 'http://www.ivistar.de/index.php?id=296&L=0&L=18' )
{
window.location.href = 'http://www.ivistar.de/index.php?id=259&L=0&L=18';
}

if( tmpStr == 'http://www.ivistar.de/index.php?id=296&L=18&L=0' )
{
window.location.href = 'http://www.ivistar.de/index.php?id=259&L=18&L=0';
}

if( tmpStr == 'http://www.ivistar.de/index.php?id=288&L=0&L=18' )
{
window.location.href = 'http://www.ivistar.de/index.php?id=260&L=0&L=18';
}

if( tmpStr == 'http://www.ivistar.de/index.php?id=288&L=18&L=0' )
{
window.location.href = 'http://www.ivistar.de/index.php?id=260&L=18&L=0';
}


/////////////////////////////////////////////////////////////////////////////////
// ToolTip  Currentprojects
/////////////////////////////////////////////////////////////////////////////////

function prepareCurrentProj(){

  if(!document.getElementById('ivi_curProjects')) return false;
  var myParent = document.getElementById('ivi_curProjects');
  if(!document.getElementById('firstProject')) return false;
  myParent.getElementsByTagName('a')[0].onmouseover = function(){ showToolTip(this); };
  myParent.getElementsByTagName('a')[0].onmouseout = function(){ hideToolTip(); };
  if(!document.getElementById('secondProject')) return false;
  myParent.getElementsByTagName('a')[1].onmouseover = function(){ showToolTip(this); };
  myParent.getElementsByTagName('a')[1].onmouseout = function(){ hideToolTip(); };
}

var myKill = 0;
var myKillTimeOut;

function showToolTip(me){
  myID = me.id;
  if(myKill == 0){
    var myParent = document.getElementById('ivi_curProjects');
    var tmpStr = me.getElementsByTagName('img')[0].alt;
    
    myDIV = document.createElement("div");
    myDIV.id="bubble";
    myDIV.className = "sizeM";
    myP = document.createElement("p");
    myTEXT = document.createTextNode(tmpStr);
    myP.appendChild(myTEXT);
    myDIV.appendChild(myP);
    myParent.appendChild(myDIV);
    
    myDIV.className = 'sizeS';
    
    if(parseInt(myDIV.getElementsByTagName('p')[0].offsetHeight) > 20 ){
     myDIV.className = 'sizeM'; 
    }
    if(parseInt(myDIV.getElementsByTagName('p')[0].offsetHeight) > 44 ){
     myDIV.className = 'sizeL';
    }
    if(parseInt(myDIV.getElementsByTagName('p')[0].offsetHeight) > 68 ){
     myDIV.className = 'sizeXL';
    }
    if(parseInt(myDIV.getElementsByTagName('p')[0].offsetHeight) > 92 ){
     myDIV.className = 'sizeXXL';
    }
    
    (myID == "firstProject") ? myDIV.style.left="12px" : myDIV.style.left="87px"
    myDIV.style.visibility = "visible";
    myKill = 1;
  }
}

function hideToolTip(){
if(myKill == 1){
    myDelete = document.getElementById('bubble');
    myParent = myDelete.parentNode;
    myParent.removeChild(myDelete);
    myKill = 0;
  }
}
/*ToolTip Ende */


/////////////////////////////////////////////////////////////////////////////////
// ToolTip Team
/////////////////////////////////////////////////////////////////////////////////

function prepareTeam()
{
  var count, myParent;
  if(!document.getElementById('team')) return false;
  myParent = document.getElementById('team');
  count = myParent.getElementsByTagName('img').length;
  for (i=0; i<count; i++)
  {
    if( myParent.getElementsByTagName('img')[i].alt && myParent.getElementsByTagName('img')[i].alt != "")
    {
      myParent.getElementsByTagName('img')[i].onmouseover = function(){ showTeamToolTip(this); };
    }
  }
}

var myKillTeam = 0;
var myKillTimeOutTeam;

var BubbleOver = 0;

function showTeamToolTip( me ){
  var myElement, mySecondElement, myParent, tmpStr;
  hideTeamToolTip();
  if(myKillTeam == 0){
    myParent = document.getElementById('team').parentNode;
    tmpStr = me.alt;

    tmpStr = tmpStr.replace(', ', '<br />');
    me.title="";
    myElement = document.createElement("p");
    myElement.id="bubbleTeam";
    myElement.className = "sizeM";
    myElement.innerHTML = tmpStr;
    myElement.className = 'sizeM';
    myElement.style.padding = '0px 0px 0px 8px';
    myElement.style.left = parseInt( me.parentNode.offsetLeft )  + 60 + 'px';
    myElement.style.top = parseInt( me.parentNode.offsetTop ) + parseInt(document.getElementById('team').offsetTop) + -40 + 'px';
    myElement.style.position = "absolute";
    myParent.appendChild(myElement);
   
    mySecondElement = document.createElement("p");
    mySecondElement.id="hotSpot";
    mySecondElement.onmouseout = function(){ hideTeamToolTip(this); };
    mySecondElement.style.width = "120px";
    mySecondElement.style.height = "168px";
    mySecondElement.style.position = "absolute";
    mySecondElement.style.left = parseInt( me.parentNode.offsetLeft )  + 'px';
    mySecondElement.style.top = parseInt( me.parentNode.offsetTop ) + parseInt(document.getElementById('team').offsetTop)  +'px';
    mySecondElement.style.background = "#ffffff";
    mySecondElement.style.filter="Alpha(opacity= 0 )";
    mySecondElement.style.opacity = "0";
    mySecondElement.style.zIndex = "99999";
    myParent.appendChild(mySecondElement);
    
    myElement.style.visibility = "visible";
    myKillTeam = 1;
  }
}


function hideTeamToolTip(){
  var myDelete, myParent;
  if( myKillTeam == 1 ){
    myDelete = document.getElementById('bubbleTeam');
    myParent = myDelete.parentNode;
    myParent.removeChild(myDelete);
    
    myDelete = document.getElementById('hotSpot');
    myParent = myDelete.parentNode;
    myParent.removeChild(myDelete);
    myKillTeam = 0;
  }
}


/////////////////////////////////////////////////////////////////////////////////
// ToolTip Calculator
/////////////////////////////////////////////////////////////////////////////////

function showCalculatorToolTip( me, objText ){
	var myElement = document.getElementById('calculatorBubble');
	var myTextElement = myElement.getElementsByTagName('p')[0]
	myElement.style.display = "block";
	myTextElement.innerHTML = objText;
	myElement.style.left =  parseInt( me.offsetLeft ) + 'px';
	myElement.style.top =  parseInt( me.offsetTop ) - parseInt( myTextElement.offsetHeight ) - 40 + 'px';
}

function hideCalculatorToolTip(){
  	var myElement = document.getElementById('calculatorBubble');
		myElement.style.display = "none";
		//myElement.style.left = "-999px";
}
/*ToolTip Ende */

/////////////////////////////////////////////////////////////////////////////////
// Diashow
/////////////////////////////////////////////////////////////////////////////////

function prepareDiaShow() {

if (!document.getElementById) return false;
if (!document.getElementById('slideShowBox')) return false;

//document.getElementById('slideShowImage').getElementsByTagName('span')[0].firstChild.insertData(0, myDia[0]['Text']);

var myText = document.createTextNode(myDia[0]['Text']);
  document.getElementById("slideShowImage").getElementsByTagName("span")[0].appendChild(myText);


  for (var i = 0; i < myDia.length; i++) {
    for (var Eigenschaft in myDia[i])
      if( Eigenschaft == "Dateiname"){
        var myName = checkDateiname(myDia[i][Eigenschaft])
        var myIMG = document.createElement("img");
        myIMG.id = myName;
        myIMG.alt = myDia[i]["Text"];
        myIMG.src = myPath+ "/" + myName + "-thumb.jpg";
        myIMG.height = "54";
        myIMG.width = "105";
        myIMG.onclick = function(){changeMainImage(this);};
        document.getElementById("imageBox").appendChild(myIMG);
      }
    }
    
  document.getElementById('slideShowScroll').getElementsByTagName('button')[0].onmouseover = function(){setButtonClassTop(this,'topScrollOver')};
  document.getElementById('slideShowScroll').getElementsByTagName('button')[0].onmouseout =  function(){setButtonClassTop(this,'topScroll')};
  document.getElementById('slideShowScroll').getElementsByTagName('button')[1].onmouseover = function(){setButtonClassBottom(this,'bottomScrollOver')};
  document.getElementById('slideShowScroll').getElementsByTagName('button')[1].onmouseout = function(){setButtonClassBottom(this,'bottomScroll')}; 
  
  document.getElementById('slideShowScroll').getElementsByTagName('button')[0].className = 'topScrollInaktiv';
  
  if(parseInt(document.getElementById("imageBox").offsetHeight) < 190){  
    document.getElementById('slideShowScroll').getElementsByTagName('button')[1].className = 'bottomScrollInaktiv';
  }
  
}

function checkDateiname (myValue){
  var tmpArray = myValue.split('.jpg');
  myValue = tmpArray[0];
  return myValue;
}

function setButtonClassTop(me,myClass){
  var elem = document.getElementById("imageBox"); 
  if (!elem.style.top) {
    elem.style.top = "0px";
  }
  if( parseInt(elem.style.top) == 0) {
    document.getElementById('slideShowScroll').getElementsByTagName('button')[0].className = 'topScrollInaktiv';
  }
  else {
    me.className = myClass;
  } 
}

function setButtonClassBottom(me,myClass){
  if (me.className != "bottomScrollInaktiv"){
    me.className = myClass;
  }
}


var myMoving = 0;

function goTop(me){
if (myMoving == 1) return;
var myParent = me.parentNode.id;
var elem = document.getElementById("imageBox");
  if (!elem.style.top) {
    elem.style.top = "0px";
  }
var myMove = parseInt(elem.style.top) +60;
var testVar = (myMove) + 2*60;
if( parseInt(elem.style.top) < 0) {
  moveElement( "imageBox", myMove, 10)
  myMoving = 1;
  document.getElementById('slideShowScroll').getElementsByTagName('button')[0].className = 'topScrollOver';
  document.getElementById('slideShowScroll').getElementsByTagName('button')[1].className = 'bottomScroll';
  }
  else {
  document.getElementById('slideShowScroll').getElementsByTagName('button')[0].className = 'topScrollInaktiv';
  }
}

function goDown(me){
if (myMoving == 1) return;
var myParent = me.parentNode.id;
var elem = document.getElementById("imageBox");
  if (!elem.style.top) {
    elem.style.top = "0px";
  }
var myMove = parseInt(elem.style.top) -60;
var testVar = (myMove*-1) + 2*60;
if( testVar <  document.getElementById("imageBox").offsetHeight) {
  moveElement( "imageBox", myMove, 10)
  myMoving = 1;
  document.getElementById('slideShowScroll').getElementsByTagName('button')[1].className = 'bottomScrollOver';
  document.getElementById('slideShowScroll').getElementsByTagName('button')[0].className = 'topScroll';
  } 
  else {
    document.getElementById('slideShowScroll').getElementsByTagName('button')[1].className = 'bottomScrollInaktiv';
  }
}

function moveElement(elementID,final_y,interval) {
  if (!document.getElementById) return false;
  if (!document.getElementById(elementID)) return false;
  var elem = document.getElementById(elementID);
  if (elem.movement) {
    clearTimeout(elem.movement);
  }
  if (!elem.style.top) {
    elem.style.top = "0px";
  }
  var ypos = parseInt(elem.style.top);
  if (ypos == final_y) {
    myMoving = 0;
    if (ypos == 0) {
      document.getElementById('slideShowScroll').getElementsByTagName('button')[0].className = 'topScrollInaktiv';
    }
    var tmp = ypos + (parseInt(document.getElementById("imageBox").offsetHeight)-180);
    if( tmp == 0 ) {
      document.getElementById('slideShowScroll').getElementsByTagName('button')[1].className = 'bottomScrollInaktiv';
    }
    
    return true;
  }
  if (ypos < final_y) {
    var dist = Math.ceil((final_y - ypos)/10);
    ypos = ypos + dist;
  }
  if (ypos > final_y) {
    var dist = Math.ceil((ypos - final_y)/10);
    ypos = ypos - dist;
  }
  elem.style.top = ypos + "px";
  var repeat = "moveElement('"+elementID+"',"+final_y+","+interval+")";
  elem.movement = setTimeout(repeat,interval);
}

var TmpText;
function changeMainImage (me){
  var tmpArray = me.src.split("/");
  var tmpFileName = tmpArray.pop();
  var tmp2Array = tmpFileName.split("-thumb.jpg");
  var tmpFileName = tmp2Array[0]+".jpg";
  var myPath = tmpArray.join("/"); 
  myPath = myPath + "/" + tmpFileName
  document.getElementById('mainImage').src = myPath;
  
  var myKill = document.getElementById("slideShowImage").getElementsByTagName("span")[0];
  document.getElementById("slideShowImage").removeChild(myKill);
  
  var mySPAN = document.createElement("span");
  var myText = document.createTextNode(me.alt);
  mySPAN.appendChild(myText);
  document.getElementById("slideShowImage").appendChild(mySPAN);

}
/////////////////////////////////////////////////////////////////////////////////
// ReQuestFormular
/////////////////////////////////////////////////////////////////////////////////

function showFormBlock( me ) {
	var myID = me.id;
	switch (myID){
		case 'arbeitsplaetze':
			if ( me.checked == true){
				document.getElementById('block_arbeitsplaetze').style.display = 'block';
			}
			else{
				document.getElementById('block_arbeitsplaetze').style.display = 'none';
			}
		break;
		case 'konferenzraeume':
			if ( me.checked == true){
				document.getElementById('block_konferenzraeume').style.display = 'block';
				document.getElementById('block_tuerschilder_detail').style.display = 'block';
				document.getElementById('block_konferenzraeume_detail').style.display = 'block';
			}
			else{
				document.getElementById('block_konferenzraeume').style.display = 'none';
				document.getElementById('block_tuerschilder_detail').style.display = 'none';
				document.getElementById('block_konferenzraeume_detail').style.display = 'none';
			}
		break;
	}
}



function checkForm(){
	var myCheck = true;
	var obj = document.getElementById('vorname');
	
	
	if ( obj.value == '' ){
		obj.parentNode.getElementsByTagName('label')[0].className = 'error';
	}
	else{
		obj.parentNode.getElementsByTagName('label')[0].className = '';
	}
	var obj = document.getElementById('nachname');
	if ( obj.value == '' ){
		obj.parentNode.getElementsByTagName('label')[0].className = 'error';
		myCheck = false;
	}
	else{
		obj.parentNode.getElementsByTagName('label')[0].className = '';
	}
	var obj = document.getElementById('telefon');
	if ( obj.value == '' ){
		obj.parentNode.getElementsByTagName('label')[0].className = 'error';
		myCheck = false;
	}
	else{
		obj.parentNode.getElementsByTagName('label')[0].className = '';
	}
	var obj = document.getElementById('e_mail');
	if ( obj.value == '' ){
		obj.parentNode.getElementsByTagName('label')[0].className = 'error';
		myCheck = false;
	}
	else{
		obj.parentNode.getElementsByTagName('label')[0].className = '';
	}
	var obj = document.getElementById('firma');
	if ( obj.value == '' ){
		obj.parentNode.getElementsByTagName('label')[0].className = 'error';
		myCheck = false;
	}
	else{
		obj.parentNode.getElementsByTagName('label')[0].className = '';
	}
	//myCheck = false;
	return myCheck;
}


addLoadEvent(prepareDiaShow);
addLoadEvent(prepareCurrentProj);
addLoadEvent(prepareTeam);
