/// <reference path="jquery-1.6.2-vsdoc.js" />
/// <reference path="jquery-ui-1.8.14.custom.min.js" />
/// <reference path="jquery.hoverIntent.js" />


// define the top-level namespace object.
dm = {};

// define the namespace object for shared features.
dm.core = {};
dm.core.loggingEnabled = true;
dm.core.loggingToViewEnabled = false;
dm.core.animationCount = 0;


$(document).ready(function () {

	try {
		dm.core.registerValuePropAnimations();
	} catch (e)
	{ dm.core.logMessage("core error - registerValuePropAnimations: " + e.message); }

	try {
		dm.core.generateTestimonials();
	} catch (e)
	 { dm.core.logMessage("core error - generateTestimonial: " + e.message); }

	try {
		dm.core.activateSlider($(".defaultSlider"), 8000, 750, true);
	} catch (e)
	  { dm.core.logMessage("core error - activateSlider: " + e.message); }
});


dm.core.activateSlider = function (sliderElement$, timeToCycleSlider, fadeSpeed, showControls) {

	if (!sliderElement$ || sliderElement$.length == 0) {
		return;
	}

	var options =
	{
		autoplay: true,
		showControls: showControls,
		showPosition: false,
		wait: timeToCycleSlider,
		fade: fadeSpeed
	};

	sliderElement$
		.show()
		.slider(options);
};




dm.core.getHoverPluginConfig = function (overFunc, outFunc, delay) {

	var hiConfig = {
		sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)
		interval: delay, // number = milliseconds for onMouseOver polling interval
		timeout: 100, // number = milliseconds delay before onMouseOut
		over: overFunc,
		out: outFunc
	};

	return hiConfig;
};



dm.core.registerValuePropAnimations = function () {

	var mainInteractiveArea$ = $("#home-main-subsection-supporting-learning-details");
	var detailsContainer$ = $("#learningDetailsContainer");

	if (!mainInteractiveArea$ || mainInteractiveArea$.length < 1) {
		return;
	}

	var hiConfigMain = dm.core.getHoverPluginConfig(
		function () { // over function
			detailsContainer$.slideDown(200);
		},
		function () { // leave function
			detailsContainer$.slideUp(200);
		},
		100);

	mainInteractiveArea$.hoverIntent(hiConfigMain);

	var beforeClass$ = $("#learningBeforeClassLink");
	var duringClass$ = $("#learningDuringClassLink");
	var afterClass$ = $("#learningAfterClassLink");

	var beforeDetails$ = $("#learningBeforeClassDetails");
	var duringDetails$ = $("#learningDuringClassDetails");
	var afterDetails$ = $("#learningAfterClassDetails");

	dm.core.registerFadeInFadeOutAnimations(
		beforeClass$,
		duringDetails$.add(afterDetails$),
		beforeDetails$);

	dm.core.registerFadeInFadeOutAnimations(
		duringClass$,
		beforeDetails$.add(afterDetails$),
		duringDetails$);

	dm.core.registerFadeInFadeOutAnimations(
		afterClass$,
		beforeDetails$.add(duringDetails$),
		afterDetails$);
};





dm.core.registerFadeInFadeOutAnimations = function (mouseTarget$, otherAreas$, detailsArea$) {

	var hiConfig = dm.core.getHoverPluginConfig(
		function () { // over function
			otherAreas$.hide();
			detailsArea$.fadeIn(200);
		},
		function () { // leave function
			detailsArea$.fadeOut(150);
		},
		300);

	mouseTarget$.hoverIntent(hiConfig);
};




dm.core.logToHomePage = function (message) {
	$("#console").append("<p>" + message + "</p>");
};




dm.core.logMessage = function (msg) {
    if (dm.core.loggingEnabled && dm.core.loggingToViewEnabled) {
        var console$ = $("#messageConsole");
        if (console$.length < 1) {
            console$ = $("<div id='messageConsole'><p>session log: </p></div>");
            $("body").append(console$);
            console$ = $("#messageConsole");
        }

        console$.append("<p style='color: gray; font-weight: bold;'>" + msg + "</p>");
    }

    if (dm.core.loggingEnabled) {
        console.log(msg);
    }
};

