﻿$(document).ready(function() {
    if ($("#search-template").length) {
        console.log("Running search results page JS...");

        var mapLoader = new BenefulMap.ResultsMapLoader();
        var resultsMap = new BenefulMap.ResultsMap("#map-canvas", "#info-win");
        // infoWinDiv, previewImgDiv, navItems, tabs, closeBtn, contentDiv
        var infoWin = new BenefulMap.InfoWin("#info-win", "#info-img", "#info-win-nav li a", "#info-send-nav li a, #info-win-content a.sendBtn", "#info-win-close", "#info-win-content");
        var spotCount = new BenefulMap.MapCount("#spot-count", "#map-top-panel-contents h3");
        var location = new BenefulMap.MapLocation("#current-location", "#target-address");
        var favorites = new BenefulMap.AddToFavorites("infowin", ".details_of_place li.first");

        BenefulMap.setupTellFriendResultsModal();
        BenefulMap.setupLinkToModal();
		BenefulMap.slideUpCounter();
        
        $(document).bind('map.loaded', function(event, mapState) {
            var resultsPane = new BenefulMap.ResultsPane(BenefulMap.Constants.Snippets.PANE_RESULTS, "#results-pane", "results-pane", mapState, "#results-sort");
            // snippet, pane, mapState, paginationLinks
            var pagePane = new BenefulMap.PaginationPane(BenefulMap.Constants.Snippets.PANE_PAGINATION, "#pagination-pane", mapState, "#pagination-pane a");
        });
    }
});

BenefulMap.slideUpCounter = function() {
    setTimeout(function () {
    	$("#wwWelcomeUsers").animate({ 
        	opacity: "0"
    	}, 1000);
    	setTimeout(function() {
        	$("#wwWelcomeUsers").slideUp(850);
        	$("#search-bar").animate({ 
        		height: "70px"
    		}, 850 );
    		
    		$("#search-top").animate({ 
        		height: "525px"
    		}, 850 );
    	}, 1000);
	}, BenefulMap.Constants.WELCOME_MESSAGE_DURATION);
	
	$.cookie('counterViewed', 1);
};

BenefulMap.setupTellFriendResultsModal = function() {
	var tellFriendEls = {
		modalDiv: "#modal-tell-friend-results",
		targetElement: "#send-results"
	};
	var formEls = {
		fieldsToSerialize: "#modal-tell-friend-results input:text, #modal-tell-friend-results textarea",
		fieldsToValidate: "#modal-tell-friend-results input:text, #modal-tell-friend-results textarea",
		requiredFields: "#modal-tell-friend-results input:text"
	};
	var newModal = new BenefulMap.ModalTellFriend("Send These Results", tellFriendEls, {}, BenefulMap.Constants.Snippets.MODAL_TELL_FRIEND, {}, formEls, "results");	
};

BenefulMap.setupLinkToModal = function() {
	LinkToEls = {
		modalDiv: "#modal-link-to",
		targetElement: "#link-results"
	};
	var newModal = new BenefulMap.ModalLinkTo("Link to Results", LinkToEls, {}, BenefulMap.Constants.Snippets.MODAL_LINK_RESULTS, {});
};