// JavaScript Document



var numMeses, enlace, extension, fechaHoy,este, difMes, testMes, testSelec, menosYear;
function calendario(seleccionado, mostrar_finde){
	
	
	/* CONFIGURACION LINKS: Queda: enlace+YYYY-MM-DD+extesion, por ejemplo: diario_detalle.php?dia=2009-12-06 (con extension="";) */
	enlace="canal_ymedia_diario_detalle.php?dia=";
	extension="";
	
	
	document.write('<div id="calendario">');
	initCal(seleccionado, mostrar_finde);
	document.write('</div>');
	muestraCal('init');
	este=seleccionado;

}



function getEl(elementId){
	if (document.getElementById(elementId)){
		return document.getElementById(elementId);
	}
	else{
		alert ('No encuentro el elemento '+elementId);
	}
}


// Cambia la class del elem id por clase, y anula las class de los posibles span
function rollover(id,clase) {
	// Agrego la class
	id.className+=" "+clase;
	// Saco las class de los posibles span
	subCLass=id.getElementsByTagName('span');
	for (i=0; i<subCLass.length; i++) {
			subCLass[i].className+="q";
	}
}
// Restaura la funcion rollover
function rollout(id,clase) {
	// Compruebo si es o no class unica
	if (id.className==" "+clase){
		classAnt=" "+clase;
	}
	else{
		classAnt=clase;
	}
	// Saco la class que agregue
	id.className=id.className.replace(classAnt, "");
	// Restauro las clss de los span
	subCLass=id.getElementsByTagName('span');
	for (i=0; i<subCLass.length; i++) {
			subCLass[i].className=subCLass[i].className.replace("q", "");
	}
}

function calendario_tmp(){
	document.write('<img src="images/canal/calendario_tmp.jpg" />');
}

//variables globales para muestra lista
var yaInit = new Array()
var indexInit = 0;
var text = new Array(); 
var cant = new Array();
var currPos = new Array();
var init=true;
var currIndex;
function muestraLista(id ,numero){
	
	//numeros
	if (document.getElementById(numero)){
			var num = document.getElementById(numero);
			var nums = num.getElementsByTagName('a');
	}
	
	//traigo el elemento
	var cont = document.getElementById(id);
	//traigo los tags
	lista = cont.getElementsByTagName('ul');
	//traigo  las flechas 
	flechas = lista[1].getElementsByTagName('li');
	var prev = flechas[0];
	var next = flechas[1];
	//Armo un array con los ya inicializados y me fijo si hay alguno inicizlizado
	indexa(id);
	if(init){
		currIndex=indexInit;
		yaInit[currIndex]=id;
		currPos[currIndex]=0;
		indexInit++;
	}
	//tags a mostrar
	//text[currIndex] = lista[1].getElementsByTagName('li');
	text[currIndex] = traeElem(lista[0],'LI');
	//cant de tags y ultimo elem
	cant[currIndex] = text[currIndex].length;
	//inicializo
	if (init){
		//if (cant!=0){
			text[currIndex][currPos[currIndex]].style.display = "block";
			//prev.style.display = "none";
			//if (cant==1){
				//next.style.display = "none";
			//}
		//}
		init=true;
	}
	//hizo click en flecha derecha
	prev.onmousedown = function() {
		indexa(id);
		ultPos=cant[currIndex]-1;
		if (currPos[currIndex]!=0){
			text[currIndex][currPos[currIndex]].style.display = "none";
			currPos[currIndex]--;
			text[currIndex][currPos[currIndex]].style.display = "block";
			/*if (currPos[currIndex]==0){
				prev.style.display = "none";
				next.style.display = "block";
			}
			else{
				prev.style.display = "block";
				next.style.display = "block";
			}*/
		}
		else{
			text[currIndex][currPos[currIndex]].style.display = "none";
			currPos[currIndex]=ultPos;
			text[currIndex][currPos[currIndex]].style.display = "block";
			//next.style.display = "none";
		}
	}
	//hizo click en flecha izquierda
	next.onmousedown = function() {
		indexa(id);
		ultPos=cant[currIndex]-1;
		if (currPos[currIndex]!=ultPos){
			text[currIndex][currPos[currIndex]].style.display = "none";
			currPos[currIndex]++;
			text[currIndex][currPos[currIndex]].style.display = "block";
			/*if (currPos[currIndex]==ultPos){
				next.style.display = "none";
				prev.style.display = "block";
			}
			else{
				next.style.display = "block";
				prev.style.display = "block";
			}*/
		}
		else{
			text[currIndex][currPos[currIndex]].style.display = "none";
			currPos[currIndex]=0;
			text[currIndex][currPos[currIndex]].style.display = "block";
			//next.style.display = "none";
		}
	}
	//hizo click en un numero
	if (document.getElementById(numero)){
		for (j=0;j<nums.length;j++){
			thisNum=nums[j];
			thisNum.onmousedown = function() {
				indexa(id);
				var numPos;
				for(k=0;k<nums.length;k++){
					if(nums[k]==this){
						numPos=k;
					}
				}
				text[currIndex][currPos[currIndex]].style.display = "none";
				currPos[currIndex]=numPos;
				text[currIndex][currPos[currIndex]].style.display = "block";
			}
		}
	}
}

