// shouldn't that be part of jQuery?
// http://snipplr.com/view/3658/stop-default-action/
function stop(e) {
    if (!e) e = window.event;
    (e.stopPropagation) ? e.stopPropagation() : e.cancelBubble = true;
    (e.preventDefault) ? e.preventDefault() : e.returnValue = false;
    return false;
}

function toggle_user_area(event) {
    $("#expanded").toggle()
    $("#folded").toggle();
    //adjust_top_margin()
    $.get("/header/toggle")
    stop(event)
}

function header() {
    $("a[href^=/header/]")
    .click(function(event) {
        toggle_user_area(event)
    })
    $("#header")
    .click(function (event) {
        var target = $(event.target)
        if (target.is("div")) {
            toggle_user_area(event)
        }
    })
}

function slidebar() {

    // make the hide div visible
    $("#hide").show()
    // hide the slidebar if no filter and no add-form
    if (($("div[name=filters] ul li").length == 0)
       && ($("#slidebar form[name=add-form]").length == 0))
    {
            $("#slidebar").hide()
    }

    // if add-form hide filters
    // and replace add href in footer
    if ($("#slidebar form[name=add-form]").length != 0) {
        $("div[name=filters]").hide()
        $("#footer a[href=/add]")
        .click(function (event) {
            $("#slidebar #add").show()
            $("form[name=add-form]").show()
            $("div[name=filters]").hide()
            $("#slidebar").show()
            $("form[name=add-form] input[name=isbn]").focus()
            stop(event)
            })
    }

    // when click on hide hide slidebar
    $("#hide>a").click(function(){
        $("#slidebar").hide()
    })
}

function set_focus() {
    // always focus on the command line unless ...
    var flag = $("input.focus").focus().length
    if (flag<1) $("input[name=query]").focus();
}

function search() {
    // when focus on query show slidebar if any filter
    // if add-form hide it and show filters
    $("input[name=query]").focus(function() {
        if ($("div[name=filters] ul li").length > 0)
            $("#slidebar").show()
        else
            $("#slidebar").hide()
        if ($("#slidebar form[name=add-form]").length != 0) {
            $("div[name=filters]").show()
            $("#slidebar form[name=add-form]").hide()
        }
    })
}

function toggle_password(event) {
    input = $("#main input[name=password]")
    switch ($(this).html()) {
        case "show": {
            $(this).html("hide")
            input.replaceWith('<input size="10" name="password" type="text" value="'
                + input.val() + '" />')
            break
        }
        case "hide": {
            $(this).html("show")
            input.replaceWith('<input size="10" name="password" type="password" value="'
                + input.val() + '" />')
            break
        }
    }
    stop(event)
}

// hovering book list && click on book title -> zoom_click
function book_hover() {
    $("div[class=book]")
    .not('.done')
    .not('.no_hover')
    .hover(
        function () {
            $(this)
            .css("background","#EEE8AA")
            .not('.round')
            .corner('round')
            .addClass('round')
            },
        function () {
            color = ""
            zoom = $(this).children('.zoom_loading')
            if ((zoom.length > 0) && (zoom.is(':visible'))) color = "#EEE";
            $(this).css("background", color)
            }
    )
    .click(function(event) {
        var target = $(event.target)
        if (target.is("div")) {
            zoom_click($(this))
        }
    })
    .addClass('done')

    // A - ZOOM
    $("a[href^=/zoom/]")
    .not('.done')
    .click(function(e) {
        var target = $(this).parents("div.book")
        zoom_click($(target))
        stop(e)
    })
    .addClass('done')

}

// adjust top margin based on lent/borrowed in header
function adjust_top_margin() {
    if ($("#expanded").css("display") == "none") {
        $("#main").css("margin-top", "4.5em")
    } else {
        switch ($("div.in-out").length) {
            case 0: {
                $("#main").css("margin-top", "7.5em")
                break
            }
            case 1: {
                $("#main").css("margin-top", "8.5em")
                break
            }
            case 2: {
                // default the CSS #main margin-top
                $("#main").css("margin-top", "10em")
                break
            }
        }
    }
}

// round corners of message and context
function rounded_corners() {
    $("#message").corner()
    $("#context").corner()
}

// JS only i.e. not unobstrusive but there anyway
// toggle sidebar
// highlights on hover and round tags

function toggle_sidebar() {
    if ($("#sidebar").is(':visible')) {
        $("#sidebar").hide();
        $("td.sidebar").width(10);
    } else {
        $("#sidebar").show();
        $("td.sidebar").width(200);
    }
}

function sidebar() {
    $("td.sidebar").
    click(function(event) {
            var target = $(event.target)
            if (target.is('td.sidebar')) toggle_sidebar();
    })

    $("#sidebar .tag")
    .hover(
    function () {
        $(this)
        .css('background','#EEE8AA')
        .not('.round').addClass('round').corner()
    },
    function () {
        $(this).css('background', '')
    })
    .click(function() {
        var href = $(this).find("a")[0].href
        window.location.replace(href)
        return false
    })

}

function main() {

    book_hover()
    // password field toggle
    $("a.js_display").show()
        .click(toggle_password)
    //adjust_top_margin()
    rounded_corners()
}

function set_button_width() {
    $("button").width(
    Math.max.apply(
        Math,
        jQuery.map($("button"), function(b) {return $(b).width()})
    )+20)
}

$(document).ready(function(){

    header()
    main()
    sidebar()
    slidebar()
    search()
    set_button_width()
    set_focus()

})

