// JavaScript Document

function gup( name ){
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp ( regexS )
    var tmpURL = window.location.href;
    var results = regex.exec( tmpURL);
    if( results==null )
        return null;
    else
         return results[1];
}

var title = gup('trailer');

function Request() { 	 
	if (window.ActiveXObject) { 	 
		return new ActiveXObject("Microsoft.XMLHTTP"); 	 
	} else if (window.XMLHttpRequest) { 	 
		return new XMLHttpRequest(); 	 
	} 	 
}

var trailer_request;
var source;

function get_source(tit) {
	var url = "http://www.cinemexicano.tv/movies.php?trailer="+ tit;
	trailer_request = Request();
	trailer_request.open("GET", url);
	trailer_request.onreadystatechange = Complete;
	trailer_request.send(null);
}




function Complete() {
	if (trailer_request.readyState==4) {
		source=trailer_request.responseText.split("=");
		var trailer_box = document.getElementById("trailer");
		if(source[0]=="embed"){
				var player=document.getElementById("player");
				player.setEmbedCode(source[1]);
		}
		
		else {
			if(source[1]!="")
				trailer_box.innerHTML='<img src="arts/'+source[1]+'" width="600px" />';
			else
				trailer_box.innerHTML='<img src="arts/cm.jpg" width="600px" />';
		}
	}
}

function syn_BD(tit){
	var url = "http://www.cinemexicano.tv/movies.php?syn="+ tit;
	var syn_request
	var txt;
	syn_request = Request();
	syn_request.open("GET", url);
	syn_request.onreadystatechange = function(){
		if (syn_request.readyState==4) {
			txt=syn_request.responseText.split("@");
			if(txt){
			  document.getElementById("syn_tit").innerHTML= '<strong style="margin:10px">'+unescape(title).toUpperCase()+'</strong>';
			  document.getElementById("synopsis").innerHTML='<p style="margin:0 15px 0 15px">'+txt[0]+'</p>';
			  document.getElementById("info").innerHTML='<div class="synopsis" style="float:right; height:20px; width:260px; margin-top:25px; color:#FFFFFF; font-size:14px; font-weight:bold" align="center">'+ txt[2] +'</div><br /><br /><br /><p align="rigth" style=" line-height:1.5; margin-left:20px; font-weight:bold">Title:&nbsp;&nbsp; <span style="font-weight:500">'+unescape(title)+'</span><br /> <br /> Actors:&nbsp;&nbsp; <span style="font-weight:500">'+ txt[1]+'</span>';
			}
		  }
	};
	
	syn_request.send(null);
}


