if (typeof EarthWidgets == 'undefined') {
    EarthWidgets = new Object();
}

if (typeof EarthWidgets.Search == 'undefined') {
    EarthWidgets.Search = {
        models: {},
        views: {},
        targets: {},

        install: function(target, id) {
            this.targets[id] = target;
            var model = this.models[id];
            var views = this.views[id];
            if (model.what != '' && model.where != '') {
                jQuery(target).html(views.results);
            } else {
                jQuery(target).html(views.form);
            }
        },

        reload: function(id) {
            var self = this;
            var target = this.targets[id];
            var model = this.models[id];
            var views = this.views[id];
            if (model.what != '' && model.where != '') {
                jQuery(target).html(views.results);
            } else {
                jQuery(target).html(views.form);
            }
        },

        setView: function(id, name) {
            var target = this.targets[id];
            var views = this.views[id];
            jQuery(target).html(views[name]);
        },

        doSearch: function(form) {
            jQuery('head').append('<scr' + 'ipt src="http://search.earth911.com/widget/?' + jQuery(form).serialize() + '&callback=EarthWidgets.Search.reload"></scr' + 'ipt>');
        },

        pendingGeocodeForm: null,

        doGeocode: function(form, where, mapsApiKey) {
            EarthWidgets.Search.pendingGeocodeForm = form;
            jQuery.ajax({
                type: 'GET',
                url: 'http://maps.google.com/maps/geo?output=json&q=' + encodeURIComponent(where) + '&key=' + mapsApiKey + '&callback=EarthWidgets.Search.onGeocode',
                dataType: 'script',
                cache: true
            });
        },

        onGeocode: function(result) {
            var form = EarthWidgets.Search.pendingGeocodeForm;
            EarthWidgets.Search.decodeGeocode(form, result);
            EarthWidgets.Search.doSearch(form);
        },

        decodeGeocode: function(form, result) {
            if (typeof result.Placemark != 'undefined'
                && typeof result.Placemark[0] != 'undefined') {
                var placemark = result.Placemark[0];
                var latitude = placemark.Point.coordinates[1];
                var longitude = placemark.Point.coordinates[0];
                if (typeof placemark.AddressDetails != 'undefined') {
                    var addressDetails = placemark.AddressDetails;
                    if (typeof addressDetails.Country != 'undefined'
                        && addressDetails.Accuracy > 2) {
                        var country = addressDetails.Country;
                        jQuery(form + " input[name=country]").val(country.CountryNameCode);
                        jQuery(form + " input[name=latitude]").val(latitude);
                        jQuery(form + " input[name=longitude]").val(longitude);
                        if (typeof country.AdministrativeArea != 'undefined') {
                            var administrativeArea = country.AdministrativeArea;
                            var province = administrativeArea.AdministrativeAreaName;
                            jQuery(form + " input[name=province]").val(province);
                            if (typeof administrativeArea.Locality != 'undefined') {
                                var locality = administrativeArea.Locality;
                                var city = locality.LocalityName;
                                jQuery(form + " input[name=city]").val(city);
                            } else if (typeof administrativeArea.SubAdministrativeArea != 'undefined') {
                                var subAdministrativeArea = administrativeArea.SubAdministrativeArea;
                                if (typeof subAdministrativeArea.Locality != 'undefined') {
                                    var locality = subAdministrativeArea.Locality;
                                    if (typeof locality.DependentLocality != 'undefined') {
                                        var dependentLocality = locality.DependentLocality;
                                        var city = dependentLocality.DependentLocalityName;
                                    } else {
                                        var city = locality.LocalityName;
                                    }
                                    jQuery(form + " input[name=city]").val(city);
                                }
                            }
                        }
                    }
                }
            }
        }
    };
}

