$(document).ready(function() {
    $("div.participate.loading").removeClass("loading");
    $("div#masthead select").chosen().change(function() {
        var destination = $(this).attr("value");
        var data = {
            from: window.location.hostname, 
            to: destination.replace(/http:\/\//, "")
        };
        mpq.track("Header nav", data, function() { window.location = destination; });
    });
    $("body").on("click", "a.item, a.statement", function(ev) {
        ev.preventDefault();
        var el = ev.target;
        var destination = $(el).attr("href");
        var data = {
            slug: $(el).data("slug"),
            kind: $(el).attr("class"),
            category: $(el).data("category"),
            source: $(el).data("source")
        };
        mpq.track("Assertion clicked", data, function() { window.location = destination; });
    });
    $("body").on("click", "div.participate a", function(ev) {
        ev.preventDefault();
        var el = ev.target;
        var destination = $(el).attr("href");
        var data = {
            category: $(el).data("category")
        };
        mpq.track("Participate", data, function() { window.location = destination; });
    });

    var languages = $("div#question ol");

    if (languages.length > 0){
      var updateValues = function(arg){
          $("input#ranking").val(languages.sortable('toArray').join(", "))
      };
      
      $("div#question ol li, div#question ol li *").disableSelection();
      languages.sortable({
          update: updateValues
      });
      updateValues();
    }
    
    $(".share .shortlink").click(function() {
        document.execCommand('selectAll',false,null)
    });

    $("form.compare")
        .find("select")
        .change(function() {
            $(this).closest("form").find("button").trigger("click");
        });
});

/**
 * fix the HTML BUTTON submission bug.
 * 
 * Problem: IE6/7 ignore the value attribute of button elements and
 * instead submit the innerHTML of the button. Even using the JavaScript
 * getAttribute('value') method will return the incorrent value in IE6.
 * 
 * To fix this issue, add the BUTTON.id property to the below map, with
 * the desired value propery.
 * 
 * The script will automatically execute on DOM READY and search for any
 * BUTTON elements within a web form. If found, it will:
 * 
 * 1. Rename the name property so 'foo' becomes '_foo' (this workarounds
 *    the IE6 issue of submitting all of the BUTTON elements
 * 2. Add a click listener to inctercept the click.
 * 3. Look up the value of the element ID in the map object
 * 4. Set the dynamically generated hidden field VALUE to the desired
 *    value
 * 5. Submit the form.
 * 
 * @param {String} name - The name property of the buttons to process
 */
 
function fixButtonSubmit(name) {
    var map = {
        buttonSubmit: 'next',
        buttonBack: 'back'
    };
    $('form').each(function () {
        var that = this,
            fixSubmit = false;
        $(this).find('button[name='+name+']').each(function () {
            this.name = '_'+name;
            fixSubmit = true;
            $(this).click(function () {
                $('input#'+name).val(map[this.id]); 
                that.submit();
                return false;
            });
        });
        if (fixSubmit) {
            $(this).append($('<input id="'+name+'" type="hidden" name="'+name+'" value="" />'));
        }
    });
}
 
$(function () {
    // return if not IE
    if (!$.browser.msie) {
        return;
    }
    // set browser version
    var isIE6 = false,
        isIE7 = false,
        isIE8 = false;
 
    switch ($.browser.version) {
    case '6.0':
        isIE6 = true;
        break;
    case '7.0':
        isIE7 = true;
        break;
    case '8.0':
        isIE8 = true;
        break;
    }
    if (isIE6 || isIE7) {
        fixButtonSubmit('action');
    }
});

