var navOpen = false;
var whichNav = '';

$(document).ready( function() {

	if(IE6) {
		document.execCommand("BackgroundImageCache",false,true);
	}

	//
	// CAROUSEL -- homepage
	//
	$('#carousel ul').dynamItCarousel({
		next: '.arrowright',
		prev: '.arrowleft',
		width: 966,
		wraparound: true,
		slideshow: true,
		slideshowtimeout: 7000
	})

	//
	// DROPDOWN MENUS
	//
	//$('ul#nav').dynamItSlide();
    $('ul#nav').children('li').children('ul').addClass('opened');
    $('ul#nav').children('li').children('a').not('#nav-find, #nav-qual').attr('href', 'javascript:;').click(function() {
        if( navOpen ) {
            if(whichNav == $(this).attr('id')) {
                navOpen = false;
                $('ul.opened').slideUp('slow');
            } else {
                $('ul.opened').hide();
                $(this).siblings('ul').show();
                whichNav = $(this).attr('id');
            }
        } else {
            $(this).siblings('ul').slideDown('slow');
            navOpen = true;
            whichNav = $(this).attr('id');
        }
        return false;
    });

	//
	// AUX NAV SINGLE DROPDOWNS (Locations, My Adena)
	//
	$('.droptab').click( function() {
		$(this).children('ul').slideToggle('slow');
	});

	//
	// HOMEPAGE NEWS : ACCORDION
	//
	$('#homenews li a.open').click( function() {

		var id = $(this).parent().parent().attr('class');
		$('#homenews li').removeClass('open');
		$('#homenews li:not(.'+id+') div.exp').slideUp('slow',function () {
			$('#homenews li.'+id).addClass('open');
			$('#homenews li.'+id+' div.exp').slideDown('slow');
		});
		return false;
	});
	$('#homenews li a.close').click( function() {
		$('#homenews li').removeClass('open');
		$(this).parent().parent().find('div.exp').slideUp('slow');
		return false;
	});
	
	//
	// HOMEPAGE NEWS/CEO: SWAP
	//
	$('#homecontent a.hp_swap_news').click(function() { 
		$('#homenews .wrap.ceo').addClass('hidden');
		$('#homenews .wrap.news').removeClass('hidden');
		return false;
	});
	$('#homecontent a.hp_swap_ceo').click(function() { 
		$('#homenews .wrap.news').addClass('hidden');
		$('#homenews .wrap.ceo').removeClass('hidden');
		return false;
	});	
	
	//
	// forms and stuff.
	//
	$('input#physicianname, input#physq').focus( function() {
		if( $(this).val() == $(this).attr('title') ) {
			$(this).val('');
		}
	}).blur( function() {
		if( $(this).val() == '' ) {
			$(this).val( $(this).attr('title') );
		}
	});

	if(!IE6) {
		$('input.btn').hover( function() {
			$(this).addClass('on');
		}, function() {
			$(this).removeClass('on');
		});
	}

	//
	// FIND A DOCTOR - AUTO COMPLETE
	//
	$('input#physicianname').dynamItAutoComplete( {
		url: '/find/namecomplete',
		onselect: function() {
			var $field = $(this).closest('ol').siblings('input');

			$('#physicianid').val( $(this).attr('title') );
			$field.val( $(this).text() );
			$(this).closest('ol').remove();
			return false;
		}
	} );

	$('input#physq').dynamItAutoComplete( {
		url: '/find/autocomplete',
		onselect: function() {
			location.href = $(this).attr('href');
		}
	} );

	//
	// attach the date picker
	//
	$('.dateselect').datepicker();
	$('.daterangeselect').datepicker({rangeSelect: true});
	$('.dateselectfuture').datepicker({minDate: 0});
	
	//
	// Testimonial Preview Touts
	//
	$('#testimonials a').click(function() {
		var id = $(this).attr('id').replace('t_','');
		
		preview_testimonial(id);
		return false;
	});
	
}).click( function(e) {

   if( navOpen ) {
        if( !$(e.target).closest("#nav").length ) {

            navOpen = false;
            $('ul.opened').slideUp('slow');
        }
    }

});


/**
 * Load News Video
 *
 * Loads an associated news video for a given news slug.
 *
 * @param string q (News Slug)
 */