function indexa(id){
	num=yaInit.length;
	i=0;
	while (i<=num){
		if (id==yaInit[i]){
			init=false;
			currIndex=i;
		}
		i++;
	}
}

function traeElem(id,elem){
		var elemNP = new Array();
		contened = id;
		j=0;
		for (i=0; i<contened.childNodes.length; i++) {
			node = contened.childNodes[i];
			if (node.nodeName==elem) {
				elemNP[j]=node;
				j++;
			}
		}
		return elemNP;
}



numMeses=26; 
menosYear=2;
var eventoDia = new Array()
var eventoMes = new Array()
var eventoTipo = new Array()
var eventoTexto = new Array()
var eventoLink = new Array()
var nuevoId = 0


var hoy = new Date()
var thisYear = hoy.getFullYear()
if (thisYear < 1000)
thisYear-=100
var thisMonth = hoy.getMonth()
function initCal(seleccionado, mostrar_finde){
	
document.write('<div class="navCal">');
document.write('<div class="navCalBtns"><a href="javascript:muestraCal(\'pre\');" onmouseover="swapImage(this,\'images/canal/left_click.jpg\',\'morado\')" onmouseout="restoreImage()"><img src="images/canal/left.jpg" name="flechaLeft" border="0" class="floatLeft" id="flechaLeft" /></a><a href="javascript:muestraCal(\'pos\');" onmouseover="swapImage(this,\'images/canal/right_click.jpg\',\'morado\')" onmouseout="restoreImage()"><img src="images/canal/right.jpg" name="flechaRight" border="0" class="floatRight" id="flechaRight" /></a></div>');
document.write('</div>');
	//for (i=thisMonth; i<thisMonth+numMeses; i++){
		cual=0;
		n=0;
		mes=thisMonth;
		agno=thisYear;
		agno-=menosYear;
		while (n<numMeses){
		if (mes>11){
			mes=0;
			agno+=1;
		}
		/*if (agno>2000){
			agno-=2000;
		}
		agno='0'+agno*/
		if (cual==2){
				//alert('llamo a setCal')
			setCal(mes,agno,2,seleccionado, mostrar_finde);
		}
		else{
			setCal(mes,agno,1,seleccionado, mostrar_finde);
		}
		mes++;
		n++;
	}
}
//calendario();
function getTime() {
var now = new Date()
var hour = now.getHours()
var minute = now.getMinutes()
alert (now);
now = null
var ampm = "" 

if (hour >= 12) {
hour -= 12
ampm = "PM"
} else
ampm = "AM"
hour = (hour == 0) ? 12 : hour

if (minute < 10)
minute = "0" + minute // do not parse this number!

return hour + ":" + minute + " " + ampm
}

