//Relative path to the images...
var PATH_IMAGES = "img";
var MINIMUM_DATE = new Date(2000,4,17);
var MAXIMUM_DATE = new Date();
MAXIMUM_DATE.setDate(MAXIMUM_DATE.getDate() - 1);
var CORTE_DATE = new Date(2007,6,24);
var SECCIONES_ANTES_DE_CORTE = ["0", "portada", "afondo", "galicia", "opinion", "espana", "internacional", "maritima", "economia", "deportes", "sociedad", "culturaytv", "ultima", "tecnologia", "alba", "acoruna", "amarina", "arousa", "barbanza", "carballo", "dezatabeiros", "ferrol", "lemos", "lugo", "ourense", "pontevedra", "santiago", "vigo", "madrid","carballino","valdeorras","dinero","motor"];
var SECCIONES_DESPUES_DE_CORTE = ["0", "portada", "afondo", "galicia", "opinion", "espana", "mundo", "maritima", "economia", "deportes", "sociedad", "culturaytv", "ultima", "tecnologia", "alba", "coruna", "amarina", "arousa", "barbanza", "carballo", "dezatabeiros", "ferrol", "lemos", "lugo", "ourense", "pontevedra", "santiago", "vigo", "madrid","carballino","valdeorras","dinero","motor"];
var PORTADA_ANTES_DE_CORTE = "portada";
var PORTADA_DESPUES_DE_CORTE = "inicio";
var URL_ANTES_DE_CORTE = "http://www.lavozdegalicia.es/hemeroteca/";
var URL_DESPUES_DE_CORTE = "http://www.lavozdegalicia.es/";

function Maximun_date(ano,mes,dia)
{
	MAXIMUM_DATE=new Date(ano,mes,dia);
	MAXIMUM_DATE.setDate(MAXIMUM_DATE.getDate() - 1);
}

	function es2gal(url){
		return url;
	}

function select_date(iYear, iMonth, iDay,iSeccion, iHora)
{
	var parameters;
	fecha_pasada= new Date(iYear, iMonth-1, iDay);
	var ceroDay = "";
	var ceroMonth = "";
	if (iDay < 10 ) 
		ceroDay = "0";
	if (iMonth < 10 ) 
		ceroMonth = "0";
	
	//MGP, ahora se puede seleccionar maņana, tarde y noche
	var iHour = "manana";
	if (iHora!=null){
		switch (iHora){
			case 1:
			  iHour = "manana";
			  break;
			case 2:
			  iHour = "tarde";
			  break;
			case 3:
			  iHour = "noche";
			  break;
			case "1":
			  iHour = "manana";
			  break;
			case "2":
			  iHour = "tarde";
			  break;
			case "3":
			  iHour = "noche";
			  break;
		}
		
	}
	var home = URL_ANTES_DE_CORTE  + SECCIONES_ANTES_DE_CORTE[iSeccion] + "/" + iYear + "/" + ceroMonth + iMonth + "/" + ceroDay + iDay + "/index.shtml";
	if (fecha_pasada >= CORTE_DATE) 
		home = URL_DESPUES_DE_CORTE +  SECCIONES_DESPUES_DE_CORTE[iSeccion] + "/" + iYear + "/" + ceroMonth + iMonth + "/" + ceroDay + iDay + "/" + iHour + "/index.htm";
    	if (fecha_pasada<=MAXIMUM_DATE && fecha_pasada>=MINIMUM_DATE){
		window.location = es2gal(home);
	}else{
		alert ("La fecha seleccionada no es correcta");
	}	
}

function get_prev_month(iYear, iMonth)
{
  if(iMonth == -1)
    return 11;
  else
    return iMonth-1;
}

function get_prev_year(iYear, iMonth)
{
 if(iMonth == -1)
  return iYear-1;
 else
  return iYear;
}

function modify_prev_month(iYear, iMonth)
{
  if(iMonth == -1)
    return 11;
  else
    return iMonth;
}

function modify_next_month(iYear, iMonth)
{
  if(iMonth == 11)
    return 0;
  else
    return iMonth;
}

function get_next_month(iYear, iMonth)
{
  if(iMonth == 11)
    return 0;
  else
    return (parseInt(iMonth,10)+1);
}

function get_next_year(iYear, iMonth)
{
  if(iMonth == 11)
    return iYear+1;
  else
    return iYear;
}

