var filters = {}; //filters namespace
var map = {}; //map namespace

map.scalingFactor = 4.65; //scaling factor between big map and small map
map.popupOpen = false;
map.scrollInterval = null;

$(document).ready(function () {
	$("img").mousedown(function(e){
		e.preventDefault();
	});
	
	$("#selection ul li").click(function(e){
		$("#selection ul li").removeClass('selected');
		$(this).addClass('selected');
		
		switch($(this).text()){
			case 'Restaurants':
				filters.showCategories('Restaurants');
				break;
			case 'Shops':
				filters.showCategories('Shops');
				break;
			case 'Events':
				filters.showCategories('Events');
				break;
		}
	});
	
	$("#filters #selection .categories a").click(filters.showItems);
	
	$("#windowshade-control").click(filters.toggleState);
	
	$(".highlight").draggable({ axis: 'x' , containment: 'parent', drag:map.moveBigMapFromSmallMap});
	
	$("#map-small").click(map.scrollToPoint);
	
	$(".result a").live('click', map.scrollToLocation);
	
	$("#map-window").click(map.testForPopups);
	
	$("#map-window").mousemove(map.highlightPlace);
	
	$("#map-small, #filters, #arrow-left, #arrow-right").mousedown(map.hidePopups);
	
	$(".close-box").hover(function(e){
			$(this).attr('src', 'images/close_over.gif');
		},
		function(e){
			$(this).attr('src', 'images/close.gif');
		}
	).click(map.hidePopups);
	
	$("#arrow-left").mousedown(map.scrollLeft).hover(function(e){
		$(this).attr("src", "images/arrow_left_over.png");
	},
	function(e){
		$(this).attr("src", "images/arrow_left.png");
	});
	
	$("#arrow-right").mousedown(map.scrollRight).hover(function(e){
		$(this).attr("src", "images/arrow_right_over.png");
	},
	function(e){
		$(this).attr("src", "images/arrow_right.png");
	});
	
	$(document).mouseup(function(e){
		clearInterval(map.scrollInterval);
		map.scrollInterval = null;
	});
});

filters.showCategories = function(category){
	$(".categories").hide();
	$("#"+category.toLowerCase()).fadeIn();
	
	//update breadcrumb trail
	$("span.selection").empty().removeClass('nothing').append(category);
};

filters.showItems = function(e){
	//change style of current link
	$(".categories a").removeClass('selected');
	$(this).addClass('selected');
	
	//get list of results out of JSON object
	var category = $(this).parent().attr('id');
	var subObj = directory[category];
	var categoryResults = subObj[$(this).text()];
	
	//update breadcrumb trail
	var uppercaseCategory = category.slice(0,1).toUpperCase() + category.slice(1, category.length);
	$("span.selection").empty().append(uppercaseCategory + " &nbsp&nbsp > &nbsp&nbsp " + $(this).text());
	
	//display list in results pane
	$("#result-list").empty();
	for (var x = 0; x < categoryResults.length; x++){
		$("#result-list").append("<div class='result'><span class='number'> " + (x+1) + "</span><a href='#'>" + categoryResults[x] + "</a></div>");
	}
	
	//add place markers to big and small maps
	var circleSize = 55;
	$(".place-marker").remove();
	for (x = 0; x < categoryResults.length; x++){
		var locationName = categoryResults[x];
		locationName = locationName.replace(/&amp;/g, '&');
		var locationCoords = coordinates[locationName];
		var xPos = locationCoords[0] + (locationCoords[2]/2) - (circleSize/2);
		var yPos = locationCoords[1] + (locationCoords[3]/2) - (circleSize/2);
		$("#map-window").append("<div class='place-marker' style='top:"+yPos+"px; left:"+xPos+"px'><img src='images/circle_big.png' /><span class='number-big'> " + (x+1) + "</span></div>");
		$("#map-small").append("<div class='place-marker' style='top:"+((yPos/map.scalingFactor)-30)+"px; left:"+(xPos/map.scalingFactor)+"px'><img src='images/circle.png' /><span class='number'> " + (x+1) + "</span></div>");
	}
};

