var m_currentW = 0;
var m_seleccionado;
var jsc = new Array();
var m_wHeight = 350;
var loaded = new Array();
var m_currentC = new Array();
for (i=0; i<7; ++i) {
  loaded[i] = false;
  m_currentC[i] = 0;
}
loaded[0] = true;

function htmlScape(s) {
// return s.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
return s;
};

function hlog(s) {
  $("#log").append(htmlScape(s)+"<hr/>");
}

function loadcRight( c ) {
  $("#c"+m_currentW).html( jsc[m_currentW][c] );
  $("#c"+m_currentW).DropInRight( 'slow' );

}

function loadcLeft( c ) {
  $("#c"+m_currentW).html( jsc[m_currentW][c] );
  $("#c"+m_currentW).DropInLeft( 'slow' );

}


function loadc( c ) {
  if (typeof(jsc[m_currentW]) == "undefined") return;
  if (jsc[m_currentW].length == 0) return;

  if (c < 0) c = jsc[m_currentW].length -1;
  else if (c >= jsc[m_currentW].length) c = 0;
  
  if ( m_currentC[m_currentW] < c ) {
    $("#c"+m_currentW).DropOutLeft( 'slow', function(){
      setTimeout("loadcRight("+c+")", 200);
    });
  } else {
    $("#c"+m_currentW).DropOutRight( 'slow', function(){
      setTimeout("loadcLeft("+c+")", 200);
    });
  }
  
  $("#goto"+m_currentW+"_"+m_currentC[m_currentW]).css("border-color", "#222222");
  $("#goto"+m_currentW+"_"+c).css("border-color", "#ffffff");
  m_currentC[m_currentW] = c;
}

function stopPage(e) {
  cancelEvent(e);  //this function stop links click event
  stopEvent(e);  //this function stop links click event
  return false;
}
function createNav(w) {
  if (typeof(jsc[w]) == "undefined") return;
  if (jsc[w].length < 2) return;
  // $("<html>") crea un html, justo despues le añade la propiedad click.
  // $("<html>").click(fn()) es un objeto jquery añadido despues de #mw
    $("#m"+w).after($("<a href='#.' class='navprev'>&#060;&#060;&#060;</a>").click( function(){ loadc( parseInt(m_currentC[m_currentW])-1 ) })) ;
    $("#m"+w).after($("<a href='#.' class='navnext'>&#062;&#062;&#062;</a>").click( function(){ loadc( parseInt(m_currentC[m_currentW])+1 ) }));

  var nav=$("<div class='wnav' id='nav"+w+"'></div>");
//   nav.html("");
  for( i=0; i<jsc[w].length; ++i ){
    nav.append("<a href='#.' id='goto"+m_currentW+"_"+i+"'>"+i+"</a>");
  }
  $("#wm"+w).append(nav);
  $("#nav"+w+" a").click( function() {
    var c = $(this).attr("id").substr(6);
    loadc( c );
  });
}

function initW(xml){
  var wxml = $(xml).find('window');
  var w = parseInt(wxml.attr('w'));
//   hlog("initW: "+w);
  jsc[w] = new Array();
  loaded[w] = true;
  wxml.find('content').each( function() {
    jsc[w][$(this).attr('c')] = $(this).text()
  });
  createNav(w);
  $("#c"+w).html(jsc[w][0]);
}

function aerror(xml) {
hlog("aerror: "+xml);
}

function setupM(){
//   alert( m_seleccionado );
  $("a.m").mouseover( function(){ $(this).fadeTo( "fast", 1.0 ); });
  $("a.m").mouseout( function(){
    if ($(this).attr("id") != m_seleccionado.attr("id") ) $(this).fadeTo( "fast", 0.6 );
  });
  $("a.m").removeAttr("href");
}

$(document).ready( function() {
  m_seleccionado = $("#m0");
  
$("#contenedor").fadeTo( "fast", 1.0, function(){  
    $("a.m").not("#m0").fadeTo( "fast", 0.6, function() { $("#wm0").animate( {height: m_wHeight}, "slow"); });
  });
  
  $.ajax({
    type: "GET",
    url: "c.xml.php?w="+m_currentW,
    dataType: "xml",
    async:true,
    success: initW,
    error:aerror
  });
  setTimeout("setupM()", 1500);
  
  $("a.m").bind("click", function(){
    if ($(this).attr("id") == m_seleccionado.attr("id") ) return;
    m_seleccionado.fadeTo( "fast", 0.6 );
    m_currentW = $(this).attr("id").charAt(1);
//     hlog("a.m click: "+m_currentW+" / "+loaded[m_currentW]);
    if(!loaded[m_currentW]){
      $("#c"+m_currentW).html("<br/><br/><br/><br/><img src='img/ajax-load.gif' alt='cargando' />");
      $.ajax({
        type: "GET",
        url: "c.xml.php?w="+m_currentW,
        dataType: "xml",
        async:true,
        success: initW,
        error:aerror
      });
     }
    $("#w"+m_seleccionado.attr("id")).animate( {height: 30}, "slow" ); 
    m_seleccionado = $(this);
    $("#w"+$(this).attr("id")).animate( {height: m_wHeight}, "slow" ); 
  });
});