﻿/// <reference path="jquery.intellisense.js"/>
//jQuery.noConflict();
jQuery(document).ready(function () {
	App.initialise();
	App.bindNavHover();
});

var App =
{
	bindNavHover: function() {
		$("#Nav ul.TopNav li.NavAboutUs div ul li a").hover(function () {
			$("#Nav ul.TopNav li.NavAboutUs a.TopNavA").addClass("NavAboutUsHover");
		},
		function () {
			$("#Nav ul.TopNav li.NavAboutUs a.TopNavA").removeClass("NavAboutUsHover");
		});

		$("#Nav ul.TopNav li.NavWhyWalk div ul li a").hover(function () {
			$("#Nav ul.TopNav li.NavWhyWalk a.TopNavA").addClass("NavWhyWalkHover");
		},
		function () {
			$("#Nav ul.TopNav li.NavWhyWalk a.TopNavA").removeClass("NavWhyWalkHover");
		});

		$("#Nav ul.TopNav li.NavGetWalking div ul li a").hover(function () {
			$("#Nav ul.TopNav li.NavGetWalking a.TopNavA").addClass("NavGetWalkingHover");
		},
		function () {
			$("#Nav ul.TopNav li.NavGetWalking a.TopNavA").removeClass("NavGetWalkingHover");
		});

		$("#Nav ul.TopNav li.NavTakeAction div ul li a").hover(function () {
			$("#Nav ul.TopNav li.NavTakeAction a.TopNavA").addClass("NavTakeActionHover");
		},
		function () {
			$("#Nav ul.TopNav li.NavTakeAction a.TopNavA").removeClass("NavTakeActionHover");
		});

		$("#Nav ul.TopNav li.NavWhatsOn div ul li a").hover(function () {
			$("#Nav ul.TopNav li.NavWhatsOn a.TopNavA").addClass("NavWhatsOnHover");
		},
		function () {
			$("#Nav ul.TopNav li.NavWhatsOn a.TopNavA").removeClass("NavWhatsOnHover");
		});

		$("#Nav ul.TopNav li.NavNews div ul li a").hover(function () {
			$("#Nav ul.TopNav li.NavNews a.TopNavA").addClass("NavNewsHover");
		},
		function () {
			$("#Nav ul.TopNav li.NavNews a.TopNavA").removeClass("NavNewsHover");
		});
	},

	initialise: function()
	{
		App.bindIE6Flyouts();

		jQuery("#SearchBar input:text").focus(function()
		{
			this.value = "";
		});

		jQuery(".PanelList div.PanelListInner2").each(App.bindHover);

		App.bindComments();
		App.bindGroupMap();
	},

	bindIE6Flyouts: function()
	{
		if (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6)
		{
			jQuery("#Nav > ul > li").each(function(i)
			{
				var parentItem = jQuery(this);
				parentItem.find("li:first").addClass("FirstChild");
				parentItem.mouseover(function()
				{
					jQuery(this).toggleClass("Hover");
				});
				parentItem.mouseout(function()
				{
					jQuery(this).toggleClass("Hover");
				});
			});
		}
	},

	bindComments: function()
	{
		var comments = jQuery("#Comments");
		var header = jQuery("#CommentsHeader");

		var hiddenOnLoad = (jQuery.cookie("ShowComments") == null || jQuery.cookie("ShowComments") == "false");
		if (hiddenOnLoad)
		{
			comments.hide();
		}

		var changeText = function(visible)
		{
			header.find("span").text("Click here to " + (visible ? "show" : "hide") + " comments");
		};

		changeText(hiddenOnLoad);
		header.addClass("Dynamic");
		header.click(function()
		{
			var show = (comments.css("display") == "none");
			if (show) comments.slideDown("slow", function() { changeText(false) });
			else comments.slideUp("slow", function() { changeText(true) });
			jQuery.cookie("ShowComments", show, { expires: 7 });
		});
	},

	bindGroupMap: function()
	{
		if (jQuery.browser.msie)
		{
			jQuery("#Main").prepend(jQuery("#MapCanvas"));
		}
	},

	getPreviousCaseStudy: function(resultsHandler)
	{
		App.ajax("GetPreviousCaseStudy", null, resultsHandler);
	},

	getNextCaseStudy: function(resultsHandler)
	{
		App.ajax("GetNextCaseStudy", null, resultsHandler);
	},

	bindHover: function()
	{
		var item = jQuery(this);
		var link = item.find("h3 a");
		if (link.length > 0)
		{
			item.addClass("Hover");
			item.click(function(evt)
			{
				if (evt.target.tagName != "A")
				{
					var target = link.attr("target");
					if (target)
					{
						window.open(link.attr("href"), target);
					}
					else
					{
						window.location.href = link.attr("href");
					}
				}
			});
		}
	},

	ajax: function(methodName, data, successHandler, errorHandler)
	{

		if (!data) data = {};
		if (!successHandler) successHandler = function(data, textStatus) { return true; }
		if (!errorHandler) errorHandler = function(xhr, textStatus, errorThrown)
		{
			try
			{
				var errorJson = eval("(" + xhr.responseText + ")");
				var msg = "";
				if (errorJson.ExceptionType)
				{
					msg += errorJson.ExceptionType + "\n";
				}
				if (errorJson.Message)
				{
					msg += errorJson.Message + "\n";
				}
				if (errorJson.StackTrace)
				{
					msg += "\nStack Trace:\n" + errorJson.StackTrace;
				}
				if (msg)
				{
					alert(msg);
					return;
				}
			}
			catch (ex) { }
			alert(textStatus + "\nResponse status code: " + xhr.status);
		}

		return jQuery.ajax(
		{
			type: "POST",
			url: "/AjaxHandler.axd/" + methodName,
			contentType: "application/json; charset=utf-8",
			dataType: "json",
			processData: false,
			data: JSON.stringify(data),
			success: function(data, textStatus)
			{
				if (data.d) data = data.d;
				successHandler(data, textStatus);
			},
			error: errorHandler
		});
	}
}

