
/*
 rules: 
	1) less is more.  Do as much data work with PHP as possible
	2) URL encode all javascript
	
	//test



*/
/* FOR MAP */


function getUrlVars() { 
	var map = {}; 
	var parts = window.location.search.replace(/[?&]+([^=&]+)(=[^&]*)?/gi, function(m,key,value) { 
		map[key] = (value === undefined) ? true : value.substring(1); }); 
	return map; 
}

var ticks_array;
var map = null;
var geocoder = null;
Array.prototype.flip=function(a1, a2){ var tmp=this[a2]; this[a2]=this[a1]; this[a1]=tmp; }

/* END FOR MAP */
var map_has_been_created = 0;

$(document).ready(function(){

$('.track_pdf_views_download').click(function() { 
		
		var theUrl = $(this).attr('href');
	
		_gaq.push(['_trackPageview', theUrl]);
		//console.log(theUrl);
		return true;
		
		
	});


$('input[name="sub_category"]').change(function() { 
	$(this).parent().submit();

});
$('input[name="population"]').change(function() { 
	$(this).parent().submit();

});
$('.track_resources').click(function() { 
	var track_resources = $(this).attr('href');
	_gaq.push(['_trackPageview',  track_resources]);
	return true;
});
/*
$.fn.qtip.styles.withtooltip {
	border: {
      width: 5,
      radius: 6,
      color: '#A0ABA7'
   },
   background: '#F8F6EB',
   color: '#333'
}
*/
$('.with-tooltip').qtip({
	style: { 
		'background': '#F8F6EB', 
		'padding': '10px'
	},
	content: false,
	target: 'bottomRight'
	

});



$('.endo_pic').qtip(
	
   {
	  style: 'cream',
      content: 'Endocrinology - This clinic specializes in treating diabetes and other hormone-related conditions.  Practices without this symbol are primary care. Primary care doctors care for a wide range of conditions and are often a patient\'s first contact for medical help.'   
   }
);

var store_value = $('#p').val();
$('#home_page_search #measure_list_dropdown').change(function() { 

	if($(this).val() == "43") { 
		
		$('#p').val('hospitals_patient_experience');
		$('#category_section_tmp').val('patient_experience');
	} else { 
		$('#p').val(store_value);
	}
	
	if($("#sf").val() == 'hospital' && $(this).val() == '1') { 
			$('#p').val('hospital_reports');
			$('#category_section_tmp').val('outcome_of_care');
	
	
	}
	if($("#sf").val() == 'clinic' && $(this).val() == '4') { 
		
		$('#category_section_tmp').val('category_condition');
		
	}
	
	
});
/*
$('.health_tracker_click_link').click(function() { 
	pageTracker._trackPageview('/healthtracker_download/');
	return true;
});*/
$('#page_sort').change(function() { 
	if($(this).val() != "") {
		var sorting_info = String($(this).val()).split('||');
		
		if(sorting_info[0] == 'asc') { 
			sort_order = 'asc';
		} else if(sorting_info[0] == 'desc') { 
			sort_order = 'desc';
		} else { 
			sort_order = 'desc';
		}
			sort_column = sorting_info[1];
			
		url_items = getUrlVars();
		
		var url_string = "";
		
		$.each(url_items, function(index, value) {
			if(index != 'sort_order' && index != 'sort_column') {
				url_string = url_string + "&" + index + '=' + value;	
			}
		});
		url_string = url_string + '&sort_order=' + sort_order + '&sort_column=' + sort_column;
		
		window.location = "index.php?" + url_string + "#skip_to_data_anchor";
	}
	
});
	//DD_belatedPNG.fix('.pngfix');
	
	$('#nav ul li').hover(
		function(){		
			$(this).find('img').attr('src', ''+$(this).find('img').attr('src').replace('_off', '_over')+'');
		},
		function(){		
			$(this).find('img').attr('src', ''+$(this).find('img').attr('src').replace('_over', '_off')+'');
		}		
	);
		
	$('#toggle_advanced_search').click(function(){
		if( $('#search_doctors_input').val() == '' ){
			$('#advanced_search').show();
			$('#search_doctors_input').val('on');
		}
		else {
			$('#advanced_search').hide();
			$('#search_doctors_input').val('');		
		}
	});

	/*-----Contact Form toggle email/phone ---- */
	if( $('#contact_phone_selected').attr('checked') ){
		$('#contact_email').hide();
		$('#contact_phone').show();		
	}
	
	$('#contact_email_selected').click(function(){
		$('#contact_phone').hide();
		$('#contact_email').show();
	});
	$('#contact_phone_selected').click(function(){
		$('#contact_email').hide();
		$('#contact_phone').show();
	});

$('#conditions_modal').jqm();

$('#conditions_modal_open').click(function() { 

	$('#conditions_modal').jqmShow();

});

$('.pe_categories_modal').jqm();
$('.pe_four_cats').click(function() { 
	$('.pe_categories_modal').jqmShow();
});
$('.pe_four_cats_close').click(function() { 
	$('.pe_categories_modal').jqmHide();
});

$('.hosp_poc_scip_treatments_jqm').jqm()

$('.hosp_poc_scip_treatments_jqm_open').click(function() { 
	$('.hosp_poc_scip_treatments_jqm').jqmShow();
	
});

$('.hosp_poc_scip_treatments_jqm').click(function() { 
	$('.hosp_poc_scip_treatments_jqm').jqmHide();
	return false;
});


$('#conditions_modal_close').click(function() { 
	$('#conditions_modal').jqmHide();
	return false;
});
// search clearing form items

$('input[name=search_phrase]').keypress(function(){ 
		$('input[name=zipcode]').val("");	
});
$('input[name=zipcode]').keypress(function(){ 
		$('input[name=search_phrase]').val("");	
});


// end search clearing form

( $.cookie('tcookie') ) ? tchange($.cookie('tcookie')) : tchange('text_0');

$('#text_size a').click(function() { 
	tchange($(this).attr('id'));
});

function tchange(t) { 
switch(t){ 
		case 'text_0' 	: $('body').css({'font-size' : '80%'}); break;
		case 'text_1' 	: $('body').css({'font-size' : '87%'}); break;  
		case 'text_2' 	: $('body').css({'font-size' : '95%'}); break;  
	}
	
	$('.active_size').removeClass('active_size'); 
	$('#' +t+'').addClass('active_size'); 
	$.cookie('tcookie', ''+t+'');

}



		flipA=0;
		flipB=0;
/*---------------------------------------------------------------------------------------------------------------------------------------
	OUR REPORTS
---------------------------------------------------------------------------------------------------------------------------------------*/




$('#read_more_measure_text_button').click( function(){ 
		
	// the standard measure descriptions
	if( ($(this).attr('src') == 'assets/buttons/read_more.gif') || ($(this).attr('src') == 'assets/buttons/read_less.gif') ){
		($(this).attr('src') == 'assets/buttons/read_more.gif') ? $(this).attr('src', 'assets/buttons/read_less.gif') : $(this).attr('src', 'assets/buttons/read_more.gif'); 
	}
	else {
		( $(this).attr('src') == 'assets/buttons/read_more_top.gif') ? $(this).attr('src', 'assets/buttons/read_less_top.gif') : $(this).attr('src', 'assets/buttons/read_more_top.gif'); 
	}
	
	$('.read_more_copy_area').toggle('fast'); 
	return false; 

});

$('#read_more_measure_text_button2').click( function(){  // the data
	// the standard measure descriptions
	if( ($(this).attr('src') == 'assets/buttons/read_more.gif') || ($(this).attr('src') == 'assets/buttons/read_less.gif') ){
		($(this).attr('src') == 'assets/buttons/read_more.gif') ? $(this).attr('src', 'assets/buttons/read_less.gif') : $(this).attr('src', 'assets/buttons/read_more.gif'); 
	}
	else {
		( $(this).attr('src') == 'assets/buttons/read_more_bottom.gif') ? $(this).attr('src', 'assets/buttons/read_less_bottom.gif') : $(this).attr('src', 'assets/buttons/read_more_bottom.gif'); 
	}
	
	$('.read_more_copy_area2').toggle('fast'); 
	return false; 
});


	// Setup Read More for cost information
	
	$('#procedure_info_readmore').click( function(){ $('#procedure_info_text2').toggle('fast', function(){ flipA++; button = (flipA%2)? $('#procedure_info_readmore').attr('src', 'assets/buttons/read_less.gif') : $('#procedure_info_readmore').attr('src', 'assets/buttons/read_more.gif'); } ); return false; } );
	
	// cost landing
	$('#read_more_cost_landing_button').click( function(){ $('#read_more_cost_landing').toggle('fast', function(){ flipA++; button = (flipA%2)? $('#read_more_cost_landing_button').attr('src', 'assets/buttons/read_less.gif') : $('#read_more_cost_landing_button').attr('src', 'assets/buttons/read_more.gif'); } ); return false; } );
	
	// Setup Legend Display link
	$('#legend_link').click( function(){ $('#legend').toggle('slow', function(){ flipB++; button = (flipB%2)? $('#legend_link').text('Hide Legend') : $('#legend_link').text('What do these numbers mean?'); } ); return false; } );
	
	// Setup Compare Button
	$('#compare_button').click(function(){ sendToCompare($('#category').val(), $('#sub_category').val()); });


/*---------------------------------------------------------------------------------------------------------------------------------------
	COST DATA
---------------------------------------------------------------------------------------------------------------------------------------*/


	// reload page on cost cat filter change
	$('#cost_cat_filter').change(function() { 	
				
		var url;
		url = "?p=" + $('#page').val() +
			  "&category=" + $('#category').val() + 
			  "&sf=group" + 
			  "&cost_cat_filter=" + $(this).val() + 
			  "&search_phrase=" + $('#search_phrase').val() + 
			  "&cost_cpt_filter=" + $('#cost_cpt_val').val() + "";

			window.location = url;			
	});
	$('#cost_cpt_filter').change(function() { 	
				
		var url;
		url = "?p=" + $('#page').val() +
			  "&category=" + $('#category').val() + 
			  "&sf=group" + 
			  "&cost_cat_filter=" + $('#cost_cat_filter').val() + 
			  "&search_phrase=" + $('#search_phrase').val() + 
			  "&cost_cpt_filter=" + $(this).val() + "";

			window.location = url;			
	});

/*---------------------------------------------------------------------------------------------------------------------------------------
	HIT DATA
---------------------------------------------------------------------------------------------------------------------------------------*/


$('.read_more_measure_text_button_hit').click(function() { 
	var div_to_show_and_hide = $(this).parent().find('.read_more_measure_text_hit');
	div_to_show_and_hide.toggle();
	if($(this).html() == 'Read More &gt;') { 
		$(this).html('&lt Read Less');
	} else { 
		$(this).html('Read More &gt;');
	}
	return false;
})
$('.hit_grid').jqm();

$('.hit_grid_jqm').click(function() { 
	$('.hit_grid').jqmShow();	
	//return false;
});

$('.hit_grid_jqm_close').click(function() { 
	$('.hit_grid').jqmHide();	
	//return false;
});

$('.hit_statewide').jqm();

$('.hit_statewide_show').click(function() { 
	$('.hit_statewide').jqmShow();	
	//return false;
});
$('.hit_statewide_close').click(function() { 
	$('.hit_statewide').jqmHide();	
	//return false;
});

$('.hit_example').jqm();
$('.hit_example_open').click(function() { 
	$('.hit_example').jqmShow();	
	//return false;
});
$('.hit_example_close').click(function() { 
	$('.hit_example').jqmHide();	
	//return false;
});


/*---------------------------------------------------------------------------------------------------------------------------------------
	PATIENT EXPERIENCE
---------------------------------------------------------------------------------------------------------------------------------------*/


/*---------------------------------------------------------------------------------------------------------------------------------------
	HOSPITALS
---------------------------------------------------------------------------------------------------------------------------------------*/

$('.hosp_poc_ha_jqm').jqm();

$('.hosp_poc_ha').click( function(){ $('.hosp_poc_ha_jqm').jqmShow(); } ); 
$('.hosp_poc_ha_close').click(function(){ $('.hosp_poc_ha_jqm').jqmHide(); }); 



$('.hosp_poc_hf_jqm').jqm();

$('.hosp_poc_hf').click( function(){ $('.hosp_poc_hf_jqm').jqmShow(); } ); 
$('.hosp_poc_hf_close').click(function(){ $('.hosp_poc_hf_jqm').jqmHide(); }); 



$('.hosp_poc_p_jqm').jqm();

$('.hosp_poc_p').click( function(){ $('.hosp_poc_p_jqm').jqmShow(); } ); 
$('.hosp_poc_p_close').click(function(){ $('.hosp_poc_p_jqm').jqmHide(); }); 

$('.hosp_poc_scip_jqm').jqm();
$('.hosp_poc_scip').click( function(){ $('.hosp_poc_scip_jqm').jqmShow(); } ); 
$('.hosp_poc_scip_close').click(function(){ $('.hosp_poc_scip_jqm').jqmHide(); }); 


function sendToCompare(category, sub_category){
	
	var sf = $('#sf').val();
	var search_phrase = $('#search_phrase').val();
	var population = $('#population').val();

	// Get all of the compare check boxes
	compare_check_boxes = $('.compare_checkbox');
	category_section = $('#category_section').val();
	
	// Build a string of the checked boxes
	checked_boxes = '';
	for(i=0; i<compare_check_boxes.length; i++){
		checked_boxes = (compare_check_boxes[i].checked)? checked_boxes+compare_check_boxes[i].value+"," : checked_boxes+"";
	}
	
	// Send checked boxes along with category/sub_category to comparison page (if 2 or 3 entries have been checked)
	if( (checked_boxes.split(",")).length !=3 && (checked_boxes.split(",")).length !=4 ){ alert("Please choose either 2 or 3 entries for comparision."); }
	else {
	if($('#page').val() == 'cost_reports'){
		window.location = "?p=cost_compare&sf="+sf+"&category="+category+"&sub_category="+sub_category+"&compare="+checked_boxes+"&search_phrase="+search_phrase+"&category_section="+category_section+"";	
	} else {
		window.location = "?p=compare&sf="+sf+"&category="+category+"&sub_category="+sub_category+"&compare="+checked_boxes+"&search_phrase="+search_phrase+"&category_section="+category_section+"&population="+population;
	}
	}
	return false;
}
/*---------------------------------------------------------------------------------------------------------------------------------------
	PROFILE
---------------------------------------------------------------------------------------------------------------------------------------*/
		$('#show_overview').click(function(){
			$('#profile_affiliations').hide(); 	$('#show_affiliations').removeClass('selected');	
			$('#profile_location').hide();		$('#show_location').removeClass('selected');
			$('#profile_overview').show();		$('#show_overview').addClass('selected');
			
			$('#profile_category_data').show();
			
			return false;
		});
		$('#show_affiliations').click(function(){
			$('#profile_location').hide();		$('#show_location').removeClass('selected');
			$('#profile_overview').hide();		$('#show_overview').removeClass('selected');
			$('#profile_affiliations').show();	$('#show_affiliations').addClass('selected');

			$('#profile_category_data').hide();

			return false;
		});
		$('#show_location').click(function(){
			$('#profile_overview').hide();		$('#show_overview').removeClass('selected');
			$('#profile_affiliations').hide();	$('#show_affiliations').removeClass('selected');
			$('#profile_location').show();		$('#show_location').addClass('selected');

			$('#profile_category_data').hide();
			sf = $('#sf').val();
			name_id = $('#name_id').val();
			category = $('#category').val();
			xml = $('#category').val();
			if(!map_has_been_created){
				showMap(sf, name_id, category, address_line);
			}
			return false;
		});		
// FLOT 

			
var d1;
var d2;
				    
// END FLOT
function showMap(sf, name_id, category, address_lines){
	
	map_has_been_created=1;
	showAddress(address_lines);	
		$('#map_link').append("<br /><a target='_blank' href='http://maps.google.com/maps?q="+addressline1+" "+addressline2+" "+city+" "+state+" "+zipcode+"'>Find this Location on Google Maps >> </a><br /><br />");
		

}


});
