﻿$(document).ready(function () {
	jQuery.support.cors = true;

	var urlHost = (("https:" == document.location.protocol) ? "https://" : "http://");
	var url = urlHost + 'services.webedge.mcmurry.com/webedgeservice.svc/erWaitTime/GetWaitTimesByLocationId/' + $('#_websiteId').val();

	if ($.browser.msie && window.XDomainRequest) {
		// Use Microsoft XDR
		var xdr = new XDomainRequest();
		xdr.open("get", url);
		xdr.onload = function () {
			// XDomainRequest doesn't provide responseXml, so if you need it:
			var dom = new ActiveXObject("Microsoft.XMLDOM");
			dom.async = false;
			dom.loadXML(xdr.responseText);
			processData(dom);
		};
		xdr.onerror = processError;
		xdr.send();
	} else {
		$.ajax({
			url: url,
			type: 'GET',
			dataType: 'xml',
			cache: false,
			success: function (data) { processData(data); },
			error: function () { processError(); }
		});
	}

	function processData(data) {
		var i = 0;
		$('.erwaittime-locations').empty();
		$(data).find('ErWaitTime').each(function () {
			var name = $(this).find('LocationName').text();
			var time = $(this).find('CreationDate').text();
			var wait = $(this).find('TotalWaitTime').text();
			$('<div class="erwaittime-location erwaittime-location-' + i + '">').html('<span class="erwaittime-location-name">' + name + '</span>').appendTo('.erwaittime-locations');

			if (wait.length > 0) {
				$('<span class="erwaittime-location-wait"></span>').html('<span class="erwaittimes-location-time">' + parseFloat(wait.substring(3, 5)) + '</span> <span class="erwaittimes-location-label">min</span>').appendTo('.erwaittime-location-' + i);
			}
			else {
				$('<span class="erwaittimes-waittime-unavailable">Currently Unavailable</span>').appendTo('.erwaittime-location-' + i);
			}
			i++;
		});
	}

	function processError() {
		$('.erwaittime-locations').empty();
		$('.erwaittime-locations').append('<span class="erwaittimes-service-unavailable">Service  Unavailable</span>');
	}


	var today = new Date();
	var month = today.getMonth() + 1;
	var day = today.getDate();
	var hours = today.getHours();
	var ampm = "AM";
	if (hours > 11) { ampm = "PM"; }
	var minutes = today.getMinutes();
	if (minutes < 10) { minutes = "0" + minutes; }
	var updateTime = "Last Updated on " + month + "/" + day + " " + hours + ":" + minutes + " " + ampm;
	$('#erwaittime .erwaittime-lastupdated').text(updateTime);

	var isRotating = true;
	if (typeof rotateLocations != "undefined") {
		isRotating = rotateLocations;
	}
	if ($('#erwaittime .erwaittime-location').length > 1) {
		if (isRotating != false) {
			$('#erwaittime').prepend('<a href="javascript:void(0);" class="erwaittime-btn erwaittime-btn-prev" title="Previous">Previous</a><a href="javascript:void(0);" class="erwaittime-btn erwaittime-btn-next" title="Next">Next</a>');
			$.getScript('/Resources/Common/Js/jquery.cycle.all.min.js', function () {
				$('#erwaittime .erwaittime-locations div[id$=UpdatePanel1]').cycle({ fx: 'fade', prev: '.erwaittime-btn-prev', next: '.erwaittime-btn-next', speed: '300', timeout: '5000', pause: 1 });
			});
		}
	}
	$('#erwaittime .erwaittime-disclaimer').hover(function () {
		$('#erwaittime .erwaittime-disclaimer-text').fadeIn('medium');
	}, function () {
		$('#erwaittime .erwaittime-disclaimer-text').fadeOut('fast');
	});
	$.getScript('/Resources/Common/Js/DD_belatedPNG_0.0.8a-min.js', function () {
		DD_belatedPNG.fix('#erwaittime');
	});
});

