$(document).ready(function(){


/* Setup Search
------------------------------------------------------------ */
	
	if(	$('#t2_input').val() == '' ){
		$('#t2_input').val('Hospital name or City');
		$('#t2_input').addClass('fadded');
	}
	
	$('#t2_input').click(function(){
		if($(this).val() == 'Hospital name or City'){
			$(this).val('');
			$('#t2_input').removeClass('fadded');
		}
	});

	if(	$('#t3_input').val() == '' ){
		$('#t3_input').val('Zip code');
		$('#t3_input').addClass('fadded');
	}
	
	$('#t3_input').click(function(){
		if($(this).val() == 'Zip code'){
			$(this).val('');
			$('#t3_input').removeClass('fadded');
		}
	});
	
	
	
	$('#t2_submit').click(function(){
		if($('#t2_input').val() == 'Hospital name or City'){
			return false;
		}
	});	
	$('#t3_submit').click(function(){
		if($('#t3_input').val() == 'Zip code'){
			return false;
		}
	});

	
	
/* Setup Text Size Changer 
------------------------------------------------------------ */
	
	( $.cookie('tcookie') )? tchange( $.cookie('tcookie') ) : tchange( 'text0' );
	$('.tchange').click(function(){ tchange( $(this).attr('id') ); return false; });


	function tchange(t){
		switch(t){ 
			case 'tchange_0' 	: $('body').css({'fontSize' : '100%'}); break;
			case 'tchange_1' 	: $('body').css({'fontSize' : '105%'}); break;  
			case 'tchange_2' 	: $('body').css({'fontSize' : '110%'}); break;  
		}
		$('.tchange').removeClass('tchange_selected'); 
		$('#'+t+'').addClass('tchange_selected'); 
		$.cookie('tcookie', ''+t+'');
	}
	

/* Setup Table Sort 
------------------------------------------------------------ */

	$('#data_table').tablesorter(
		{ widgets: ['zebra'] }
	);


/* Setup tool tips 
------------------------------------------------------------ */

   $("<div id='qtip_blanket'>").css({
         position: 'absolute',
         top: $(document).scrollTop(), // Use document scrollTop so it's on-screen even if the window is scrolled
         left: 0,
         height: $(document).height(), // Span the full document height...
         width: '100%', // ...and full width

         opacity: 0.7, // Make it slightly transparent
         backgroundColor: 'black',
         zIndex: 5000  // Make sure the zIndex is below 6000 to keep it below tooltips!
      })
      .appendTo(document.body) // Append to the document body
      .hide(); // Hide it initially

	
	$('.report_description_button').each(function(){
		
		$(this).qtip({
			content: {
				title: {
					text: 'Report Info:',
					button: 'Close (x)'
				},
				text: $(this).parent().find('.report_description').html()
			},
			show: {
				when: 'click', // Show it on click
				solo: true // And hide all other tooltips
			},
			position: {
				target: $(document.body), // Position it via the document body...
				corner: 'topMiddle', // ...at the center of the viewport
				adjust: {
					y: 10
				}
			},
			hide: false,
			style: {
				fontSize: '0.7em',
				lineHeight: '1.2em',
				fontFamily: 'verdana',
				background: '#ffffff',
				color: 'black',
				textAlign: 'left',
				width: 850,
				padding: '10px',
				border: {
					width: 5,
					radius: 5,
					color: '#555555'
				},
				name: 'light'
			},
			api: {
				beforeShow: function(){
					// Fade in the modal "blanket" using the defined show speed
					$('#qtip_blanket').fadeIn(this.options.show.effect.length);
				},
				beforeHide: function(){
					// Fade out the modal "blanket" using the defined hide speed
					$('#qtip_blanket').fadeOut(this.options.hide.effect.length);
				}
			}
		});

		
	});


	$('.category_description_button').each(function(){
		
		$(this).qtip({
			content: {
				title: {
					text: 'Category Info:',
					button: 'Close (x)'
				},
				text: $(this).parent().find('.category_description').html()
			},
			show: {
				when: 'click', // Show it on click
				solo: true // And hide all other tooltips
			},
			position: {
				target: $(document.body), // Position it via the document body...
				corner: 'topMiddle', // ...at the center of the viewport
				adjust: {
					y: 10
				}
			},
			hide: false,
			style: {
				fontSize: '0.7em',
				lineHeight: '1.2em',
				fontFamily: 'verdana',
				background: '#ffffff',
				color: 'black',
				textAlign: 'left',
				width: 850,
				padding: '10px',
				border: {
					width: 5,
					radius: 5,
					color: '#555555'
				},
				name: 'light'
			},
			api: {
				beforeShow: function(){
					// Fade in the modal "blanket" using the defined show speed
					$('#qtip_blanket').fadeIn(this.options.show.effect.length);
				},
				beforeHide: function(){
					// Fade out the modal "blanket" using the defined hide speed
					$('#qtip_blanket').fadeOut(this.options.hide.effect.length);
				}
			}
		});

		
	});

	
	$('.entry_profile_button').each(function(){
		
		$(this).qtip({
			content: {
				title: {
					text: 'Profile:',
					button: 'Close (x)'
				},
				text: $(this).parent().find('.entry_profile').html()
			},
			show: {
				when: 'click', // Show it on click
				solo: true // And hide all other tooltips
			},
			position: {
				target: $(document.body), // Position it via the document body...
				corner: 'center', // ...at the center of the viewport
				adjust: {
					y: 10
				}
			},
			hide: false,
			style: {
				fontSize: '0.7em',
				fontFamily: 'verdana',
				background: '#ffffff',
				color: 'black',
				textAlign: 'left',
				width: 450,
				padding: '10px',
				border: {
					width: 5,
					radius: 5,
					color: '#555555'
				},
				name: 'light'
			},
			api: {
				beforeShow: function(){
					// Fade in the modal "blanket" using the defined show speed
					$('#qtip_blanket').fadeIn(this.options.show.effect.length);
				},
				beforeHide: function(){
					// Fade out the modal "blanket" using the defined hide speed
					$('#qtip_blanket').fadeOut(this.options.hide.effect.length);
				}
			}
		});
		
	});	
		
	
	$('.selectable_item').each(function(){
		//$(this).append('asdf');
		
		max_length = 90;
		test_string = this.innerHTML;
				
		if( test_string.length > max_length ){

			$(this).qtip({
				content: $(this).html(),
				show: {
					delay: 0
				},
				position: { 
			      corner: {
			         target: 'topLeft',
			         tooltip: 'bottomLeft'
			      },
			      adjust: { 
			      	x: 7, 
			      	y: 0 
			      }
				},
				style: { 
					fontSize: '0.7em',
					fontFamily: 'verdana',
					width: 300,
					padding: 5,
					background: '#caf1ff',
					color: 'black',
					textAlign: 'left',
					border: {
						width: 2,
						radius: 3,
						color: '#0a7da8'
					},
					tip: 'bottomLeft',
					name: 'dark' // Inherit the rest of the attributes from the preset dark style
				}

			});
			
			// Truncate
			test_string = test_string.substring(0, max_length);
			test_string = test_string.replace(/\w+$/, '');
			
			test_string += '...';
			
			$(this).html(test_string);
		}
	});

	
/* Setup Collapse / Expand 
------------------------------------------------------------ */

	$('#hide_show').click(function(){
		
		hide_choices = $('#hide_choices');
		show_choices = $('#show_choices');
		
		
		if( $(this).hasClass('hide')){
			
			$('.selectable_item').each(function(){
				if(! $(this).hasClass('selected')){
					$(this).fadeOut("fast");
				}
			});
			

			hide_choices.fadeOut('fast', function(){
				show_choices.fadeIn('fast');
			});
			
			$(this).removeClass('hide');
			
		}
		else {
			
			$('.selectable_item').fadeIn('fast', function(){
				show_choices.fadeOut('fast', function(){
					hide_choices.fadeIn('fast');
				});
			});
			
			$(this).addClass('hide');
			
		}
		
		return false;
		
	});
	
	
/* Setup View More 
------------------------------------------------------------ */

	$('#info_view_more').click(function(){
		$('#info_measure_description').fadeIn();
		$(this).fadeOut('fast', function(){
			$('#info_view_less').fadeIn('fast');
		});
		return false;
	});

	$('#info_view_less').click(function(){
		$('#info_measure_description').fadeOut();
		$(this).fadeOut('fast', function(){
			$('#info_view_more').fadeIn('fast');
		});
		return false;
	});

});
