// tcgs javascript default functions

var TcgsVersion="0.5";

// a dummy console if there is none

if (typeof console == 'undefined') {
    var console = {};
    console.log = function(dump_this_message) {
        return;
    };
}

function TcgsAbout() {
  alert("Tiny Content Generation System, Version " + TcgsVersion + ", (c) 2003-2011 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;
  console.log('setting style from title attribut: '+title+' ...');
  // loop all link elements
  for (i=0; (link = document.getElementsByTagName("link")[i]); i++) {
    // but operate on stylesheet elements only
    if (link.getAttribute("rel").indexOf("stylesheet") != -1 && link.getAttribute("title")) {
      // turn off
      link.disabled = true;
      link.setAttribute("rel",'alternate stylesheet');
      // turn on the one given as paramter
      if (link.getAttribute("title") == title) {
        link.setAttribute("rel",'stylesheet');
        link.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");
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) return a.getAttribute("title");
  }
  return null;
}

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

function getFirstStyleSheetTitle() {
  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=/';
  console.log('cookie set: '+name+"="+value+expires+'; path=/');
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

window.onunload = function(e) {
//  var title = getActiveStyleSheet();
//  createCookie("style", title, 365);
//  console.log ('setting style cookie: '+title);
}

window.onload = function(e) {
  var cookie = readCookie("style");
  console.log ('read style cookie: '+cookie);
  var title = cookie ? cookie : getFirstStyleSheetTitle();
  console.log ('calling style set: '+title);
  setActiveStyleSheet(title);
  setupZoom(); // fancy zoom init
}

// execute following code immediatly after this file has loaded. do not wait on page loading completed
var cookie = readCookie("style");
console.log ('read style cookie: '+cookie);
var title = cookie ? cookie : getFirstStyleSheetTitle();
console.log ('calling style set: '+title);
setActiveStyleSheet(title);
setupZoom(); // fancy zoom init

