$(function(){

	$("a.solapa_senal").click(function(){

		$.post(base_href+"services/common/cambiarsenal.php", {senal: $(this).attr("params")}, function(){
			
			if(typeof(redireccionar) == "undefined")
				window.location.reload(true);
			else
				location.href = base_href + redireccionar;

		})

		return false;

	})
	
	
	//dropdown país

	
	 jQuery("#select1").bind("mouseleave",function(){
	   $("#select1 li:first").show();
		jQuery("#select1 li").hide();	
		jQuery("#select1 li:first").show();
		return false;
    });


	$("#select1 li:first").show();	
	$("#select1 .fl, #select1 li:first").click(function(){
		$("#select1 li:first").hide();
		$("#select1 li").toggle();	
		$("#select1 li:first").toggle();
		return false;

	});

	
	$("#select1 ol li a").not(":first").click(function(){
			
		$.post(base_href+"services/common/cambiarpais.php", {pais: $(this).attr("href").replace("#", "")}, function(){
			if(typeof(redireccionar) == "undefined")
				window.location.reload(true);
			else
				location.href = base_href+redireccionar;
		})

		jQuery("#select1 li").hide();	
		jQuery(this).parent().show();	
		return false;

	})	
	
	//dropdown meses
	
	jQuery("#select2").bind("mouseleave",function(){
		$("#select2 li:first").hide();
		jQuery("#select2 li").hide();	
		jQuery("#select2 li:first").show();
		return false;
    });	
	
	$("#select2 li:first").show();	
	$("#select2 .fl, #select2 li:first").click(function(){
		$("#select2 li:first").hide();
		$("#select2 li").toggle();	
		$("#select2 li:first").toggle();
		return false;
	});	
	
	$("#select2 ol li a").not(":first").click(function(){
		
		$.post(base_href+"services/common/cambiarmes.php", {mes: $(this).attr("href").replace("#", "")}, function(){
			
			if(typeof(redireccionar) == "undefined")
				window.location.reload(true);
			else
				location.href = base_href + redireccionar;
				
			return false;

		})

		jQuery("#select2 li").hide();	
		jQuery(this).parent().show();	
		return false;

	})	

	$("li#mis_archivos").css("border", "none");
	$(".ui-effects-transfer").css("border", "none");
	
	
	//autocompletar campo título
	$("#filtro_buscador").click(function(){this.select()})
	$("#filtro_buscador").autocomplete("services/common/peliculas_idioma.php",
			{
				delay:10,
				minChars:2,
				matchSubset:1,
				matchContains:1,
				cacheLength:10,
				onItemSelect:selectItemBusqueda,
				onFindValue:findValueBusqueda,
				formatItem:formatItemBusqueda,
				autoFill:true,
				extraParams: {espanol: '1'}
			}
									
		);	
	
	
	//filtro búsqueda
	$("input[name='media']").click(function(){
		if($(this).is(":checked")){
			$("input[name='tipomedio_fotos']").attr("checked", "checked");
			$("input[name='tipomedio_videos']").attr("checked", "checked");
			$("input[name='tipomedio_comunicados']").attr("checked", "checked");
			$("input[name='tipomedio_personajes']").attr("checked", "checked");
			
		} else {
			$("input[name='tipomedio_fotos']").attr("checked", "");
			$("input[name='tipomedio_videos']").attr("checked", "");
			$("input[name='tipomedio_comunicados']").attr("checked", "");			
			$("input[name='tipomedio_personajes']").attr("checked", "");
		}
												
	})
	
	$("input[name='tipomedio_fotos']").click(tildarFiltro)	
	$("input[name='tipomedio_videos']").click(tildarFiltro)	
	$("input[name='tipomedio_comunicados']").click(tildarFiltro)	
	$("input[name='tipomedio_personajes']").click(tildarFiltro)
	
	//dropdown señales
	$("select[name='id_canal']").change(function(){
		
		$.post(base_href+"services/common/tiposprograma_senal.php", {id_canal: $(this).val()}, function(resp){
			$("select[name='tipoprograma']").html("");																						
			var s = "<option value=\"0\">Todos los tipos de programa</option>";
			for(i = 0; i < resp.length; i++){
				s += "<option value=\""+resp[i].id_tipoprograma+"\">"+resp[i].des_tipoprograma+"</option>";

			}
			$("select[name='tipoprograma']").html(s);																						
			
		}, "json" )
													 
	})
	
	//link header
	$("#link_header").click(function(){

		var link = $(this).attr("href");
		$.post("services/common/cambiarsenal.php", {senal: ""}, function(){

			location.href = base_href+link;

		})
		return false;
		
	})		
	
	//descarga de material
	$("a.link_descarga").click(function(e){
		
		e.preventDefault();
		var u = $(this).attr("href");
		
		confirmar(DISCLAIMER, function () {
										
			//var ext = u.substr(u.lastIndexOf(".")+1, 3);

			//if(ext.toLowerCase() == "jpg")
				//window.open(u);
			//else
				location.href = u;
		});		
		
		
										
	});
	
	
})


 //autocompletar
 function findValueBusqueda(li) {
	//if( li == null ) return alert("No match!");

	// if coming from an AJAX call, let's use the CityId as the value
	if( !!li.extra ) var sValue = li.extra[0];

	// otherwise, let's just display the value in the text box
	else var sValue = li.selectValue;

	//alert("The value you selected was: " + sValue);

	$("input[name='id_programa']").val(sValue);
	
	$("#cargando").fadeIn();
	
	$("form#form_filtro").get(0).submit();
	
	$("#campo_titulo").blur();
}

function selectItemBusqueda(li) {
	findValueBusqueda(li);
}

function formatItemBusqueda(row) {
	//return row[0] + " (id: " + row[1] + ")";
	return row[0];
}

function lookupAjaxBusqueda(){
	var oSuggest = $("#campo_titulo")[0].autocompleter;

	oSuggest.findValueBusqueda();

	return false;
}
var transferirAMisArchivos = function(elemento){
	elemento.effect("transfer", {to: "li#mis_archivos"}, 2000, function(){
		var cant = parseInt($("#cant_archivos_guardados").html(), 10);
		cant++;
		$("#cant_archivos_guardados").html(cant);
	})

}

var tildarFiltro = function(){
	if ($("input[name='tipomedio_fotos']").is(":checked") && $("input[name='tipomedio_videos']").is(":checked") && $("input[name='tipomedio_comunicados']").is(":checked") && $("input[name='tipomedio_personajes']").is(":checked") ){
		$("input[name='media']").attr("checked", "checked");
	} else {
		$("input[name='media']").attr("checked", "");
	}
	

}


function confirmar(message, callback) {
	$('#confirm').modal({
		zIndex: 2000000000,
		close:false,
		position: ["10px",],
		overlayId:'confirmModalOverlay',
		containerId:'confirmModalContainer', 
		onShow: function (dialog) {
			dialog.data.find('.message').append(message);

			dialog.data.find('.yes').click(function () {
				if ($.isFunction(callback)) {
					callback.apply();
				}
				$.modal.close();
			});
		}
	});
}    