$(function() {
    var eraseState, mouseDownState, curColor;
    if ($(".color.selected").length > 0) {
        curColor = $(".color.selected").attr("data-color");
    }
    $("#clear").click(function() {
        rows = $("#gridSize").val().split(",")[0];
        cols = $("#gridSize").val().split(",")[1];
        buildGrid(rows, cols);
    });
    $("#drawing-table tbody").delegate("td", "mousedown", function() {
        mouseDownState = true;
        $el = $(this);
		console.log($(this));
        if (eraseState) {
            $el.removeAttr("style");
        } else {
            $el.css("background", curColor);
        }
    }).delegate("td", "mouseenter", function() {
        if (mouseDownState) {
            $el = $(this);
            if (eraseState) {
                $el.removeAttr("style");
            } else {
                $el.css("background", curColor);
            }
        }
    });
    $("html").bind("mouseup", function() {
        mouseDownState = false;
    });
    $(document).keydown(function(event) {
        if (event.keyCode == 18) {
            eraseState = true;
            $(".selected").addClass("previous");
            $(".color").removeClass("selected");
            $(".eraser").addClass("selected");
        }
    }).keyup(function(event) {
        if (event.keyCode == 18) {
            eraseState = false;
            $(".color").removeClass("selected");
            $(".previous").addClass("selected").removeClass("previous");
            $("." + curColor).addClass("selected");
        }
    });
    $("#color-selector").delegate(".color", "click", function() {
        $el = $(this);
        var pulledVal = $el.attr("data-color");
        if (pulledVal == 'eraser') {
            eraseState = true;
        } else {
            eraseState = false;
            curColor = pulledVal;
        }
        $(".color").removeClass("selected");
        $(this).addClass("selected");
    });
    $("#tracing-image-form").submit(function() {
        var url = $("#fileLocation").val();
        $("<div />", {
            id: "tracing-image"
        }).appendTo("#table-wrap");
        $("#drawing-table").css("opacity", 0.5);
        $("#toggle-tracing-mode").show();
        $("#tracing-image-form").remove();
        tracingMode = true;
        return false;
    });
    $("#toggle-tracing-mode").click(function() {
        if (tracingMode) {
            $("#tracing-image").css("visibility", "hidden");
            $(this).html("Toggle Tracing Mode On");
            $("#drawing-table").css("opacity", 1);
            tracingMode = false;
        } else {
            $("#tracing-image").css("visibility", "visible");
            $(this).html("Toggle Tracing Mode Off");
            $("#drawing-table").css("opacity", 0.5);
            tracingMode = true;
        }
    });
    $("#get-html-button").click(function() {
        $("#the-html").val("<table style='width: 100%; border-collapse: collapse;'>" + $("#drawing-table").html() + "</table>");
    });

    $("#btnSubmit").click(function(){
        $(".reside").css("background", ""); //passing value as null.
        $(".residex").css("background", ""); //passing value as null.
    });
	
	
	function resetSelectForm() {
	  document.getElementById("selectForm").reset();
	  jQuery('.reside').removeClass('a1').removeClass('c1').removeClass('k1').removeClass('e1');
	}

	if($("#colormenu").length>0){ 
		$("#colormenu").css('width', $("#colormenu").width()+10).css('height', '30px');
		var mcolpos = $("#colormenu").position().top; var mcolposlef = $("#colormenu").position().left;
		$(window).load(function() {
			if ($(this).scrollTop() >= mcolpos) {
				$("#colormenu").addClass('fixed').css({'left':mcolposlef});
			}else{
				$("#colormenu").removeClass('fixed').css({'left':'unset'});
			}
		});
		$(window).scroll(function() {
			if ($(this).scrollTop() >= mcolpos) {
				$("#colormenu").addClass('fixed').css({'left':mcolposlef});
			}else{
				$("#colormenu").removeClass('fixed').css({'left':'unset'});
			}
		});
	}
	$('#ekorc').keyup(function () {
		$('.reside:nth-last-child(5n+2)').each(function () {
			if ($.trim($('#ekorc').val()).length) {
				var r = $(this).text();
				var t = $("#ekorc").val(); 	
				if (r == t ) {
					$(this).addClass("e1");
				}
			}				
		});
	});
	$('#kepalac').keyup(function () {
		$('.reside:nth-last-child(5n+3)').each(function () {
			if ($.trim($('#kepalac').val()).length) {
				var r = $(this).text();
				var t = $("#kepalac").val(); 	
				if (r == t ) {
					$(this).addClass("k1");
				}
			}

		});
	});
	$('#kopc').keyup(function () {
		$('.reside:nth-last-child(5n+4)').each(function () {
			if ($.trim($('#kopc').val()).length) {
				var r = $(this).text();
				var t = $("#kopc").val(); 	
				if (r == t ) {
					$(this).addClass("c1");
				}
			}
		});
	});
	$('#asc').keyup(function () {
		$('.reside:nth-last-child(5n+5)').each(function () {
			if ($.trim($('#asc').val()).length) {
				var r = $(this).text();
				var t = $("#asc").val(); 	
				if (r == t ) {
					$(this).addClass("a1");
				}
			}				
		});
	});
});