var RUTA = "";
var RUTA_VENTA = "";
var InitFuncArray = new Array();
var browser = navigator.appName;

// Funciones que se llaman al terminar de cargar la pagina
function onLoadInit(calendar)
{
	if(calendar){
		var popupcalendar = new Epoch('cal','popup',document.getElementById('obl_fecNacReg'),false);
	}
	for (var i = 0; i < InitFuncArray.length; i++)
	{
		eval(InitFuncArray[i]).call();
	}
	checkScrollTo();
}

function addInitFunc(func)
{
	InitFuncArray.push(func);
}

function recargar_div(div,pagina, func_OnComplete){
	var url = RUTA+pagina;
	new Ajax.Updater(div,url, {onComplete: func_OnComplete,evalScripts: true});
}

function recargar_div_venta(div,pagina, func_OnComplete){
	var url = RUTA_VENTA+pagina;
	new Ajax.Updater(div,url, {onComplete: func_OnComplete,evalScripts: true});
}

//permite cambiar entre las sub secciones de una seccion
function cambiar_seccion(seccion,subseccion){
	recargar_div('contenido_'+seccion,subseccion);
	
	var menu = $('menu_'+seccion).getElementsByTagName('a');
	for (var i = 0;i<menu.length; i++){
		menu[i].className = "";
		if(menu[i].id == subseccion){
			menu[i].className = "txt_submenu_selected";
		}
		else{
			menu[i].className = "txt_submenu pointer";
		}
	}
	if(menu == "menu_cine"){
		if(seccion == "cartelera"){
			$('div_comp_cine').style.display = "";
		}
		else{
			$('div_comp_cine').style.display = "none";
		}
	}/*
	if(seccion == 'ninios'){
		if(subseccion == 'cumples'){
			$('td_contenido').className = 'border_box bg_box';
		}
		else{
			$('td_contenido').className = 'border_box padding15 bg_box';
		}
	}*/
}

function imprimir(tipo_funcion,id){
	if(tipo_funcion == 1){
		var funcion = "pelicula";
	}
	else {
		var funcion = "teatro";
	}
	var url = RUTA+'estructura/imprimir_'+funcion+'.php?id='+id;
	var my_win = window.open(url,null,'scrollbars=yes,width=600,height=650');
	my_win.moveTo(window.screen.availWidth/2-300, window.screen.availHeight/2-325);
}

function set_banner_size(width, height, n)
{	
	var my_swf = (document.all) ? eval("document.banner_swf_"+n) : $("banner_swf_"+n);
	my_swf.width = width;
	my_swf.height = height;
}

function borrarInput(miInput,valor){
	if(miInput.value==valor)
		miInput.value = "";
}

function llenarInput(miInput,valor){
	if(miInput.value == ""){
		miInput.value = valor;
	}
}

function get_captcha(color) {
	var rand = Math.random();
	//$('captcha').src = RUTA+"captcha/"+color+"/"+rand+"/";
	var captcha = $('captcha');
	if(captcha)
		captcha.src = RUTA+"include/captcha/imagebuilder.php?color="+color+"&rando="+rand;
}

function ingresar(accion){
	showDiv('divLoguin');
	if(accion == 'comprar'){
		$('texto').innerHTML = "Para continuar con la compra debes iniciar sesi&oacute;n:";
		$('btn_ingresar').innerHTML = '<img src="'+RUTA+'img/btn/iniciar.jpg" alt="iniciar" title="iniciar" onclick="login_compra();" class="pointer" />';
		$('btn_crear').innerHTML = '<img border="0" src="'+RUTA+'img/btn/crear_cuenta.jpg" class="pointer" onclick="crear_cuenta(true);" />';
	}
	else{
		$('texto').innerHTML = "Usuarios Registrados ";
	}
}

function buscar(distribuye){
	var texto = $('txt_buscar').value;
	if(texto != ''){
		if(distribuye){
			window.location.href = RUTA+'distribuye_movie/busqueda/'+texto;
		}
		else{
			window.location.href = RUTA+'busqueda/'+texto;
		}
	}
}

//SELECTS
function mostrar(ele){
	$(ele).show();
}

function ocultar(div){
	$(div).hide();
}