dm.core.generateTestimonials = function () {

	dm.core.loadTestimonialsFor(
			"#sidebarTestimonialContainer",
			"#testimonialSidebarTemplate",
			"/services/develop_com_services/testimonials/any",
			false,
			0);

	dm.core.loadTestimonialsFor(
			"#coursesTestimonialContainer",
			"#testimonialOfClassTemplate",
			"/services/develop_com_services/testimonials/courses/20",
			true,
			8000);

	dm.core.loadTestimonialsFor(
			$("#instructorsTestimonialContainer"),
			$("#testimonialOfInstructorTemplate"),
			"/services/develop_com_services/testimonials/instructors/20",
			true,
			12000);

	dm.core.loadTestimonialsFor(
			"#companyTestimonialContainer",
			"#testimonialOfCompanyTemplate",
			"/services/develop_com_services/testimonials/company/20",
			true,
			14000);
};



dm.core.loadTestimonialsFor = function (
	outerContainerSelector,
	testimonialTemplateSelector,
	serviceUrl,
	expectSourceAsArray,
	timeToCycleSlider) {

	var outerContainer$ = $(outerContainerSelector);

	if (outerContainer$.length < 1) {
		return;
	}

	//
	// This is a little funky in that we re-run these selectors several times in the method.
	// Oddly this is necessary because the way the sidebar replacement works (see develop.com-sidebar-1.1.js)
	// The page elements have change between the document.ready() call and the completion of the callback here.
	//
	var testimonialTemplate$ = $(testimonialTemplateSelector);
	var liveTestContainer$ = $(".liveTestimonialContainer", outerContainer$);
	var staticTestContainer$ = $(".staticTestimonialContainer", outerContainer$);

	var errorHandler = function (response) {
		outerContainer$ = $(outerContainerSelector);
		liveTestContainer$ = $(".liveTestimonialContainer", outerContainer$);
		staticTestContainer$ = $(".staticTestimonialContainer", outerContainer$);

		outerContainer$.attr("style", null);
		liveTestContainer$.hide();
		staticTestContainer$.fadeIn("fast");
	};

	$.getJSON(serviceUrl, null, function (data) {

		if (data.Error != null) {
			dm.core.logMessage("Error getting testimonial for " + serviceUrl + ": " + data.Error);
			errorHandler(null);
			return;
		}

		var testimonials$ = dm.core.normalizeTojQueryArray(data, expectSourceAsArray);

		if (testimonials$.length === 0) {
			errorHandler(null);
			return;
		}

		outerContainer$ = $(outerContainerSelector);
		liveTestContainer$ = $(".liveTestimonialContainer", outerContainer$);
		staticTestContainer$ = $(".staticTestimonialContainer", outerContainer$);

		outerContainer$.attr("style", null);
		liveTestContainer$.html("");
		liveTestContainer$.hide();
		staticTestContainer$.hide();

		$.each(testimonials$, function (index, item) {
			var copy$ = testimonialTemplate$.clone();

			$(".testimonialImage", copy$).attr("src", item.LogoUrl);
			$(".testimonialText", copy$).html(item.TestimonyText);
			$(".testimonialName", copy$).html(item.TestifierName);
			$(".testimonialCompanyName", copy$).html(item.CompanyName);
			$(".testimonialCourseName", copy$).html(item.CourseName);
			$(".testimonialInstructorName", copy$).html(item.InstructorName);

			liveTestContainer$.append(copy$);

			copy$.show();
		});

		liveTestContainer$.fadeIn("fast");

		if (expectSourceAsArray) {
			var slider$ = $(".liveTestimonialContainer", $(outerContainerSelector));
			dm.core.activateSlider(slider$, timeToCycleSlider, 750, false);
		}
	})
	.error(errorHandler);
};



dm.core.normalizeTojQueryArray = function (data, expectSourceAsArray) {
	var testimonials$;
	if (expectSourceAsArray) {
		testimonials$ = $(data);
	} else {
		testimonials$ = $.makeArray([data]);
	}

	return testimonials$;
};