function leapYear(year) {
if (year % 4 == 0) // basic rule
return true // is leap year
return false // is not leap year
}

function getDays(month, year) {
var ar = new Array(12)
ar[0] = 31 // January
ar[1] = (leapYear(year)) ? 29 : 28 // February
ar[2] = 31 // March
ar[3] = 30 // April
ar[4] = 31 // May
ar[5] = 30 // June
ar[6] = 31 // July
ar[7] = 31 // August
ar[8] = 30 // September
ar[9] = 31 // October
ar[10] = 30 // November
ar[11] = 31 // December

return ar[month]
}

function getMonthName(month) {
var ar = new Array(12)
ar[0] = "Enero"
ar[1] = "Febrero"
ar[2] = "Marzo"
ar[3] = "Abril"
ar[4] = "Mayo"
ar[5] = "Junio"
ar[6] = "Julio"
ar[7] = "Agosto"
ar[8] = "Septiembre"
ar[9] = "Octubre"
ar[10] = "Noviembre"
ar[11] = "Diciembre"

return ar[month]
}

function setCal(month,year,cual,seleccionado, mostrar_finde) {
	
	//alert('en setCal')
var now = new Date()
//alert(now.getMonth())
/*
var year = now.getYear()
if (year < 1000)
year+=1900
year=2010
var month = now.getMonth()
*/
var monthName = getMonthName(month)
var date = now.getDate()
now = null

var firstDayInstance = new Date(year, month, 0)
var firstDay = firstDayInstance.getDay()
firstDayInstance = null

/*
alert (year)
alert (month)
alert (firstDay)
*/

var days = getDays(month, year)

if(cual==2){
	//alert('llamo a drawCal2')
	drawCal2(firstDay + 1, days, date, monthName, year, month,seleccionado, mostrar_finde)
}
else{
	drawCal(firstDay + 1, days, date, monthName, year, month,seleccionado, mostrar_finde)
}
}

