﻿jQuery.fn.toggleHomeFlash = function() 
{
	$('#home-content').hide();
	var hasRequestedVersion = DetectFlashVer(9, 0, 0);
	if (hasRequestedVersion) {
		var flashVars = {
			homepageSRC: '/ContentFiles/Swf/Homepage.swf',
			flvSRC: '/ContentFiles/Flv/homepage-jk-intro.flv',
			selfHelpURL: (GetBaseURL() + '/content/depression/self%2Btest')
		};
		var attr = { name: 'flash', id: 'flash' }
		swfobject.embedSWF('/ContentFiles/Swf/HomePageLoader.swf', "flash", "960", "532", "9.0.0", null, flashVars, null, attr);
		var backup = window.__flash__addCallback;
		window.__flash__addCallback = function(instance, name) { try { backup(instance, name) } catch (x) { } };
	} else {
		$('#home-content').show();
	}
}

var BaseURL = '';
$(document).ready(function()
{
	BaseURL = GetBaseURL();
	$('body').addClass('js'); /*Add js class if js is enabled*/
	$('#jsContent').show(); /*Display Video List content if JS enabled*/
	
	$('A[rel="external"]').click(function()
	{
		window.open($(this).attr('href')); return false;
	});

	if ($(".jcarousel").jCarouselLite)
	{
		$(".jcarousel").jCarouselLite({scroll: 1, btnNext: ".next", btnPrev: ".prev", circular: false}); 
	}

	//Populate the search by month text to default to current year
	var myDate = new Date();
	var thisYear = myDate.getFullYear();
	var month = '';
	var region = '';
	var year = thisYear;

	$('#year').html(thisYear);

	$(".prev").not(".prev.carousel").hover(
		function(){ $(this).attr({src: "/ContentFiles/Img/arrow-left-hover.png"}).css({"cursor": "pointer"}); }, 
		function(){ $(this).attr({src: "/ContentFiles/Img/arrow-left.png"}).css({"cursor": "auto"}); }
	);

	$(".next").not(".next.carousel").hover(
		function(){ $(this).attr({src: "/ContentFiles/Img/arrow-right-hover.png"}).css({"cursor": "pointer"}); }, 
		function(){ $(this).attr({src: "/ContentFiles/Img/arrow-right.png"}).css({"cursor": "auto"}); }
	);

	$(".prev.carousel").hover(
		function(){ $(this).attr({src: "/ContentFiles/Img/arrow-left-carousel-hover.gif"}).css({"cursor": "pointer"}); }, 
		function(){ $(this).attr({src: "/ContentFiles/Img/arrow-left-carousel.gif"}).css({"cursor": "auto"}); }
	);

	$(".next.carousel").hover(
		function(){ $(this).attr({src: "/ContentFiles/Img/arrow-right-carousel-hover.gif"}).css({"cursor": "pointer"}); }, 
		function(){ $(this).attr({src: "/ContentFiles/Img/arrow-right-carousel.gif"}).css({"cursor": "auto"}); }
	);

	//Clear search input on focus and input text on blur
	$('#search input').focus(function(){ if($(this).val() == "search site") $(this).val(""); })
	$('#search input').blur(function(){ if($(this).val() == "") $(this).val("search site"); })

	var region1 = $('#map li#region1 a');
	var region2 = $('#map li#region2 a');
	var region3 = $('#map li#region3 a');
	var region4 = $('#map li#region4 a');

	$('#year-search img').hover(function(){ $(this).css({"cursor": "pointer"}); }, function(){$(this).css({"cursor": "auto"}); });

	$('#year-search img#prev').click(function()
	{
		year = parseFloat($('#year').html()) - 1;
		$('#year').html(year);
		GetEventList(month, year, '');
		return false;
	});

	$('#year-search img#next').click(function() 
	{
		year = parseFloat($('#year').html()) + 1;
		$('#year').html(year);
		GetEventList(month, year, '');
		return false;
	});

	$('#calendar li a').click(function()
	{
		if($(this).hasClass('active')) { $('#calendar li a').removeClass('active'); month = ''; }
		else { $('#calendar li a').removeClass('active'); $(this).addClass('active'); month = $(this).text(); }
		if($('#map li a').hasClass('active')) { $('#map li a').removeClass(); $('#map li a').removeClass('active'); region = ''; }
		year = $('#year').text();
		GetEventList(month, year, '');
		return false;
	});

	$('#map li a').click(function()
	{
		var clickedRegionId = $(this).parent().attr('id');
		if($(this).hasClass('active'))
		{
			$(this).removeClass(); 
			region = ''; 
			$('#map li a').removeClass();
		} else { 
			$('#map li a').removeClass().not(this).addClass(clickedRegionId+"active");
			$(this).addClass('active'); 
			region = $('#map li a.active').text();
		}
		if($('#calendar li a').hasClass('active')){ $('#calendar li a').removeClass('active'); month = ''; }
		GetEventList('', '', region);
		return false;
	});

	$('#event-textSearchForm').submit(function() 
	{
		var keywords = $('#event-textSearch').val();
		$.ajax({
			type: "POST",
			url: BaseURL + form.attr("action"),
			data: $('#event-textSearchForm').serialize(),
			cache: false,
			success: function(html) { $("#self-help12").html(html); },
			error: function() { $("#self-help12").html('An error has occured while trying to retrieve the events listing.'); }
		});
		return false;
	});

	$('a.ajaxify').ajaxify();
	$('#selftestquiz #A0, #A1, #A2, #A3').trackSelfHelpTestAnswer(); // Self Help Test Google Analytics tracking 
	
	// Video thumbnail hover text
	$("#carousel img.video").hover(
		function(){ $('#video-rollover-text').html($(this).attr('alt')); }, 
		function(){ $('#video-rollover-text').html('&nbsp;'); }
	);
});
/* End Document Ready*/

