﻿$(function() {
	$('.bubbleInfo').each(function() {
		// options
		var distance = 5;
		var time = 250;
		var hideDelay = 500;

		var hideDelayTimer = null;

		// tracker
		var beingShown = false;
		var shown = false;

		var trigger = $('.trigger', this);
		var popup = $('.popup', this).css('opacity', 0);

		// set the mouseover and mouseout on both element
		$([trigger.get(0), popup.get(0)]).mouseover(function() {
			// stops the hide event if we move from the trigger to the popup element
			if (hideDelayTimer) clearTimeout(hideDelayTimer);

			// don't trigger the animation again if we're being shown, or already visible
			if (beingShown || shown) {
				return;
			} else {
				beingShown = true;

				// reset position of popup box
				popup.css({
					top: -120,
					left: -133,
					display: 'block' // brings the popup back in to view
				})

				// (we're using chaining on the popup) now animate it's opacity and position
        .animate({
        	top: '-=' + distance + 'px',
        	opacity: 0.9
        }, time, 'swing', function() {
        	// once the animation is complete, set the tracker variables
        	beingShown = false;
        	shown = true;
        });
			}
		}).mouseout(function() {
			// reset the timer if we get fired again - avoids double animations
			if (hideDelayTimer) clearTimeout(hideDelayTimer);

			// store the timer so that it can be cleared in the mouseover if required
			hideDelayTimer = setTimeout(function() {
				hideDelayTimer = null;
				popup.animate({
					top: '-=' + distance + 'px',
					opacity: 0
				}, time, 'swing', function() {
					// once the animate is complete, set the tracker variables
					shown = false;
					// hide the popup entirely after the effect (opacity alone doesn't do the job)
					popup.css('display', 'none');
				});
			}, hideDelay);
		});
	});
	setPopupPostion();
});

$(document).ready(function() {
	//get Brampton for first load
	firstLocation = "Brampton";
	$(".Area_Name").text(firstLocation);
	$.get("ScheduleAjax.aspx", { Area: firstLocation }, function(data) {
		$('#Schedule_Container').html(data);
		$('#Area ul li#' + firstLocation).addClass("current");
	});

	//set click event
	$('#Area ul li').click(function() {
		id = this.id;
		$(".Area_Name").text($(this).text());
		$.get("ScheduleAjax.aspx", { Area: id }, function(data) {
			$('#Schedule_Container').html(data);
			$('#Area ul .current').removeClass("current");
			$('#Area ul li#' + id).addClass("current");
		});
	});

	//$('#'+  area.text().toLowerCase()).addClass("current");
});


/*
$(document).ready(function() {
  var area = $(".Area_Name", this);
  var index = 0;
  var city = jQuery.trim(area.text().toLowerCase());
  var isChinese = city.indexOf("(");
  if (isChinese != -1) {
    city = city.substr(0, city.indexOf("("));
  }
  switch (jQuery.trim(city)) {
    case "brampton":
      index = 0;
      break;
    case "downtown central":
      index = 1;
      break;
    case "downtown east":
      index = 2;
      break;
    case "east york":
      index = 3;
      break;
    case "etobicoke":
      index = 4;
      break;
    case "markham":
      index = 5;
      break;
    case "mississauga":
      index = 6;
      break;
    case "scarborough":
      index = 7;
      break;
    case "thornhill":
      index = 8;
      break;
    case "toronto":
      index = 9;
      break;
    case "woodbridge":
      index = 10;
      break;
  }
  //$('#'+  area.text().toLowerCase()).addClass("current");
  $('#Area li:eq(' + index + ')').addClass("current");
});
*/

function setPopupPostion() {
	var mapTop = $(".CityMap").attr("offsetTop");
	var mapLeft = $(".CityMap").attr("offsetLeft");

	// the offset is different from IE8 and IE7, adjust for IE7
	var ua = navigator.userAgent;
	var MSIEOffset = ua.indexOf("MSIE ");
	var IEVersion;
	if (MSIEOffset != -1) {
		IEVersion = parseFloat(ua.substring(MSIEOffset + 5, ua.indexOf(";", MSIEOffset)));
	}

	if (IEVersion < 8) {	//for IE7- 
		mapTop += 143;
		mapLeft += 286;
	}
	//alert(mapTop + "" + mapLeft);
	
	$(".bubbleInfo").css("position", "absolute");
	$(".bubbleInfo").css("position", "absolute");
	$("#cnt").css("top", mapTop + 190 + "px");
	$("#cnt").css("left", mapLeft + 245 + "px");

	$("#cmg").css("top", mapTop + 135 + "px");
	$("#cmg").css("left", mapLeft + 420 + "px");

	$("#gdp").css("top", mapTop + 135 + "px");
	$("#gdp").css("left", mapLeft + 330 + "px");

	$("#mkm").css("top", mapTop + 185 + "px");
	$("#mkm").css("left", mapLeft + 500 + "px");

	$("#fmp").css("top", mapTop + 125 + "px");
	$("#fmp").css("left", mapLeft + 490 + "px");

	$("#dtn").css("top", mapTop + 380 + "px");
	$("#dtn").css("left", mapLeft + 330 + "px");

	$("#nyk").css("top", mapTop + 180 + "px");
	$("#nyk").css("left", mapLeft + 300 + "px");

	$("#sca").css("top", mapTop + 245 + "px");
	$("#sca").css("left", mapLeft + 540 + "px");

	//$("#gdp").remove();

}

function loadSchedules(area) {
	var objArea = $('#Area ul li#' + area);
	
	$(".Area_Name").text(objArea.text());
	$.get("ScheduleAjax.aspx", { Area: area }, function(data) {
		$('#Schedule_Container').html(data);
		$('#Area ul li').removeClass("current");
		objArea.addClass("current");
	});
	location.href="#Area"
}