function drawCal(firstDay, lastDate, date, monthName, year, month,seleccionado, mostrar_finde) {
	
var headerHeight = 50 // height of the table's header cell
var border = 0 // 3D height of table's border
var cellspacing = 0 // width of table's border
var headerColor = "midnightblue" // color of table's header
var headerSize = "+3" // size of tables header font
var colWidth = 30 // width of columns in table
var dayCellHeight = 15 // height of cells containing days of the week
var dayColor = "darkblue" // color of font representing week days
var cellHeight = 20 // height of cells representing dates in the calendar
var todayColor = "red" // color specifying today's date in the calendar
var timeColor = "purple" // color of font representing current time

var diaDest = new Array()
var tipoDest = new Array()
var textDest = new Array()
var linkDest = new Array()
j=0;
for (m=0; m<eventoMes.length; m++){
	if (eventoMes[m]==month+1){
		diaDest[j]=eventoDia[m];
		tipoDest[j]=eventoTipo[m];
		textDest[j]=eventoTexto[m];
		linkDest[j]=eventoLink[m];
		j++;
	}
}

if (year>2000){
	year-=2000;
}
if (year<10){
	year='0'+year;
}
//year=year+'.gif';
var text = "" // initialize accumulative variable to empty string
text += '<li style="display:none">'
text += '<p align="center">'+monthName+'.'+year+'</p>'
text += '<TABLE BORDER=' + border + '  align="center" CELLSPACING=' + cellspacing + '>' // table settings
//text += '<TH COLSPAN=7 HEIGHT=' + headerHeight + '>' // create table header cell
//text += '<FONT COLOR="' + headerColor + '" SIZE=' + headerSize + '>' // set font for table header
//text += '</FONT>' // close table header's font settings
//text += '</TH>' // close header cell


var weekDay = new Array(7)
weekDay[0] = "D"
weekDay[1] = "L"
weekDay[2] = "M"
weekDay[3] = "X"
weekDay[4] = "J"
weekDay[5] = "V"
weekDay[6] = "S"

/*
text += '<TR>'
for (var dayNum = 0; dayNum < 7; ++dayNum) {
	if (dayNum==6){
		text += '<TD>' + weekDay[dayNum] + '</TD>' 
	}
	else{
		text += '<TD class="bordeRight">' + weekDay[dayNum] + '</TD>' 
	}
}
text += '</TR>'
*/
text += '<TR>'
for (var dayNum = 0; dayNum < 7; ++dayNum) {
	if (dayNum==6){
		text += '<TD style="height:1px"></TD>' 
	}
	else{
		text += '<TD></TD>' 
	}
}
text += '</TR>'

var digit = 1
var curCell = 1

for (var row = 1; row <= Math.ceil((lastDate + firstDay - 1) / 7); ++row) {
text += '<TR>'
for (var col = 1; col <= 7; ++col) {
if (digit > lastDate)
break


if (curCell < firstDay) {
text += '<TD>&nbsp;</TD>';
curCell++
} else {

dest=false;	
for (k=0;k<diaDest.length;k++){
	if(diaDest[k]==digit){
		dest=true;
		destCale='destCal' + tipoDest[k];
		destText=textDest[k];
		destLink=linkDest[k];
		break;
	}
}

var now = new Date()
var dia = now.getDate()
//alert (dia);
if (dia < 10) dia='0'+''+dia;
var mes = now.getMonth()
mes++;
if (mes < 10) mes='0'+''+mes;
var agno = now.getFullYear()
fechaHoy = agno+''+mes+''+dia;


seleccionado+='';
mostrar_finde+='';

diaSeleccionado=seleccionado.substring(6, 8)
mesSeleccionado=seleccionado.substring(4, 6)
anoSeleccionado=seleccionado.substring(0, 4)

// difMes: diferencia hacia atras, es decir desde el mas actual cuantos meses voy hacia atras (hasta llegar al mes seleccionado)
difMes=mes-mesSeleccionado;
// Cada vez que voy un aņo atras del aņo acual, sumo 12 a difMes
difMes+=12*(agno-anoSeleccionado);

if (mostrar_finde!='undefined' && mostrar_finde!=''){
	seleccionado_tmp=mostrar_finde;
}
else{
	seleccionado_tmp=seleccionado;
}


if (seleccionado!='undefined' && seleccionado!=''){
	seleccionado_2=seleccionado_tmp-1;
	seleccionado_3=seleccionado_tmp-2;
}
else{
	seleccionado_2=seleccionado;
	seleccionado_3=seleccionado;
}



if (seleccionado=='undefined' || seleccionado==''){
	difMes=0;
}
else{
	seleccionado*=1;
}



nuevoId++;
//;
imgId = 'image' + nuevoId
cartelId = 'cartel' + nuevoId

mesLink=month+1;


cMes=mesLink;
cDia=digit;
if (cMes<10) cMes='0'+cMes;
if (cDia<10) cDia='0'+cDia;
desdeHoy='20'+year+''+cMes+''+cDia;

if (desdeHoy>fechaHoy) { // current cell represent today's date
	text += '<TD class="grisClaro">' + digit + '</TD>'
} 
else{
	if (desdeHoy==seleccionado || desdeHoy==mostrar_finde || (desdeHoy==seleccionado_3 && col==5) || (desdeHoy==seleccionado_2 && col==6)) { // Cuando selecciona vier sab o dom el php siempre pasa dom, y esto deja como clikeados los 3
		text += '<TD class="diaRojo">' + digit + '</TD>'
	} 
	else{
		text += '<TD><a href="'+enlace+'20'+year+'-'+mesLink+'-'+digit+extension+'">' + digit + '</a></TD>'
	}
}
dest=false;
digit++
}
}
text += '</TR>'
}

text += '</TABLE>'
text += '</li>'
//alert (desdeHoy+' - '+fechaHoy);
//alert (seleccionado);
document.write(text) 
}

