// requires jquery library

var LeapCPStart = $.extend({},{
	
	init: function() {
		// $('#ratings_button').css({cursor:'pointer'}).bind('click', function(e) { LeapCPStart.clickRatingsButton(); });
		$('#safety_button').css({cursor:'pointer'}).bind('click', function(e) { LeapCPStart.clickProceduresButton(); });
		$('#checkbox_show_all').bind('click', function(e) { LeapCPStart.clickShowAllButton(); })
		
		if (this._getButtonValue('ratings') == '1') {
			this._activateRatings();
		} else {
			this._deActivateRatings();
		}
		if (this._getButtonValue('procedures') == '1') {
			this._activateProcedures();
		} else {
			this._deActivateProcedures();
		}
		
		$('#criteria').bind('click', function(e) { LeapCPStart.updateSearchForm(); })
		this._initFocusFields();

		$('#popup .item .itemLabel').bind('click', function() {
			LeapCPStart.toggleCheckbox(this.id);
		});

		this.updateSearchForm();
		
		// init tooltips
		$('img.info').tooltip({
			extraClass: 'summary',
			delay: 200,
			showURL: false
		});
	},
	
	clickRatingsButton: function() {
		// if (this._getButtonValue('ratings')  == '1') {
		// 	this._deActivateRatings();
		// 	this._setButtonValue('ratings',0);
		// } else {
		// 	this._activateRatings();
		// 	this._setButtonValue('ratings',1);
		// }

		// ratings always required
		this._setButtonValue('ratings',1);
	},
	
	clickProceduresButton: function() {
		if (this._getButtonValue('procedures')  == '1') {
			this._deActivateProcedures();
			this._setButtonValue('procedures',0);
		} else {
			this._activateProcedures();
			this._setButtonValue('procedures',1);
		}
		
		$('#checkbox_show_all').attr('checked', false);
	},
	
	clickShowAllButton: function() {
		if ($('#checkbox_show_all').attr('checked')) {
			// activate ratings
			this._activateRatings();
			this._setButtonValue('ratings',1);

			// activate procedures
			this._activateProcedures();
			this._setButtonValue('procedures',1);

			// check all
			$('.procedure_type_item').attr('checked',true);
		} else {
			// deactivate procedures
			this._deActivateProcedures();
			this._setButtonValue('procedures',0);

			// check all
			$('.procedure_type_item').attr('checked',false);
		}
	},

	toggleCheckbox: function(label_id) {
		var group_code = this.idToGroupCode(label_id);
		$('#procedure_type_item_'+group_code).click();
	},
		

	idToGroupCode: function(element_id) {
		return element_id.replace(/^(.*_).*?/,"");
	},


	_activateRatings: function() {
		this._changeSrcToRed('ratings_button');
	},
	_deActivateRatings: function() {
		this._changeSrcToGreen('ratings_button');
	},
	
	_activateProceduresWithHiddenPopup: function() {
		// update img src
		this._changeSrcToRed('safety_button');
		// hide div
		$('#popup').hide();
	},

	_activateProcedures: function() {
		// update img src
		this._changeSrcToRed('safety_button');

		// show div
		$('#popup').show();
	},
	_deActivateProcedures: function() {
		// update img src
		this._changeSrcToGreen('safety_button');

		// hide div
		$('#popup').hide();
	},


	
	_changeSrcToRed: function(btn_id) {
		var src = $('#'+btn_id).attr('src');
		src = src.replace(/_g\.jpg/, '_r.jpg');
		$('#'+btn_id).attr('src', src);
	},
	_changeSrcToGreen: function(btn_id) {
		var src = $('#'+btn_id).attr('src');
		src = src.replace(/_r\.jpg/, '_g.jpg');
		$('#'+btn_id).attr('src', src);
	},
	
	_getButtonValue: function(button_type) {
		var val = $('#show_'+button_type).attr('value');
		if (typeof val == 'undefined') return '0';
		if (val.length == 0) val = '0';
		return val;
	},
	_setButtonValue: function(button_type, value) {
		$('#show_'+button_type).attr('value',value);
	},
	
	
	/* search form */
	updateSearchForm: function() {
		var value = $('#criteria').val();
		switch(value) {
			case 'zip':
				$('input.zip').show();
				$('select.miles').show();
				$('input.city').hide();
				$('select.state').hide();
				$('input.hospitalname').hide();
			break;
			case 'state':
				$('input.zip').hide();
				$('select.miles').hide();
				$('input.city').hide();
				$('select.state').show();
				$('input.hospitalname').hide();
			break;
			case 'hospital':
				$('input.zip').hide();
				$('select.miles').hide();
				$('input.city').hide();
				$('select.state').hide();
				$('input.hospitalname').show();
			break;
			case 'city':
			default:
				$('input.zip').hide();
				$('select.miles').hide();
				$('input.city').show();
				$('select.state').show();
				$('input.hospitalname').hide();
			break;
		}
		
	},
	
	_initFocusFields: function() {
		// attach focus events
		$('input.zip').bind('focus', function(e) {
			if ($(this).val() == 'Zip Code') $(this).val('');
		});
		$('input.zip').bind('blur', function(e) {
			if ($(this).val() == '') $(this).val('Zip Code');
		});
		
		
		$('input.city').bind('focus', function(e) {
			if ($(this).val() == 'city') $(this).val('');
		});
		$('input.city').bind('blur', function(e) {
			if ($(this).val() == '') $(this).val('city');
		});
		

		$('input.hospitalname').bind('focus', function(e) {
			if ($(this).val() == 'Hospital Name') $(this).val('');
		});
		$('input.hospitalname').bind('blur', function(e) {
			if ($(this).val() == '') $(this).val('Hospital Name');
		});
		
		
		
	},

	_end: null
});