//Compare if date1 is before date2
function isBefore(date1,date2){
 if (date1.getTime() < date2.getTime()) return true;
 else return false;
}

//Compare if date1 is after date2
function isAfter(date1,date2){
 if (date1.getTime() > date2.getTime()) return true;
 else return false;
}

function change_month(iYear, iMonth, sOption)
{
  button = document.getElementById("next_month_button");
  script_link = "javascript:change_month("+get_next_year(iYear, iMonth)+","+get_next_month(iYear, iMonth)+",'next')"
  button.setAttribute("href", script_link);
  button = document.getElementById("prev_month_button");
  script_link = "javascript:change_month("+get_prev_year(iYear, iMonth)+","+get_prev_month(iYear, iMonth)+",'prev')"
  button.setAttribute("href", script_link);
  switch (sOption){
 case 'next':
  //iYear = get_next_year(iYear, iMonth);
  //iMonth = modify_next_month(iYear,iMonth);
  break;
 case 'prev':
  iYear = get_prev_year(iYear, iMonth);
  iMonth = modify_prev_month(iYear,iMonth);
  break;
  }
  show_month(iYear, iMonth);
  prev_month_button(iYear, iMonth);
  next_month_button(iYear, iMonth);
  show_month_name(iYear, iMonth);
}

function show_month_name(iYear, iMonth)
{
  month_names = ["Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio",
                 "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"];
  mybody = document.getElementById("navega");
 if(!mybody)
  mybody = document.getElementsByTagName("body").item(0);
 text = document.createTextNode(month_names[iMonth]+" "+iYear);
 mybody.appendChild(text);
}

  
function next_month_button(iYear, iMonth)
{
  mybody = document.getElementById("navega");
 if(!mybody)
  mybody = document.getElementsByTagName("body").item(0);
 button = document.createElement("a");
 button.setAttribute("id", "next_month_button");
 button.setAttribute("class","boton sig");
 button.className="boton sig";
 button.setAttribute("href", "javascript:change_month("+get_next_year(iYear, iMonth)+","+get_next_month(iYear, iMonth)+",'next')");
 spantag = document.createElement("span");
 spantag.className="no";
 span_text = document.createTextNode("Mes siguiente");
 spantag.appendChild(span_text);
 button.appendChild(spantag);
  mybody.appendChild(button);
}

function prev_month_button(iYear, iMonth)
{
  mybody = document.getElementById("navega");
  if(!mybody)
    mybody = document.getElementsByTagName("body").item(0);
  button = document.createElement("a");
  button.setAttribute("id", "prev_month_button");
  button.setAttribute("class","boton ant");
  button.className="boton ant";
  button.setAttribute("href", "javascript:change_month("+get_prev_year(iYear, iMonth)+","+get_prev_month(iYear, iMonth)+",'prev')");
  spantag = document.createElement("span");
  spantag.className="no";
  span_text = document.createTextNode("Mes anterior");
  spantag.appendChild(span_text);
  button.appendChild(spantag);
  mybody.appendChild(button);
}

function days_in_month(iYear, iMonth)
{
  switch (iMonth) {
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
      iLargoMes = 31;
    break;
    case 4:
    case 6:
    case 9:
    case 11:
      iLargoMes = 30;
    break;
    case 2:
      if (iYear % 4 == 0)
        iLargoMes = 29;
      else
        iLargoMes = 28;
    break;
  }
  return iLargoMes;
}

