var $body;
var $box;
var $closeBox;
var $contactForm;
var $contactFormMessage;
var $contactFormSubject;
var $contactFormSubmit;
var $contactLoading;
var $nav;
var $navInline;
var $tweet;
var mo;
var mo2;

$(document).ready(function () {
    $contactForm = $("#contact_form");
    $contactFormMessage = $contactForm.find("#message");
    $contactFormSubject = $contactForm.find("#subject");
    $contactFormSubmit = $contactForm.find("#submit");
    $contactLoading = $contactForm.find("#dvContactLoading");
    $tweet = $("#twitter_feed");
    $("#portfolio_slider").removeClass("slider_no_js");
    $("#gallery_nav").removeClass("gallery_nav_no_js");

    $("a[rel^='prettyPhoto']").prettyPhoto({
        theme: "light_square"
    });

    $tweet.tweet({
        avatar_size: null,
        count: 1,
        fetch: 1,
        loading_text: "Loading Twitter feed...",
        query: "#shamrocktheville"
    })
		.unbind("empty").bind("empty", emptyTweet)
		.unbind("loaded").bind("loaded", fixTweet);

    $.fn.qtip.styles.custom = {
        fontSize: 10,
        padding: 5,
        background: "#252525",
        color: "#8d8d8d",
        border: {
            width: 0,
            radius: 0,
            color: "#282828"
        }
    };

    $(".click_box").qtip({
        content: "More...",
        style: "custom",
        position: {
            corner: {
                target: "topLeft",
                tooltip: "bottomLeft"
            }
        },
        show: "mouseover",
        hide: "mouseout"
    });

    $("#navigation").each(function () {
        var $links = $(this).find("a"),
        panelIds = $links.map(function () {
            return this.hash;
        }).get().join(","),
        $panels = $(panelIds),
        $panelwrapper = $panels.filter(":first").parent(),
        delay = 400,
        heightOffset = 40;
        $panels.hide();

        $links.click(function (e) {
            $(".boxes").animate({
                opacity: 0
            }, delay, function () {
                $(".boxes").hide();
            });

            var link = this,
            $link = $(this);
            var nextPage = $link.attr("href");
            var newHeight = $(nextPage).outerHeight(true);

            if ($link.is(".selected"))
                return false;

            $links.removeClass("selected");
            $link.addClass("selected");

            $panels.stop().animate({
                opacity: 0
            }, delay);
            $(".panel img, .project").animate({
                opacity: 0
            }, delay);

            $("html, body").animate({
                scrollTop: 0
            }, function () {
                $panelwrapper.stop().animate({
                    height: newHeight
                }, delay, function () {
                    var height = $panels.hide().filter(link.hash).animate({
                        opacity: 1
                    }).show().height() + heightOffset;
                    $(".panel img, .project").animate({
                        opacity: 1
                    }, delay);
                    $panelwrapper.animate({
                        height: height
                    }, delay);
                });
            });

            if ($link.attr("href") == "#contact")
                clearContactForm();

            var $target = $(e.target);
            var subject = $target.attr("subject");

            if (subject) {
                $contactFormSubject.val(subject);
                $target.removeAttr("subject");
            }

            return true;
        });
    });

    $("#navigation").find("a").filter(window.location.hash ? function (index) {
        return $("#navigation").find("a").eq(index).attr("href") == window.location.hash;
    } : ":first").click();

    $contactForm.ajaxForm({
        target: "#message_outcome",
        beforeSubmit: function () {
            contactShowLoading();
            $contactFormSubmit.attr("disabled", "disabled");
            $("#message_outcome").addClass("visible");
        },
        success: function () {
            $("#message_outcome").show().delay(3000).fadeOut("slow", function () { $(this).html(""); clearContactForm(); });
            $contactFormSubmit.removeAttr("disabled");
            contactHideLoading();
        }
    });

    $(".textbox, #message").focus(function () {
        $(this).css({
            backgroundColor: "#141414",
            borderColor: "#303030"
        });
        $(this).blur(function () {
            $(this).css({
                backgroundColor: "#121212",
                borderColor: "#282828"
            });
        });
    });

    $("ul.social_links li").hover(function () {
        $(this).css("backgroundColor", "#1b1b1b");
    }, function () {
        $(this).css("backgroundColor", "#161616");
    });

    $("#portfolio_slider").cycle({
        fx: "fade",
        speed: 500,
        timeout: 0,
        cleartype: false,
        startingSlide: 0,
        next: "#next",
        prev: "#prev"
    });

    $("#latest_projects_slider").cycle({
        fx: "fade",
        speed: 500,
        timeout: 5000,
        cleartype: false,
        startingSlide: 0,
        next: "",
        prev: ""
    });

    $(function () {
        $(".gallery img").animate({
            "opacity": 1
        });

        $(".gallery img").hover(function () {
            $(this).stop().animate({
                "opacity": .5
            });
        },
        function () {
            $(this).stop().animate({
                "opacity": 1
            });
        });
    });

    $("boxes").css("opacity", 0);

    $(".close_box").click(function () {
        $(".boxes").animate({
            opacity: 0
        }, 200, function () {
            $(".boxes").hide();
        });

        return false;
    });

    $(".click_box").click(function (e) {
        var bid = $(e.srcElement).attr("box");
        var chk = bid && bid.substr(1, bid.length) == $(".boxes:visible").attr("id");

        if (!chk) {
            if ($(".boxes").is(":visible")) {
                $(".boxes").animate({
                    opacity: 0
                }).hide();
            }

            $($(this).attr("box")).show().animate({
                opacity: 1
            }, 400);
        }

        return false;
    });

    $body = $('body');
    $box = $(".boxes");
    $closeBox = $(".close_box");
    $nav = $("#navigation");
    $navInline = $(".navigation-inline");

    $body
    .unbind("click")
    .click(bodyClick);

    $box
    .unbind("click")
    .click(boxClick);

    $navInline.click(navInlineClick);
});