filters.toggleState = function(e){
	if ($("#windowshade-control").text() == 'Hide filters'){
		$("#selection").animate({height:"0px", paddingTop:"0px", paddingBottom:"0px"}, 500);
		$("#windowshade-control").text('Show filters');
	}
	else{
		$("#selection").animate({height:"348px", paddingTop:"36px", paddingBottom:"36px"}, 500);
		$("#windowshade-control").text('Hide filters');
	}
};

map.moveBigMapFromSmallMap = function(e, ui){
	var mapPos = ui.position.left * map.scalingFactor;
	$("#map-window").scrollLeft(mapPos);
};

map.moveSmallMapFromBigMap = function(){
	var mapPos = $("#map-window").scrollLeft() / map.scalingFactor;
	//$(".highlight").animate({'left':mapPos}, 1000);
	$(".highlight").css('left', mapPos);
};

map.scrollToPoint = function(e){
	var leftEdge = e.pageX - 333;
	if (leftEdge < 0){
		leftEdge = 0;
	}
	
	var rightEdge = leftEdge + 205;
	if (rightEdge > 963){
		leftEdge = 750;
	}
	
	$(".highlight").css('left', leftEdge +'px');
	var left = $(".highlight").css('left');
	var mapPos = left.substring(0, left.length - 2) * map.scalingFactor;
	$("#map-window").scrollLeft(mapPos);
};

map.scrollToLocation = function(e){
	if ($("#windowshade-control").text() == 'Hide filters'){
		$("#selection").animate({height:"0px", paddingTop:"0px", paddingBottom:"0px"}, 500);
		$("#windowshade-control").text('Show filters');
	}
	
	var locationName = $(this).text();
	var locationCoords = coordinates[locationName];
	$("#map-window").animate({"scrollLeft":locationCoords[0] - 400}, 1000, '', function(){
		map.moveSmallMapFromBigMap();
		if (locationCoords[1] < 300){
			map.showPopup(locationName, "#popup-top");
		}
		else{
			map.showPopup(locationName, "#popup-bottom");
		}
	});
};

map.scrollLeft = function(e){
	e.preventDefault();
	if (map.scrollInterval === null){
		map.scrollInterval = setInterval(map.scrollLeftInner, 10);
	}
};

map.scrollLeftInner = function(){
	var curPos = $("#map-window").scrollLeft();
	$("#map-window").scrollLeft(curPos-8);
	map.moveSmallMapFromBigMap();
};

map.scrollRight = function(e){
	e.preventDefault();
	if (map.scrollInterval === null){
		map.scrollInterval = setInterval(map.scrollRightInner, 10);
	} 
};

map.scrollRightInner = function(e){
	var curPos = $("#map-window").scrollLeft();
	$("#map-window").scrollLeft(curPos+8);
	map.moveSmallMapFromBigMap();
};

map.testForPopups = function(e){
	var mapCoordX = e.pageX + $("#map-window").scrollLeft()-$("#map-window").offset().left;
	var mapCoordY = e.pageY - $("#map-window").offset().top;
	var location = null;
	var thisPopup;
	if ((mapCoordY < 300) && (mapCoordX > xCoordinatesTop[0][0])){//use top index
		location = map.binarySearch(mapCoordX, xCoordinatesTop, 0, xCoordinatesTop.length);
		thisPopup = "#popup-top";
	}
	else if ((mapCoordX > xCoordinatesBottom[0][0])){//use bottom index
		location = map.binarySearch(mapCoordX, xCoordinatesBottom, 0, xCoordinatesBottom.length);
		thisPopup = "#popup-bottom";
	}
	
	if (location !== null){
		var rightBoundaryTest = mapCoordX < (coordinates[location][0] + coordinates[location][2]);
		var topBoundaryTest = mapCoordY >= (coordinates[location][1]);
		var bottomBoundaryTest = mapCoordY <= (coordinates[location][1] + coordinates[location][3]);
		if (rightBoundaryTest && topBoundaryTest && bottomBoundaryTest){
			map.showPopup(location, thisPopup);
		}
	}
};

