// this file holds the ajax code for requesting and building the tag cloud

// minimum tag size in the tagcloud in percent
var min_size = 90;
// maximum tag size in the tagcloud in percent
var max_size = 198;
// the color of the tags
//var tag_color = '#9933CC';
var tag_color = '#0000FF';

// the XMLHttpRequest
var req;

// saved data for AJAX request
var _pagename;
var _data_url;
var _max_tags;
var _sort;
var _max_urls;

// initializes the req object
function initAjax() {
  try {
    if( window.XMLHttpRequest ) {
      req = new XMLHttpRequest();
    } else if( window.ActiveXObject ) {
      req = new ActiveXObject( "Microsoft.XMLHTTP" );
    } else {
      document.getElementById("tagCloud").innerHTML = '<i>Fehler: AJAX is not supported</i>';
    }
    if( req.overrideMimeType ) {
       req.overrideMimeType( 'text/xml' );
    }
  } catch( e ) {
    document.getElementById("tagCloud").innerHTML = '<i>Fehler: AJAX is not available</i>';
  }
}

function reloadTagCloud() {
	//alert(_pagename+","+ _data_url+","+ _max_tags+","+ _sort+","+ _max_urls);
	//retreiveTagCloud(_pagename, _data_url, _max_tags, _sort, _max_urls);

	if( req ) {
    // ajax request
    var url = _data_url + "?page="+_pagename+"&max_tags="+_max_tags+"&sort="+_sort+"&max_urls="+_max_urls+"&reload=true";
    req.open( "GET", url, true );
    req.onreadystatechange = writeTagCloud;
    req.send( null );
  }


}

// starts the AJAX request for the tag cloud
function retreiveTagCloud(pagename, data_url, max_tags, sort, max_urls) {
  if( req ) {
    // save variables
    _pagename = pagename;
    _data_url = data_url;
    _max_tags = max_tags;
    _sort = sort;
    _max_urls = max_urls;
  
    // ajax request
    var url = data_url + "?page="+pagename+"&max_tags="+max_tags+"&sort="+sort+"&max_urls="+max_urls;
    req.open( "GET", url, true );
    req.onreadystatechange = writeTagCloud;
    req.send( null );
  }
}

// starts the AJAX request for the links
function retrieveLinks(encoded) {
  if( req ) {
    var url = _data_url + "?page="+_pagename+"&max_tags="+_max_tags+"&sort="+_sort+"&max_urls="+_max_urls+"&tag="+encoded;
    req.open( "GET", url, true );
    req.onreadystatechange = writeLinks;
    req.send( null );
  }
}

function writeLinks() {
  if( 4 == req.readyState ) {
    if( 200 != req.status ) {
      document.getElementById("tagCloud").innerHTML = '<i>Keine Daten vorhanden!</i>';
    } else {
      xmldata = req.responseXML.documentElement;
      // save the URLs
      var tag_name;
      var row_list = new Array();
      var url;
      var page;
      var url_count;
      var link;
      var tag_list = xmldata.getElementsByTagName('tag');
      
      var base_url = decodeURIComponent( document.URL );
      //alert(base_url);
      if (tag_list.length == 1){
        tag_name = tag_list[0].getElementsByTagName('name')[0].firstChild.data;
        var url_list = tag_list[0].getElementsByTagName('url');
        var url_index = 0;
        while (url_index < url_list.length){
          url = url_list[url_index].getElementsByTagName('link')[0].firstChild.data;
          page = url_list[url_index].getElementsByTagName('page')[0].firstChild.data;
          url_count = url_list[url_index].getElementsByTagName('urlcount')[0].firstChild.data;
          //link = "<td style=\"text-align: right;\">"+url_count+"</td><td><a href=\""+url+"\" title=\""+url+"\">"+page+"</a></td>";
          var page_name = url;
          var cat;
	if ((cat = url.lastIndexOf("/")) != -1) {
		page_name = url.substr(cat+1);
	}
	
        
          link = "<li><a id=\"taglist_"+url_index+"\" href=\"javascript:openthisurl('"+url+"')\" onmousedown=\"ajaxLinkTracker.recordJScript('taglist_"+url_index+"', '"+url+"', '"+page_name+"')\" title=\""+url+"\">"+page_name+"</a></li>";
          row_list.push(link);
          url_index++;
        }
      } else
        tag_name = '<i>Kein passendes Tag vorhanden</i>';
      placeLinks(tag_name, row_list);
      

	//showLinks();
    }
  }
}

function openthisurl(url) {
//alert('/'+_pagename+'/link/'+url.substring("http://www.austria-lexikon.at/af/".length));
	// pageTracker._trackPageview('/'+_pagename+'/__link__/'+url.substring("http://www.austria-lexikon.at/af/".length));

	window.location.href = url;
}

