// JavaScript Document

function escapar(str) {
str=str.replace(/\\/g,'\\\\');
str=str.replace(/\"/g,'\&quot;');
str=str.replace(/\0/g,'\\0');
return str;
}

function hrs()
{
	var init=document.getElementById('sched').options[document.getElementById('sched').selectedIndex].value;
	var schedule=document.getElementById('hours');
	var inith = h = parseInt(init);
	var initm = "00";
	var part = "am";
	for(var i=0; i<48;i++)
	{
		schedule.tBodies[0].insertRow(i);
		schedule.tBodies[0].rows[i].insertCell(0);
		schedule.tBodies[0].rows[i].cells[0].innerHTML=h+":"+initm+part;
		schedule.tBodies[0].rows[i].cells[0].align="center";
		schedule.tBodies[0].rows[i].cells[0].className="celda_hr";
		initm = parseInt(initm)+30;
		if(initm==60){
			initm="00";
			inith += 1;
			h = inith;
			if(inith > 12){
				h= inith-12;
			}
			if(inith > 11){
				part="pm";
			}
			if(inith > 24){
				h= inith-24;
			}
			if(inith >23){
				part="am";
			}
		}
	}
}

var mousex, mousey;
function scroll_table(lang){									
	var today=date_today();
		
	var startd, stime, length, title, genre, stars, synopsis;
	var count = day = month = row = 0;
	var daynum = 7;
	var col = 0;
	var th, id_cell;
	var thumbs = document.getElementById('belt_sched');
	var day_head= document.getElementById('centro');
	var pannel, pn_day, t_days, t_main, stepto, flag_current=false;
	var init_date, end_date, cell_class="celda";
	var pops;
	
	jQuery.ajax({
				type: "GET",
				url: "cinemex.xml",
				cache: false,
				dataType: (jQuery.browser.msie)? "text" : "xml", //reconocer el browser
				success: function(data) {
					var xml;
					if(typeof data == 'string'){
						xml = new ActiveXObject('Microsoft.XMLDOM');
						xml.async = false;
						xml.loadXML(data);
					} else {
						xml = data;
					}
						
					jQuery(xml).find('Show').each(function(){
							    startd = jQuery(this).find('StartDateBroadcast').text();
								stime = jQuery(this).find('StartTime').text();
								length = jQuery(this).find('Length').text();
								title = jQuery(this).find('TitleName').text();
								genre = jQuery(this).find('Genres').text();
								stars = jQuery(this).find('Stars1st2nd').text();
								synopsis = jQuery(this).find('TitleSynopsis').text();
								
								var len = row_span(length);
								
								if(daynum==7){
									col=0;
									daynum= 0;
									
									pn_day=document.createElement('div');
									pn_day.className="dia";
									pannel=document.createElement('div');
									pannel.className="panel";
									pannel.align="center";
									
									
									if(t_days){
										pn_day.appendChild(t_days);
										day_head.appendChild(pn_day);
									}
									
								if(t_main){
									end_date=schedule_date(startd, 6, lang);									
									pannel.appendChild(t_main);
									thumbs.appendChild(pannel);
									pannel.Title=init_date+" - "+end_date;
								}
								
									t_days=document.createElement('table');
									t_days.appendChild(document.createElement('tbody'));
									t_days.tBodies[0].insertRow(0);
									t_days.width="840px";
									t_days.cellSpacing="7px";
									
									t_main=document.createElement('table');
									tbody=document.createElement('tbody');
									t_main.appendChild(tbody);
									t_main.tBodies[0].insertRow(0);
									t_main.tBodies[0].rows[0].style.verticalAlign="top";
									t_main.cellPadding="0px";
									t_main.cellSpacing="6px";
									t_main.width="840px";
									count++;
									pannel.id=count;
							}
								
								if(day != startd.split("/")[1]){
									day = startd.split("/")[1];
									row = 0;
									t_main.tBodies[0].rows[0].insertCell(col);
									t_main.tBodies[0].rows[0].cells[col].style.width="14%";
									table=document.createElement('table');
									table.appendChild(document.createElement('tbody'));
									table.cellSpacing="3px";
									t_main.tBodies[0].rows[0].cells[col].appendChild(table);
									t_main.tBodies[0].rows[0].cells[col].style.verticalAlign="top";
									t_days.tBodies[0].rows[0].insertCell(col);
									t_days.tBodies[0].rows[0].cells[col].align="center";
									t_days.tBodies[0].rows[0].cells[col].className="headers";
									t_days.tBodies[0].rows[0].cells[col].innerHTML=dayname(daynum, lang)+"<br />"+day;
									
									if(daynum==0){
										init_date=schedule_date(startd, daynum, lang);//this function is into date_letter.js
									}
									col ++;
									daynum ++;
									
								}
								
								    id_cell="p"+count+"r"+row+"c"+col;
									id_tp="s"+count+"r"+row+"c"+col;
									table.tBodies[0].insertRow(row);
									table.tBodies[0].rows[row].insertCell(0);
									
									start=startd.split("/");
									fecha=calculate_day(start[0], start[1], start[2], lang)  + "  " + monthname(start[0]-1, lang) + "  " + start[1]+ ",  " + start[2];
									fixedtit=fixTitle(title)[0];//this function is into  trailer.js
									DBtit=article_first(fixedtit);//this function is into  trailer.js
	
									//if this date is today highlight it
									if(today==startd){
										stepto=count;
										cell_class="day_hgl Ntooltip";
										
										if(timenow(stime, length)&& !flag_current){
											cell_class="celda_hgl Ntooltip";
											flag_current=true;
										}
									}
									else
										cell_class="celda Ntooltip";
										
									table.tBodies[0].rows[row].cells[0].className=cell_class;
									table.tBodies[0].rows[row].cells[0].innerHTML= title;
									pops+='<span id="' + id_cell+ '_tp' + '" class="invisible"><div class="caja" onmouseout="document.getElementById(\''+id_cell+'\').style.border=\'none\'" onmouseover="document.getElementById(\''+id_cell+'\').style.border=\'solid #CC00FF 3px\'"><div class="cajaarriba"><div class="cajaabajo"><div style="text-align:justify"><div style="max-height:170px; overflow:hidden; text-align:center" id="'+ id_cell +'_img" alt="'+escapar(DBtit)+'"> </div> <br /> <p class="synopsis" style="font-size:13px; font-weight:bold;line-height:0">' + genre.toUpperCase() + '</p> <p class="synopsis" style="font-size:11px; font-weight:800; line-height:1">' + fecha + '</p> <p class="synopsis" style="font-size:13px; font-weight:bold; color:#1E6186; line-height:1; text-align:center">' + DBtit.toUpperCase() +'</p> <div class="synopsis" style="font-size:12px; overflow:auto; max-height:120px; text-align:justify">' + synopsis + '</div> </div></div></div></div> </span>';
									table.tBodies[0].rows[row].cells[0].align="center";
									table.tBodies[0].rows[row].cells[0].id=id_cell;
									table.tBodies[0].rows[row].cells[0].height=(len*20)+((len-1)*2) + 3 +'px';
									table.tBodies[0].rows[row].cells[0].onmouseover=function(){onCellover(this, lang);};
									table.tBodies[0].rows[row].cells[0].onmouseout=function(){onCellout(this);};
									row++;			
						});
					
					
					
						stepcarousel.setup({
							galleryid: 'table_scroll', //id of carousel DIV
							beltclass: 'belt', //class of inner "belt" DIV containing all the panel DIVs
							panelclass: 'panel', //class of panel DIVs each holding content
							autostep: {enable:false, moveby:1, pause:3000},
							panelbehavior: {speed:500, wraparound:true, persist:true},
							defaultbuttons: {enable: false, moveby: 1, leftnav: ['images/larrow.gif', -5, 80], rightnav: ['images/rarrow.gif', -20, 80]},
							statusvars: ['statusA', 'statusB', 'statusC'], //register 3 variables that contain current panel (start), current panel (last), and total panels
							contenttype: ['inline'] //content setting ['inline'] or ['ajax', 'path_to_external_file']
						});
						
						stepcarousel.setup({
							galleryid: 'days', //id of carousel DIV
							beltclass: 'centro', //class of inner "belt" DIV containing all the panel DIVs
							panelclass: 'dia', //class of panel DIVs each holding content
							autostep: {enable:false, moveby:1, pause:3000},
							panelbehavior: {speed:500, wraparound:true, persist:true},
							defaultbuttons: {enable: false, moveby: 1, leftnav: ['images/larrow.gif', -5, 80], rightnav: ['images/rarrow.gif', -20, 80]},
							statusvars: ['current', 'last', 'total'], //register 3 variables that contain current panel (start), current panel (last), and total panels
							contenttype: ['inline']//content setting ['inline'] or ['ajax', 'path_to_external_file']		
						});
						
						stepcarousel.stepTo('table_scroll', stepto);
						stepcarousel.stepTo('days', stepto);
						change_date();
						document.getElementById('pops').innerHTML=pops;						
					}
				});

}


function row_span(length){
	var len = length.split(':');
	var minutes = len[0]*60;
	minutes += parseInt(len[1]);
	var span = minutes/30;
	return span;
}

function change_date(){
	id=parseInt(document.getElementById("statusA").innerHTML)+1;
	el=document.getElementById(id);
	document.getElementById("rango").innerHTML=el.Title;
}

function onCellout(el){
	document.getElementById(el.id+"_tp").className="invisible";
	document.getElementById(el.id).style.border="none";
}

function onCellover(el, lang){

	document.getElementById(el.id).style.border="solid #CC00FF 3px";
	document.getElementById(el.id+"_tp").className="visible";
	
	coord=getDimensions(el);	
	height=parseInt(coord.y)+coord.h;
	var Tam = TamVentana();
	
	var DBtit=document.getElementById(el.id+"_img").getAttribute("alt");
		var image;
		var req_img = "http://www.cinemexicano.tv/movies.php?both="+escape(DBtit);
	
		image=Request();
		
		image.open("GET", req_img);
		
		image.onreadystatechange = function(){
		if(image.readyState==4){		

			var data = image.responseText.split('@');

			document.getElementById(el.id+"_img").innerHTML='<img src="arts/' + data[0] + '" width="300px" name="'+DBtit+'" onload="ajuste('+coord.y+', '+Tam[1]+', '+el.id+')" />';
			
			var clicked;
			if(!data[1])
				clicked = "image="+data[0];
			else
				clicked = "embed="+data[1];
			
			document.getElementById(el.id).onclick=function(){
				if(lang=="en") {window.location="trailer.html?" + clicked + "&trailer=" + escape(DBtit);} 
				else{ window.location="trailer_es.html?" + clicked + "&trailer=" + escape(DBtit);}
			};
			document.getElementById(el.id+"_img").onclick=function(){
				if(lang=="en") {window.location="trailer.html?" + clicked + "&trailer=" + escape(DBtit);} 
				else{ window.location="trailer_es.html?" + clicked + "&trailer=" + escape(DBtit);}
			};
		};
		}
		image.send(null);

		
	if(coord.y>Tam[1]- (Tam[1]/3)){
     	
		  document.getElementById(el.id+"_tp").style.top= (Tam[1]-getDimensions(document.getElementById(el.id+"_tp")).h)+"px";
	}
	else if (coord.y<130){
		document.getElementById(el.id+"_tp").style.top= coord.y+"px";
	}
	else{
		document.getElementById(el.id+"_tp").style.top=(parseInt(coord.y)-180)+"px";
	}
	
	
	if(coord.x>Tam[0]-(Tam[0]/2)){
		document.getElementById(el.id+"_tp").style.left=(parseInt(coord.x)-320)+"px";
	}
	else{
		document.getElementById(el.id+"_tp").style.left=(parseInt(coord.x)+100)+"px";
	}
}


function ajuste(y, Tam, el){
	if(y>Tam- (Tam/3)){
		  document.getElementById(el.id+"_tp").style.top= (Tam-getDimensions(document.getElementById(el.id+"_tp")).h)+"px";
	}
	else if (y<130){
		document.getElementById(el.id+"_tp").style.top= y+"px";
	}
	else{
		document.getElementById(el.id+"_tp").style.top=(parseInt(y)-180)+"px";
	}
}


getDimensions = function(oElement) {
var x, y, w, h;
x = y = w = h = 0;
if (document.getBoxObjectFor) { // Mozilla
var oBox = document.getBoxObjectFor(oElement);
x = oBox.x-1;
w = oBox.width;
y = oBox.y-1;
h = oBox.height;
}
else if (oElement.getBoundingClientRect) { // IE
var oRect = oElement.getBoundingClientRect();
x = oRect.left-2;
w = oElement.clientWidth;
y = oRect.top-2;
h = oElement.clientHeight;
}
return {x: x, y: y, w: w, h: h};
}


function TamVentana() {  
	var Tamanyo = [0, 0];  
	if (typeof window.innerWidth != 'undefined')  {    
		Tamanyo = [window.innerWidth, window.innerHeight];  
	}  else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth !='undefined' && document.documentElement.clientWidth != 0)
	{ 
	   Tamanyo = [document.documentElement.clientWidth, document.documentElement.clientHeight];  
	 }  else   {
		 Tamanyo = [document.getElementsByTagName('body')[0].clientWidth, document.getElementsByTagName('body')[0].clientHeight];  
	 }  
     return Tamanyo;
}
	// window.onresize = function() {  var Tam = TamVentana();  alert('La ventana mide: [' + Tam[0] + ', ' + Tam[1] + ']');};
