﻿// JavaScript Document


$(document).ready(function(){
			var lang, count = 0;
			var newdiv="";
			var tooltip="";
			var now = date_today();
			var startd, stime, title, genre, length, synopsis,stars;
			
			URLtmp=document.location.toString();

			if(URLtmp.lastIndexOf('index.html')>0 || URLtmp.lastIndexOf('/')==URLtmp.length-1)
				lang='_es';
			else
				lang='';

			$.ajax({
				type: "GET",
				url: "cinemex.xml",
				cache: false,
				dataType: ($.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;
					}
						
					$(xml).find('Show').each(function(){
							startd = $(this).find('StartDate').text();
							if(startd == now){
								stime = $(this).find('StartTime').text();
								length = $(this).find('Length').text();
						if(timenow(stime, length)){
									title = $(this).find('TitleName').text();
									title_rating = fixTitle(title);
									genre = $(this).find('Genres').text();
									stars = $(this).find('Stars1st2nd').text();
									synopsis = $(this).find('TitleSynopsis').text();
									
									title_DB=article_first(title_rating[0]);//this is into trailer.js
									
									newdiv += '<a class="showTip el'+count+'" onclick="window.location=\'trailer' + lang + '.html?trailer='+escape(title_DB)+'\'"> <div class="panel"> <p align="right" class="littlel">'+fixHour(stime)+'</p> <p class="littlel" style="color:#FFFFFF" align="right">'+ genre.toUpperCase()+ '</p> <p align="left" class="title">'+ title_rating[0] +'</p> </div> </a>'
								
									tooltip += '<div  id="el'+count+'" class="tipContent"><br /><br /><br /><p align="right" class="littlel" style="color:#FFFFFF">'+ (date("en"))[2]+ ' ' + (date("en"))[1] +'&nbsp;&nbsp; /'+fixHour(stime)+'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p><br /><p align="rigth" style=" line-height:1.5; margin-left:20px" class="littlel">Title:&nbsp;&nbsp;'+title_rating[0]+'<br /> Genres:&nbsp;&nbsp;'+ genre +'<br /> Rating:&nbsp;&nbsp;'+ title_rating[1] +'<br /> Actors:&nbsp;&nbsp;'+ stars+'<br /><br />Synopsis<br /></p><div style="width:370px; height:90px; overflow:auto; margin-left:20px; margin-top:10px"><p class="littlel" style="font-weight:200">'+synopsis+'</p></div> </div>';
								count++;
								}
							}
						});
					
						document.getElementById('tooltips').innerHTML = tooltip;
						document.getElementById('prog').innerHTML = newdiv;

						//stepcarousel setup
						stepcarousel.setup({
							galleryid: 'todayCM', //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:false},
							defaultbuttons: {enable: false, moveby: 1, leftnav: ['images/larrow.gif', -5, 50], rightnav: ['images/rarrow.gif', -20, 50]},
							//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']
						});
						
					}
				});	
			
			
			
		
		//here are requested all the information for Cine Mexicano Recomends section from the DB
			var info;
			var Tit, Dat, Tim;
			var req_gen = "http://www.cinemexicano.tv/highlights_CM.php";
			info=Request();
			info.open("GET", req_gen);
			info.onreadystatechange = function(){
			if(info.readyState==4){
				var arreglo=info.responseText.split("-");
				var count=1;
				
				for(i=0; i<arreglo.length; i++){
					if(arreglo[i])
						document.getElementById('img'+count).style.backgroundImage="url(images/recomends/"+arreglo[i]+")";
					++i; 
					Tit=arreglo[i];
					document.getElementById('title'+count).innerHTML=Tit.toUpperCase();
					++i;
					Dat=arreglo[i];
					++i;
					Tim=arreglo[i];
					
					document.getElementById('date'+count).innerHTML=Dat+" "+Tim;

					//Request the genres for each title.
							
					get_Genre(Tit, document.getElementById('date'+count));
					
					if(document.getElementById('boton'+count)){
						document.getElementById('boton'+count).name=Tit;
						document.getElementById('boton'+count).onclick=function(){window.location='trailer_es.html?trailer='+escape(this.name)};
					}
					else{
						document.getElementById('btn'+count).name=Tit;
						document.getElementById('btn'+count).onclick=function(){window.location='trailer.html?trailer='+escape(this.name)};
					}
					count++;
				}
			}
		};
		info.send(null);
		
		});//ends document ready

//this function gets the genres from a title

function get_Genre(tit, el){
	var gen=Request();
	gen.open("GET","http://www.cinemexicano.tv/highlights_CM.php?genre="+tit);
	gen.onreadystatechange=function(){
	if(gen.readyState==4)
		el.innerHTML+='<br />'+gen.responseText.toUpperCase();
	};
	gen.send(null);
}

function formatSyn(synopsis){
	var end = synopsis.indexOf('~');
	if(end<0)
		return(synopsis);
	else{
	var syn_es = synopsis.substring(0, end);
	return syn_es;
	}
}


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};
}



var xmlHttp;
function survey(lang) {
	var url = "http://www.cinemexicano.tv/encuesta.php?lang="+ escape(lang);
	xmlHttp = Request();//this function is in trailer.js
	xmlHttp.open("GET", url);
	xmlHttp.onreadystatechange = onCompleteData;
	xmlHttp.send(null);
}


function onCompleteData() {
	if (xmlHttp.readyState==4) {
		arreglo=xmlHttp.responseText.split("-");
		document.getElementById("question").innerHTML=arreglo[0];
		radio_buttons="";
		for(i=1; i<arreglo.length; i++){
			radio_buttons +='<input name="answer" type="radio" value="'+arreglo[i]+'"/>'+arreglo[i]+'<br />';
		}
		document.getElementById("radio").innerHTML=radio_buttons;
	}
}

var emitido;
function voto_emitir(voto, lang){
	if(voto){
	var votar = "http://www.cinemexicano.tv/encuesta.php?voto="+escape(voto)+"&lang='"+ escape(lang)+"'";
	emitido=Request();
	emitido.open("GET", votar);
	emitido.onreadystatechange = function(){
		if(emitido.readyState==4){
			res=emitido.responseText.split("-");
			if(res!="-1"){
				results=document.createElement('table');
				results.appendChild(document.createElement('tbody'));
				results.align="center";
				results.cellSpacing="3px";
				results.width="100%";
				
				
				document.getElementById("vota").style.visibility="hidden";
				
				if(lang=="es")
					document.getElementById("question").innerHTML="<br />¡Gracias por votar!";
				else
					document.getElementById("question").innerHTML="<br />Thanks for your vote!";
				
				document.getElementById("radio").innerHTML="";
				document.getElementById("radio").appendChild(results);

					cont=0;
				for(i=1; i<res.length; i++){
					results.tBodies[0].insertRow(cont);
					results.tBodies[0].rows[cont].insertCell(0);
					results.tBodies[0].rows[cont].insertCell(1);
					results.tBodies[0].rows[cont].cells[0].innerHTML="<strong>"+res[i]+"</strong>";
					results.tBodies[0].rows[cont].cells[1].innerHTML="<strong>"+res[++i]+"</strong>";
					cont++;
				}
				
			}

			else{
				if(lang=="es")
					alert("Tu voto no pudo ser registrado. Por favor intenta más tarde.");
				else
					alert("Your vote could not be registered. Please try later.");
			}
		}
		};
	emitido.send(null);
	}
}


function selected_answer(control){
	for(i=0;i<control.length;i++){
        if(control[i].checked)
			return control[i].value;
	}
}