$(window).load(function () {
    mo = initMap("map", 36.7535, -95.9793, 15);

    mo.addMarker(36.7510697, -95.9763878, "Haskell Loft", function () {
        $("#aPRPAddress").click();
    });

    mo2 = initMap("race-map", 36.756, -95.997, 13);

    mo2.addLayerKML("http://shamrocktheville.com/kml/map-course.kml", function () {
        $("#aMapLink").click();
    });
});

function bodyClick(e) {
    if ($box.is(":visible"))
        $closeBox.click();
}

function boxClick(e) {
    catchEvent(e);

    if (e.target.nodeName == "A")
        window.location = e.target.href;
}

function catchEvent(e) {
    e.preventDefault();
    e.stopPropagation();
}

function clearContactForm() {
    $contactForm.find("input").filter(".textbox").val("").blur();
    $contactFormMessage.val("").blur();
    $contactForm.find("select").val("Subject");
    return false;
}

function contactHideLoading() {
    $contactLoading.hide();
    return false;
}

function contactShowLoading() {
    $contactLoading.show();
    return false;
}

function emptyTweet() {
    $tweet.html('<div class="tweet_list">Tweets with <a href="http://search.twitter.com/search?q=&tag=shamrocktheville&lang=all">#shamrocktheville</a> will appear here</div>');
}

function fixTweet() {
    var $time = $tweet.find(".tweet_time");
    $tweet.find(".tweet_first.tweet_odd").prepend('<a href="http://search.twitter.com/search?q=&tag=shamrocktheville&lang=all"><img src="img/twitter-icon.png" class="twitter-icon" alt="Twitter Feed" /></a>');
    $time.detach();
    $tweet.find(".tweet_list").find("li:first").append($time);
}

function navInlineClick(e) {
    var $e = $(e.srcElement);
    var s = $e.attr("subject");
    var t = $e.attr("href");
    var $n = $nav.find("li").find('a[href="' + t + '"]');

    if (s)
        $n.attr("subject", s);

    $n.click();
}
