/*
* page.js
* Page-specific functions.
*/

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 var expires = "";
  document.cookie = 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;
}

function eraseCookie(name) {
  createCookie(name,"",-1);
}


function makeHomePage(obj) {
  try {
    obj.style.behavior='url(#default#homepage)'; 
    obj.setHomePage(location.href);
  }
  catch (ex) { }
}

// ---------------------------------------------------------------------------------

Page = {};

var boxes_loaded = false;


Page.Save = function () {
  document.body.style.cursor= "Wait";
  //alert("Paxinas SAVE");
  //alert (MM.getXML());
  //alert(escape(MM.getXML()));
  makeXMLRequest('loadsave.php?save', handlePageSaveResponse, "xml="+escape(MM.getXML()));
}


Page.Load = function () {
  if ((readCookie('Paxinas')) && (boxes_loaded ==false))  {
    // request previously saved page:
    //alert("Paxinas");
    makeXMLRequest('loadsave.php?load', handlePageLoadResponse);
    boxes_loaded = true;
  }
  else if (boxes_loaded == false){
    Page.LoadDefault();
    boxes_loaded = true;
  }
}

Page.Restore = function () {
  if (confirm ("¿Desexa volver á configuración orixinal?")) {
    //alert("Paxinas RESTORE");
    makeXMLRequest('loadsave.php?restore', handlePageRestoreResponse);
  }
}

Page.LoadDefault = function () {

  //var module = new Module("Sobre esta p&aacute;xina", "a.usage.1");
  //module.defaultExpand = true;
  //MM.addModule(module, 2);

  //var module = new Module("Desenvolvido por", "a.who");
  //module.defaultExpand = true;
  //MM.addModule(module, 2);

  //MM.addModule(new Module("Contido de exemplo", "a.exemplo"), 1);
  //MM.addModule(new Module("Galicia", "ns.LaVozDeGalicia.Portada"), 1);
  //
  //MM.addModule(new Module("Paxinas - Santiago de Compostela", "ns.Paxinas.Santiago"), 1);
  //MM.addModule(new Module("Paxinas - Cultura", "ns.Paxinas.Cultura"), 1);
  MM.addModule(new Module("Galicia - La Voz de Galicia", "ns.LaVozdeGalicia.Galicia"), 1);
  //MM.addModule(new Module("Galicia - P&aacute;xinas", "ns.Paxinas.Galicia"), 1);
  MM.addModule(new Module("Portada - Faro de Vigo", "ns.FaroDeVigo.Portada"), 1);
  MM.addModule(new Module("&Uacute;ltima Hora - El Correo Gallego", "ns.CorreoGallego.UltimaHora"), 1);

  MM.addModule(new Module("Novas da Xunta de Galicia", "bl.Xunta"), 2);
  MM.addModule(new Module("C&oacute;digo Cero", "ns.CodigoCero.Portada"), 2);
  MM.addModule(new Module("Sociedade da Informaci&oacute;n", "bl.SociedadeDaInformacion"), 2);

  MM.addModule(new Module("Cultura Gallega", "ns.CulturaGalega.EdicionXeral"), 3);
  MM.addModule(new Module("Br&eacute;temas", "bl.Bretemas"), 3);
  MM.addModule(new Module("Microsiervos", "bl.Microsiervos"), 3);
  //MM.addModule(new Module("Vieiros-&Uacute;ltima Hora", "bl.VieirosUltimaHora"), 3);
  //MM.addModule(new Module("Periodistas 21", "bl.Periodistas21"), 3);
  //MM.addModule(new Module("Juan Freire", "bl.JuanFreire"), 3);
  //MM.addModule(new Module("Enrique Dans", "ns.Blogs.EnriqueDans"), 2);
  //MM.addModule(new Module("SGHN - Ferrol", "ns.Blogs.SGHN"), 3);
  //MM.addModule(new Module("Marcus", "ns.Blogs.Marcus"), 3);

 // MM.addModule(new Module("&iquest;Qu&eacute; &eacute; Paxinas?", "a.about"), 3);
 // MM.addModule(new Module("C&oacute;ntanos a t&uacute;a experiencia en Paxinas.com", "a.feedback"), 3);
  //MM.addModule(new Module("Contacte connosco", "a.contact"), 3);
//  MM.addModule(new Module("O equipo", "a.who"), 3);
  
}

