﻿// Class MapState
//
// - Requires -
// JSON2.js
// jQuery.js
//
// - Properties -
// latitude: user latitude
// longitude: user longitude
// location: user location
// page: which page of results
// radius: search radius in miles
// sortOrder: order results are sorted in
// spotIds: array of id numbers representing the spots on the map
// categories: array of category filter types
// totalSpots: the number of total spots via current search
// queryStatus: 0: valid results 2: not logged in & no ip detected, 3: invalid user input
// markers: array of the GMarkers visible on the map

BenefulMap.MapState = Class.extend({
	init: function(latitude, longitude, keyword, location, page, radius, sortOrder, spotIds, categories, totalSpots, queryStatus, markers) {
	    this.latitude = latitude;
    	this.longitude = longitude;
    	this.keyword = keyword;
    	this.location = location;
    	this.page = page;
    	this.radius = radius;
    	this.sortOrder = sortOrder;
    	this.spotIds = spotIds;
    	this.categories = categories;
    	this.totalSpots = totalSpots;
    	this.queryStatus = queryStatus;
    	this.markers = markers;
    	this.map;
    	this.mapCenter = BenefulMap.Constants.CENTER_LATLNG;
    	this.defaultZoom = BenefulMap.Constants.USA_ZOOM;
		this.goButtonClicked = false;		
		this.defaultViewState = false;
		this.totalFriendlyCount;
	},
	getTotalFriendlyCount: function() {
		return this.totalFriendlyCount;	
	},
	setTotalFriendlyCount: function(newFriendlyCount) {
		this.totalFriendlyCount = newFriendlyCount;	
	},
	getDefaultView: function() {
		return this.defaultViewState;
	},
	setDefaultView: function(newDefaultViewState) {
		var self = this;
		this.defaultViewState = newDefaultViewState;
		console.debug('Set Default View: ' + newDefaultViewState);
		$('#results-sort option.best-match').remove();
		$('#results-sort option.distance').remove();
		if(newDefaultViewState === true) {
			$('#results-sort option.highest-rated').attr('selected', 'selected');
		}
		else {
			var keyword = $('#keyword-search').val();
			var location = $('#address-search').val();
			if(keyword && keyword !== BenefulMap.Constants.SEARCH_KEYWORD_DEFAULT_VALUE) {
				console.debug('Set Results Pane to Best Match');
				$('#results-sort option.highest-rated').removeAttr('selected');
				this.setSortOrder(7);
				$('<option class="distance" value="1">Distance</option>').prependTo("#results-sort");
				$('<option class="best-match" value="7" selected="selected">Best Match</option>').prependTo("#results-sort");
			}
			else {
				console.debug('Set Results Pane to Distance');
				$('#results-sort option.highest-rated').removeAttr('selected');
				this.setSortOrder(1);
				$('<option class="distance" value="1" selected="selected">Distance</option>').prependTo("#results-sort");
			}
			console.debug('Grabbing sort id: ' + this.getSortOrder());
		}
	},
	isDefaultView: function() {
		return this.defaultViewState;
	},
	getKeyword: function() {
		return this.keyword;
	},
	setKeyword: function(keyword) {
		this.keyword = keyword;
	},
	getGoButtonClicked: function() {
		return this.goButtonClicked;
	},
	setGoButtonClicked: function(goButtonClicked) {
		this.goButtonClicked = goButtonClicked;
	},
    getDefaultZoom: function() {
        return this.defaultZoom;
    },
    setDefaultZoom: function(zoom) {
        this.defaultZoom = zoom;
    },
    getMapCenter: function() {
        return this.mapCenter;
    },
    setMapCenter: function(mapCenter) {
        this.mapCenter = mapCenter;
    },
    setLatLngToMapCenter: function() {
        var center = this.getMapCenter();
        this.setLatitude(center.lat());
        this.setLongitude(center.lng());
    },
    getGoogleMap: function() {
    	return this.map;
    },
    setGoogleMap: function(newMap) {
    	this.map = newMap;
    },
    getLatitude: function() {
    	return this.latitude;
    },
    setLatitude: function(latitude) {
    	this.latitude = latitude;
    },
    getLongitude: function() {
    	return this.longitude;
    },
    setLongitude: function(longitude) {
    	this.longitude = longitude;
    },
    getLocation: function() {
    	return this.location;
    },
    setLocation: function(location) {
    	this.location = location;
    },
    getPage: function() {
    	return parseInt(this.page, 10);
    },
    setPage: function(page) {
    	this.page = parseInt(page, 10);
    },
    getRadius: function() {
    	return this.radius;
    },
    setRadius: function(radius) {
    	this.radius = radius;
    },
    getSortOrder: function() {
    	return this.sortOrder;
    },
    setSortOrder: function(sortOrder) {
    	this.sortOrder = sortOrder;
    },
    getSpotIds: function() {
    	return this.spotIds;
    },
    setSpotIds: function(spotIds) {
    	this.spotIds = spotIds;
    },
    getCategories: function() {
    	return this.categories;
    },
    setCategories: function(categories) {
    	this.categories = categories;
    },
    getTotalSpots: function() {
    	return this.totalSpots;
    },
    setTotalSpots: function(totalSpots) {
    	this.totalSpots = totalSpots;
    },
    getQueryStatus: function() {
    	return this.queryStatus;
    },
    setQueryStatus: function(queryStatus) {
    	this.queryStatus = queryStatus;
    },
    resetPage: function() {
    	this.page = 1;
    },
    noCookieDetected: function() {
    	this.setQueryStatus(1);
    	$("#target-address").removeClass("mapInputError");
    },
    isNoCookieDetected: function() {
    	if (this.getQueryStatus() == 1) {
    		return true;
    	} else {
    		return false;
    	}
    },
    inputError: function() {
    	this.setQueryStatus(2);
    	$("#target-address").addClass("mapInputError");
    },
    isInputError: function() {
    	if (this.getQueryStatus() == 2) {
    		return true;
    	} else {
    		return false;
    	}
    },
    validQuery: function() {
    	this.setQueryStatus(0);
    	$("#target-address").removeClass("mapInputError");
    },
    isValid: function() {
        if (this.getQueryStatus() == 0) {
            return true;
        } else {
            return false;
        }
    },
    getMarkers: function() {
    	return this.markers;
    },
    setMarkers: function(markers) {
    	this.markers = markers;
    },
    scrapeSpotIds: function(elements) {
    	var spotIds = []; 
        $(elements).each(function() {
           spotIds.push($(this).attr("id").replace(BenefulMap.Constants.Strings.RESULTS_ITEM_PREFIX, ""));
        });
        this.setSpotIds(spotIds);
    },
    scrapeCategories: function(elements) {
    	var categories = [];
    	$(elements).each( function() {
    		if (this.checked == true) {
    			var catNum = parseInt(this.name.replace(BenefulMap.Constants.Strings.CATEGORY_FILTER_PREFIX,''));
    			categories.push(catNum);
    		}
    	});
    	if (categories.length == 0) {
    		categories.push(0);
    	}
    	this.setCategories(categories);
    },
    scrapeRadius: function(element) {
    	this.setRadius( $(element).val() );
    },
	scrapeSearchField: function(selector) {
		this.setKeyword( $(selector).val() );
	},
    toJSON: function() {
    	var output = {};
		output.Keyword = $("#keyword-search").val();
		output.Location = this.getLocation();
		output.Page = this.getPage();
    	output.SortBy = this.getSortOrder();
    	output.BusinessId = this.getSpotIds();
    	output.State = parseInt(this.getQueryStatus());
    	output.GoButtonClicked = this.getGoButtonClicked();
		if (jQuery.url.param("ref")) {
			output.PageContext = jQuery.url.param("ref");
		} else {
			output.PageContext = $("#page-context-wrap input").val();
		}
    	return this._stringify(output);
    },
    _stringify: function(input) {
    	if (typeof JSON.stringify === 'function') {
    		return JSON.stringify(input);
    	} else {
    		throw new Error('JSON.stringify not found');
    	}
    }
});

window.onerror = function() {
	return false;
};