EarthWidgets.Search.models["search1"] = {"what":"","where":"55807"};
EarthWidgets.Search.views["search1"] = {"form":"<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"margin: 0; border: 0; padding: 0; background: none;\"><tr><td style=\"margin: 0; border: 0; padding: 0; background: none;\">\n\n<div style=\"position: relative; zoom: 1;\">\n    <div style=\"position: absolute; \n                font-family: arial, helvetica, clean, sans-serif;\n                text-align: left;\n                left: 20px; \n                top: 3px;\">\n        <a style=\"margin: 0;\n                  text-decoration: none;\n                  text-align: left;\n                  border: 0;\n                  background: transparent;\"\n           href=\"http:\/\/earth911.com\/?utm_campaign=widget&amp;utm_medium=search_result&amp;utm_source=earth911.com\"\n           target=\"_blank\">\n            <img style=\"float: none; border: 0; margin: 0; padding: 0; background: transparent;\" src=\"http:\/\/search.earth911.com\/media\/images\/powered-by-e911.png\" \/>\n            <\/a>\n        <a style=\"margin: 0 0 0 30px;\n                  text-decoration: none;\n                  text-align: left;\n                  border: 0;\n                  background: transparent;\"\n           href=\"http:\/\/earth911.com\/widgets\/?utm_campaign=widget&amp;utm_medium=search_result&amp;utm_source=earth911.com\"\n           target=\"_blank\">\n            <img style=\"float: none; border: 0; margin: 0; padding: 0; background: transparent;\" src=\"http:\/\/search.earth911.com\/media\/images\/get-this-widget.png\" \/>\n            <\/a>\n    <\/div>\n<\/div>\n<div style=\"background: #aac77a;\n            width: 300px;\n            height: 250px;\">\n    <div style=\"background-color: #008000;\n                border-top: 20px solid #aac77a;\n                border-left: 10px solid #aac77a;\n                border-right: 10px solid #aac77a;\n                border-bottom: 10px solid #aac77a;\n                margin: 0;\n                padding: 10px;\n                font-family: arial, helvetica, clean, sans-serif;\n                text-align: left;\">\n        <div style=\"height: 200px; text-align: left;\">\n            <form id=\"earth-widgets-search-search1\" action=\"http:\/\/search.earth911.com\/\" style=\"display: inline;\">\n                <input type=\"hidden\" name=\"bgcolor\" value=\"#008000\" \/>\n                <input type=\"hidden\" name=\"bgimage\" value=\"http:\/\/search.earth911.com\/media\/images\/search-small-bg-short.png\" \/>\n                <input type=\"hidden\" name=\"bgstyle\" value=\"no-repeat scroll center top\" \/>\n                <input type=\"hidden\" name=\"bordercolor\" value=\"#aac77a\" \/>\n                <input type=\"hidden\" name=\"bordersize\" value=\"10\" \/>\n                <input type=\"hidden\" name=\"width\" value=\"300\" \/>\n                <input type=\"hidden\" name=\"height\" value=\"250\" \/>\n                <input type=\"hidden\" name=\"helpcolor\" value=\"#d7e4be\" \/>\n                <input type=\"hidden\" name=\"lblwhatcolor\" value=\"#ffffbb\" \/>\n                <input type=\"hidden\" name=\"lblwherecolor\" value=\"#ffffbb\" \/>\n                <input type=\"hidden\" name=\"lblwhat\" value=\"what?\" \/>\n                <input type=\"hidden\" name=\"lblwhere\" value=\"where?\" \/>\n                <input type=\"hidden\" name=\"submitimage\" value=\"http:\/\/search.earth911.com\/media\/images\/widget-submit.png\" \/>\n                <input type=\"hidden\" name=\"textcolor\" value=\"#ffffff\" \/>\n                <input type=\"hidden\" name=\"headingcolor\" value=\"#ebeae2\" \/>\n                <input type=\"hidden\" name=\"resultcolor\" value=\"#ffffff\" \/>\n                <input type=\"hidden\" name=\"whatexample\" value=\"aluminum cans, motor oil\" \/>\n                <input type=\"hidden\" name=\"whereexample\" value=\"zip or city, state\" \/>\n                <input type=\"hidden\" name=\"target\" value=\"_top\" \/>\n                <input type=\"hidden\" name=\"id\" value=\"search1\" \/>\n                <input type=\"hidden\" name=\"mapsapikey\" value=\"\" \/>\n\n                <input name=\"latitude\" value=\"\" type=\"hidden\" \/>\n                <input name=\"longitude\" value=\"\" type=\"hidden\" \/>\n                <input name=\"country\" value=\"\" type=\"hidden\" \/>\n                <input name=\"province\" value=\"\" type=\"hidden\" \/>\n                <input name=\"city\" value=\"\" type=\"hidden\" \/>\n\n                <h2 style=\"line-height: 14px; \n                           font-family: arial, helvetica, clean, sans-serif;\n                           color: #ebeae2; \n                           font-size: 18px;\n                           margin: 0;\n                           padding: 4px 0 4px 0;\">Find a recycling location<\/h2>\n                <div style=\"margin-top: 8px; text-align: left;\">\n                    <label style=\"color: #d7e4be; font-size: 11px; margin: 0;\">\n                        <input name=\"what\"\n                               value=\"\"\n                               type=\"text\"\n                               style=\"width: 250px;\n                                      border: 1px solid #000;\n                                      font-size: 13px;\n                                      font-weight: bold;\n                                      font-family: arial, helvetica, clean, sans-serif;\n                                      margin: 0;\n                                      padding: 4px 3px;\" \/>\n                        <em style=\"color: #ffffbb; margin: 0 0 0 3px\">what?<\/em>\n                        aluminum cans, motor oil\n                    <\/label>\n                <\/div>\n                <div style=\"margin-top: 8px; text-align: left;\">\n                    <label style=\"color: #d7e4be; font-size: 11px; margin: 0;\">\n                        <input name=\"where\"\n                               value=\"55807\"\n                               type=\"text\"\n                               style=\"width: 250px;\n                                      border: 1px solid #000;\n                                      font-size: 13px;\n                                      font-weight: bold;\n                                      font-family: arial, helvetica, clean, sans-serif;\n                                      margin: 0;\n                                      padding: 4px 3px;\" \/>\n                        <em style=\"color: #ffffbb; margin: 0 0 0 3px\">where?<\/em>\n                        zip or city, state\n                    <\/label>\n                <\/div>\n                <input type=\"image\"\n                       style=\"margin: 10px 0 0 0;\"\n                       src=\"http:\/\/search.earth911.com\/media\/images\/widget-submit.png\"\n                       value=\"Go\" \/>\n                <\/form>\n        <\/div>\n    <\/div>\n<\/div>\n<\/td><\/tr><\/table>\n\n<script type=\"text\/javascript\">\n\/\/<![CDATA[\n\n    jQuery('#earth-widgets-search-search1').submit(function() {\n        var usPostal = \/^\\s*\\d+\\s*$\/;\n        var canadaPostal = \/^\\s*\\w\\d\\w\\s*\\d\\w\\d\\s*$\/;\n        var where = jQuery('#earth-widgets-search-search1 input[name=where]').val();\n        if (usPostal.test(where) || canadaPostal.test(where) || true) {\n            EarthWidgets.Search.doSearch('#earth-widgets-search-search1');\n        } else {\n            EarthWidgets.Search.doGeocode('#earth-widgets-search-search1', where, '');\n        }\n        return false;\n    });\n\n\/\/]]>\n<\/script>\n","results":"<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"margin: 0; border: 0; padding: 0; background: none;\"><tr><td style=\"margin: 0; border: 0; padding: 0; background: none;\">\n\n<div style=\"position: relative; zoom: 1;\">\n    <div style=\"position: absolute; \n                font-family: arial, helvetica, clean, sans-serif;\n                text-align: left;\n                left: 20px; \n                top: 3px;\">\n        <a style=\"margin: 0;\n                  text-decoration: none;\n                  text-align: left;\n                  border: 0;\n                  background: transparent;\"\n           href=\"http:\/\/earth911.com\/?utm_campaign=widget&amp;utm_medium=search_result&amp;utm_source=earth911.com\"\n           target=\"_blank\">\n            <img style=\"float: none; border: 0; margin: 0; padding: 0; background: transparent;\" src=\"http:\/\/search.earth911.com\/media\/images\/powered-by-e911.png\" \/>\n            <\/a>\n        <a style=\"margin: 0 0 0 30px;\n                  text-decoration: none;\n                  text-align: left;\n                  border: 0;\n                  background: transparent;\"\n           href=\"http:\/\/earth911.com\/widgets\/?utm_campaign=widget&amp;utm_medium=search_result&amp;utm_source=earth911.com\"\n           target=\"_blank\">\n            <img style=\"float: none; border: 0; margin: 0; padding: 0; background: transparent;\" src=\"http:\/\/search.earth911.com\/media\/images\/get-this-widget.png\" \/>\n            <\/a>\n    <\/div>\n<\/div>\n<div style=\"background: #aac77a;\n            width: 300px;\n            height: 250px;\">\n    <div style=\"background-color: #008000;\n                border-top: 20px solid #aac77a;\n                border-left: 10px solid #aac77a;\n                border-right: 10px solid #aac77a;\n                border-bottom: 10px solid #aac77a;\n                margin: 0;\n                padding: 10px;\n                color: #ffffff;\n                font-size: 12px;\n                font-family: arial, helvetica, clean, sans-serif;\n                text-align: left;\">\n        <div style=\"height: 200px; text-align: left;\">\n            <div style=\"font-size: 14px;\n                        font-weight: bold;\n                        padding-bottom: 4px;\n                        color: #EBEAE2;\n                        border-bottom: 1px solid white;\n                        text-align: left;\">\n                <span style=\"color: #ebeae2\">Recycle<\/span>\n                <span style=\"color: #ffffbb\"><\/span>\n                <span style=\"color: #ebeae2\">near<\/span>\n                <span style=\"color: #ffffbb\">55807<\/span>\n            <\/div>\n\n            <ul style=\"list-style: none; margin: 10px 0 0 10px; padding: 0\">\n                <li style=\"list-style-type: none; list-style-image: none; padding-right: 10px\">\n                    We don't know where to recycle\n                    &ldquo;&rdquo; near\n                    &ldquo;55807&rdquo;.<br \/>\n                    <br \/>\n                    <a style=\"border: 0; color: #ffffff; background: transparent\"\n                       href=\"http:\/\/manage.earth911.com\/inquiry\/suggest-a-listing\/\"\n                       target=\"_top\">Help us improve these results.<\/a>\n                <\/li>\n                <\/ul>\n\n            <div style=\"margin-top: 10px; padding-top: 4px; border-top: 1px solid white\">\n                <div style=\"float: left\">\n                    <a style=\"border: 0; color: #ffffff; background: transparent; text-decoration: none; position: relative; z-index: 1;\"\n                       href=\"javascript:EarthWidgets.Search.setView(&quot;search1&quot;, 'form')\">\n                        &laquo; Change location<\/a>\n                <\/div>\n                <div style=\"float: right\">\n                    <a style=\"border: 0; color: #ffffff; background: transparent; text-decoration: none; position: relative; z-index: 1\"\n                       href=\"http:\/\/search.earth911.com\/?what=&amp;where=55807&amp;utm_campaign=widget&amp;utm_medium=search_result&amp;utm_source=earth911.com\"\n                       target=\"_top\">\n                        View all results &raquo;<\/a>\n                <\/div>\n                <br class=\"clear\" \/>\n            <\/div>\n        <\/div>\n    <\/div>\n<\/div>\n<\/td><\/tr><\/table>\n"};

jQuery.fn.earthSearchWidget = function(id) {
    EarthWidgets.Search.install(this, id);
};

