// In case we forget to take out console statements. IE becomes very unhappy when we forget. Let's not make IE unhappy
if(typeof(console) === 'undefined') {
    var console = {}
    console.log = console.error = console.info = console.debug = console.warn = console.trace = console.dir = console.dirxml = console.group = console.groupEnd = console.time = console.timeEnd = console.assert = console.profile = function() {};
}


$(document).ready(function(){
	// Overlay Script
	var overlayScript = "/app_themes/Main/js/jquery.colorbox-min.js"
	if ( !!$(".openOverlay")[0] ) {
		$.getScript( overlayScript, function(data, status) {
			$(".openOverlay").colorbox({ iframe:true, width:'770px', height:'100%', href:$(this).attr('href'), transition: "none",
									   onCleanup: function(){ $('#colorbox iframe').attr('src', 'about:blank'); } } );
		});
	}

	if ( !!$(".videoCaption")[0] ) {
		$(".videoCaption a").click( function () { window.parent.location.href = this.href; return false; } );
	}

	// handle Search input value
	$("#search .searchText").val("Search Our Site");

	$("#search .searchText").focus(function(){
        var val = ($(this).val() != "Search Our Site") ? $(this).val() : "";
        $(this).val(val);
    });

    $("#search .searchText").blur(function(){
        var val = ($(this).val() != "") ? $(this).val() : "Search Our Site";
        $(this).val(val);
    });

	window.flashLaunchColorbox = function flashLaunchColorbox (url) {
		$.colorbox( { iframe:true, width:'770px', height:'100%', href:url, transition: "none", onCleanup: function(){ $('#colorbox iframe').attr('src', 'about:blank'); } } );
	}

	// handle homepage tabs
	if(document.getElementById("homeTabs")){
		$("#homeHealthTab").click(function(e){
			e.preventDefault();
			$("#homeHealthTab").css("background-position","left top");
			$("#hospiceTab").css("background-position","left bottom");
			$("#tab_homeHealth").show();
			$("#tab_hospice").hide();
		});
		$("#hospiceTab").click(function(e){
			e.preventDefault();
			$("#homeHealthTab").css("background-position","left bottom");
			$("#hospiceTab").css("background-position","left top");
			$("#tab_hospice").show();
			$("#tab_homeHealth").hide();
		})
	}

	// handle FAQ tabs
	if(document.getElementById("faqTabs")){
		$(".faqBox").hide();
		$(".faqBox").first().show();
		$("#faqTabs li").first().addClass("selected");

		$(".feature_expandable").addClass("allClosed");

		setTimeout( function () { $(".faqBox").each( function () { $(this).find(".expandPlus").first().click() } ) }, 5 );

		$("#faqTabs li a").click(function(e){
			$("#faqTabs li").removeClass("selected");
			$(this).parents("li").addClass("selected");
			var targ = $(this).attr("href");
			$(".faqBox").stop(true,true).fadeOut(100);
			$(".faqBox").filter( targ ).stop(true,true).delay(99).fadeIn(300);
		});
	}

	// handle expandable features

	$(".feature_expandable .expandPlus").show();
	$(".feature_expandable .feature_body").hide();

	$(".feature_expandable .expandPlus").attr("rel","-");

    $(".feature_expandable .expandPlus, .feature_expandable h3").click(function(){
		var plus = ($(this).attr("class") == "expandPlus") ? $(this) : $(this).siblings(".expandPlus");
		
		var oldRel = plus.attr("rel");
		var newRel = plus.text();
		plus.text(oldRel);
		plus.attr("rel", newRel);
		

		$(this).parents(".feature_expandable").children(".feature_body").toggle();
    });


	if ( !!window.location.hash === false ) {
		if ( !$(".feature_expandable").hasClass("allClosed") ) {
			$(".feature_expandable .expandPlus").first().click();
		}
	} else {
		var itemId = window.location.hash;
		$(itemId).children(".expandPlus").click();
		setTimeout( function () { $("html").scrollTop( $("html").scrollTop() - 30 ); }, 50 );
	}


	// handle topnav selected states

	if(window.location.href.toLowerCase().indexOf("/patients-and-caregivers") != -1){
		$("#nav_patients").addClass("selected");
	}

	if(window.location.href.toLowerCase().indexOf("/physicians-and-health-care-professionals") != -1){
		$("#nav_physicians").addClass("selected");
	}

	if(window.location.href.toLowerCase().indexOf("/our-company") != -1){
		$("#nav_company").addClass("selected");
	}

	if(window.location.href.toLowerCase().indexOf("/news-media") != -1){
		$("#nav_news").addClass("selected");
	}

	if( window.location.href.toLowerCase().indexOf("/careers") != -1 ){
		$("#tnav_careers").addClass("selected");
	}

	if( window.location.href.toLowerCase().indexOf("/investors") != -1 ){
		$("#tnav_investors").addClass("selected");
	}

	if( window.location.href.toLowerCase().indexOf("/find-a-care-center") != -1 ){
		$("#tnav_carecenters").addClass("selected");
	}
	
	// cycle the masthead images
	if(document.getElementById("masthead")){
		$("#masthead").cycle({fx: 'fade'});
	}
	
	// cycle the Featured News items
	if( document.getElementById("newsCycle") ){
		$("#newsCycle").cycle({
			fx: 'fade',
			timeout:7000,
			pause: 1,
			pager:'#featuredNewsNav',
			pagerAnchorBuilder: function (idx, slide) {
				return '<li><a href="#">'+(idx+1)+'</a></li>';
			}
		});
	}

	if (window.location.href.toLowerCase().indexOf("/careers") != -1) {
        $("#leftnav a[href^='http://']").attr("target", "_blank");
    }
	
	
	$(".gmapinfowindow").parent().parent().css("overflow", "hidden");
});

