﻿function trim(string) {
    if (string == undefined)
        return "";

    return string.replace(/^\s*/, "").replace(/\s*$/, "");
}
function handleDefaults() {
    $(".default").each(function () {
        var defaultValue = trim($(this).attr("default"));
        $(this).addClass("input-default-text");
        $(this).val(defaultValue);

        $(this).focus(function () {
            $(this).removeClass("input-default-text");
            if (trim($(this).val()) == defaultValue) {
                $(this).val("");
            }
        });

        $(this).blur(function () {
            if ($(this).val().length == 0) {
                $(this).val(defaultValue);
                $(this).addClass("input-default-text");
            }
        });
    });
}

$(document).ready(function () {
    handleDefaults();

    $("#topSearch").keydown(function (e) {
        if (e.keyCode == 13)
            return false;
    });

    $("#topSearch").keyup(function (e) {
        if (e.keyCode == 13)
            DoSearch();
    });
});

function DoSearch() {
    var keywords = trim($("#topSearch").val());

    if (keywords.length > 0) {
        window.location = "/Search.aspx?keywords=" + escape(keywords);
    }
}

function Retweet() {
    window.open("http://twitter.com/home/?status=RT+@yukon_wild+" + document.title + "+" + document.location.href.replace("#", ""));
}



function TrackOutgoingOperatorLink(anchor, tag) {
    _gaq.push(['_trackPageview', '/outgoing/operator/' + tag]);
    //alert("testing");
    //setTimeout("window.open('" + anchor.attr("href") + "');", 100);
}



var SavedTripButton;

function AddSavedTrip(buttonElement, tripId) {
    SavedTripButton = buttonElement;
    AsyncService.AddSavedTrip(tripId, addSavedTripSuccess, addSavedTripFailed);
}


function addSavedTripSuccess(result) {
    if (! $("#MyTripsButton").is(":visible")) {
        $("#MyTripsButton").fadeIn();
    }

    if (SavedTripButton != null) {
        SavedTripButton.removeClass("trips-botton-inactive").addClass("trips-button-inactive");
        SavedTripButton.text("Saved to My Trips");
        SavedTripButton.removeAttr("onclick");
        SavedTripButton.unbind("click");
        SavedTripButton.bind("click", function () { return false; });
        SavedTripButton = null;
    }
}

function addSavedTripFailed(result) {
    alert("Saving that trip failed. Try refreshing this page and trying again.");
}

function RemoveSavedTrip(buttonElement, tripId) {
    SavedTripButton = buttonElement;
    AsyncService.RemoveSavedTrip(tripId, RemoveSavedTripSuccess, RemoveSavedTripFailed);
}

function RemoveSavedTripSuccess(result) {
    if (SavedTripButton != null) {
        SavedTripButton.closest(".media-box").slideUp();
    }
}

function RemoveSavedTripFailed(result) {
    alert("Removing that trip failed. Try refreshing this page and trying again.");
}


function ShowInformationNoAni() {
    $(".control-bar a").removeClass("active");
    $(".control-bar a.first").addClass("active");

    $(".control-panel.second").hide();
    $(".control-panel.first").show();
}
function ShowExpertsNoAni() {
    $(".control-bar a").removeClass("active");
    $(".control-bar a.second").addClass("active");

    $(".control-panel.first").hide();
    $(".control-panel.second").show();
}
function ShowExperts() {
    $(".control-bar a").removeClass("active");
    $(".control-bar a.second").addClass("active");

    $(".control-panel.first").fadeOut();
    $(".control-panel.second").fadeIn();
}
function ShowInformation() {
    $(".control-bar a").removeClass("active");
    $(".control-bar a.first").addClass("active");

    $(".control-panel.second").fadeOut();
    $(".control-panel.first").fadeIn();
}

function ShowMapHover(element) {
    var popup = $("#MapPopup");

    var textDivName = element.attr("alt") + "-Text";
    popup.html($("#" + textDivName).html());
    popup.css("margin-top", 23);
    popup.css("margin-left", 437);
    popup.fadeIn();

    var closer = popup.prepend("<div style='width: 24px; height: 24px; background: url(/Images/NewMapClose.png) no-repeat; float: right; cursor: pointer;'></div>").first("div");
    closer.click(function () {
        DoPopupClose();
        $(".map-circle").attr("src", "/Images/NewMapCircle.png").removeClass("selected");
    });
}
function DoPopupClose() {
    closeHandle = null;
    $("#MapPopup").fadeOut();
}

function ShowMapHover2(element) {
    var popup = $("#MapPopup2");

    var textDivName = element.attr("alt") + "-Text";
    popup.html($("#" + textDivName).html());
    popup.css("margin-top", 23);
    popup.css("margin-left", 437);
    popup.fadeIn();

    var closer = popup.prepend("<div style='width: 24px; height: 24px; background: url(/Images/NewMapClose.png) no-repeat; float: right; cursor: pointer;'></div>").first("div");
    closer.click(function () {
        DoPopupClose2();
        $(".map-circle").attr("src", "/Images/NewMapCircle.png").removeClass("selected");
    });
}
function DoPopupClose2() {
    $("#MapPopup2").fadeOut();
}