/*CALENDARO EVENTOS*/
function drawCal2(firstDay, lastDate, date, monthName, year, month) {
	//alert('en drawCal2')
	var headerHeight = 50 // height of the table's header cell
	var border = 0 // 3D height of table's border
	var cellspacing = 0 // width of table's border
	var headerColor = "midnightblue" // color of table's header
	var headerSize = "+3" // size of tables header font
	var colWidth = 30 // width of columns in table
	var dayCellHeight = 15 // height of cells containing days of the week
	var dayColor = "darkblue" // color of font representing week days
	var cellHeight = 20 // height of cells representing dates in the calendar
	var todayColor = "red" // color specifying today's date in the calendar
	var timeColor = "purple" // color of font representing current time

	var diaDest = new Array()
	var tipoDest = new Array()
	var textDest = new Array()
	j=0;
	destMes=false;
	for (m=0; m<eventoMes.length; m++){
		if (eventoMes[m]==month+1){
			diaDest[j]=eventoDia[m];
			tipoDest[j]=eventoTipo[m];
			textDest[j]=eventoTexto[m];
			j++;
			destMes=true;
		}
	}

	if (year>2000){
		year-=2000;
	}
	if (year<10){
		year='0'+year;
	}
	year=year+'.gif';
	

	var text = "" // initialize accumulative variable to empty string
	if (destMes){
	text += '<ul style="display:none">'
	text += '<p> <img src="images/' + monthName + '" /> <img src="images/' + year + '" />  </p>'
	text+= '<li></li>'
	text+= '<li></li>'
	text+= '<li></li>'
	text+= '<li></li>'

	var digit = 1
	var curCell = 1

			for (dia=1; dia<=lastDate; dia++){
				dest=false;	
				for (k=0;k<diaDest.length;k++){
					if(diaDest[k]==digit){
						dest=true;
						destCale='destCal' + tipoDest[k]
						destText=textDest[k]
						break;
					}
				}

				if (dest) { // current cell represent today's date
 					text+= '<li><a href="'+destText+'">'+digit+'</a></li>'
				} 
				dest=false;
				digit++
			}
	text += '</ul>'
	}
	destMes=false;
	document.write(text) 
}

var text2, cant2, currPos2, currPos;
currPos2 = numMeses-2;


function muestraCal(pos,opc){
	
	
	if (opc==2){
		var cont = document.getElementById('calenEventos');
		text2 = cont.getElementsByTagName('ul');
	}
	else{
		var cont = document.getElementById('calendario');
		text2 = cont.getElementsByTagName('li');
	}
	cant2 = text2.length;
	ultPos=cant2-1;
	var flechaL = document.getElementById('flechaLeft');
	var flechaR = document.getElementById('flechaRight');
	if (pos=='init'){
		// difMes sale de drawCal()
		currPos2-=difMes;

		if (cant2!=0){
			text2[currPos2].style.display = "block";
			//flechaL.style.display = "none";
			if (cant2==1){
				//flechaR.style.display = "none";
			}
			if (currPos2==0){
				flechaL.style.display = "none";
			}
		}
	}
	if (pos=='pre'){
		if (currPos2!=0){
			text2[currPos2].style.display = "none";
			currPos2--;
			text2[currPos2].style.display = "block";
			if (currPos2==0){
				flechaL.style.display = "none";
				flechaR.style.display = "block";
			}
			else{
				flechaL.style.display = "block";
				flechaR.style.display = "block";
			}
		}
		else{
			text2[currPos2].style.display = "none";
			currPos2=ultPos;
			text2[currPos2].style.display = "block";
			flechaR.style.display = "none";
		}
	}
	if (pos=='pos'){
		if (currPos2!=ultPos){
			text2[currPos2].style.display = "none";
			currPos2++;
			text2[currPos2].style.display = "block";
			if (currPos2==ultPos){
				flechaR.style.display = "none";
				flechaL.style.display = "block";
			}
			else{
				flechaR.style.display = "block";
				flechaL.style.display = "block";
			}
		}
		else{
			text2[currPos2].style.display = "none";
			currPos2=0;
			text2[currPos2].style.display = "block";
			flechaR.style.display = "none";
		}
	}
}



