﻿

$(document).ready(function() {

    $("#hpsearch").focus(function() {
        $("#hpsearch").addClass("focus");
        if (this.value == 'Search By Zip Code') {
            this.value = "";
        }
    }).blur(function() {
        if (!this.value.length) {
            this.value = 'Search By Zip Code';
        }
    }).keypress(function(e) {
        return numbersOnly(e);
    });

    $("#searchResults .hot").live("click", function() {
        window.location.href = $(this).attr("pid") + ".aspx"
    });

    $("#pagination a").live("click", function() {
        var p = $(this).attr("page");
        dbAJAX("/scripts/yoddle/ui.aspx?", "action=page&page=" + p + salt(), pageResults);
        return false;
    });

    $("#searchResults table tr th").live("click", function() {
        var sort = $(this).attr("sort");
        if (sort != undefined) {
            dbAJAX("/scripts/yoddle/ui.aspx?", "action=sort&sort=" + sort + salt(), refreshResults);
        }
    });

    $("#enlarge_controlbar").live("click", function() {
        $("#wrapper").removeClass("overlay");
        $("#enlarge_layer").fadeOut(300);
    });


    $(".asset").live("click", function() {

        var imgw = 800;
        var imgh = 600;
        if ($(this).hasClass("googlemap") == true) {
            $("#enlarge_image").hide();
            $("#enlarge_map").appendTo($("#enlarge_layer"));
            $("#enlarge_map").show();
            //$(".enlarge").attr("target", "locus");
            $("#enlarge_controlbar h5").html("LOCUS MAPS");

        } else {
            var imgurl = $(this).attr("href");
            var imgthumb = $(this).attr("thumb");
            var os = 0;
            imgw = $(this).attr("imgw");
            imgh = $(this).attr("imgh");

            $("#enlarge_image").html("<img src='/assets" + imgurl + "'/>")
            if (imgw > imgh) {

                if (imgw > 800) {
                    os = imgw / 800;
                    imgw = imgw / os;
                    imgh = imgh / os;
                }
                $("#enlarge_image img").attr("width", imgw);

            } else {

                if (imgh > 600) {
                    os = imgw / 600;
                    imgw = imgw / os;
                    imgh = imgh / os;
                }
                $("#enlarge_image img").attr("height", imgh);
            }
            $("#enlarge_layer").show();
            var title = $(this).siblings("label").html();
            $(".enlarge").attr("target", "");

            $("#enlarge_controlbar h5").html(title);
            $("#enlarge_map").hide();
            $("#enlarge_map").appendTo($("#farfaraway"));

            $("#enlarge_image").show();
            //$("#asset_thumb").html("<img src='" + imgthumb + "'/>")
            //$("#asset_thumb").show();

        }

        var tar = "#enlarge_layer";
        var ypos = (($(window).height() / 2) + $(window).scrollTop()) - (imgh / 2);
        var xpos = ($(window).width() / 2) - (imgw / 2);
        if (ypos < 10) { ypos = 20 };

        $(tar).css("top", ypos + "px");
        $(tar).css("left", xpos + "px");
        $(tar).hide();
        $("#wrapper").addClass("overlay");
        $(tar).fadeIn(300);

        //for IE but it still doesn't work.  Sigh
        //$("#enlarge_image").hide();
        //$("#enlarge_map").hide();
        //$("#enlarge_layer").hide();
    });
    
    $(".assetzzz").live("click", function() {
        $(".asset").removeClass("selected");
        $(this).addClass("selected");

        //for IE but it still doesn't work.  Sigh
        $("#enlarge_layer").css("left", "-9999px");
        $("#enlarge_layer").show();
        $("#enlarge_image").show();
        $("#enlarge_map").show();

        if ($(this).hasClass("googlemap") == true) {

            $("#asset_thumb").hide();
            $("#enlarge_image").hide();
            $("#enlarge_map").appendTo($("#enlarge_layer"));
            $("#map_canvas").show();
            $("#enlarge_map").show();
            $(".enlarge").attr("target", "locus");
            $("#enlarge_controlbar h5").html("LOCUS MAP");

        } else {
            var imgurl = $(this).attr("href");
            var imgthumb = $(this).attr("thumb");
            var title = $(this).children("label").html();
            $(".enlarge").attr("target", "");
            $("#enlarge_image").html("<img src='/assets" + imgurl + "'/>")
            $("#enlarge_controlbar h5").html(title);
            $("#enlarge_map").hide();
            $("#enlarge_map").css("visible", "hidden");
            $("#map_canvas").hide();
            $("#enlarge_map").appendTo($("#farfaraway"));

            $("#enlarge_image").show();

            //$("#enlarge_map div").hide();

            $("#asset_thumb").html("<img src='" + imgthumb + "'/>")
            $("#asset_thumb").show();
        }
        //for IE but it still doesn't work.  Sigh
        $("#enlarge_image").hide();
        $("#enlarge_map").hide();
        $("#enlarge_layer").hide();
    });

    $(".enlargezzz").live("click", function() {
        var tar = "#enlarge_layer";
        var w = $(".enlarge").attr("target");
        //if (w == "locus") { tar = "#enlarge_map"; }
        $(tar).css("top", "-2000px");
        $(tar).css("left", "-2000px");
        $(tar).show();

        var ypos = (($(window).height() / 2) + $(window).scrollTop()) - ($(tar).height() / 2);
        var xpos = ($(window).width() / 2) - ($(tar).width() / 2);
        if (ypos < 10) { ypos = 20 };

            $("#enlarge_image").hide();
            //$("#enlarge_map *").show();
            $("#enlarge_map").show();

        if (w == "locus") {

        } else {
            //get the image to place here...
            //$("#enlarge_map *").hide();
            
            //$("#enlarge_map").hide();
            //$("#enlarge_image").show();
        }

        $(tar).hide();
        $(tar).css("top", ypos + "px");
        $(tar).css("left", xpos + "px");

        $("#wrapper").addClass("overlay");
        $(tar).fadeIn(300);
        //$(tar).show();
        //if (w != "locus") { $("#enlarge_map").hide(); }
        //$("#enlarge_map").hide();
        //$("#enlarge_image").show();

    });
    $("#help_header").live("click", function() {
        $("#regionHelp").hide();
        $("#mask").hide();
    });

    $(".help.region").live("click", function() {
    $("#regionHelp").show();
    var maskHeight = $(document).height();
    var maskWidth = $(window).width();
    $("#mask").css({ "width": maskWidth, "height": maskHeight });
    $("#mask").show(); help_header
    });
    
});

function refreshResults(r) {
    var ra = r.split("||");

    if (ra[0] == "OK") {
        $("#lit_searchResults").html(ra[1]);
        $("#pagination").html(ra[2]);
        $("#searchResults").show();
    }
}
function pageResults(r) {
    var ra = r.split("||");

    if (ra[0] == "OK") {
        $("#lit_searchResults").html(ra[1]);
        $("#pagination").html(ra[2]);
        $("#searchResults").show();
    }
}
function clearForm(form) {

    $(':input', form).each(function() {
        var type = this.type;
        var tag = this.tagName.toLowerCase();
        // normalize case       
        // it's ok to reset the value  attr of text inputs,      
        // password inputs, and  textareas       
        if (type == 'text' || type == 'password' || tag == 'textarea') this.value = "";
        // checkboxes and radios need  to have their checked state cleared       
        // but should *not* have  their 'value' changed       
        else if (type == 'checkbox' || type == 'radio')
            this.checked = false;
        // select elements need to  have their 'selectedIndex' property set to -1       
        // (this works for both  single and multiple select elements)       
        else if (tag == 'select')
            this.selectedIndex = -1;
            
    })
}