// AJAX PageRequestManager instance
var prm;

$(document).ready(function() {

    prm = Sys.WebForms.PageRequestManager.getInstance();
    //if (prm != null) {
    prm.add_pageLoaded(setupTooltips);
    prm.add_pageLoaded(wireSearchBoxJobs);
    //};

    //Main Navigation Slide Down Effect
    $("#main_nav ul.main_navigation > li").mouseover(function () {
        var sub_nav = $(this).children(".sub_nav");
        var info_text = $(sub_nav).children(".sub_nav_info_container").children(".sub_nav_info_text");
        var photo = $(sub_nav).children(".sub_nav_info_container").children(".sub_nav_photo");
        $(sub_nav).stop(true, true);
        $(sub_nav).slideDown(400, function () {
            $(info_text).height(($(sub_nav).innerHeight() - 35) - $(photo).innerHeight());
        });
    }).mouseleave(function () {
        $(this).children(".sub_nav").slideUp(200);
    });

    //Search Box Text
    wireSearchBox(".search_box.main");
    wireSearchBox(".search_box.main_results");
    wireSearchBox(".search_box.fad");
    wireSearchBox(".search_box.pnp");


    //Follow Us Hover Effects
    $(".follow_us a.facebook").mouseover(function () {
        $(".follow_us a.facebook").stop(true, true);
        $(".follow_us a.facebook").animate({
            left: '+=5'
        }, 200);
    }).mouseleave(function () {
        $(".follow_us a.facebook").stop(true, true);
        $(".follow_us a.facebook").animate({
            left: '-=5'
        }, 200);
    });

    $(".follow_us a.twitter").mouseover(function () {
        $(".follow_us a.twitter").stop(true, true);
        $(".follow_us a.twitter").animate({
            left: '+=5'
        }, 200);
    }).mouseleave(function () {
        $(".follow_us a.twitter").stop(true, true);
        $(".follow_us a.twitter").animate({
            left: '-=5'
        }, 200);
    });

    $(".follow_us a.rss").mouseover(function () {
        $(".follow_us a.rss").stop(true, true);
        $(".follow_us a.rss").animate({
            left: '+=5'
        }, 200);
    }).mouseleave(function () {
        $(".follow_us a.rss").stop(true, true);
        $(".follow_us a.rss").animate({
            left: '-=5'
        }, 200);
    });

    //Rotating Slides on the home page
    $(".slide_content").carouFredSel({
        direction: "left",
        items: { visible: 1 },
        scroll: { items: 1, pauseOnHover: true, duration: 2000, easing: 'swing' },
        auto: { pauseDuration: 9000 },
        pagination: { container: '.slide_navigation' }
    });

    //Once you click one of the slide buttons, stop the auto animation
    $(".slide_navigation").click(function () {
        $(".slide_content").trigger("pause", true);
    });

    //Rotating winners of Color by Kids categories
    $(".category_winners").each(function () {
        //Find the category index from the ID and setup a carousel for each category
        var i = $(this).attr("id");
        i = i.replace("category", "");
        $("#category" + i).carouFredSel({
            direction: "left",
            items: { visible: 1 },
            scroll: { items: 1, pauseOnHover: true, duration: 1000, easing: 'swing' },
            auto: { pauseDuration: 5000 }
        });
    });

    //Once you click the next/previous buttons, stop the auto animation and fire the event
    $(".prev_winner").click(function (e) {
        //Find the category index from the ID and use it trigger prev/next on correct category carousel
        e.preventDefault();
        var i = $(this).attr("id");
        i = i.replace("prev", "");
        $("#category" + i).trigger("pause", true);
        $("#category" + i).trigger("prev", 1);
    });

    $(".next_winner").click(function (e) {
        //Find the category index from the ID and use it trigger prev/next on correct category carousel
        e.preventDefault();
        var i = $(this).attr("id");
        i = i.replace("next", "");
        $("#category" + i).trigger("pause", true);
        $("#category" + i).trigger("next", 1);
    });

    //Colorbox
    $(".colorbox_photo").colorbox({ opacity: "0.75", photo: true, maxHeight: "80%", maxWidth: "80%" });
    $(".colorbox_website").colorbox({ opacity: "0.75", iframe: true, height: "80%", width: "80%" });
    $(".colorbox_calendar").colorbox({ opacity: "0.75", inline: true, maxHeight: "80%", onComplete: function () {
        adjustDayHeight();
        prm.add_pageLoaded(adjustDayHeight);
    }
    });

    //Move to next step of Patient Pre-Registration Form
    $(".next_step").click(function (e) {
        e.preventDefault();
        var current_step = $(".step.active");
        var next_step = $(current_step).next(".step");
        $(current_step).removeClass("active");
        $(next_step).addClass("active");
        var yPos = $(".step.active").offset();
        window.scrollTo(0, yPos.top);
    });

    //Move to previous step of Patient Pre-Registration Form
    $(".prev_step").click(function (e) {
        e.preventDefault();
        var current_step = $(".step.active");
        var next_step = $(current_step).prev(".step");
        $(current_step).removeClass("active");
        $(next_step).addClass("active");
        var yPos = $(".step.active").offset();
        window.scrollTo(0, yPos.top);
    });

    //Move to next step of Patient Pre-Registration Form
    $(".submit_prereg").click(function () {
        $(".step.active").removeClass("active");
        $(".step.one").addClass("active");
        setupErrorLinks();
    });

    //Hide (Collapse) the toggle containers on load
    $(".toggle_container").hide();

    //Switch the "Open" and "Close" state per click then slide up/down (depending on open/close state)
    $("a.trigger").click(function () {
        $(this).toggleClass("active").next().slideToggle("slow");
        return false; //Prevent the browser jump to the link anchor
    });

    //Form wizard navigation
    $("#application_steps a, .form_button_container a").click(function (e) {
        e.preventDefault();
        var formstep = $(this).attr("href");
        var formstepID = formstep.replace('#', '');
        $("#application_steps a, .form_button_container a").removeClass("active");
        $("#job_application .step, .form_button_container .step").removeClass("active");
        $("#application_steps, .form_button_container").find("a." + formstepID).addClass("active");
        $(formstep).addClass("active");
    });

    //Job Application Form - No Work History - Populates 1st job box with NA
    //    $(".no-work-history input").mousedown(function () {
    //        $("#jobs-container").slideToggle("slow");
    //        return false; //Prevent the browser jump to the link anchor
    //    });

});