function BuildMapPopups() {
    var mapContainer = $(".yukon-map-container");
    var riverContainer = $(".yukon-river-container");

    createCircle(riverContainer, "Firth", 60, 144, 40, ShowMapHover2, DoPopupClose2);
    createCircle(riverContainer, "Peel", 241, 259, 40, ShowMapHover2, DoPopupClose2);
    createCircle(riverContainer, "Snake", 301, 289, 40, ShowMapHover2, DoPopupClose2);
    createCircle(riverContainer, "BonnetPlume", 350, 279, 40, ShowMapHover2, DoPopupClose2);
    createCircle(riverContainer, "Wind", 322, 234, 40, ShowMapHover2, DoPopupClose2);
    createCircle(riverContainer, "Hart", 275, 219, 40, ShowMapHover2, DoPopupClose2);
    createCircle(riverContainer, "Blackstone", 285, 171, 40, ShowMapHover2, DoPopupClose2);
    createCircle(riverContainer, "Liard", 624, 397, 40, ShowMapHover2, DoPopupClose2);
    createCircle(riverContainer, "Hyland", 613, 460, 40, ShowMapHover2, DoPopupClose2);
    createCircle(riverContainer, "Yukon", 541, 216, 50, ShowMapHover2, DoPopupClose2);
    createCircle(riverContainer, "Alsek", 619, 122, 50, ShowMapHover2, DoPopupClose2);
    createCircle(riverContainer, "Dezadeash", 586, 172, 40, ShowMapHover2, DoPopupClose2);
    createCircle(riverContainer, "TeslinR", 603, 269, 40, ShowMapHover2, DoPopupClose2);
    createCircle(riverContainer, "Nisutlin", 613, 313, 40, ShowMapHover2, DoPopupClose2);
    createCircle(riverContainer, "BigSalmon", 519, 259, 40, ShowMapHover2, DoPopupClose2);
    createCircle(riverContainer, "McQuesten", 411, 175, 40, ShowMapHover2, DoPopupClose2);
    createCircle(riverContainer, "Hess", 440, 271, 40, ShowMapHover2, DoPopupClose2);
    
    createCircle(mapContainer, "Ivvavik", 47, 131, 70, ShowMapHover, DoPopupClose);
    createCircle(mapContainer, "EaglePlains", 237, 213, 40, ShowMapHover, DoPopupClose);
    createCircle(mapContainer, "Tombstone", 308, 152, 60, ShowMapHover, DoPopupClose);
    createCircle(mapContainer, "Dawson", 366, 109, 50, ShowMapHover, DoPopupClose);
    createCircle(mapContainer, "PeelRiver", 310, 260, 70, ShowMapHover, DoPopupClose);
    createCircle(mapContainer, "Mayo", 391, 222, 70, ShowMapHover, DoPopupClose);
    createCircle(mapContainer, "YukonRiver", 463, 151, 40, ShowMapHover, DoPopupClose);
    createCircle(mapContainer, "Junction", 562, 169, 40, ShowMapHover, DoPopupClose);
    createCircle(mapContainer, "Kluane", 610, 120, 70, ShowMapHover, DoPopupClose);
    createCircle(mapContainer, "Logan", 607, 49, 40, ShowMapHover, DoPopupClose);
    createCircle(mapContainer, "Whitehorse", 595, 227, 40, ShowMapHover, DoPopupClose);
    createCircle(mapContainer, "Teslin", 562, 270, 70, ShowMapHover, DoPopupClose);
    createCircle(mapContainer, "Chilkoot", 659, 228, 40, ShowMapHover, DoPopupClose);
    createCircle(mapContainer, "Watson", 575, 422, 70, ShowMapHover, DoPopupClose);
}

function createCircle(container, alt, top, left, size, clickAction, closeAction) {
    var circle = container.prepend("<img src='/Images/NewMapCircle.png' class='map-circle' alt='" + alt + "'/>").children(".map-circle::first");
    circle.css("margin-top", top).css("margin-left", left).css("width", size).css("height", size);
    
    circle.click(function () {
        if (circle.is(".selected")) {
            $(".map-circle").attr("src", "/Images/NewMapCircle.png").removeClass("selected");
            circle.attr("src", "/Images/NewMapHover.png").removeClass("selected");
            closeAction();
        }
        else {
            $(".map-circle").attr("src", "/Images/NewMapCircle.png").removeClass("selected");
            circle.attr("src", "/Images/NewMapSelect.png").addClass("selected");

            clickAction(circle);
        }
    });
    circle.mouseenter(function () { if (!circle.is(".selected")) circle.attr("src", "/Images/NewMapHover.png"); });
    circle.mouseleave(function () { if (!circle.is(".selected")) circle.attr("src", "/Images/NewMapCircle.png"); });
}