function seleccionar(td,elem,lista){
	
	var texto = td.innerHTML;
	if(texto=='Montevideo'){
		var barrio = $('barrio');
		var esp_barrio = $('espacio_barrio');
		
		if(barrio!= null)
			barrio.style.display = "";
		if(esp_barrio!=null)
			esp_barrio.style.display = "";
	}
	else if(elem=='obl_ciudad_ciudad'){
		var barrio = $('barrio');
		var esp_barrio = $('espacio_barrio');
		if(barrio!= null)
			barrio.style.display = "none";
		if(esp_barrio!=null)
			esp_barrio.style.display = "none";
	}
	
	if(elem=="obl_genero"){
		var genero = td.id.split('_');
		var idNum = genero[1];
		$('id_gen_pref').value = idNum;
	}

	var ele = parent.$(elem);
	ele.value = texto;
	
	if(elem == 'input_peliculas'){
		$('hidden_pelicula').value = td.id;
	}
	else if(elem == 'input_complejo'){
		$('hidden_complejo').value = td.id;
	}
}

function pintar_celda(celda,color,color_fuente){
	celda.style.background = color;
	celda.style.color = color_fuente;
}

// POP UPS

function mostrar_registro(){
	showDiv('divRegistro');
	recargar_div('divRegistro','registro');
	var my_width  = getWindowWidth();
  	 var my_height = getWindowHeight();
	$('divRegistro').style.left = (my_width /2)-355;
	$('divRegistro').style.top = (my_height/2)-210;
}

function mostrar_swf(swf,dato1,dato2){
	//para galeria dato1 = modulo  y  dato2 = id_ref
	//para trailer dato1 = trailer  y  dato2 = titulo
	if(swf == 'galeria')
		var url = 'galeria/'+dato1+'/'+dato2+'/';
	else if (swf == 'trailer')
		var url = 'trailer/'+dato1+'/'+dato2+'/';
	recargar_div('div_swf',url);
	showDiv('div_swf');
	centrar_swf(swf);
}

function ocultar_swf(){
	hideDiv($('div_swf'));
	hideBg();
}

function centrar_swf(swf){
	var my_width  = getWindowWidth();
    var my_height = getWindowHeight();
	
	if(swf=='galeria'){
		var ancho = 620;
		var alto = 539;
	}
	else{
		var ancho = 830;
		var alto = 500;
	}
	$('div_swf').style.left = (my_width - ancho)/2;
	$('div_swf').style.top = (my_height - alto)/2;
}
function show_preferencias(){
	mostrar('preferencias');
	var from = window.location.href.split("#");
	window.location.href = from[0]+'#preferencias';
}
function ocultar_preferencias(recargar){
	hideDiv($('preferencias'));
	if(recargar==1){
		window.location.href = RUTA+'mi_movie/';
	}
}

function recomendar(tipo_funcion,featureid,idref){
	var url = 'recomendar/'+tipo_funcion+'/'+featureid+'/'+idref+'/';
	recargar_div('div_recomendar',url);
	showDiv('div_recomendar');
	var my_width  = getWindowWidth();
	var my_height = getWindowHeight();
	$('div_recomendar').style.left = (my_width - 380)/2;
	$('div_recomendar').style.top = (my_height - 380)/2;
}

function showDiv(div){
	$$('#compra select').invoke('hide');
	$('overlay').style.height =  document.body.scrollHeight+"px";
	$('overlay').show();
	center(div);
	if(div == 'divLoguin'){
		centrar_loguin();
	}
}

function centrar_loguin(){
	var my_width  = getWindowWidth();
	var my_height = getWindowHeight();
	$('divLoguin').style.left = (my_width - 480)/2;
	$('divLoguin').style.top = (my_height - 315)/2;
}

function hideDiv(div){
	$$('#compra select').invoke('show');
	div = parent.$(div);
	div.hide();  
}

function hideBg(){
	$('overlay').hide();
}

function center(element){
 
 	element = $(element);
	
	var my_width  = getWindowWidth();
	var my_height = getWindowHeight();

	element.style.position = 'absolute';
	element.style.zIndex   = 999999999;

   var scrollY = 0;

    if ( document.documentElement && document.documentElement.scrollTop ){
        scrollY = document.documentElement.scrollTop;
    }else if ( document.body && document.body.scrollTop ){
        scrollY = document.body.scrollTop;
    }else if ( window.pageYOffset ){
        scrollY = window.pageYOffset;
    }else if ( window.scrollY ){
        scrollY = window.scrollY;
    }

    var elementDimensions = Element.getDimensions(element);
    var setX = ( my_width  - elementDimensions.width  ) / 2;
    var setY = ( my_height - elementDimensions.height ) / 2 + scrollY;

    setX = ( setX < 0 ) ? 0 : setX;
    setY = ( setY < 0 ) ? 0 : setY;

    element.style.left = setX + "px";
    element.style.top  = setY + "px";

    element.style.display  = 'block';
}