function GetEventList(month, year, region) 
{
	$.ajax({
		type: "POST",
		url: BaseURL + "/Content/GetEventsList",
		data: ("month=" + month + "&year=" + year + "&region=" + region),
		cache: false,
		success: function(html) { $("#self-help12").html(html); },
		error: function() { $("#self-help12").html('An error has occured while trying to retrieve the events listing.'); }
	});
}

function playFLV(flvSrc, captionSrc, storyFlvSrc, storyCaptionSrc, wayThroughFlvSrc, wayThroughCaptionSrc, otherSideFlvSrc, otherSideCaptionSrc) 
{
	if ($('#flash-movie').length > 0) {
		$('#flash-movie').get(0).playFLV(flvSrc, captionSrc, storyFlvSrc, storyCaptionSrc, wayThroughFlvSrc, wayThroughCaptionSrc, otherSideFlvSrc, otherSideCaptionSrc);
	}
}
function pauseFLV(){ if ($('#flash-movie').length > 0) { $('#flash-movie').get(0).pause(); } }
function initSwf(swf) { return true; } // Called from flash modules when they are ready

// Link ajaxification
$.fn.ajaxify = function() 
{
	return this.each(function() {
		var link = $(this);
		var url = link.attr('href');
		var target = $('#' + link.attr('rel'));
		if (typeof link.attr('href') != 'undefined' && link.attr('href').substr(1, 1) != '#') {
			link.unbind('click');
			link.click(function() {
				$.ajax({ 
					type: "POST", 
					url: url,
					data: 'isAjax=true', 
					cache : false,
					success: function(response) {
						var xhtml = typeof(response.responseText) == 'undefined' ? response : response.responseText;
						target.html(xhtml);
						target.show();
					},
					error: function(response) {
						alert('Ajax callback error: url: [' + url + '], [target: ' + target + ']');
						target.html("Ajax callback error");
					}
				});
				return false;
			});
		}
	});
};

// Self Help Test Google Analytics tracking
$.fn.trackSelfHelpTestAnswer = function()
{
	return this.each(function() 
	{
		var link = $(this);
		var category = 'Q' + getCurrentQuestionIndex();
		var option = link.attr('id');
		link.click(function() 
		{
			pageTracker._trackEvent(category, option); 
			return false;
		});
	});
};