Toggleize = $.klass({
   initialize: function(to_hide) {
		this.to_hide = to_hide;
	},
   onclick: function() {
		$(this.element).parent().parent().find('p').toggleClass("hide");
		return false;
	}
 });

Zebralizer = $.klass({
	initialize: function(zebralizerElement) {
		this.zebralizerElement = zebralizerElement;
		$(this.element).find(zebralizerElement).removeClass('odd').end().find(zebralizerElement+':odd').addClass('odd');
	}
});
	
	
Searchleize = $.klass({
    initialize: function() {
	if ($('#search').val()){
		this.onsubmit();
	}
	if (!window.caSettings.USE_GOOGLE_DB_SEARCH){
		$("#search_where_cnt").hide(0);
	}
    },
    onsubmit: function() {
            //cargar spinner
//	$('#search-form input[type=submit]').attr('disabled', true);
	var statusesTr = {
		
'believed_alive_1':'Encontrado',
'believed_dead_1':'Encontrado',
'believed_missing_1':'Encontrado',
'information_sought_1':'Encontrado',
'is_note_author_1':'Encontrado',
'believed_alive_0':'Posiblemente Viva',
'believed_dead_0':'Posiblemente Muerta',
'believed_missing_0':'Posiblemente Perdida',
'information_sought_0':'Estado desconocido'
	
	};
	var q = $('#search').val();
	if (!q){
		return false;
	}
	var where = $('#search_where').val();
        $('#spinner').show();
	
	var newPeopleFinderUrl = 'http://peoplefinder.chileayuda.com/webservice/search?';
	var oldPeopleFinderUrl = 'http://dev.epistemonikos.org/chilepersonfinder/?';
	
	if (window.caSettings.USE_NEW_SEARCH){
		var peopleFinderUrl = newPeopleFinderUrl;
	} else {
		 var peopleFinderUrl = oldPeopleFinderUrl;
	}



	if (window.caSettings.USE_GOOGLE_DB_SEARCH){
		googleSource= 'google-cladb';
	} else {
		googleSource='google';
	}

	var name = "&name=" + escape(q);

	var info = '';
	if (where && window.caSettings.USE_GOOGLE_DB_SEARCH){
		info =  "&info=" + escape(where);
	}
	var calls = [
		{source:'google',url:peopleFinderUrl + "source=" + googleSource + name + info +  "&format=json&jsoncallback=?", target:'#search_results_google'}
	];

	if (window.caSettings.USE_TWITTER_SEARCH){
		calls.push({source:'twitter',url:peopleFinderUrl + "source=twitter" + name + info +  "&format=json&jsoncallback=?", target:'#search_results_twitter'});
	};
	
	var totalCalls = calls.length;
	var completedCalls = 0;
	for( var j=0;j<calls.length;j++){
		 
		$(calls[j].target).hide(0);
	        $.getJSON(calls[j].url, 
		(function(){
			var call=calls[j]; 
			return (function(data){
				completedCalls++;
					if (completedCalls==totalCalls){
			            $('#spinner').hide();
					$('#search-form input[type=submit]').attr('disabled', false);
				}
		            $(call.target + " tbody tr").remove();
			if (data && data.length>0){
		            $.each(data, function(i,item){
				var estado = item.estado;
				if (call.source=='google'){
					if (statusesTr[estado]){
						estado = statusesTr[estado];
					} else {	
						estado = 'Estado desconocido';
					}
				} else {
					estado = '';
				}
		                $(call.target + " tbody").append("<tr style='cursor:pointer!important;'><td class='texto'>"+item.texto+"</td><td class='center'>"+estado+"</td><td class='center'>"+ item.fuente+"</td><td class='center'><a href=\'"+item.enlace+"\'>Ver</a></td></tr>");

		            });
		            $(call.target).fadeIn('fast');
			 $(call.target).find("tr").removeClass('odd').end().find("tr:odd").addClass('odd');

		            $(call.target + ' td.texto').highlight(q);
		            $(q.split(" ")).each(function(){
   		                $(call.target + ' td.texto').highlight(this);
		            });
				$(call.target + ' td').click(function(){document.location = $(this).parents('tr').find('a').attr('href');});
			
			} else {
				$(call.target).hide(0);
			}
        		});
		})()
		);
	}
        return false;
    }
});

	
jQuery(document).ready(function(){
	$("#faq a").attach(Toggleize , "#faq p.to_hide");
	$("#search_results").attach(Zebralizer , "tr");
	$('#search-form').attach(Searchleize);	
	$('#search_results .relatize').relatizeDate();
	
	$("#box").tweet({
		avatar_size: 32,
		count: 7,
		query: "%23terremotochile+OR+%23chile+OR+%23chilequake+OR+%23chileayuda",
		loading_text: "Buscando en Twitter..."
	});
});


