var query = location.search;
$(document).ready(function () {
  // Tabs
  if ($('#tabs-container').length) {
    $('#tabs-container').tabs();

    var toggle_tabs = {1: {'num': 2, 'text': 'Back to your review'}, 2: {'num': 1, 'text': 'Quick switch to application'}};
    $("#tabs-wrapper").append('<div id="quickswitch"><a href="#">Quick switch to application</a></div>');
    $("#quickswitch").click(function () {
      var next_tab = toggle_tabs[$("#tabs-container").activeTab()]['num'];
      var next_text = toggle_tabs[$("#tabs-container").activeTab()]['text'];
      $("#quickswitch a").html(next_text);
      $("#tabs-container").triggerTab(next_tab);
      return false;
    });
  }

  // Overall Score
  var $overall_score = $("span.overall_score");
  $("select.score").change(function () {
    $overall_score.attr("class", "overall_score");
    var sum = 0
    $("select.score").each(function () {
      var val = +$(this).val();
      sum = sum + val;
    });
    $overall_score.html(sum);
  });
	query = query.split("/",1); //?q=lang
	if ($("#edit-round-member").length > 0) {
		$("#edit-round-member").change(function () {
			$.post(Drupal.settings.jstools.basePath +"?q=/round/"+ $(this).val() +"/windows/options", function(data) {
	 			$("#edit-thematic-windows-member-select").html(data);
				$("#edit-thematic-windows-member-select option").each(function() {
	    		if ($("#edit-thematic-windows-member-"+ $(this).val()).length > 0) {
						$(this).remove();
					}
				});
			});
		});
	
		$.post(Drupal.settings.jstools.basePath +"?q=/round/"+ $("#edit-round-member").val() +"/windows/options", function(data) {
	 			$("#edit-thematic-windows-member-select").html(data);
			$("#edit-thematic-windows-member-select option").each(function() {
	    		if ($("#edit-thematic-windows-member-"+ $(this).val()).length > 0) {
					$(this).remove();
				}
			});
		});
		$("#edit-thematic-windows-member-add").bind("click", function() {
			$("span + input[@id^='edit-thematic-windows-member-delete']:last").prev().prepend("<span style='color:red'>"+ $("#edit-round-member option:selected").text() +": </span>");
		});
	}

	if ($("#edit-round-convener").length > 0) {
		$("#edit-round-convener").change(function () {
			$.post(Drupal.settings.jstools.basePath + query +"/round/"+ $(this).val() +"/windows/options", function(data) {
	 			$("#edit-thematic-windows-convener-select").html(data);
				$("#edit-thematic-windows-convener-select option").each(function() {
	    		if ($("#edit-thematic-windows-convener-"+ $(this).val()).length > 0) {
						$(this).remove();
					}
				});
			});
		});
	
		$.post(Drupal.settings.jstools.basePath + query +"/round/"+ $("#edit-round-member").val() +"/windows/options", function(data) {
	 		$("#edit-thematic-windows-convener-select").html(data);
			$("#edit-thematic-windows-convener-select option").each(function() {
	    		if ($("#edit-thematic-windows-convener"+ $(this).val()).length > 0) {
					$(this).remove();
				}
			});
		});
		$("#edit-thematic-windows-convener-add").bind("click", function() {

			$("span + input[@id^='edit-thematic-windows-convener-delete']:last").prev().prepend("<span style='color:red'>"+ $("#edit-round-convener option:selected").text() +": </span>");
		});
	}




});
