// tcgs javascript default functions

var TcgsVersion="0.4";

/*
 * if console is not defined (e.g., if Firebug console is 
 * not installed or not enabled or if it is a not a Firefox
 * browser) we make our own dummy console, that will not
 * break our console.log code.
 */
if (typeof console == 'undefined') {
    var console = {};
    console.log = function(dump_this_message) {
        return;
    };
}

function TcgsAbout() {
	alert("Tiny Content Generation System, Version " + TcgsVersion + ", (c) 2003-2010 hartmann it-design");
}

function TcgsNoop() {
}

function TcgsPopuUpWindow(url,name,height,width,attribs) {
	// menubar=yes|no
	// toolbar=yes|no
	// scrollbars=yes|no
	// locationbar=yes|no
	// directories=yes|no
	// statusbar=yes|no
	// status='foobar'
	// resizable=yes|no
	// width=200,height=150
	// screenX=100,screenY=100
	popupwindow=window.open(url,name,"");
}

function setActiveStyleSheet(title) {
  var i, a, main;
  for (i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if (a.getAttribute("title") == title) {
        a.disabled = false;
        createCookie("style", title, 365);
      }
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for (i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getFirstDefaultStyleSheet() {
  var i, a;
  for (i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if (a.getAttribute("rel").indexOf("stylesheet") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function getFirstDefinedStyleSheet() {
  var i, a;
  for (i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if (a.getAttribute("rel").indexOf("stylesheet") != -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var all_cookies = document.cookie.split(';');
  console.log('all cookies read: '+all_cookies);
  for (var i=0;i < all_cookies.length;i++) {
    var a_cookie = all_cookies[i];
    while (a_cookie.charAt(0)==' ') a_cookie = a_cookie.substring(1,a_cookie.length);
// for some onknown and weired reason we read a cookie although there
// is none defined. the value of this non existing cookie is null, so
// we will not accept null as a value ;)
    if (a_cookie.indexOf(nameEQ) == 0
       && a_cookie != name+'=null') {
      console.log('cookie(' + name + ') read: '+a_cookie);
      return a_cookie.substring(nameEQ.length,a_cookie.length);
    }
  }
  return false;
}

window.onload = function(e) {
  console.log('window.onload processing...');
  var cookie = readCookie("style");
  var first_defined_stylesheet = getFirstDefinedStyleSheet();
  console.log('first defined stylesheet: ' + first_defined_stylesheet);
  var title = cookie ? cookie : first_defined_stylesheet;
  setActiveStyleSheet(title);
  setupZoom(); // fancy zoom init
}

window.onunload = function(e) {
/*
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
*/
}
