// JavaScript Document

var $api_key = '01fa42883937103d60f1f3227c08d7d479d4651a';
var $basepath = '/';

function centres_init(){
	$("a[rel='centreOpener']").click(function(e){
		
		e.preventDefault();
		var $id = $(this).attr("href");
		$id = $id.replace("#","");		
		if($('#centre_'+$id+'_info').length == 0){			
			collapse_other_centres();
			
			$(this).text("Less info");
			//alert($basepath + 'process.php?api_key='+$api_key+'&a=get.centre&cid='+$id);
			$.getJSON($basepath + 'process.php?api_key='+$api_key+'&a=get.centre&cid='+$id,function(data){
				//add the rowspann to the first column of the parent td
				$("#centre_"+$id+" td").eq(0).attr("rowspan","3");
				var centre_html = '<tr id="centre_'+$id+'_info" class="centre_extra_info"><td colspan="3"><span class="centre-left-space">Contact:</span><span class="centre-right-space">'+data.post_centre_contact;
				if(data.post_centre_email != ""){
					centre_html += '<br /><a href="mailto:'+data.post_centre_email+'">'+data.post_centre_email+'</a>';
				}//end if
				centre_html += '</span></td></tr><tr id="centre_'+$id+'_info_2" class="centre_extra_info"><td colspan="3"><span class="centre-left-space">Location:</span><span class="centre-right-space">'+data.post_message;
				
				if(data.post_centre_lat != ""){
					centre_html += '<br /><a href="'+$basepath+'map.php?id='+$id+'&api_key='+$api_key+'&iframe=true&width=620&height=500" rel="prettyPhoto">view on the map</a>';
				}//end if
				centre_html += '</span></td></tr>';
				$("#centre_"+$id).after(centre_html);	
				$("a[rel^='prettyPhoto']").prettyPhoto(); 			
			});
		} else {
			$(this).text("More info");
			collapse_centre($id);
		}//end if
	});
}

function collapse_other_centres(){
	$("a[rel='centreOpener']").text("More info");
	$('.centre_title').attr("rowspan",1);
	$(".centre_extra_info").remove();
}

function collapse_centre(id){
	$('.centre_title').attr("rowspan",1);
	$('#centre_'+id+'_info').remove();
	$('#centre_'+id+'_info_2').remove();
}

$.fn.sameHeights = function() {
	
	$(this).each(function(){
		var tallest = 0;
		
		$(this).children().each(function(i){
		if (tallest < $(this).height()) { tallest = $(this).height(); }
		});
		$(this).children().css({'height': tallest});
	});
	return this;
};
