/*CVS Add-ins*/
/* 
$Revision: 1.5 $
$RCSfile: behaviour.js,v $
author: JDE
Copyright (c) A&B.face2net GmbH
*/

/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */
/*
    behaviour.js fuer MABB
    25.04.2007 JDE
*/
/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */


/**
* executes functions when the page loads.
* @param func_name name of function
*/
function addLoadEvent(func_name) {
	var lastonload = window.onload;
	if (typeof window.onload != 'function') 
		{ window.onload = func_name; } 
	else {	window.onload = function() {
      			lastonload(); func_name(); }
  	}
}

    var viewportwidth= 1024;
    var viewportheight= 768;

	var a = new Array("bg_back.gif", "bg_body.jpg" , "bg_col_sf.gif", "bg_iconaudio.gif", 
    "bg_iconfilm.gif", "bg_iconfoto.gif", "bg_icongesetz.gif", "bg_iconglossar.gif", 
    "bg_iconlink.gif", "bg_iconpdf.gif", "bg_more.gif", "bg_pdfdownload.gif", "bg_print.gif", 
    "bg_service.gif", "bg_skip.gif", "bg_up.gif", "bg_wrapper.gif");
	var b = new Array();
	var i;
	for (i = 0; i < a.length; i++) {
		b[i] = new Image();
		 b[i].src = '../css/images/styleimages/'+a[i];
	}

	
/**
* sets an attribute to a node.
* @param elem name of node
* @param attr kind of attribute
* @param value attribute value
*/
function set_attr(elem, attr, value) {
  var myNode = elem; myNode.setAttribute(attr, value ); }
/**
* sets an URL to a node.
* @param elem name of node
* @param url url
*/
function set_href(elem, url) {
  var myNode = elem; myNode.setAttribute("href",url );  }
/**
* sets a title to a node.
* @param elem name of node
* @param title node title
*/
function set_title(elem, title) {
  var myNode = elem; myNode.setAttribute("title", title ); }
/**
* sets a textnode to a parent node.
* @param par name of parent
* @param text value of title
*/
function set_textNode(par, text) {
  if (!document.createTextNode) return false;
  var myNode = par; myNode = document.createTextNode(text);
  return true; }
  
  /**
* returns the list of objects with a certain class name and tagname
* @param class_name name of class
* @param tag_name name of tag
*/  
function get_elementsByClassName(class_name, tag_name){
  var all_elements; var class_elements=new Array();
  var j=0; var i; var teststr;
  if(document.getElementsByTagName){ 
    all_elements=document.getElementsByTagName(tag_name);   
  }
  
  for(i=0;i<all_elements.length;i++){
    if(all_elements[i].className.indexOf(class_name)!=-1) {
	  teststr=","+all_elements[i].className.split(" ").join(",")+",";
	  if(teststr.indexOf(","+class_name+",")!=-1){
        class_elements[j]=all_elements[i];
		j++;
      }
    }
  }
  
  return class_elements;
  
}  
  
  
  
/**
* creates a link with link text, title and url.
* @param txt link text
* @param ltitle link title
* @param url  link destination
* @return link element (a);
*/
function create_link(txt, ltitle, url ) {
  if (!document.createElement || !document.createTextNode) return false;
  var myNode = document.createElement("a");
  var myNode_text = document.createTextNode(txt);
  myNode.appendChild(myNode_text);
  set_title(myNode, ltitle);
  set_href(myNode, url) ;
  return myNode;}
/**
* creates a print link using create_link(txt, ltitle, url ) and set_attr(elem, attr, value)
*/
function create_printLink() {
  var printlink = create_link("Drucken", "Diese Seite drucken", "javascript:window.print();" );
  set_attr(printlink, "onclick", "window.print();return false;");
  /*do not use onkeypress otherwise in FF the tab will get stuck in the printlink */ }
/**
* creates a list item.
* @param listid id of parent ul or ol
* @return list item (li);
*/
function create_ListItem(listId) {
  if (!document.getElementById) return false;
  var myList = document.getElementById(listId);
  if(myList){
    var myItem = document.createElement("li");
    myList.appendChild(myItem);
    return myItem;
  } else{
  return false;}
 }

/**
* creates a list item with a print link.
*/
function create_printLinkItem() {
  var printlink = create_link("Drucken", "Diese Seite drucken", "javascript:window.print();" );
  if(document.getElementById("footerul")) {
    create_ListItem("footerul").appendChild(printlink);
    set_attr(printlink, "onclick", "window.print();return false;");
    /*do not use onkeypress otherwise in FF the tab will get stuck in the printlink */
  }
}


function detect_id(id_value){
    if(document.getElementById && document.getElementById(id_value)) return true;
    else return false; 
}

function changeId(currentId, newId){
    if(detect_id(currentId)){
        var myElem = document.getElementById(currentId);
        set_attr(myElem, "id", newId);
    }
}

function compareClientHeights(teaserkind, height_teaser, height_navMain){
    var teaserlist = get_elementsByClassName(teaserkind,"div");
        if(teaserlist.length > 0){
            for(var i=0; i<teaserlist.length; i++){
                height_teaser = height_teaser + teaserlist[i].clientHeight;
            }//for
        }//if
     return height_teaser;
}

var counter= 0;

function set_kartenteaserposition(){
    
    
    if((detect_id("kartenteaser")||detect_id("kartenteaser_js")) && detect_id("navMainContainer")) {
        var height_navMain = document.getElementById("navMainContainer").clientHeight;
        //alert(height_navMain);
        var height_teaser = 0;
        var currentId_01 = "kartenteaser";
        var currentId_02 = "map";
        var newId_01 = "kartenteaser_js";
        var newId_02 = "map_js";
        var height_teaser = compareClientHeights("topteaser", height_teaser, height_navMain);
        if(height_teaser > height_navMain){ changeId(currentId_01, newId_01); changeId(currentId_02, newId_02); return false;  }
        
        
        var height_teaser = compareClientHeights("textteaser", height_teaser, height_navMain);
        if(height_teaser > height_navMain){ changeId(currentId_01, newId_01 ); changeId(currentId_02, newId_02); return false;   }
        
        
        var height_teaser = compareClientHeights("listenteaser", height_teaser, height_navMain);
        if(height_teaser > height_navMain){ changeId(currentId_01, newId_01 ); changeId(currentId_02, newId_02); return false;   }
       
        
        var height_teaser = compareClientHeights("glossarteaser", height_teaser, height_navMain);
        if(height_teaser > height_navMain){ changeId(currentId_01, newId_01 ); changeId(currentId_02, newId_02); return false;   } 
        
        if(height_teaser < height_navMain){ changeId(newId_01, currentId_01 ); changeId(newId_02, currentId_02);   }   
          
    }
    return true;
}

function check_viewport(){ 
  viewportwidth = document.body.clientWidth; 
  viewportheight =  document.body.clientHeight;
}

function check_viewportChange(){ 
    //alert(viewportwidth);
    var current_viewportwidth = checkWidth();
    var current_viewportheight =  checkHeight();
    if(current_viewportwidth != viewportwidth || current_viewportheight != viewportheight ){
        viewportwidth = current_viewportwidth;
        viewportheight = current_viewportheight;
        set_kartenteaserposition() 
    }
    
    
}

function checkWidth(){ 
    var viewportwidth = document.body.clientWidth;
    return viewportwidth;
}
function checkHeight(){
    var viewportheight = document.body.clientHeight;
    return viewportheight;
}


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
* loaded events
*/
addLoadEvent(create_printLinkItem);
addLoadEvent(set_kartenteaserposition);
addLoadEvent(check_viewport);
	