function get_Synopsis(lang){
	var index, title_syn=unescape(title).toUpperCase(), con=0;
	var article=new Array("EL", "LOS", "LAS", "LA", "UN", "UNA", "UNO", "UNAS", "UNOS");//it's important that both, title and article, are uppercase or lowercase
	
	while(con<article.length){
		index=unescape(title.toUpperCase()).indexOf(article[con]);
		if(index==0){
			title_syn=title_syn.substr(index+parseInt(article[con].length)+1);
			title_syn+=", "+article[con];
			break;
		}
			con++;
		}
	
	
	var syn_flag=false;
	var tabla=document.getElementById("upcoming");
	jQuery.ajax({
				type: "GET",
				url: "cinemex_full.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(){
							    startdCM = jQuery(this).find('StartDate').text();
								startdCM_sep=startdCM.split("/");
								stimeCM = jQuery(this).find('StartTime').text();
								lengthCM = jQuery(this).find('Length').text();
								titleCM = jQuery(this).find('TitleName').text();
								titleCM_rating = fixTitle(titleCM);
								genreCM = jQuery(this).find('Genres').text();
								starsCM = jQuery(this).find('Stars1st2nd').text();
								synopsisCM = jQuery(this).find('TitleSynopsis').text();
								
								if(titleCM.indexOf(title_syn)==0){
									tabla.tBodies[0].insertRow(tabla.rows.length);
									tabla.tBodies[0].rows[tabla.rows.length-1].className="littlel";
									tabla.tBodies[0].rows[tabla.rows.length-1].style.color="#585858";
									tabla.tBodies[0].rows[tabla.rows.length-1].align="center";
									tabla.tBodies[0].rows[tabla.rows.length-1].height="43px";
									tabla.tBodies[0].rows[tabla.rows.length-1].style.verticalAlign="middle";
									
									tabla.tBodies[0].rows[tabla.rows.length-1].insertCell(0);
									tabla.tBodies[0].rows[tabla.rows.length-1].cells[0].width="24%";
									tabla.tBodies[0].rows[tabla.rows.length-1].cells[0].height="43px";
									tabla.tBodies[0].rows[tabla.rows.length-1].cells[0].innerHTML=calculate_day(startdCM_sep[0], startdCM_sep[1], startdCM_sep[2], lang).toUpperCase();
									
									
									tabla.tBodies[0].rows[tabla.rows.length-1].insertCell(1);
									tabla.tBodies[0].rows[tabla.rows.length-1].cells[1].width="33%";
									tabla.tBodies[0].rows[tabla.rows.length-1].cells[1].style.color="white";
									tabla.tBodies[0].rows[tabla.rows.length-1].cells[1].style.borderBottom="solid #585858 3px";
									tabla.tBodies[0].rows[tabla.rows.length-1].cells[1].innerHTML=startdCM;
									
									tabla.tBodies[0].rows[tabla.rows.length-1].insertCell(2);
									tabla.tBodies[0].rows[tabla.rows.length-1].cells[2].width="43%";
									tabla.tBodies[0].rows[tabla.rows.length-1].cells[2].style.fontSize="11px";
									tabla.tBodies[0].rows[tabla.rows.length-1].cells[2].innerHTML=fixHour(stimeCM);

									if(!syn_flag){
										document.getElementById("syn_tit").innerHTML= '<strong style="margin:10px">'+unescape(title).toUpperCase()+'</strong>';
										document.getElementById("synopsis").innerHTML='</strong></p><p style="margin: 0 15px 0 15px">'+synopsisCM+'</p>';
										document.getElementById("info").innerHTML='<div class="synopsis" style="float:right; height:20px; width:260px; margin-top:25px; color:#FFFFFF; font-size:14px; font-weight:bold" align="center">'+ genreCM.toUpperCase() +'</div><br /><br /><br /><p align="rigth" style=" line-height:1.5; margin-left:20px; font-weight:bold">Title:&nbsp;&nbsp; <span style="font-weight:500">'+titleCM_rating[0]+'</span><br /> <br />Rating:&nbsp;&nbsp; <span style="font-weight:500">'+ titleCM_rating[1] +'</span><br /><br /> Actors:&nbsp;&nbsp; <span style="font-weight:500">'+ starsCM+'</span>';
										syn_flag=true;
									}
									
								}
						});
					
						
					}
				});
}


function fixTitle(title){
	var rat = title.lastIndexOf('TV', title.length);
	var fixedT = new Array(title.substring(0, rat), title.substring(rat));
	return fixedT;
}


function setTrailer(playerId, eventName, eventParams){
	switch(eventName) {
		case "apiReady": 
			
		 break;
	}
}

function cambia_dir(lang){
	var trailer = window.location;
	if(lang=="es")
		url="trailer_es.html"+trailer.search;
	else
		url="trailer.html"+trailer.search;
	window.location=url;
}

//this function change the article from the title to the first position from caballo, El to El caballo. This is for doing the query to the data base.
function article_first(text){
	var art_f=text;
	var con=0;
	var article=new Array("EL", "LOS", "LAS", "LA", "UN", "UNA", "UNO", "UNAS", "UNOS");
	
	while(con<article.length){
		index=text.toUpperCase().lastIndexOf(", "+article[con]);
			
		if(index!=-1){
			var comp=text.substr(parseInt(index+article[con].length+2));
			var vacio=/^\s*$/;
			
			if(vacio.test(comp)){
				art_f=article[con]+" "+text.substr(0, index);
				return art_f;
				break;
			}
		}
			con++;
		}
		return art_f;
}