// start jquery goodness
$(document).ready(function(){
	
	//get the iCare mission projects file, and run the initial parse
	$.ajax({
		url: "/files/scripts/icare/icare_projects_json.js",
		dataType: "json",
		success: function(json)
		{
		    icare = json;
		    details_load();
		},
		error:  details_fail
	});
	
}); //end $(document).ready()
// end jquery goodness

function details_load()
{
    //number of projects
    projectCount = icare.projects.length;
    //choose random project if not already defined
    if(typeof project_id == 'undefined')
        project_id = Math.floor( Math.random()*projectCount); 
    else if (project_id < projectCount-1)
        project_id++;
    else
        project_id = 0;
        
    //create output
    output = '';	
	output += '<h4>'+ icare.projects[project_id].name +'</h4>';
	if(icare.projects[project_id].image)
        output += '<div class="img"><a href="'+ icare.projects[project_id].page +'"><img src="'+ icare.projects[project_id].image +'"></a></div>';
    output += '<p>'+ icare.projects[project_id].hook +'</p>';
	output += '<a href="'+ icare.projects[project_id].page +'">Read More &gt;&gt;</a>';
	//output += '&nbsp;|&nbsp;';
	//output += '<a href="#" onclick="show_new_icare_project(); return false;">Next iCare Project &gt;&gt;</a>';
    
    //append output
	$("#icare_project_rotation").append(output);
}

function show_new_icare_project(){
	$("#icare_project_rotation").fadeOut("fast",function(){
		$("#icare_project_rotation").empty();
		details_load();
		$("#icare_project_rotation").fadeIn("fast");
	});
	return false;
}
    
function details_fail()
{
    alert('Could not parse projects');
}