function loadNewsVideo(q) {
	dynamItLoad('/news/video.dT/' + q, null, 600, 'auto', null);
}




/** ******************************************************** **/
/**                                                          **/
/**   colon health survey - bwhitman - 2011-02-24            **/
/**                                                          **/
/** ******************************************************** **/

var surveyWidth = 462;
var surveyIndex = 0;
var surveyNumQuestions = 0;


/**
 * Launch Survey
 * 
 * Shows a survey with a given url and applies the supplied title.
 * Displays as an AJAX Model Dialog
 *
 * @param string url
 * @param string title
 */
function adenaLaunchSurvey(url, title) {
	dynamItLoad(url, null, 492, 'auto', function() {
		// reset it
		surveyIndex = 0;
		// attach actions to nav
		$('ol.surveynav li a').click(function() {
			var i = parseInt($(this).attr('href').substring(1));
			surveyIndex = i - 1;
			adenaSurveyDialog(1);
			return false;
		});
		// store number of questions
		surveyNumQuestions = $("#numquestions").val();
	});

	/// google analytics event tracking
	_gaq.push(['_trackEvent', title, 'Launch Survey', '']);
}


/**
 * Send Survey
 *
 * Submits a completed survey.
 *
 * @param  object (form object)
 * @param  string title
 * @return 
 */
function adenaSendSurvey(f, title) {
	// google analytics event tracking
	_gaq.push(['_trackEvent', title, 'Submit Survey', '']);
	// submit survey
	return dynamItPublish(f);
}
 
 
/**
 * Load Survey Page
 *
 * Used to move backward/forward through a survey.
 *
 * @param int direction
 */
function adenaSurveyDialog(direction) {
	surveyIndex += direction;
	$("ul.survey").animate({
		left: -1*surveyWidth*surveyIndex + 'px'
	}, 900);

	// if on last question change button to submit
	if(surveyIndex == surveyNumQuestions) {
		$(".surveydialog .next").hide();
		$(".surveydialog .submit").show();
	} else if( surveyIndex == 0 ) {
		$(".surveydialog .prev").hide();
	} else { 
		$(".surveydialog .prev").show();
		$(".surveydialog .next").show();
		$(".surveydialog .submit").hide();
	}

	// update nav
	$("ol.surveynav").children('li').removeClass('active').eq(surveyIndex-1).addClass('active');

}




/** ******************************************************** **/
/**                                                          **/
/**   testimonials - bill - 2011-06-01                       **/
/**                                                          **/
/** ******************************************************** **/


/**
 * Load Testimonial Preview
 *
 * Loads a preview for a given testimonal id.
 * Displayed as an AJAX Model Dialog
 *
 * @param int id
 */
function preview_testimonial(id) {
	
	// Preview Box Size
	var h = 650;
	var w = 576;

	// Get Preview Data
	$.getJSON('testimonials/preview?id='+id, function(data) {
		
	  	// Build HTML Display
		var body = '';
		body += '<div class="pop_bg">';
		body += "  <h2>"+ data.title + " <span class='sub'>" + data.category + "</span></h2>";
		body += "  <div class='hr'></div>";
		body += "  " + data.body;
		body += '</div>';

		dynamItOpenDialog(body, w, h);

	});

}


/**
 *
 *
 *
 */
function map_info_block(address, loc_title) {

	var geocoder = new google.maps.Geocoder();
	var latlng = new google.maps.LatLng(39.968261, -83.005217);
		
	geocoder.geocode( {'address': address}, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK) {
	        if (results[0]) {          
	            var latlng = new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng());
			    var myOptions = {
			      zoom: 15,
			      center: latlng,
			      mapTypeId: google.maps.MapTypeId.ROADMAP
			    };
			    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
			    var marker = new google.maps.Marker({ position: latlng, map: map, title: loc_title, icon: "img/location/marker.png"});   
			}
		}
	});
	
	$('#get_directions').click(function() {
		var visitor_address =  $('#visitor_address').val();
		var adena_address = $('#map_address').html();
		var url = 'http://maps.google.com/maps?saddr='+visitor_address+'&daddr='+adena_address;
		window.open(url,'_blank');
	});
	
}


