/**
* @fileoverview ENT Site specific JS file
*
* @update Fri Oct 30 10:47:26 EDT 2009
* 
*/

	var site = {
		// set site name
		the_name: 'Department of Otorhinolaryngology | Weill Cornell Medical College',
		// set root absolute root dir
		root: 'http://www.cornellent.com/',
		// path to the health library article to server map
		service_map_location: 'http://test.cornellent.com/static/js/ent_hl_services_map.js'
	};
	
	
	// SIZE CREAM STRIP HACK --------------------------------
	// since there just doesn't seem to be a way 
	// to make that sucker work with out scroll bars
	function size_cream_strip(){
		var win_width = $(window).width(); 
		var cream_width = (win_width - 960) / 2 ;
		$('.cream_extend').css('width', cream_width + 'px');
	}
	
	// because of current model for json service/article mapping
	// we need to provide expetions until model update
	// we have a regular expressiong formatting bug where a \s followed by a / produces two __
	var services_format_exceptions = {
		"Acoustic Neuroma Neurinoma" : "Acoustic Neuroma (Neurinoma)",
		"Allergens Dust and Dust Mites" : "Allergens: Dust and Dust Mites",
		"Allergens Mold" : "Allergens: Mold",
		"Bells Palsy" : "Bell's Palsy",
		"Dermabrasion Dermaplaning" : "Dermabrasion/Dermaplaning",
		"Meniere Disease" : "Ménière's Disease",
		"pharyngitis  tonsillitis" : "Pharyngitis/Tonsillitis",
		"Age Appropriate Hearing Milestones" : "Age-Appropriate Hearing Milestones",
		"Age Appropriate Speech and Language Milestones" : "Age-Appropriate Speech and Language Milestones"
	};
	
	
	/* -----------------------------------------------------------------
	--------------------------------------------------------------------
	   JQUERY WRAPPER fire all events on document load
	--------------------------------------------------------------------
	----------------------------------------------------------------- */
	
	var DEBUG = false;
	
	$(document).ready(function(){
		
		// Uncomment the below line for debugging
		// to use: WC.Debug.write(content);
		if (DEBUG === true)
			WC.Debug.init('body');
		
		// fix me some ie6 transparency 
		$(document).pngFix(); 
		// init that wc object
		WC.init();
		// set our generic clear field function
		WC.focus_clear_input('#main_search','#333');
		// size the cream strip on homepage
		size_cream_strip();
		$(window).resize(function(){ size_cream_strip(); });
		
		
		
		/* --------------------------------------------------------------
		   ADD ENT GLOSSARY CONTENT TO GLOSSARY PAGE
		-------------------------------------------------------------- */
		
		if ($('#col_2').find('#glossary_content').length === 1)
			$('#glossary_content').append($('.ent_glossary').html());
		
		
		
		/* --------------------------------------------------------------
		   HIDE AND SHOW MORE CHILDREN - building on same functionality 
		   as hide/show health articles on service pages
		-------------------------------------------------------------- */
		
			//show_hide( '#related_physicians', 6 );
		
			function show_hide( selector, max_show ){
				var children = $(selector).children(),
					child_len = children.length,
					to_hide = child_len - max_show;
				if (child_len > max_show){
					for (var i = max_show; i < child_len; i+=1)
						$(children[i]).css('display','none');
					$(selector).append('<a href="" class="show">Show All</a>');
					$('a.show').bind('click', function(event){
						if (children.is(':hidden')){
							$(selector).children().css('display','inline');
							$(this).html('Show Less');
						}
						else {
							$(this).remove();
							show_hide( selector, max_show );
						}
						return event.preventDefault();
					});
				}
			}
		
		
		
		
		
		
		
				
		/* --------------------------------------------------------------
		   HEALTH LIBRARY FILTER EVENTS
		-------------------------------------------------------------- */
		
		if ($('body').attr('id') === 'health_library'){
			WC.HealthLibrary.filter.create_filter_collection('ul.library li a');
		}
		
		$('.hl_filter')
			.val('Filter Articles')
			.focus(function(){
				$(this)
					.val('')
					.css('color','#333')
					.keyup(function(){
						var query = $(this).val();
						WC.HealthLibrary.filter.filter_articles('#col_2', 'ul.library li', query);
					});
			}).blur(function(){
				// set a timeout to that the user has time to get a click event on an article
				// and so that you don't see the article links fade in at the same time as you
				// see the modal fade in - sloppy
				setTimeout("WC.HealthLibrary.filter.show_collection('ul.library li')", 300);
				$(this)
					.val('Filter Articles')
					.css('color','#777');
			});
			
			
			
			
		/* --------------------------------------------------------------
		   FACULTY MODALS EVENTS
		-------------------------------------------------------------- */
				
		$('.modal img, .modal a').click(function(event){
						
			var is_faculty_page = $('.our_faculty').length;
			var parent = $(this).parent();
			var offset = $(this).offset();
			// if a modal already exists, hide it and remove the copied image
			if ($('.modal_container_person').is(':visible'))
				$('.modal_container_person').hide().find('img').remove();
							
			var img_clone = parent.find('img').clone();
			var modal_cont  = parent.find('.modal_container_person');	
			var person_detail  = parent.find('.person_detail');
			
			// light up faculty div
			if (parent.hasClass('active'))
				$('.related_physician').removeClass('active');
			else {
				$('.related_physician').removeClass('active');
				parent.addClass('active');
			}
			
			// prepend the photo at a larger size
			person_detail.prepend(img_clone);
			// if on faculty page we need a rule that modal goes to right instead of left
			if (is_faculty_page)
				modal_cont.css({ 'left': '-25px', 'top': '-21px' });
			//fade in the modal
			modal_cont.fadeIn(300);
			// size the left repeating shadow
			$('.l', modal_cont).css('height', person_detail.outerHeight() - 6);
			// stop page from refreshing
			event.preventDefault();
		});
	
		
			
	
		/* --------------------------------------------------------------
		   HEALTH LIBRARY MODAL EVENTS
		-------------------------------------------------------------- */
		
		// bind the click function to each article link
		// init our modal object
		$('ul.library li a').bind('click', function(event){
			var article_id = $(this).attr('id');
			WC.HealthLibrary.modal.setup(article_id);
			event.preventDefault();
		});
		// reposition modal on window resizing
		$(window).bind('resize', function(){
			WC.HealthLibrary.modal.position_modal();
		});
		// bind the close function to the modal
		// use "live" since it is appended to the DOM
		$('.hl_modal #close').live('click', function(event){
			WC.HealthLibrary.modal.close();
			$('.hl_img_modal').remove();
			event.preventDefault();
		});
		

		// on the fly image viewer for health articles
		$('.hl_modal .thumbnail_align_right a, .hl_modal .thumbnail_align_center a').live('click', function(event){
			
			var lg_img_src = $(this).attr('href');
			var img_offset = $('img', this).offset();
			/**
			private method
			modal image
			*/
			var modal_img = function(stripped){
				// just clear any if they exist, no sniffing
				$('.hl_img_modal').remove();
				if (stripped != null){
					lg_img_src = stripped;
				}
				// append the img - it's css is display none
				$('body').append('<img class="hl_img_modal" src="' + lg_img_src + '" />');
				// bind the close click event to the large image
				$('.hl_img_modal').live('click', function(){ 
					$(this).fadeOut(300, function(){ $(this).remove() });
				});
				
				// establish our heights, widths and pos
				var appended_img_width = $('.hl_img_modal').width();
				var appended_img_height = $('.hl_img_modal').height();
				var img_pos_top = Math.floor(img_offset.top) - (appended_img_height / 2);
				var lg_img_pos_top = img_pos_top <= 140 ? Math.floor(img_offset.top) : img_pos_top;
		
				// if there is no width then we know the image does not exist
				// kill the script
				if (appended_img_width ===  0) return;
								
				// give our appended image some style and fade it in
				$('.hl_img_modal').css({
					'position': 'absolute',
					'left': $(window).width() / 2 - (appended_img_width / 2),
					'top': lg_img_pos_top,
					'z-index': '2000',
					'border': '10px solid #eee'
				}).fadeIn(300);	
			}
			
			// if there is a percent sign in the a href then we need to retrn the base only
			// then we fire the event with the stripped argument
			if (lg_img_src.indexOf('%') !== -1){
				var stripped = lg_img_src.match(/http\:\/\/.*(\.jpg|\.gif)/gm);
				modal_img(stripped);
				return event.preventDefault();
			}
			// else lets fire our modal function normal
			modal_img();
			// prevent default link behavior
			event.preventDefault();
		});
		
		
		
		/* --------------------------------------------------------------
		   SERVICES PAGES HEALTH LIBRARY / MODAL / EVENTS
		-------------------------------------------------------------- */
		
		
		
		if (page.section === 'Healthcare Services'){
			// grab the related articles
			WC.HealthLibrary.show_related_articles();			
			// bind modal event to appended article links on service pages
			$('#related_articles ul li a').live('click', function(event){
				var article_id = $(this).attr('id');
				if (article_id === 'toggle_article_list'){
					if ($(this).hasClass('showing')){
						$('#related_articles ul li.hide').removeClass('show');
						$(this).html('Show More').removeClass('showing');
					}
					else {
						$('#related_articles ul li.hide').addClass('show');
						$(this).html('Show Less').addClass('showing');
					}
				}
				else {
					WC.HealthLibrary.modal.setup(article_id);
				}
				event.preventDefault();
			});	
		}
				
	});
	
	



	
	
	
	