function echo(text){	console.info(text);  }

var site_ready = false;
//lastOpened



function showData( mode, value, object ){

  if (mode == 'first'){

    document.getElementById("loading").style.display = "none";
    document.getElementById("loading_text").style.display = "none";
    
    // insert the open values
    for ( var i=0; i<sitedata.length; i++ ){
      if ( sitedata[i]["type"] == "section" ){
        sitedata[i]["open"] = false;
        show_item( sitedata[i]["id"] );
      } else if ( sitedata[i]["type"] == "subsection" ){
        sitedata[i]["open"] = false;
        hide_item( sitedata[i]["id"] );
      } else if ( sitedata[i]["type"] == "item" ) {
      	if ( sitedata[i]["content_type"] == "image" ){
      		// do nothing
      	} else if ( sitedata[i]["content_type"] == "video" ){
          sitedata[i]["open"] = false;
          sitedata[i]["more"] = false;
      	} else {
      		console.error("no type in array " + i + " at 'showData'-'first' ");
      	}
        hide_item( sitedata[i]["id"] );
      } else {
        console.error("no type in array " + i + " at 'showData'-'first' ");
      }
    }

    // reset the sizes
    setSiteSize(null, null);
    site_ready = true;
  }

  else if (site_ready && mode == 'switch'){
  	if ( all_closed ) close_all_finish();
    switch_item(value);
    setSiteSize(value, object);
    return false;
  }


  else if (site_ready && mode == 'more'){
    var index = getIntex( value );
    sitedata[index]["more"] = !sitedata[index]["more"];
    show_item( value );
    setSiteSize(value, null);

    return false;
  }

}






function getIntex(id){
  for ( var i=0; i<sitedata.length; i++ ){
    if ( sitedata[i]["id"] == id ) return i;
  }
  return -1;
}








//
//
//  after any change, we neet to calculate the
//  positions of the shwon elements
//


function setSiteSize(item_id, object_id){
  // set the containersize
  if (navigator.appName.indexOf('Netscape') != -1){
    sitewidth = self.innerWidth - 60;
    siteheight = self.innerHeight - 60;
  } else if (navigator.appName.indexOf('Microsoft') != -1){
    sitewidth = document.body.clientWidth - 60;
    siteheight = document.body.clientHeight - 60;
  } else {
    sitewidth = window.innerWidth - 60;
    siteheight = window.innerWidth - 60;
  }
  if (sitewidth <= 60) sitewidth = 60;
  document.getElementById("container").style.width = sitewidth+"px";

  var changed = true;
  var scroll_to_size = 0;
  var scroll_to_section = false;
  while ( changed ){
    changed = false;
    var mywidth  = 0;
    var mywidth_temp  = 0;
    var myheight = 0;
    var myheight_temp = 0;
    var rowheight = 0;

 
    for (var i=0; i<sitedata.length; i++){
      if ( sitedata[i]["shown"]){
        // get Dimension
        var width = parseInt(sitedata[i]["thumb_width"]);
        var height = parseInt(sitedata[i]["thumb_height"]);
        if ( sitedata[i]["type"] == "item" && sitedata[i]["content_type"] == "video" && sitedata[i]["open"] ){
          width = parseInt(sitedata[i]["content_width"]);
          height = parseInt(sitedata[i]["content_mod_height"]);
        }
        if ( item_id != null && sitedata[i]["id"] == item_id ) {
        	scroll_to_size = height;
        	if ( (sitedata[i]["type"]=='section'||sitedata[i]["type"]=='subsection') && (sitedata[i]["open"]) ){
            scroll_to_section = true;
        	} 
        }
        // analyze
        mywidth_temp = mywidth + width + item_width_offset;
        if ( mywidth_temp > sitewidth ){
          mywidth = 0;
          myheight = myheight + rowheight;
          rowheight = 0;
          if (sitedata[i]["id"] != item_id) scroll_to_section = false;
        } else {
        	if (scroll_to_section){
        		if (sitedata[i]["type"] == 'section' && sitedata[i]["id"] != item_id) scroll_to_section = false;
        		if ( height > scroll_to_size ) scroll_to_size = height;
        	}
        }
        // set properties
        set_position( sitedata[i]["id"], mywidth, myheight );
        // set height
        if ( height + item_height_offset > rowheight ) rowheight = height + item_height_offset;
        // set width
        if ( mywidth_temp <= sitewidth ) {
          mywidth = mywidth_temp;
        } else {
          mywidth = width + item_width_offset;
        }

      }
    }

    // scroll to position
    if ( isallclosed() ) {
    	startScrollTo( -1, 0 );
    } else if (item_id != null && scroll_to_size>0){
      startScrollTo(item_id, scroll_to_size);
    }

    // set the container
    shrink_containersize = myheight+rowheight + 30;
    checkScroll();
  }
}

function set_position( id, x, y ){
  var id_string = "shown_item_" + id;
  var index = getIntex(id);

  if ( document.getElementById(id_string+"_open") != null ) set_position_help( id_string+"_open", x, y ); 
  if ( document.getElementById(id_string+"_close") != null ) set_position_help( id_string+"_close", x, y ); 
  if ( document.getElementById(id_string+"_more") != null ) set_position_help( id_string+"_more", x, y ); 

  sitedata[index]["shown"] = true;
  sitedata[index]["pos_y"] = y;
}

function set_position_help( id_string, x, y ){
  document.getElementById(id_string).style.float = "none";
  document.getElementById(id_string).style.position = "absolute";
  document.getElementById(id_string).style.left = x +"px";
  document.getElementById(id_string).style.top = y+"px";
}







var lastOpened = -1;
var lastClosed = -1;

function activate_hover(value, mode){
  if ( lastOpened != -1 && (value != lastOpened || mode=="open")){
    document.getElementById("image_"+lastOpened+"_open_href").className = "image_"+lastOpened+"_open";
    document.getElementById("image_"+lastOpened+"_open_hrefimg").className = "picture_text";
    lastOpened = -1;
  }
  if ( lastClosed != -1 && (value != lastClosed || mode=="close")){
    document.getElementById("image_"+lastClosed+"_close_href").className = "image_"+lastClosed+"_close";
    document.getElementById("image_"+lastClosed+"_close_hrefimg").className = "picture_text";
    lastClosed = -1;
  }
}