// ---------------------------------------------------------------------------------

/* Shows or Hides the Addcontents section. */
function showAC(yes) {
  if (yes) {
    ge("AC").style.display = "block";
    ge("ACLink").style.display = "none";
  }
  else {
    ge("AC").style.display = "none";
    ge("ACLink").style.display = "block";
  }
}


function initpage(){

   // Hide AddContents section:
   if (boxes_loaded == false)
   {
     showAC (false);

     // Creates adcontent tree
     initACtree();
     // Creates initial modules
     //   MM.initialize();
     //buildModules();

     Page.Load();
     MM.render("modules");

   // start refreshing of module contents:
     MMRefresh();
   }
}

function submitBetaTesterForm(){

   // Kill submit button
   ge('submit').disabled = true;
   ge('submit').value = 'submitting...';
   // Grab Email
   var email = ge('email').value;
   callBooleanFunction('x.php?email='+email, foo);
   return false;
}

function handlePageSaveResponse (responseText, responseXML) {
  try {
    //alert ("ResponseTEXT:"+responseText);
    //alert ("ResponseXML:"+responseXML);
    document.body.style.cursor= "auto";
    sid = responseXML.getElementsByTagName('sid')[0].firstChild.data;
    //alert(sid);
    //   createCookie ('Paxinas', sid, 365*10); 
    alert ("A súa páxina foi gardada con éxito"); 
  }
  catch(ex) {
    //alert (ex);
    alert ("Sentímolo - houbo un erro gardando a súa paxina.");
  }
}


function handlePageLoadResponse (responseText, responseXML) {

  try {
  //  alert (responseText);
    mm = responseXML.getElementsByTagName('MM')[0]; 
    MM.buildFromXML (mm); 
    MM.render("modules");
  }
  catch(ex) {
    //alert ("Exception in handlePageLoadResponse:" + ex);
    //alert ("Response was:\n" + responseText);
    Page.LoadDefault();
  }
}

function handlePageRestoreResponse (responseText, responseXML) {
  window.location.reload();
  //document.body.style.cursor= "auto";
}

function handlePageStateOpenResponse (responseText, responseXML) {
  // do nothing
}

function handlePageStateCloseResponse (responseText, responseXML) {
  // do nothing
}


function handleContentUpdate(responseText, responseXML){
  //alert (responseText);
  //showProps (responseXML);
  //showProps(responseXML.getElementsByTagName('script')[0].firstChild);
  try {
    id =  responseXML.getElementsByTagName('id')[0].firstChild.data;
    content = responseXML.getElementsByTagName('content')[0].firstChild.data;
    var module = MM.modules[id];
    //alert ("content:" + content);
    try {
      title =  responseXML.getElementsByTagName('title')[0].firstChild.data;
      if (title) module.setTitle(title);
    }
    catch(ex) {}
    try {
      script =  responseXML.getElementsByTagName('script')[0].firstChild.data;
      //  if (script) alert (script);
      if (script) eval (script);
    }
    catch(ex) {}
    try {
      refresh = responseXML.getElementsByTagName('refresh')[0].firstChild.data;
      if (refresh) module.setRefresh(refresh);
    }
    catch(ex) {}
  }
  catch (ex) {
    //TODO:Get speficic exception(TypeError:responseXML.getElementsByTagName is not a function)***********
    //alert (ex);
  }              
//  alert (content);
  if (module) if (content)  module.setContent(content);
}