App.facebookConnectComplete = function (redirectUrl) {
	window.location.href = "/FacebookReturn.aspx" + (redirectUrl ? "?redirectUrl=" + redirectUrl : "");
};

//App.facebookConnectComplete = function () {
//	$.jGrowl("In the facebookConnectComplete function");
//	$.ajax({
//		type: 'POST',
//		url: '/FacebookService.asmx/FacebookLogin',
//		dataType: 'json',
//		contentType: 'application/json; charset=utf-8',
//		success: function () {
//			$.jGrowl("Logged In");
//			$.get('/FacebookService.asmx/FacebookUID', function (data) {
//				$("#FacebookImage").attr("src", $(data).text());
//				jQuery.jGrowl($(data).text());
//			});
//		}
//	});
//};

App.facebookConnectLogout = function () {
	if (window.FB) {
		return false;
	}
};


/**************************************************************************
/*
/* Below is a section for the polls control and associated results graphs.
/* Functions for setting up the modal dialog and the jqplot graphs.
/*
***************************************************************************/

//sets the height of the poll wrapper.
//function is called from the poll iframe to 
//resize the poll section to required height.
function setPollContainerHeight(height) {
	$("#Poll").height(height);
	$("#PollIFrame").attr("height", height);
}


//sets up the modal dialog from the current and previous polls. utilises jqplot functions
function setUpPollModal(currentPoll, previousPoll, showPreviousFirst) {

	//set enable plugins
	$.jqplot.config.enablePlugins = true; // on the page before plot creation.

	//launch modal
	$("#Charts").modal();

	//need to build the current array again after passing to method.
	var currentSeries1 = [];
	for (var i = 0; i < currentPoll.results.length; i++) {
		currentSeries1.push([currentPoll.results[i][0], currentPoll.results[i][1]]);
	}

	//set current pie
	var plot1 = $.jqplot('CurrentPie', [currentSeries1], {
		title: currentPoll.question,
		seriesDefaults: { renderer: $.jqplot.PieRenderer },
		legend: { show: true },
		seriesColors: ["#1a7373", "#6cafac", "#eaffff", "#008586", "#f9f7b3", "#d6f3ff", "#002a2a"],
		grid: { background: "#ffffff", shadow: false, borderWidth: 0 }
	});

	//add current poll result comments if exist.
	if (currentPoll.comments.length > 0) {
		$("#CurrentPie").append("<div class=\"ResultComments\">" + currentPoll.comments + "</div>")
	}

	//add total responses by appending a new row to the legend.
	if (currentPoll.totalResponses)
		$("#CurrentPie .jqplot-table-legend tbody").append("<tr class=\"jqplot-table-legend\"><td class=\"jqplot-table-legend\"></td><td class=\"jqplot-table-legend\" style=\"padding-top: 0.5em;\">Total (" + currentPoll.totalResponses + ")</td></tr>")

	//check if previous results exist. Will indicate that a previous poll exists.
	if (previousPoll.results) {

		//need to build the previous array again after passing to method.
		var previousSeries1 = [];
		for (var i = 0; i < previousPoll.results.length; i++) {
			previousSeries1.push([previousPoll.results[i][0], previousPoll.results[i][1]]);
		}

		//set previous pie
		var plot2 = $.jqplot('PreviousPie', [previousSeries1], {
			title: previousPoll.question,
			seriesDefaults: { renderer: $.jqplot.PieRenderer },
			legend: { show: true },
			seriesColors: ["#1a7373", "#6cafac", "#eaffff", "#008586", "#f9f7b3", "#d6f3ff", "#002a2a"],
			grid: { background: "#ffffff", shadow: false, borderWidth: 0 }
		});

		//add previous poll result comments if exist.
		if (previousPoll.comments.length > 0) {
			$("#PreviousPie").append("<div class=\"ResultComments\">" + previousPoll.comments + "</div>")
		}

		//add total responses
		if (previousPoll.totalResponses)
			$("#PreviousPie .jqplot-table-legend tbody").append("<tr class=\"jqplot-table-legend\"><td class=\"jqplot-table-legend\"></td><td class=\"jqplot-table-legend\" style=\"padding-top: 0.5em;\">Total (" + previousPoll.totalResponses + ")</td></tr>")

		if (showPreviousFirst) {
			$("#CurrentPie").hide();
			$("#PreviousPollLink").text("Show current poll");
		}

		$("#PreviousPollLink").click(function () {
			if ($("#CurrentPie").is(":visible")) {
				$("#CurrentPie").fadeOut();
				$("#PreviousPollLink").text("Show current poll");
			} else {
				$("#CurrentPie").fadeIn();
				$("#PreviousPollLink").text("Show previous poll");
			}
		});

	} else {
		//set up the links
		$(".PollNavLinks .First").hide();
	}
}

function change_parent_url(url) {
	document.location = url;
}	
