$("document").ready(function() {
    bindNav();
    
    $.history.init(function(hash){
        if(hash == "") {
            var projectid = $.getUrlVar('p');
            if (projectid == undefined) {
                loadProject(0);
            } else {
                loadProject(projectid);
                $('html,body').animate({scrollTop: $(".middle").offset().top},500);
            }
        } else {
            loadProject(hash);
        }
    },
    { unescape: ",/" });     
    
    $("img[title]").sadcowtooltip();
    $("#twitter_link").click(function() {
        window.open('http://twitter.com/grimdotdotdot');
    });

    $(".sadcowcarousel").sadcowcarousel();

    $(".sadcowcarousel li").click(function() {
        var pid = $(this).attr("id").substr(1);
        $('html,body').animate({scrollTop: $(".middle").offset().top},500);
        //loadProject(pid);
        jQuery.history.load(pid);
    });
});


function bindNav() {
    $(".nav .navsearch input").val('');

    $(".navbutton, .nav .navsearch, .download_button, .demo_button, .newer_post, .older_post").unbind();

    $(".navbutton, .nav .navsearch, .download_button, .demo_button, .newer_post, .older_post").hover(function() {
        var pos = $(this).css("background-position").split(" ");
        $(this).css("background-position", pos[0]+" -35px");
    }, function() {
        var pos = $(this).css("background-position").split(" ");
        $(this).css("background-position", pos[0]+" 0px");
    });

    $(".navbutton, .download_button, .demo_button, .newer_post, .older_post").mousedown(function() {
        var pos = $(this).css("background-position").split(" ");
        $(this).css("background-position", pos[0]+" -70px");
    });

    $(".navbutton, .download_button, .demo_button, .newer_post, .older_post").mouseup(function() {
        var pos = $(this).css("background-position").split(" ");
        $(this).css("background-position", pos[0]+" 0px");
        //zoom!
        if ($(this).hasClass("homebutton")) {
            $('html,body').animate({scrollTop: $(".top").offset().top},500);
        }
        if ($(this).hasClass("latestbutton")) {
            $('html,body').animate({scrollTop: $(".middle").offset().top},500);
        }
        if ($(this).hasClass("contactbutton")) {
            $('html,body').animate({scrollTop: $(".bottom").offset().top},500);
        }
        if ($(this).hasClass("download_button")) {
            var dl = $(this).attr("id");
            $.ajax({url: "ajax.php", type: "post", data: ({func :  "downloadItem", downloadItem : dl }), async:false, success: function(ret) {
                $("#downloads").text(ret);
                window.open("../downloads/"+dl, "dl");
            }});
        }
        if ($(this).attr("id") == "sendsubmit") {
            getItSent();
        }
        if ($(this).attr("id") == "comsubmit") {
            postComment();
        }
        if ($(this).hasClass("newer_post") || $(this).hasClass("older_post")) {
            $(".sadcowtooltipwrapper").fadeOut(500, function() {
                $(".sadcowtooltipwrapper").remove();
            });
            //loadProject($(this).attr("id"));
            jQuery.history.load($(this).attr("id"));
            $('html,body').animate({scrollTop: $(".middle").offset().top},250);
        }
        if ($(this).hasClass("demo_button")) {
            window.open ($(this).attr("title"));
        }
    });

    $(".nav .navsearch").click(function() {
        $(".nav .navsearch").unbind('mouseenter mouseleave'); // this is the same as "hover"
        $(".nav .navsearch").children("input").unbind('focusout keydown');
        var pos = $(".nav .navsearch").css("background-position").split(" ");
        $(".nav .navsearch").css("background-position", pos[0]+" -70px");

        var searchbox = $(this).children("input");
        searchbox.keydown(function(e) {
            if(e.keyCode == 13) { //return
                if (searchbox.val() != '') {
                    doSearch(searchbox.val());
                }
                searchbox.blur();
                $(".nav .navsearch").css("background-position", pos[0]+" 0px");
                $(".nav .navsearch").children("input").val('');
                bindNav();
            }
        });
        searchbox.focusout(function() {
            if ($(this).val() != '') {
                doSearch($(this).val());
            }
            $(".nav .navsearch").css("background-position", pos[0]+" 0px");
            $(".nav .navsearch").children("input").val('');
            bindNav();
        });
    });
}

function doSearch (searchtag) {
    $.getJSON("ajax.php", { func: "search", searchText: searchtag }, function(search) {
        $(".search_box").html('<div class="close">Close</div>');
        for (var i in search) {
            var sres = '<div class="search_result">' +
                '<div class="search_result_title" id="'+search[i].id+'">'+search[i].title+'</div>' +
                '<div class="search_result_blurb">'+search[i].blurb+'</div>' +
            '</div>'

             $(".search_box").append(sres);
        }
        var boxtop = $(window).scrollTop() + 50;
        $(".search_box").css({"top":boxtop+"px"}).fadeIn();
        $(".search_box .close").unbind("click").click(function() {
            $(".search_box").fadeOut();
        });
        $(".search_result_title").unbind("click").click(function() {
            //loadProject($(this).attr("id"));
            jQuery.history.load($(this).attr("id"));
             $(".search_box").fadeOut();
            $('html,body').animate({scrollTop: $(".middle").offset().top},500);
        });
    });
}

