﻿$(function () {

	if ($.cookie("tscrole") == null || $.cookie("tscrole") == "") {
		SetAdminLogOut();
	} else {
		SetAdminLogOn();
	}

	$("#court_booking").mouseover(function () {
		var imageSrc = $(this).attr("src");
		$(this).attr("src", imageSrc.replace("logo", "logo_over"));
	});

	$("#court_booking").mouseout(function () {
		var imageSrc = $(this).attr("src");
		$(this).attr("src", imageSrc.replace("logo_over", "logo"));
	})

	$("#court_booking").click(function () {
		window.open("http://www.aspsystems.co.uk/slaunch/tringsc.dll/go", "", "width=750,height=500,scrollbars,resizable")
	})

	$(".court_booking_link").click(function () {
		window.open("http://www.aspsystems.co.uk/slaunch/tringsc.dll/go", "", "width=750,height=500,scrollbars,resizable")
	})

	// Calendar
	$(window).resize(function () {
		$('#eventInfo').hide();
	});	

	$('#calendar').fullCalendar({
		editable: true,
		events: function (start, end, callback) {
		// do some asynchronous ajax 
		contentType: "application/json; charset=utf-8",
		$.getJSON("/ws/EventService.svc/GetEvents",
		{
			startDate: "'" + start + "'",
			endDate: "'" + end + "'"
		},
		function (events) {
			callback(eval(events.d));
		});

		},
		eventRender: function (event, element) {
			element.addClass(event.type + "_event");
		},
		eventClick: function (calEvent, jsEvent, view) {

			var position = $('#calendar').position();

			$('#eventInfo').css('top', position.top + 50);
			$('#eventInfo').css('left', position.left + 10);
			$('#eventInfo').css('width', $('#calendar').width() - 34);

			$('#eventContent').html(calEvent.description);
			$('#eventInfo').show();

		},
		dayClick: function (date, allDay, jsEvent, view) {

			if ($.cookie("tscrole") != null && $.cookie("tscrole") != "") {
				$("#eventId").val("");
				$("#eventDescription").val("");
				$("#eventDate").val($.fullCalendar.formatDate(date, "dd-MMM-yyyy"));

				$.getJSON("/ws/EventService.svc/GetEvent",
				{
					date: "'" + date + "'"
				},
				function (data) {
					var eventData = jQuery.parseJSON(data.d);
					if (eventData.length > 0) {
						$("#eventTitle").val(eventData[0].title);
						$("#eventDescription").val(eventData[0].description);
						$("#eventId").val(eventData[0].id);

						$("#eventType option").each(function (i) {
							$(this).removeAttr("selected");
							if ($(this)[0].value == eventData[0].type) {
								$(this).attr("selected", "selected");
							}
						});
					}
				});
			}
		}


	});

	$('#eventInfo').click(function () {
		$('#eventInfo').hide();
	});

	$('#closeEvent').click(function () {
		$('#eventInfo').hide();
	});

	$('#loading').hide();

	$('#eventInfo').hide();

	// logon
	$("#logon").corner("round 8px").parent().css('padding', '2px').corner("round 10px");

	// downloads
	$("#downloads").corner("round 8px").parent().css('padding', '2px').corner("round 10px");

	$('#un').focusin(function () {
		$('#un').val("");
		$('#un').removeClass("logon_text");
	});

	$('#pw').focusin(function () {
		$('#pw').val("");
		$('#pw').removeClass("logon_text");
	});

	$('#pw').keypress(function (event) {
		if (event.keyCode == '13') {
			$("#logonlink").trigger('click');
		}
	});

	$("#logoutlink").click(function () {
		SetAdminLogOut();
		//$.cookie("tscrole", null, { path: '/', { path: '/', expires: -10 });	
		$.cookie('tscrole', null, { expires: -10, path: '/', domain: 'tringsquashclub.co.uk' });
		window.location.replace("/home");

	});

	function SetAdminLogOut() {
		$('.admin_tools').hide();
		$('#logout').hide();
		$('#un').addClass("logon_text");
		$('#pw').addClass("logon_text");
		$('#un').val("username");
		$('#pw').val("password");
		$('#logon').show();
	}

	function SetAdminLogOn() {
		$('.admin_tools').show();
		$('#logon').hide();
		$('#logout').show();
	}

	$("#logonlink").click(function () {

		var logonCriteria = ({ un: $("#un").val(), pw: $("#pw").val() });

		$.ajax({
			type: 'POST',
			url: "/ws/LogonService.svc/Logon",
			data: '{ "un": "' + $('#un').val() + '", "pw": "' + $('#pw').val() + '"}',
			contentType: "application/json; charset=utf-8",
			dataType: "json",
			success: function (data) {
				if (data.d) {
					$.cookie('tscrole', 'admin', { path: '/', domain: 'tringsquashclub.co.uk' });
					//$.cookie("tscrole", "admin", { path: '/' });
					SetAdminLogOn();
					window.location.replace("/siteAdmin");
				}
				else {
					//$.cookie('tscrole', null, { expires: -10, path: '/', domain: 'ivnet.co.uk' });
					$.cookie("tscrole", null, { path: '/' });
					SetAdminLogOut();
				}
			}
		});
	});

	$("#eventType").change(function () {
		var eventTypeValues = "tm cf pf mf je";
		if (eventTypeValues.indexOf($("#eventTitle").val()) > -1) {
			$("#eventTitle").val($(this).val());
		}

	});
});

