/*
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; version 2 of the License.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** Loïc DIAS DA SILVA <mglcel@mglcel.fr>
** FOAFDrive - another FOAF wizzard
**
** $Id: utils.js,v 1.10 2008-08-20 18:49:30 mglcel Exp $
*/

// Some useful stuff ----------------------------------------------------------

//-- Automatic javascript including system ------------------------------------
var js_includedFiles = new Array();
var GLOBAL_lastInserted       = null;

var newwindow = '';
function enlargeImg(url) {
	if (newwindow.location && !newwindow.closed) {
		newwindow.location.href = url;
		newwindow.focus(); }
	else {
		newwindow=window.open('popup.html?'+url,'htmlname','width=640,height=480,resizable=1');
	}
}

head_analyzed = false;

function js_include(file) {
  if (js_includedFiles[file] == 1)
    return;

  try {
    var Head = document.getElementsByTagName('head').item(0);

    if (!head_analyzed) { // Get statically included scripts
      var scripts = Head.getElementsByTagName('script');

      for (i=0; i < scripts.length; i++) {
        var scriptNode = scripts.item(i);
        var src = scriptNode.getAttribute('src');

        if (src != '') {
          var scriptNameIndex = src.indexOf('jslib/');
          if (scriptNameIndex != -1)
            js_includedFiles[src.substring(6, src.length)] = 1;
        }
      }

      head_analyzed = true;
    }

    var head_inc = document.createElement('script');
    head_inc.setAttribute('type','text/javascript');
    head_inc.setAttribute('src', 'jslib/' + file + "?" + GLOBAL_getUniqueID());
    try {
      var tmp_text = document.createTextNode(' ');
      head_inc.appendChild(tmp_text);
    } catch (e) { }

    if (GLOBAL_lastInserted == null)
      Head.appendChild(head_inc);
    else
      Head.insertBefore(head_inc, GLOBAL_lastInserted)
    GLOBAL_lastInserted = head_inc;
  } catch (e) {
    //document.write('&lt;script type="text/javascript" src="' + file + '"><\/script>');
    document.write('<scr'+'ipt type="text/javascript" src="jslib/' 
                   + file + "?" + GLOBAL_getUniqueID() + '"><\/script>');
  }

  js_includedFiles[file] = 1;
}


//-- Dump DOM structure -------------------------------------------------------
function js_dumpDOMTree (node, level) {
  if (node == null) {
    node = document.getElementsByTagName('html')[0];
    level = 0;
  }

  if (node.tagName == null)
    return;

  for (var i = 0; i < (level * 4); i++)
    document.write('&nbsp;');
  document.write('&lt;' + node.tagName);

  var attributes = node.attributes;
  if (attributes != null)
    for (var i = 0; i < attributes.length; i++)
      if (attributes[i].nodeValue != '' && attributes[i].nodeValue != null)
        document.write(' ' + attributes[i].nodeName + '=\'' + attributes[i].nodeValue + '\'');

  var childs = node.childNodes;
  var childNumber = childs.length;

  if (childNumber == 0)
    document.write(' /');
  else {
    document.write('&gt;<br />');

    for (var i = 0; i < childNumber; i++)
      if (childs[i] != node) {
        js_dumpDOMTree(childs[i], level + 1);
      }

    for (var i = 0; i < (level * 4); i++)
      document.write('&nbsp;');
    document.write('&lt;/' + node.tagName);
  }

  document.write('&gt;<br />');
}

// ----------------------------------------------------------------------------

function js_serializeDOMTree (node, level) { // TODO: manage text nodes
  if(typeof(level) == 'undefined')
    level = 0;

  var DOM_s = "";

  if (node.tagName == null)
    return;

  DOM_s += "\n";
  for (var i = 0; i < (level * 4); i++)
    DOM_s += " ";
  DOM_s += "<" + node.tagName;

  var attributes = node.attributes;
  if (attributes != null)
    for (var i = 0; i < attributes.length; i++)
      if (attributes[i].nodeValue != '' && attributes[i].nodeValue != null)
        DOM_s += ' ' + attributes[i].nodeName + '="' + attributes[i].nodeValue + '"';

  var childs = node.childNodes;
  var childNumber = childs.length;

  if (childNumber == 0)
    DOM_s += ' /';
  else {
    DOM_s += ">";

    var onlyText = true;
    for (var i = 0; i < childNumber; i++)
      if (childs[i] != node) {
        if (childs[i].nodeType == 1) {
          DOM_s += js_serializeDOMTree (childs[i], level + 1);
          onlyText = false;
        } else if (childs[i].nodeType == 3)
          DOM_s += childs[i].nodeValue;
      }

    if (!onlyText) {
      DOM_s += "\n";
      for (var i = 0; i < (level * 4); i++)
        DOM_s += ' ';
    }
    DOM_s += '</' + node.tagName;
  }

  DOM_s += ">";

  return DOM_s;
}

// ============================================================================

// Browser Window Size and Position
// copyright Stephen Chapman, 3rd Jan 2005, 8th Dec 2005
// you may copy these functions but please keep the copyright notice as well
function win_pageWidth() {
  return window.innerWidth != null ? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
} 

function win_pageHeight() {
  return  window.innerHeight != null ? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body != null ? document.body.clientHeight : null;
} 

// ============================================================================

/* Client-side access to querystring name=value pairs
	Version 1.3
	28 May 2008
	
	License (Simplified BSD):
	http://adamv.com/dev/javascript/qslicense.txt
*/
function Querystring(qs) { // optionally pass a querystring to parse
	this.params = {};
	
	if (qs == null) qs = location.search.substring(1, location.search.length);
	if (qs.length == 0) return;

// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
	qs = qs.replace(/\+/g, ' ');
	var args = qs.split('&'); // parse out name/value pairs separated via &
	
// split out each name=value pair
	for (var i = 0; i < args.length; i++) {
		var pair = args[i].split('=');
		var name = decodeURIComponent(pair[0]);
		
		var value = (pair.length==2)
			? decodeURIComponent(pair[1])
			: name;
		
		this.params[name] = value;
	}
}

// ----------------------------------------------------------------------------

Querystring.prototype.get = function(key, default_) {
	var value = this.params[key];
	return (value != null) ? value : default_;
}

// ----------------------------------------------------------------------------

Querystring.prototype.contains = function(key) {
	var value = this.params[key];
	return (value != null);
}

// ============================================================================

function getY(oElement) {
  var iReturnValue = 0;

  while (oElement != null) {
    iReturnValue += oElement.offsetTop;
    oElement = oElement.offsetParent;
  }

  return iReturnValue;
}

// ----------------------------------------------------------------------------

function getX(oElement) {
  var iReturnValue = 0;

  while (oElement != null) {
    iReturnValue += oElement.offsetLeft;
    oElement = oElement.offsetParent;
  }

  return iReturnValue;
}

// ============================================================================

function isIE () {
  return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
}



