jQuery.ajaxSetup({
//    beforeSend: function(xhr) {
//        xhr.setRequestHeader("Accept", "text/javascript")
//    },
//    dataType: 'json',
    error: function() {
        alert("A server error " +
              "has occurred and your action could not " +
              "be completed.");
    }
});




/* tabs */
(function($) {
    $(function() {
        $('.tab_panel').hide();
        $('.tab_panel:first').show();
        $('.tab:first').addClass('active')
        $('.subsection_tabs .tab').click(function() {
            $('.tab').removeClass('active');
            $(this).addClass('active');
            $('.tab_panel').hide();
            $($(this).attr('href')).show();
        })
    })
})(jQuery);





/******************************/
/* Generic Actions            */
/******************************/
jQuery(function() {
    $ = jQuery;

    $('.create_form').live('submit', function() {
        var action = $(this).attr('action') + '.json';
        $.post(action, $(this).serialize(), function(resp) {
            if (resp.success) {
                window.location = resp.redirect;
            } else {
                alert(resp.message);
            }
        });
        return false;
    });

    $('.update_form').live('submit', function() {
        var action = $(this).attr('action') + '.json';
        var post_data = $(this).serialize();
        post_data['_method'] = 'PUT';
        $.post(action, post_data, function(resp) {
            if (resp.success) {
                window.location.reload();
            } else {
                alert(resp.message);
            }
        });
        return false;
    });

    $("a.user_action_delete").click(function() {
        if (confirm("Are you sure you want to delete this item?")) {
            var domLink = this;
            $.post(this.href + '.json', {_method: 'delete'}, function(data) {
                if (data.success)
                    jQuery(domLink).closest('.content_box').slideUp();
                else
                    alert("Unable to delete the item. Do you have permission to do so?");
            });
        }
        return false;
    });

    $('.job .user_actions')
            .append("<div class='command hide'>Hide</div>")
            .click(hideJob);

    function hideJob() {
        if (confirm("Are you sure you want to hide this?")) {
            var $job = $(this).closest('.job');
            var href = $(this).parent().find('.command.delete a').attr('href');
            console.log(href);
            console.log($job);
            $.post(href + '/hide', function(resp) {
                if(resp.success)
                    $job.slideUp();
                else
                    alert("Unable to hide the item.  Do you have permission to do so?")
            });
        }
    }
});

var bpw = {};
bpw.jobs = {};
bpw.people = {};

bpw.includeJs = function(jsFile) {
    document.write('<script type="text/javascript" src="/javascripts/'
            + jsFile + '.js"></script>');
};

bpw.initializeMap = function() {
    if (google.maps.BrowserIsCompatible()) {
        this.map = new google.maps.Map2(document.getElementById("map"));
        this.geocoder = new google.maps.ClientGeocoder();
        this.bounds = new google.maps.LatLngBounds();
        this.map.addControl(new google.maps.SmallMapControl());
        this.map.setCenter(new google.maps.LatLng(0, 0), 0);
    }
};

bpw.setZoomAndCenter = function() {
    bpw.map.setZoom(bpw.map.getBoundsZoomLevel(bpw.bounds));
    bpw.map.setCenter(bpw.bounds.getCenter());
};

bpw.geocodeLocation = function(options) {
    var job = this;
    bpw.geocoder.getLocations(this.location, function(response) {
        if (!response || response.Status.code != 200) {
            // alert('City not found');
        } else {
            var place = response.Placemark[0];
            job.latlng = new google.maps.LatLng(place.Point.coordinates[1],
                    place.Point.coordinates[0]);
            if (options.addToMap) {
                job.addToMap();
            }
            bpw.bounds.extend(job.latlng);
        }
    });
};

bpw.addToMap = function() {
    if (!this.latlng) {
        //        this.geocodeLocation({ addToMap: true });
        return;
    }
    var map_item = this;
    this.marker = new google.maps.Marker(this.latlng);
    google.maps.Event.addListener(this.marker, 'click', function() {
        bpw.map.openInfoWindowHtml(map_item.latlng, map_item.description_html);
    });
    bpw.bounds.extend(this.latlng);
    bpw.map.addOverlay(this.marker);
    bpw.setZoomAndCenter();
};


bpw.Job = function(o) {
    this.location = o.location;
    this.description_html = o.description_html;
    this.latlng = o.latlng;
};

bpw.Job.prototype.geocodeLocation = bpw.geocodeLocation;
bpw.Job.prototype.addToMap = bpw.addToMap;

bpw.Person = function(o) {
    this.firstName = o.firstName;
    this.lastName = o.lastName;
    this.personTypes = o.personTypes;
    this.organizations = o.organizations;
    this.location = o.location;
    this.link = o.link;
    this.latlng = o.latlng;
    this.description_html = "<b><a href='" + this.link + "'>" + this.firstName + " "
            + this.lastName + "</a></b<br/>" + this.personTypes + "<br/>" + this.organizations
            + "<br/>" + this.location;
};

bpw.Person.prototype.geocodeLocation = bpw.geocodeLocation;
bpw.Person.prototype.addToMap = bpw.addToMap;

bpw.Organization = function(o) {
    this.name = o.name;
    this.location = o.location;
    this.numPeople = o.numPeople;
    this.link = o.link;
    this.latlng = o.latlng;
    this.description_html = "<b><a href='" + this.link + "'>" + this.name + "</a></b><br/>"
            + this.location + "<br/><b>" + this.numPeople + " members</b> are from this organization";
};

bpw.Organization.prototype.geocodeLocation = bpw.geocodeLocation;
bpw.Organization.prototype.addToMap = bpw.addToMap;

bpw.normalizeHeights = function(container) {
    if (typeof(container) == 'function')
        return;
    var max_height = 0;
    $(container).children().each(function() {
        if ($(this).height() > max_height)
            max_height = $(this).height();
    });
    $(container).find('.content_box').css('height', max_height + 'px');

};

