$(document).ready(function() {
	$("div#masthead select").chosen().change(function() {
		var destination = $(this).attr("value");
		mpq.track(
			"Header nav", {
				from: window.location.hostname, 
				to: destination.replace(/http:\/\//, "")
			}, 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();
    }
    
    $("form.compare")
        .find("select")
        .change(function() {
            $(this).closest("form").find("button").trigger("click");
        });
        
    $("label.like, label.dislike")
        .addClass("js")
        .find("input")
        .each(function() {
            if ($(this).attr("checked") == "checked") {
                $(this).closest("li").find("label").addClass("checked");
            }
        })
        .change(function() {
            var val = $(this).val();
            $(this).closest("li").find("label").removeClass("checked");
            $(this).closest("li").find("label."+val).addClass("checked");
            $(this).closest("li").addClass("highlight")
        });
});

/**
 * 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'
	};
 
	// fix the button submission bug
	$('form').each(function () {
		var that = this,
			fixSubmit = false;
 
		// prepend button names
		$(this).find('button[name='+name+']').each(function () {
			this.name = '_'+name;
			fixSubmit = true;
 
			// intercept click handler
			$(this).click(function () {
				// set the hidden input to value specified in the map
				$('input#'+name).val(map[this.id]); 
				that.submit();
				return false;
			});
		});
 
		// add hidden field to the form
		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');
	}
});