//Tooltips
function setupTooltips() {
	$("a.event_day").tooltip({
		tip: $(this).next(".tooltip").attr("id"),
		relative: true,
		// custom positioning
		position: 'top left',
		offset:[14, 46],
		// there is no delay when the mouse is moved away from the trigger
		delay:30
	});	
}

function adjustDayHeight() {
	//Set all Calendar days to the same height
	var dayHeight = 0;
	$(".Calendar tr").each(function() {
			var rowHeight = $(this).height();						
			if (rowHeight > dayHeight)
			{
				dayHeight = rowHeight;
			}
	});	
	
	$("#event_calendar_container .day, #event_calendar_container .weekend, #event_calendar_container .other_month_day, #event_calendar_container .today, #event_calendar_container .event_day").height(dayHeight);
	
	$.colorbox.resize();
}

function wireSearchBox(className)
{
	//Search Box Text
	var search_box = $(className + " input[type='text']");
	$(search_box).val("Search SMC");
	$(search_box).focus(function() {
		if ($(search_box).val() == 'Search SMC') {
			$(search_box).val("");
		};
	});
	
	$(search_box).blur(function() {
		if ($(search_box).val().trim() == '') {
			$(search_box).val("Search SMC");
		};
	});
}

function wireSearchBoxJobs() {
    //Search Box Text
    var search_box = $(".search_box.three_col input[type='text']");
    $(search_box).val("Search Jobs");
    $(search_box).focus(function () {
        if ($(search_box).val() == 'Search Jobs') {
            $(search_box).val("");
        };
    });

    $(search_box).blur(function () {
        if ($(search_box).val().trim() == '') {
            $(search_box).val("Search Jobs");
        };
    });
}

function setupErrorLinks() {
    $("a.error_step1").click(function (e) {
        e.preventDefault();
        $(".step.active").removeClass("active");
        $(".step.one").addClass("active");
    });

    $("a.error_step2").click(function (e) {
        e.preventDefault();
        $(".step.active").removeClass("active");
        $(".step.two").addClass("active");
    });

    $("a.error_step3").click(function (e) {
        e.preventDefault();
        $(".step.active").removeClass("active");
        $(".step.three").addClass("active");
    });

    $("a.error_step4").click(function (e) {
        e.preventDefault();
        $(".step.active").removeClass("active");
        $(".step.four").addClass("active");
    });

    $("a.error_step5").click(function (e) {
        e.preventDefault();
        $(".step.active").removeClass("active");
        $(".step.five").addClass("active");
    });
}