function showLinks() {
  var width;
  if (window.innerWidth) {
    width = window.innerWidth;
  } else if (document.body && document.body.offsetWidth) {
    width = document.body.offsetWidth;
  } else {
    width = 1024;
  }
  var height;
  if (window.innerHeight) {
    height = window.innerHeight;
  } else if (document.body && document.body.offsetWidth) {
    height = document.body.offsetHeight;
  } else {
    height = 700;
  }

  var tagLinks = document.getElementById("tagLinks");

  tagLinks.style.width = width/2+'px';
  tagLinks.style.height = height/2+'px';
  tagLinks.style.left = width/4+'px';
  tagLinks.style.top = height/4+'px';
  
  document.getElementById("urlheader").style.width = (width/2 - 4) + 'px';
  document.getElementById("urlheader").style.height = '28px';
  document.getElementById("urltable").style.height = (height/2 - 32) + 'px';

  tagLinks.style.display = 'block';
}

function closeLinks() {
  document.getElementById("tagLinks").style.display = 'none';
}

function placeLinks(tag, row_list) {  
  var html = "<table id=\"urlheader\" style=\"margin: 2px;\">";
  html += "<tr>";
  html += "<td style=\"border-bottom-color: #000000; border-style: solid; border-bottom-width: 1px\;\"><b>Tag:&nbsp;"+tag+"</b></td>"
  html += "<td style=\"border-bottom-color: #000000; border-style: solid; border-bottom-width: 1px\; text-align: right;\"></td>";
  html += "</tr>";
  html += "</table>"
    
  html += "<div id=\"urltable\" >";
  //html += "<table style=\"margin: 2px;\">";
  //html += "<tr><th style=\"text-align: right;\">Anzahl</th><th style=\"text-align: left;\">Seite</th></tr>"
  //html += "<tr>";
  html += "<ul>";
  //html += row_list.join("</tr><tr>");
  html+= row_list.join("");
  html += "</ul>";
  //html += "</tr>";
  //html += "</table>";
  html += "</div>";

  document.getElementById("tagLinks").innerHTML = html;


Lightbox.open([['tagLinks','600x600', 'inline']],0); 

}

function tagClicked(encoded) {
  retrieveLinks(encoded);
 // pageTracker._trackPageview('/'+_pagename+'/__tag__/'+encoded);

}

// generates the tag cloud
function writeTagCloud() {
  if( 4 == req.readyState ) {
    if( 200 != req.status ) {
      document.getElementById("tagCloud").innerHTML = '<i>Keine Daten vorhanden!</i>';
    } else {
      xmldata = req.responseXML.documentElement;
      // minimal count of one tag
      var min_count = xmldata.getAttribute('mincount');
      // maximal count of one tag
      var max_count = xmldata.getAttribute('maxcount');

      // all tags
      var tag_list = xmldata.getElementsByTagName('tag');

      var index = 0;
      // the output
      var tagcloud = '';
      // go through all tags
      while (index < tag_list.length){
	var tag_name = null;
	var tag_encoded = null;
	var tag_count = null;

	if (tag_list[index].getElementsByTagName('name')[0].firstChild != null)
        	tag_name = tag_list[index].getElementsByTagName('name')[0].firstChild.data;
	
	if (tag_list[index].getElementsByTagName('encoded')[0].firstChild != null)
        	tag_encoded = tag_list[index].getElementsByTagName('encoded')[0].firstChild.data;
        
	if (tag_list[index].getElementsByTagName('tagcount')[0].firstChild != null)
		tag_count = tag_list[index].getElementsByTagName('tagcount')[0].firstChild.data;
        
	var size;
	var offset = 10;
	if (tag_name != null)
        if(min_count == max_count)
          // all tags have the same count
          size = 100-offset;
        else {
          // calculate the size of the current tag
          size = (min_size + (max_size - min_size) * (tag_count - min_count) / (max_count - min_count))-offset;
	}
        // place one tag in the calculated size including the tag, the link for showing the urls and the tagcount
        if (tag_name != null)
		tagcloud += "<a id=\"tag_"+index+"\" href=\"javascript:tagClicked('"+tag_encoded+"');\" onmousedown=\"ajaxLinkTracker.recordJScript('tag_"+index+"', 'javascript popup', '"+tag_name+"')\" style=\"color: "+tag_color+"; font-size:"+size+"%; \" title=\"" + tag_count + "\">" + tag_name + "</a> ";
                
        index++;
      }
      if (index == 0)
        tagcloud = "<i>Keine Tags vorhanden...</i>";
      document.getElementById("tagCloud").innerHTML = tagcloud;
    }
  }
}