function toggle(element)
{
    $('#' + element).toggle('slow');
    var arrow = $('#' + element + '_arrow').text()
    if (escape(arrow) == '%BB')
    {
        $('#' + element + '_arrow').html('&#171;');
    }
    else if (escape(arrow) == '%AB')
    {
        $('#' + element + '_arrow').html('&#187;');
    }
    return false;
}
function toggle2(element1,element2)
{
    $('#' + element1).toggle('slow');
 	$('#' + element2).toggle('slow');
   
    return false;
}
/* show hover preview for regions */
function previewRegion(region, src){
	document.but.src=src;
	hideRegions();
	$('#'+region+'Div').show();
	
}
/* set the correct background and div */
function showWorldView(src, region){
	hideRegions();
	document.but.src=src;
	$('#'+region+'Div').show();
}
function showResultView(src, region){
	hideRegions();
	document.but.src=src;
	$('#allDiv').show();
}
/* hide all region divs*/
function hideRegions(){
	$('#africaDiv').hide();
	$('#eurorusDiv').hide();
	$('#middleeastDiv').hide();
	$('#southamericaDiv').hide();
	$('#northamericaDiv').hide();
	$('#asiaDiv').hide();
	$('#allDiv').hide();
}
function getResults(whichDiv, startId, howmany, region, country, theme){
	//$().ajaxStart($.blockUI).ajaxStop($.unblockUI)
	//$.blockUI();
	var url = "/dy/inc/browseprojects/gg.html?cmd=more&region=" + region+ "&country="+country+"&tag="+theme+"&count="+howmany+"&startId="+startId
	//$.get(url, function(data) { assignResults(data, whichDiv); }, "html");
	$.ajax({
			type: "GET",
			url: "/dy/inc/browseprojects/gg.html?cmd=more&region=" + region+ "&country="+country+"&tag="+theme+"&count="+howmany+"&startId="+startId,
			contentType: "application/html",
			cache: false,
			error: function(xhr, msg){                      
				var errors = eval(xhr.getResponseHeader("actionErrors"));
				var result = '';
				if (errors && errors.length > 0)
				{   
					for (var i = 0; i<errors.length; i++)
					{
						result += errors[i] + '\n';
					}
				}
				//alert(result + "Could not retrieve projects, sorry!");
				$('#resultsContainerID').unblock();	
			},
			success: function(msg, textStatus){
				var response = msg; //JSON.parse(msg);
				if (response)
				{
					$('#'+whichDiv).html(response);		
					//alert($('#'+whichDiv).html());
					$('#resultsContainerID').unblock();	
					$(function() {
				        $('a.remote').remote('#chapter', function() {
				            if (window.console && window.console.info) {
				                console.info('content loaded');
				            }
				        });     
				        $.ajaxHistory.initialize();
				    });        
				}
			}
		});
	// $.unblockUI();

}
function assignResults(data, whichDiv){
	$('#'+whichDiv).html(data);	
}
function initiateSearch(startId, howmany, region, country, theme){ 
	if(this.FirstPage){
	//$().ajaxStop($.unblockUI); 
	
	$('#resultsContainerID').block({  
	                message: '<br><h1><img src="http://cloud.globalgiving.org/img/background/busy.gif" /> &nbsp; looking for projects ...</h1>',  
	                css: { border: '1px solid #db8600', width:'300px'}  
	            });
	getResults('currentResults', startId, howmany, region, country, theme);
	this.selectedRegion = region;
	this.selectedCountry = country;
	this.selectedTheme = theme;
	this.startProjectId = startId;
	//$('#resultsContainerID').unblock();
	//$.unblockUI();	 
	this.FirstPage = false;
	}
}
function updateResultCounter(total){ 
	this.startCounter = this.anfang+1; 
	this.totalProjects = total;
	this.endCounter = this.endCounter<total? this.endCounter:total;
//	alert(endCounter);
	$('#whichResults').html('Results <b>'+this.startCounter+' - '+ this.endCounter+'</b> of <b>'+total+'</b>'); 
	if(this.totalProjects>this.endCounter){  
		$('#nextResultsPointer').show(); 
		$('#nextResultsPointer2').show(); 
	}
	 $('#resultsContainerID').unblock();
	
}
function updateResultCounter2(total, endcount){ 
	if(endcount!=-1){
	   this.endCounter = endcount;
	}
	else{
		 this.endCounter = this.anfang+this.PROJECTS_PER_PAGE;
	}
    updateResultCounter(total); 
}
function showAll(){ 

} 
function showFirst3(){  
	 this.allShown = false; 
    //$('#showOptions').html('<a href="/dy/inc/browseprojects/gg.html?cmd=more&region='+this.selectedRegion+'&country='+this.selectedCountry+'&tag='this.selectedTheme+'&count='+this.totalProjects+'&startId=0" onclick="showAll(\''+this.selectedRegion+'\',\''+this.selectedCountry+'\',\''+this.selectedTheme+'\');return false;"  >show All&nbsp;</a>'); 
	//$('#nextResultsPointer').show();  
	//this.startCounter = -2;    
	//$('#previousResults').html($('#currentResults').html());  
	//getResults('currentResults', 0 ,PROJECTS_PER_PAGE, this.selectedRegion, this.selectedCountry, this.selectedTheme);
	//$('#currentResults').html($('#previousResults').html());
	window.scrollTo(0,0); 
	this.startCounter = 1; 
	this.endCounter = 0; 
	this.endCounter += this.totalProjects > this.endCounter + PROJECTS_PER_PAGE ? PROJECTS_PER_PAGE: this.totalProjects-this.endCounter;
	updateResultCounter(this.totalProjects);
	//$('#previousResultsPointer').show();
	if(this.totalProjects>this.endCounter){  
		$('#nextResultsPointer').show(); 
		$('#nextResultsPointer2').show();    
		//alert('preloading next results starting with: '+this.startProjectId+' endcounter: '+ this.endCounter +' total number of projects: '+this.totalProjects); 
		getResults('nextResults', this.startProjectId,PROJECTS_PER_PAGE, this.selectedRegion, this.selectedCountry, this.selectedTheme);
	}
	else{
		$('#nextResultsPointer').hide();
		$('#nextResultsPointer2').hide();  
		//alert('hiding next pointer, endcounter: '+ this.endCounter +' total number of projects: '+this.totalProjects);
	}
}  

var anfang = 0;
var PROJECTS_PER_PAGE = 3;
var selectedRegion = "all";
var selectedCountry = "all";
var selectedTheme = "all";
var startProjectId = 1234;   
var firstProjectId = 1234;   
var startCounter = 1;
var endCounter = 3;
var totalProjects = 0;
