function gmapLinkify(address) {
    var plusAddress = address.replace(/\s/g, "+");
    window.location = "http://maps.google.com/maps?q="+plusAddress+"&iwstate1=dir";
}

function genEventInfo(description, address) {
    var described = "<p><strong>Description:</strong> "+description+"</p>";
    var addressed = "<p><strong>Address:</strong> "+address+"</p>";
    $('#event_info').html(described+addressed);
}

$(function() {
    var has_canvas = document.getElementById("map_canvas");
    
    if (has_canvas) {
        var geocoder = new google.maps.Geocoder();
        var myOptions = {
            zoom: 15,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(has_canvas, myOptions);
        
        var address = "";
        if ($("#event_list").length) {
            address = $('#event_list li:first span.address').text(); // address to first event on events page
        } else {
            address = $('li.address span').text(); // address to event on home page
        }
        
        function setPoint(address) {
            geocoder.geocode({'address': address}, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    map.setCenter(results[0].geometry.location);
                    var marker = new google.maps.Marker({
                        map: map,
                        position: results[0].geometry.location
                    });
                    
                    google.maps.event.addListener(marker, 'click', function(event) {
                        gmapLinkify(address);
                    });
                } else {
                    console.log("Geocode was not successful for the following reason: " + status);
                }
            });
        }
        
        setPoint(address);
    }
    
    // BAD INTERNET EXPLORER!

    if (jQuery.support.boxModel!=true) {
        $("p#bad_ie").show();
    }

    // Initialize Events Page
    $('#event_list li:first').addClass('current');
    var first_description = $('#event_list li:first span.description').html();
    genEventInfo(first_description, address);
    
    // Events Page Swap Event Info
    
    $("#event_list li").click(function() {
        var new_address = $(this).children("span.address").text();
        var new_description = $(this).children("span.description").html();
        $("#event_list li").removeClass("current");
        $(this).addClass("current");
        setPoint(new_address);
        genEventInfo(new_description, new_address);
    });
    
    // Events Page Cycle Events
    function prepEvents() {
        $("#event_list li:last").addClass("last");
        $("#event_list li:first").addClass("first");
    
        if(!$("#event_list li:eq(3)").hasClass("last")) {
            $("#event_list li:eq(3)").addClass("fake_last");
            $("#event_list li:gt(3)").hide();
        }
    }
    
    prepEvents();
    
    if(location.hash.search(/e[0-9]+/) != -1) {
        $("#event_list li").removeClass("fake_last,fake_first").hide();
        $(location.hash).show().click();
    }
    
    $("#event_list li.fake_last").live("click", function() {
        if(!$(this).hasClass("last")) {
            $("#event_list li:visible:eq(0)").slideUp().removeClass("fake_first")
            .next().addClass("fake_first");
            $(this).removeClass("fake_last")
            .next().slideDown().addClass("fake_last");
        }
    });
    
    $("#event_list li.fake_first").live("click", function() {
        if(!$(this).hasClass("first")) {
            $("#event_list li:visible:eq(3)").slideUp().removeClass("fake_last")
            .prev().addClass("fake_last");
            $(this).removeClass("fake_first")
            .prev().slideDown().addClass("fake_first");
        }
    });
    
    // Show only classes
    
    $("#classy").click(function() {
        var n = $("#event_list li.class").length;
        
        if (n != 0) {
            $("#event_list li").show().removeClass("fake_first, fake_last").not(".class").remove();
            prepEvents();
        }
        
        var plural = "";
        if (n != 1) {
            plural = "es"
        }
        $("#event_count").text(n+" upcoming class"+plural);
        return false;
    });

    // Books page clicky
    
    $("#books > li").click(function() {
        if(!$(this).hasClass("current")) {
            $("#books li").removeClass("current")
            .children("img").animate({width: "110px", height: "170px", marginLeft: "0px", marginBottom: "0px"})
            .siblings(".content").slideUp();
            $(this).addClass("current")
            .children("img").animate({width: "220px", height: "340px", marginLeft: "32px", marginBottom: "32px"})
            .siblings(".content").slideDown();
            if (!$(this).children(".buy_info").is(":visible")) {
                $(".buy_info").fadeOut();
            }
            location.hash = $(this).attr("id");
        }
    });
    
    // Show buy it div
    
    $(".buy_link").click(function() {
        $(this).parent("li").click();
        $($(this).attr("href")).fadeIn();
        return false;
    });
    
    $(".close_box").click(function() {
        $(this).parent().fadeOut();
        return false;
    });
        
    // Contact Slide Down
    
    function contactShow() {
        $("#contact_link").addClass("current").text("Close");
        $("#contact").slideDown();
    }
    
    if (location.hash == "#contact") {
        contactShow();
    } else if (location.hash) {
        $(location.hash).click();
    }
    
    $("#contact_link").click(function() {
        if(!$(this).hasClass("current")) {
            contactShow();
        }
        else {
            $(this).removeClass("current").text("Contact");
            $("#contact").slideUp();
        }
    });
});
