$(document).ready(function(){
    
    $("#narrow_your_search a").click(function(event){
        event.preventDefault();
        if($("#additional_options").is(":visible")) {
            $("#narrow_your_search a").removeClass("down_arrow_red").addClass("right_arrow_red");
            $("#additional_options").hide(200);
            $("#results_wrapper").addClass("full-width");
            $(".fourth-in-list").css("margin-right", "70px");
            $(".fifth-in-list").css("margin-right", "0px");
            $("#narrow").val("0");
            $.each($("#customise a"), function(k, el) {
                oldhref = $(el).attr("href");
                $(el).attr("href", oldhref.replace("&narrow=1", ""));
            });
            $.each($("div.pagination a"), function(k, el) {
                oldhref = $(el).attr("href");
                $(el).attr("href", oldhref.replace("&narrow=1", ""));
            });
        } else {
            $("#narrow_your_search a").removeClass("right_arrow_red").addClass("down_arrow_red");
            $("#additional_options").show(200);
            $("#results_wrapper").removeClass("full-width");
            $(".fourth-in-list").css("margin-right", "0px");
            $(".fifth-in-list").css("margin-right", "70px");
            $("#narrow").val("1");
            /* also set all customise links to include narrow=1 */
            $.each($("#customise a"), function(k, el) {
                oldhref = $(el).attr("href");
                $(el).attr("href", oldhref + "&narrow=1");
            });
            $.each($("div.pagination a"), function(k, el) {
                oldhref = $(el).attr("href");
                $(el).attr("href", oldhref + "&narrow=1");
            });
            
        }
    });
    
    $("a.exploder").click(function(event) {
        event.preventDefault();
        var object_id = $(this).attr('id');
        var details_div = "#item-details-" + object_id;
        var ajax_url = sf_relative_url_root + "/itemajax/" + object_id;
        var simplediv = "#simple-" + object_id;
        $(simplediv).hide();
        $(details_div).show();
        $(details_div).load(ajax_url, null, function(){
            $(details_div).removeClass('center');
            /* Initialise the div close function.
             * Note: We are still in the load callback here.
             */
            $("a.imploder").click(function(event) {
                event.preventDefault();
                var object_id = $(this).attr('id');
                var simplediv = "#simple-" + object_id;
                var detailsdiv = "#item-details-" + object_id;
                $(detailsdiv).hide();
                $(simplediv).show();
                 
            });
        });
    });
    
    /* this event handles the add to order functionality and the object overlay */
    $("#results_list a[rel]").overlay({
        onBeforeLoad: function(event) {
            target = this.getTarget();
            if( target.tagName != "IMG") {
                /* this is an add to order */
                $("#order_overlay_content").load(target.href);
                var x = $("#num_order_items").html();
                x++;
                $("#num_order_items").html(x);
            }
        },
        onClose: function() {
            $("#order_overlay_content").html("<img src='"+sf_relative_url_root+"/images/template/ajax-loader.gif' class='loader'>");
        }
    });
    

    

   

});

