(function($){
  
  $.fn.addAnchor = function(title) {
    title = title || "Link here";
    return this.filter("*[id]").each(function() {
      $("<a class='anchor'> \u00B6</a>").attr("href", "#" + this.id)
        .attr("title", title).appendTo(this);
    });
  }
  
  $.fn.checked = function(checked) {
    if (checked == undefined) { // getter
      if (!this.length) return false;
      return this.get(0).checked;
    } else { // setter
      return this.each(function() {
        this.checked = checked;
      });
    }
  }
  
  $.fn.enable = function(enabled) {
    if (enabled == undefined) enabled = true;
    return this.each(function() {
      this.disabled = !enabled;
      var label = $(this).parents("label");
      if (!label.length && this.id) {
        label = $("label[for='" + this.id + "']");
      }
      if (!enabled) {
        label.addClass("disabled");
      } else {
        label.removeClass("disabled");
      }
    });
  }
  
  $.loadStyleSheet = function(href, type) {
    type = type || "text/css";
    $(document).ready(function() {
      if (document.createStyleSheet) { // MSIE
        document.createStyleSheet(href);
      } else {
        $("<link rel='stylesheet' type='" + type + "' href='" + href + "' />")
          .appendTo("head");
      }
    });
  }
  
  // Used for dynamically updating the height of a textarea
  window.resizeTextArea = function (id, rows) {
    var textarea = $("#" + id).get(0);
    if (!textarea || textarea.rows == undefined) return;
    textarea.rows = rows;
  }
  
  // The following are defined for backwards compatibility with releases prior
  // to Trac 0.11
  
  window.addEvent = function(elem, type, func) {
    $(elem).bind(type, func);
  }
  window.addHeadingLinks = function(container, title) {
    $.each(["h1", "h2", "h3", "h4", "h5", "h6"], function() {
      $(this, container).addAnchor(title);
    });
  }
  window.enableControl = function(id, enabled) {
    $("#" + id).enable(enabled);
  }
  window.getAncestorByTagName = function(elem, tagName) {
    return $(elem).parents(tagName).get(0);
  }

})(jQuery);

function commentCheck(){
	var comment_field = document.getElementById('comment').value;
	var checked_radio = 0;

	if(document.getElementById('action_analysis_ready') != null){
		if(document.getElementById('action_analysis_ready').checked){
			checked_radio = 1;
		}
	}
	if(document.getElementById('action_testing_states') != null){
		if(document.getElementById('action_testing_states').checked){
			checked_radio = 1;
		}
	}
	if(document.getElementById('action_development_ready_for_test') != null){
		if(document.getElementById('action_development_ready_for_test').checked){
			checked_radio = 1;
		}
	}
	if(checked_radio) {
		if(comment_field.length > 0){
			return true;
		} else {
			document.getElementById("comment").focus();
			alert('Please write a statement about your activity into comment field!');
			return false;
		}
	} else {
		return true;
	}
}

var tool_div = "<div id=\"backlog_tools\" style=\"padding: 15px; z-index: 500px; border:1px solid #D5D5D5; box-shadow: 5px 5px 9px #CACACA; background-color: #F3F3F3; border-radius: 10px; position: absolute; left: 50%; top: 50%; margin-left: -150px; margin-top: -100px; width: 300px; height: 200px;\"><center>Tools for backlog:</center><br><center>Ticket id: <input type=\"text\" id=\"ticket_id_text\"></input></center></input><br><input type=\"radio\" name=\"set1\" value=\"1\" checked> Top of the list<br><input type=\"radio\" name=\"set1\" value=\"2\"> Bottom of the list<br><input type=\"radio\" name=\"set1\" value=\"3\"> Line: <input type=\"text\" id=\"line_to_input\"></input><br><br><center><input style=\"width:100px;\" type=\"button\" id=\"execute_something\" value=\"ok\"></center></div>";
var tool_div_state;
var sortable_ids = new Array();

$(document).ready(function() {	
	if($("#backlog").length){
		console.log('Backlog opened!');
		$('body').append(tool_div);
		$("#backlog_tools").hide();
		$("#backlog_tools").draggable({
			scroll: false
		});
		$("#line_to_input").attr('disabled', 'disabled');
		controller = new BacklogController();
	}
	$(document).keypress(function(event) {
		$.each($("dt"), function(index, value) {
			sortable_ids[index] = $(this).attr("id");
		});
		if (event.which == '42' && checkIfBacklogLoaded()) {
			if(!(tool_div_state)){
				$("#backlog_tools").show('slow');
				tool_div_state = 1;
			} else {
				$("#backlog_tools").hide('slow');
				tool_div_state = 0;
			}
		}
		if (event.which == '0' && checkIfBacklogLoaded()) {
			if(tool_div_state){
				$("#backlog_tools").hide('slow');
				tool_div_state = 0;
			}
		}
	});
	$("#execute_something").click(function () {
		var ticket_id = $("#ticket_id_text").val().replace("#", "");
		if($("#ticketID-"+ticket_id).length){
			var radio_val = $('input[name=set1]:checked').val();
			if(radio_val == 1){
				$("#ticketID-"+ticket_id).parent().prependTo(".backlog");
				controller.positionsDidChange();
			} else if(radio_val == 2){
				$("#ticketID-"+ticket_id).parent().insertBefore("#ticketID--2");
				controller.positionsDidChange();
			}  else if(radio_val == 3){
				var text_input = $("#line_to_input").val();
				var sortable_count = sortable_ids.length;
				if(!(text_input) || text_input == 0){
					$("#line_to_input").focus();
					alert("Line cannot be empty or 0!");
				} else if(text_input > sortable_count){
					$("#line_to_input").focus();
					alert("Line value cannot be more than "+sortable_count+"!");
				} else {
					$("#ticketID-"+ticket_id).parent().insertBefore("#"+sortable_ids[parseInt(text_input)-1]);
					controller.positionsDidChange();
				}
			}
		} else {
			alert("Ticket id #"+ticket_id+" is not available in this backlog list!");
		}
	});
	$('input[name=set1]').change(function(){
		var radio_val = $('input[name=set1]:checked').val();
		if(radio_val == 3){
			$("#line_to_input").attr('disabled', '');
		} else {
			$("#line_to_input").attr('disabled', 'disabled');
		}
	});
	$("#backlog").click(function () {
		$("#backlog_tools").hide('slow');
		tool_div_state = 0;
	});
	$("ul").click(function () {
		$("#backlog_tools").hide('slow');
		tool_div_state = 0;
	});
});

function checkIfBacklogLoaded(){
	
	if($(".ui-sortable").length){
		return true;
	} else {
		return false;
	}
}