var thisLink = new Array;
var antSrc;
var imagen;
var restauraColor=true;
var dejaImagen;
var restoreSrc;
var dejaSrc;
var restoreColor;
var dejaColor;
function swapImage(a,newSrc,opc){
	//alert('en swap')
	//esteElem=a;
	dejaSrc=newSrc;
	dejaColor=opc;
	thisImage=a.getElementsByTagName('img');
	thisLink=a.getElementsByTagName('a');
	imagen=thisImage[0];
	antSrc=imagen.src;
	if (newSrc==''){
		lagrgo=antSrc.length;
		imagen.src=antSrc.substr(0,lagrgo-4)+opc;
	}
	else{
		imagen.src=newSrc;
	}
	for (i=0;i<thisLink.length;i++){
		switch(opc){
			case 'naranja':
				thisLink[i].style.color='#F99C1C';
				restauraColor=true;
				break;
			case 'rojo':
				thisLink[i].style.color='#CB343D';
				restauraColor=true;
				break;
			case 'verde':
				thisLink[i].style.color='#A7B625';
				restauraColor=true;
				break;
			case 'azul':
				thisLink[i].style.color='#55B4E0';
				restauraColor=true;
				break;
			case 'morado':
				thisLink[i].style.color='#9e1f63';
				restauraColor=true;
				break;	
			case 'gris':
				thisLink[i].style.color='#B1BDC3';
				restauraColor=true;
				break;	
			default:
				restauraColor=false;
				break;
		}
	}
}
function restoreImage(){
	if(dejaImagen!=imagen){
		imagen.src=antSrc;
		if(restauraColor){
			for (i=0;i<thisLink.length;i++){
				thisLink[i].style.color='#B1BDC3';
			}
		}
	}
}
function restoreImageAnt(thisImage,thisSrc,thisLink2){
		thisImage.src=thisSrc;
			for (i=0;i<thisLink.length;i++){
				thisLink2[i].style.color='#B1BDC3';
			}
}


var i=0;
var dirFotos="imagenes/fotos/"
var imageAnt, imageNew;
var val=1;
firstGal=true;
function galeria(){
	//Fotos
	var imagesqq=getEl('galeriaCont');
	image=imagesqq.getElementsByTagName('div');
	//boton
	botones=getEl('galeriaNav');
	btn=botones.getElementsByTagName('img');
	//primero oculto todas y mestroi la primera
	if (firstGal){
		for (j=0; j<image.length; j++){
				image[j].style.display="none";
				image[j].style.opacity=1;
		}
		image[0].style.display="block";
		firstGal=false;
	}
	//Cuando hago click, oculto la actual y muestro la sgte
	btn[0].onclick = function (){
		imageAnt=image[i];
		//image[i].style.display="none";
		if (i==image.length-1){
			i=0;
		}
		else{
			i++;
		}
		imageNew=image[i];
		fadeOut()
		
	}
	
}

/**/
var opac=100;
var hiloOpac;
var imagenQueda;
var urlQ;
function opacityar() {
	var thisImage=imageAnt;
	//thisImage=document.getElementById(imagen);
	
		if (opac>6){
			opac-=5;
		}
		else{
			opac=0;
			clearInterval(hiloOpac);
			imageAnt.style.display="none";
			imageNew.style.display="block";
			//alert('listo');
			//window.location.href=urlQ;
			if (document.all){
				imageNew.style.filter = "alpha(opacity:100)";
			}
			else{
				imageNew.style.opacity=1;
			}
		}
		if (document.all){
			ImageObj=thisImage;
			ImageObj.style.filter = "alpha(opacity:"+opac+")";
		}
		else{
			cantMoz=opac/100;
			thisImage.style.opacity=cantMoz;
		}
}
function fadeOut(imagen,val){
		clearInterval(hiloOpac);
		opac=100;	
		imagenQueda = imagen;
		inOut=val;
		//urlQ=url;
		hiloOpac=setInterval("opacityar()",5);
}
