jQuery(document).ready(function($) {

	//result tabs + accordion
	jQuery(function() {
			$("#tabs").tabs();			
		});
	
	
	// tab close button clicked to clear tab
	jQuery('.close-button').click(function(){ 
		var tab_id = jQuery(this).attr("id");
		var reset_url = jQuery('#reset_tab_url').val();
		var process_graph_url = jQuery('#process_graph_url').val();
		
	 if(tab_id==1){
				jQuery('#tab-title-'+tab_id).text("Compare A Country");
		}else if(tab_id==2){
		jQuery('#tab-title-'+tab_id).text("Add 2nd Country");
		}
		else if(tab_id==3){
				jQuery('#tab-title-'+tab_id).text("Add 3rd Country");
		}
		
		
		
		
		reset_tab(tab_id,reset_url);
		process_graph(process_graph_url);	
		
	});
	
	// select a new country to add
	jQuery('.countrySelect-go').live("click", function(){ 

		var country_id = jQuery('.countrySelect').eq(jQuery('.countrySelect-go').index(this)).val();
		var member_id = jQuery('#member_id').val();
		var tab_id = jQuery('.tab_id').eq(jQuery('.countrySelect-go').index(this)).val();
		var process_url = jQuery('#process_country_url').val(); 
		var process_graph_url = jQuery('#process_graph_url').val();
 
		//	change title
		var country_name = jQuery('.countrySelect option:selected').eq(jQuery('.countrySelect-go').index(this)).text();
		jQuery('#tab-title-'+tab_id).text(country_name);
	
		process_country_select(country_id,member_id,tab_id,process_url,process_graph_url);
		pause(.2) 
	
		process_graph(process_graph_url);	
		pause(.2); 
	
		alert('New Country Added');
		// build the accordion list 
		jQuery("#accordion-"+tab_id).accordion({
							header: 'h3',
							collapsible: true,
							autoHeight: false

				});
		// build the mouse-over info bullets
		jQuery('span.popinfo').hoverbox();
		
	});
	
});

function process_country_select(country_id, member_id, tab_id, process_url, process_graph_url){
	//alert(question_num+" - "+answer);
	
	new Ajax.Updater(	'tabs-'+tab_id,
	 					process_url,
						{	method: "post",
					 		postBody: 'country_id='+country_id+'&member_id='+member_id+'&tab_id='+tab_id
						}
					);
};

function process_graph(process_graph_url){
	new Ajax.Updater(	'bigGraphBar',
						process_graph_url,
						{	method: "post",
							postBody:'country_id=hi'
						}
					);
};


function reset_tab(tab_id, reset_url){
	new Ajax.Updater(	'tabs-'+tab_id,
						reset_url,
						{	method: "post",
							postBody: 'tab_id='+tab_id
						}
		);
};

function build_accord(tab_id){
//	alert('pause');
alert("sss");

	
	jQuery("#dialog").dialog({
				bgiframe: true,
				modal: true,
				buttons: {
					Ok: function() {
						jQuery(this).dialog('close');
					}
				},
				close: function(){
					alert('111');
				}
			});
	
}


function pause(seconds) {
	var milliseconds = seconds*1000;
	var dt = new Date();
	while ((new Date()) - dt <= milliseconds) { /* Do nothing */ }
}