var IE = navigator.appName.toLowerCase().indexOf("microsoft") > -1;

function only_numbers(evt){	
// NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57, '.'=46, ','  = 44
	var key = IE ? evt.keyCode : evt.which;
	return (key <= 13 || (key >= 48 && key <= 57));
}


// Alto de la Pagina
function getWindowHeight() {

	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number') {
		windowHeight = window.innerHeight;
	}
	else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight = document.documentElement.clientHeight;
		}
		else {
			if (document.body && document.body.clientHeight) {
				windowHeight = document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}

// Ancho de la Pagina
function getWindowWidth() {

	var windowWidth = 0;
	if (typeof(window.innerWidth) == 'number') {
		windowWidth = window.innerWidth;
	}
	else {
		if (document.documentElement && document.documentElement.clientWidth) {
			windowWidth = document.documentElement.clientWidth;
		}
		else {
			if (document.body && document.body.clientWidth) {
				windowWidth = document.body.clientWidth;
			}
		}
	}
	return windowWidth;
}

// Anchor dinámico
function checkScrollTo()
{
	if (location.hash) {
		var str_anchor = location.hash.split("#")[1];
		if ($("anchor_"+str_anchor)) {
			Effect.ScrollTo("anchor_"+str_anchor);
		}
	}
}

function resize_window(){
	window.resizeTo(800, 800);
}

function sincronizar(){
	var url = RUTA+'ws/sincronizar_cartelera.php';
	new Ajax.Request(url, {
		method: 'post',
		onSuccess: function(transport){
			var results = transport.responseText.split("¦");
			
			var peliculas = results[0].split("|");
			var obras = results[1].split("|");
			var cine = 0;
			var html = '<table border="0" cellpadding="0" cellspacing="0" width="700">';
			var cines = Array();
			cines[0] = '';
			cines[1] = 'Movie Punta Carretas Shopping';
			cines[2] = 'Movie Montevideo Shopping';
			cines[3] = 'Movie Portones Shopping';
			//recorrer peliculas
			for(var i=0;i<peliculas.length;i++){
				var data_peli = peliculas[i].split("-");
				if(cine != data_peli[0]){
					cine = data_peli[0];
					html += '<tr><td class="height18"></td></tr>';
					html += '<tr><td class="recom bold" height="25">'+cines[cine]+'</td></tr>';
					html += '<tr><td class="bg_separation"></td></tr>';
				}
				html += '<tr><td class="recom" height="20">'+data_peli[1]+'</td></tr>';
			}
			
			html += '<tr><td class="height18"></td></tr>';
			html += '<tr><td class="height18"></td></tr>';
			html += '<tr><td class="recom bold" height="25">Sala Teatro</td></tr>';
			html += '<tr><td height="25" class="bg_separation"></td></tr>';
			if(obras.length > 0 && obras[0] != ''){
				for(var x=0;x<obras.length;x++){
					html += '<tr><td height="20" class="recom">'+obras[x]+'</td></tr>';
				}
			}
			else{
				html += '<tr><td height="20" class="recom">No se encontraron obras en cartelera para esta semana</td></tr>';
			}
			html += '</table>';
			//alert(html);
			$('img_title_sinc').src = RUTA+'img/title/cartelera_sincronizada.gif';
			$('contenido_sinc').innerHTML = html;
		}});
}

function sincronizar_por_complejo(idComplejo,tipo){
	var url = '';
	var cine = '';
	
	if(idComplejo == 1){
		if(tipo == 1){
			url = RUTA+'ws/sincronizacion_ptacarretas.php';
			cine = 'Movie Punta Carretas Shopping - Sincronizaci\u00F3n Cartelera';
		}else{
			url = RUTA+'ws/sincronizacion_completa_ptacarretas.php';
			cine = 'Movie Punta Carretas Shopping - Sincronizaci\u00F3n Completa';
		}
		
		
	}
	else if(idComplejo == 2){
		if(tipo == 1){
			url = RUTA+'ws/sincronizacion_montevideo.php';
			cine = 'Movie Montevideo Shopping - Sincronizaci\u00F3n Cartelera';
		}
		else{
			url = RUTA+'ws/sincronizacion_completa_montevideo.php';
			cine = 'Movie Montevideo Shopping - Sincronizaci\u00F3n Completa';
		}
		
	}
	else if(idComplejo == 3){
		if(tipo == 1){
			url = RUTA+'ws/sincronizacion_portones.php';
			cine = 'Movie Portones Shopping - Sincronizaci\u00F3n Cartelera';
		}
		else{
			url = RUTA+'ws/sincronizacion_completa_portones.php';
			cine = 'Movie Portones Shopping - Sincronizaci\u00F3n Completa';
		}
		
		
	}
	else if(idComplejo == 4){
		url = RUTA+'ws/sincronizacion_teatro.php';
	}
	
	new Ajax.Request(url, {
		method: 'post',
		onSuccess: function(transport){
			var html = '<table border="0" cellpadding="0" cellspacing="0" width="700">';
			
			if(idComplejo < 4){
				html += '<tr><td class="height18"></td></tr>';
				html += '<tr><td class="recom bold" height="25">'+cine+'</td></tr>';
				html += '<tr><td class="bg_separation"></td></tr>';
						
				var peliculas = transport.responseText.split("|");

				//recorrer peliculas
				if(peliculas.length > 0){
					for(var i=0;i<(peliculas.length-1);i++){
						var data_peli = peliculas[i].split("--");
						html += '<tr><td class="recom" height="20">'+data_peli[1]+'</td></tr>';
					}
				}
				else{
					html += '<tr><td height="20" class="recom">No se encontraron películas en cartelera para esta semana</td></tr>';
				}
			}
			else{
				
				var obras = transport.responseText.split("|");
			
				html += '<tr><td class="height18"></td></tr>';
				html += '<tr><td class="recom bold" height="25">Teatro</td></tr>';
				html += '<tr><td height="25" class="bg_separation"></td></tr>';
				
				
				if(obras.length > 0 && obras[0] != ''){
					for(var x=0;x<obras.length;x++){
						html += '<tr><td height="20" class="recom">'+obras[x]+'</td></tr>';
					}
				}
				else{
					html += '<tr><td height="20" class="recom">No se encontraron obras en cartelera para esta semana</td></tr>';
				}
			
			}
			
			html += '</table>';
			//alert(html);
			$('img_title_sinc').src = RUTA+'img/title/cartelera_sincronizada.gif';
			$('contenido_sinc').innerHTML = html;
		}});
}

function open_popup(dir,my_width,my_height){
	var my_win = window.open(dir,null,'scrollbars=yes,width='+my_width+',height='+my_height+'');
	my_win.moveTo(window.screen.availWidth/2-(my_width/2), window.screen.availHeight/2-(my_height/2));
}

function cerrar_grilla(){
	window.close();
	if(opener.document.documentElement.scrollTop){
		opener.document.documentElement.scrollTop = 0;
	}else if(opener.document.body.scrollTop){
		opener.document.body.scrollTop = 0;
	}
	opener.crear_cuenta();
}

function cargar_banners(sitio){
	var url = RUTA+'ajax/cargar_banners.php';
	new Ajax.Request(url, {
		method: 'post',
		onSuccess: function(transport){
			var results = transport.responseText.split("|");
			for(var i=0; i<=results.length; i++){
				var value = results[i].split("-");
				var html = '';
				var banner = $('banner_'+value[0]);
				html = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="267" height="130" name="banner_swf_'+value[1]+'">';
                html += '<param name="movie" value='+RUTA+'"_swfs/banner.swf?'+value[2]+'" />';
                html += '<param name="quality" value="high" />';
                html += '<param name="allowScriptAccess" value="true" />';
                html += '<param name="wmode" value="transparent" />';
                html += '<param name="FlashVars" value="&'+value[2]+'"/>';
                html += '<embed src="'+RUTA+'_swfs/banner.swf" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="267" height="130" FlashVars="&'+value[2]+'" id="banner_swf_'+value[1]+'" wmode="transparent" allowScriptAccess="true"></embed>';
                html += '</object>';
				
				$('div_banner_'+value[0]).style.display = '';
				banner.innerHTML = html;
			}
		}
	});
}

var IE = navigator.appName.toLowerCase().indexOf("microsoft") > -1;

function only_numbers(evt){	
// NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57, '.'=46, ','  = 44
	var key = IE ? evt.keyCode : evt.which;
	return (key <= 13 || (key >= 48 && key <= 57));
}