function show_month(iYear, iMonth)
{
  mybody = document.getElementById("c1");
  if(mybody == null)
    mybody = document.getElementsByTagName("body").item(0);
  day_names = ["L", "M", "Mi", "J", "V", "S", "D"];
  day_styles = ["lun", "mar", "mer", "xov", "ven", "sab", "dom"];
  var i=0;
  link1 = "";
  texto = "";
  max_day = days_in_month(iYear, iMonth+1);
  date = new Date(iYear, iMonth, i);
  day = 1;
  mytable = document.createElement("table");
  mytable.setAttribute("id", "calendario");
  mytable.setAttribute("summary", "Calendario");
  mytable.className="cal";
  
  mytablecaption = document.createElement("caption");
  mytablespan = document.createElement("span");
  mytablespan.setAttribute("id", "navega");
  mytablespan.className="nav";
  
  mytablecaption.appendChild(mytablespan);
  
  mytable.appendChild(mytablecaption);
  
  colgroup_one = document.createElement("colgroup");
  colgroup_one.setAttribute("span", "5");
  colgroup_one.className="labo";
  colgroup_one_text = document.createTextNode("&nbsp;");
  colgroup_one.appendChild(colgroup_one_text);
  mytable.appendChild(colgroup_one);
  
  colgroup_two = document.createElement("colgroup");
  colgroup_two.setAttribute("span", "2");
  colgroup_two.className="finde";
  colgroup_two_text = document.createTextNode("&nbsp;");
  colgroup_two.appendChild(colgroup_two_text);
  mytable.appendChild(colgroup_two);
  
  mythead = document.createElement("thead");
  mythead_tr= document.createElement("tr");  
  mythead_tr_th= document.createElement("th");
  mythead_tr_th.setAttribute("id", "sem");
  mythead_tr_th.className="no";
  for(var i = 0; i< 7; i++) {
  	mythead_tr_th= document.createElement("th");
  	mythead_tr_th.setAttribute("id", day_styles[i]);
  	mythead_tr_th_abbr= document.createElement("abbr");
  	mythead_tr_th_abbr.setAttribute("lang", "es");
  	mythead_tr_th_abbr.setAttribute("title", day_names[i]);
  	mythead_tr_th_abbr_text = document.createTextNode(day_names[i]);
  	mythead_tr_th_abbr.appendChild(mythead_tr_th_abbr_text);
  	mythead_tr_th.appendChild(mythead_tr_th_abbr);
  	mythead_tr.appendChild(mythead_tr_th);
  }
  mythead.appendChild(mythead_tr);
  mytable.appendChild(mythead);
  
  mytbody = document.createElement("tbody");
  
  //MGP, ahora se puede seleccionar maņana, tarde y noche (maņana = 1 por defecto)
  var horaSeleccionada = "1";
  var hora = document.getElementById("selecthora");
  if (hora!=null){
	horaSeleccionada = hora.options[hora.selectedIndex].value;
  }
	
  for(var j = 0; j < 6 ; j++) {
  	mycurrent_row= document.createElement("tr");  
	for(var i = 0; i< 7; i++) {
		if((day > 1 || (day==1 && i >= date.getDay())) && day <= max_day) {
    			var dateToValidate = new Date(iYear,iMonth,day);
    			if ((!isBefore(dateToValidate,MINIMUM_DATE)) && (!isAfter(dateToValidate,MAXIMUM_DATE))){
     				link1 = "javascript:select_date("+iYear+", "+(parseInt(iMonth,10)+1)+", "+day+",1,"+horaSeleccionada+")";
     			}else{
     				link1 = "";
    			}
    			texto = day;
    			day += 1;
      		} else {
        		link1 = "";
        		texto = ".";

      		}
      		currenttext = document.createTextNode(texto);
      		if (link1.length >0){
    			day_cell = document.createElement("a");
    			day_cell.setAttribute("href", link1);
   		}else{
    			day_cell = document.createElement("p");
   		}
   		if (i==5){
    			day_cell.setAttribute("class","sab");
    			day_cell.className="sab";
   		}
   		if (i==6){
    			day_cell.setAttribute("class","dom");
    			day_cell.className="dom";
   		}
   		if (texto=="."){
   			day_cell.style.visibility ="hidden";
   		} 
   		day_cell.appendChild(currenttext);
	   	
	  	mycurrent_col= document.createElement("td"); 
	   	mycurrent_col.setAttribute("headers", day_names[i]);
	   	mycurrent_col.appendChild(day_cell); 
	   	mycurrent_row.appendChild(mycurrent_col);
    	}
    	mytbody.appendChild(mycurrent_row);
  }        
 
  mytable.appendChild(mytbody);
  prev = document.getElementById("calendario");
  if(prev != null)
    mybody.replaceChild(mytable, prev);
  else
    mybody.appendChild(mytable);
  mytable.cellPadding=0;
  mytable.cellSpacing=0;
}

function show_calendar()
{
  today = new Date();
  today.setDate(today.getDate() - 1);
  show_month(today.getFullYear(), today.getMonth());
  prev_month_button(today.getFullYear(), today.getMonth());
  next_month_button(today.getFullYear(), today.getMonth());
  show_month_name(today.getFullYear(), today.getMonth());
}