// YUI tree control
// global variables
var tree;
var tree_objects;
var init = false;
var active_node;

//Add a window onload handler to build the tree when the load
//event fires.
YAHOO.util.Event.addListener(window, "load", getCategorieTree(0))


function getCategorieTree(id) {
	doRequest("includes/ajax_php_engine.inc.php", "function=get_categorie_tree&id="+id, cb_getCategorieTree, id, "POST");
}
		
function cb_getCategorieTree(http_request, id) {
	// Build array from response
	tree_objects = http_request.responseText.split("*");
	for (var i = 0; i < tree_objects.length; i++) {
		tree_objects[i] = tree_objects[i].split("|");
	}

	// Initialize the main tree on first call
	if (init == false) {
		treeInit();
	} else {
		buildSubTree()
	}

}

// Function to initialize the main tree
function treeInit() {
 	init = true;
  	buildMainTree();
  	document.getElementById('categories').style.display = "none";
}
// Function to dynamically load the sup tree
function loadSubTrees(node) {
    active_node = node;
	getCategorieTree(active_node.data.id);
}

//Function to create the main tree 
function buildMainTree() {

	//Instantiate the tree:
    tree = new YAHOO.widget.TreeView("categories");
    
    // Specify the method to load suptrees and the function to handle it
	tree.setDynamicLoad(loadSubTrees, 1);


	//Creates the tree nodes
	
	tree_obj = { label: 'Alle Kategorien', id:'' } ;
	var tmpNode = new YAHOO.widget.TextNode(tree_obj, tree.getRoot(), false);
	tmpNode.isLeaf = true;
	
	for (var i = 0; i < (tree_objects.length-1); i++) {
		if (tree_objects[i][1] != '-root-') {
			tree_obj = { label: tree_objects[i][1], id:tree_objects[i][0] } ;
			var tmpNode = new YAHOO.widget.TextNode(tree_obj, tree.getRoot(), false);
		}
	}
		
	// Expand and collapse happen prior to the actual expand/collapse,
    // and can be used to cancel the operation
    tree.subscribe("expand", function(node) {
		//YAHOO.log(node.index + " was expanded", "info", "example");
		// return false; // return false to cancel the expand
	});

	tree.subscribe("collapse", function(node) {
		//YAHOO.log(node.index + " was collapsed", "info", "example");
	});

	// Trees with TextNodes will fire an event for when the label is clicked:
	tree.subscribe("labelClick", function(node) {
		document.getElementById('selected_category_name').innerHTML = node.data.label;
		document.getElementById('scat').value = node.data.id;
		document.getElementById('scatname').value = node.data.label;
		document.getElementById('categories').style.display = "none";
	});

	//The tree is not created in the DOM until this method is called:
	tree.draw();
}

//Function to create the sub tree 
function buildSubTree() {
	for (var i = 0; i < (tree_objects.length-1); i++) {
		tree_obj = { label: tree_objects[i][1], id:tree_objects[i][0] } ;
		var tempNode = new YAHOO.widget.TextNode(tree_obj, active_node, false);
		if (tree_objects[i][2] == 0) tempNode.isLeaf = true;
	}
	active_node.loadComplete();
}

// YUI tree control end


function showCategoryList() {
	if (document.getElementById('categories').style.display == "none") {
		document.getElementById('categories').style.display = "inline";
	} else {
		document.getElementById('categories').style.display = "none";
	}
}




var info_open = null; // contains the id of the recently opened info box ...
var info_height = 0;
var info_timer;

function getURLParam(strParamName){ 
  var strReturn = ""; 
  var strHref = window.location.href; 
  if ( strHref.indexOf("&") > -1 ){ 
    var strQueryString = strHref.substr(strHref.indexOf("&")).toLowerCase(); 
    var aQueryString = strQueryString.split("&"); 
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){ 
      if ( 
aQueryString[iParam].indexOf(strParamName + "=") > -1 ){ 
        var aParam = aQueryString[iParam].split("="); 
        strReturn = aParam[1]; 
        break; 
      } 
    } 
  } 
  return strReturn; 
}


function openwindow (name, url, width, height) {
 if (height == null) height = 400;
 if (width == null) width = 600;
 wdw = window.open(url, name, "width="+width+",height="+height+",status=yes,scrollbars=yes,resizable=yes");
 wdw.focus();
}

function extend_search() {
  if (document.getElementById('extended_search').style.display == "none") {

    document.getElementById('searchbar').style.height = document.getElementById('searchbar').offsetHeight +50 +"px";
    document.getElementById('extended_search').style.display = "block";
    document.getElementById('btn_extend').src = "./img/arrow_up.jpg";
    if (getURLParam('embed')) {
    	document.getElementById('form_search').action = "?action=search&embed=true&extended";
    } else {
    	document.getElementById('form_search').action = "?action=search&extended";
    }
  } else {
    document.getElementById('searchbar').style.height = document.getElementById('searchbar').offsetHeight -50 +"px";
    document.getElementById('extended_search').style.display = "none";
    document.getElementById('btn_extend').src = "./img/arrow_down.jpg";
    
    if (getURLParam('embed')) {
    	document.getElementById('form_search').action = "?action=search&embed=true";
    } else {
    	document.getElementById('form_search').action = "?action=search";
    }

  }
}

function get_extended() {
  if (document.all.extended_search.style.display == "none") {
    return "1";
  } else {
    return "0";
  }
}

function show_info(id) {
  var info = document.getElementById('result_info_' + id);
  
  // if the new info is the same as the old, then cancel ...
  if (info_open == info) return;
  
  // if there's some open info box already, then close it ...
  if (info_open != null) {
    info_open.innerHTML = "";
    info_open.style.display = "none";
    info_open.style.padding = "0px";
    info_open.style.borderTop = "none";
  }
  
  // open the new info box ...
  info.innerHTML = "Informationen werden geladen ...";
  info.style.display = "block";
  info.style.borderTop = "1px #999999 solid";
  info_open = info;
  info_height = 0;
  info_open.style.paddingLeft = "10px";
  info_open.style.paddingRight = "10px";
  info_open.style.paddingTop = "5px";
  info_open.style.paddingBottom = "5px";
  
  // scroll user view to the info box ...
  // document.location = "#a_" + id;
  
  // start request ...
  doRequest("ajax/getdetails.php?id=" + id, null, show_info_cb, info, 'GET');
}

function show_info_cb(http_request, param) {
  if (http_request.status == 200) {
    param.innerHTML = http_request.responseText;
  } else {
    param.innerHTML = "Derzeit keine Informatioen verfügbar!";
  }
}