map.binarySearch = function(xPosition, searchArray, startIndex, stopIndex){
	var middleIndex = startIndex + (Math.floor((stopIndex - startIndex) / 2));
	var middleElement = searchArray[middleIndex];
	var middlePlusOne;
	
	var atEnd = false;
	if (middleIndex < searchArray.length-1){
		middlePlusOne = searchArray[middleIndex+1];
	}
	else{
		atEnd = true;
	}
	
	if (middleElement[0] > xPosition){
		return map.binarySearch(xPosition, searchArray, startIndex, middleIndex); //recurse on first half of current part
	}
	else if (middleElement[0] < xPosition){
		if (atEnd || middlePlusOne[0] > xPosition){//found it! return name of place (== key to other arrays)
			return searchArray[middleIndex][1]; 
		}
		else{
			if (startIndex >= stopIndex){ //nothing exists at this coordinate, return null
				return null;
			}
			else{
				return map.binarySearch(xPosition, searchArray, middleIndex, stopIndex); //recurse on second half of current part
			}
		}
	}
};

map.highlightPlace = function(e){
	var mapCoordX = e.pageX + $("#map-window").scrollLeft()-$("#map-window").offset().left;
	var mapCoordY = e.pageY - $("#map-window").offset().top;

	var location = null;
	if ((mapCoordY < 300) && (mapCoordX > xCoordinatesTop[0][0])){//use top index
		location = map.binarySearch(mapCoordX, xCoordinatesTop, 0, xCoordinatesTop.length);
	}
	else if ((mapCoordX > xCoordinatesBottom[0][0])){//use bottom index
		location = map.binarySearch(mapCoordX, xCoordinatesBottom, 0, xCoordinatesBottom.length);
	}

	if (location !== null && coordinates[location]){
		var rightBoundaryTest = mapCoordX < (coordinates[location][0] + coordinates[location][2]);
		var topBoundaryTest = mapCoordY >= (coordinates[location][1]);
		var bottomBoundaryTest = mapCoordY <= (coordinates[location][1] + coordinates[location][3]);
		if (rightBoundaryTest && topBoundaryTest && bottomBoundaryTest){
			if (location === "Baxter's"){
				$("#baxters").show();
			}
			else{
				$("#map-window").append("<div id='highlight'> </div>");
				$("#highlight").css('top', coordinates[location][1]).css('left', coordinates[location][0]).css('width', coordinates[location][2]).css('height', coordinates[location][3]);
			}
		}
		else{
			$("#highlight").remove();
			$("#baxters").hide();
		}
	}
	else{
		$("#highlight").remove();
		$("#baxters").hide();
	}
};

map.showPopup = function(location, thisPopup){
	$(".popup").hide();
	$("h3", thisPopup).text(location);
	
	if (info[location]['site']){
		$(".links", thisPopup).show();
		$("a:first", thisPopup).attr('href', info[location]['site']);
	}
	else{
		$(".links", thisPopup).hide();
	}
	
	if (info[location]['menu']){
		$(".menu", thisPopup).show();
		$("a:eq(1)", thisPopup).attr('href', info[location]['menu']);
	}
	else{
		$(".menu", thisPopup).hide();
	}
	
	if (info[location]['reviews']){
		$(".reviews", thisPopup).show();
		$(".reviews", thisPopup).attr('href', info[location]['reviews']);
	}
	else{
		$(".reviews", thisPopup).hide();
	}
	
	if (info[location]['phone']){
		$(".phone", thisPopup).show();
		$(".phone", thisPopup).text(info[location]['phone']);
	}
	else{
		$(".phone", thisPopup).hide();
	}
	
	if (!info[location]['site'] && !info[location]['menu'] && !info[location]['reviews']){
		$(".no-info").show();
	}
	else{
		$(".no-info").hide();
	}
	
	if (thisPopup === "#popup-top"){
		$(thisPopup).css('top', -40);
	}
	else{
		$(thisPopup).css('top', coordinates[location][1]-45);
	}
	
	var leftPosition = coordinates[location][0]+$("#map-window").offset().left-$("#map-window").scrollLeft()-30;
	$(thisPopup).css('left', leftPosition).show();
	map.popupOpen = true;
	
	if (location === "Baxter's"){
		$("#baxters").show();
	}
	else{
		$("#map-window").append("<div id='highlight-open'> </div>");
		$("#highlight-open").css('top', coordinates[location][1]).css('left', coordinates[location][0]).css('width', coordinates[location][2]).css('height', coordinates[location][3]);
	}
};

map.hidePopups = function(){
	$(".popup").hide();
	$("#highlight-open").remove();
	$("#baxters").hide();
	map.popupOpen = false;
};