var submitForm = function(formName) {
    var $form = $("#" + formName);
    $form.submit();
}

var common = new function() {
    var self = this;

    $(function() {
        self.contextPath = $("#contextPath").val() || "";
    });
};

var openPopup = function(url, windowName, width, height) {
    var left = Math.floor((screen.availWidth - width) * 0.5),
    top = Math.floor((screen.availHeight - height) * 0.5),

    left = Math.max(left, 0),
    top = Math.max(top, 0),

    attrs = "resizable=yes,scrollbars=yes,status=no,menubar=no,location=no,directories=no,toolbar=no,personalbar=no,dependent=yes";

    return window.open(url, windowName, "width=" + width + ",height=" + height + ",top=" + top + ",left=" + left + "," + attrs);
};

var trimText = function() {

}

var initEventList = function() {
	window.deny = function(index) {
		$("div[id^=denyContainer_]").hide();
		$("#denyContainer_" + index).show();
	};
	
	window.addEvent = function(id) {
		$.post(common.contextPath + "/event/card/do", {
            action: "add",
            eventId: id
        });
		
		$("#addEvent_" + id).hide();
		$("#removeEvent_" + id).show();
	};
    
	window.removeEvent = function(id) {
		$.post(common.contextPath + "/event/card/do", {
            action: "remove",
            eventId: id
        });
		
		$("#removeEvent_" + id).hide();
		$("#addEvent_" + id).show();
	};
	
    $(function() {
        $("#resultSize").change(function() {
        	$(this).parents("form").submit();
        });
        
        $("#nod").html($("#numberOfDisplayed").val());
        $("#noa").html($("#numberOfAllEvents").val());
    });
}

var initEventRegister = function() {
    var setMaxLenght = function(selector){
        var $field = $("#" + selector);
        var $max = $("#" + selector + "_max");
        var $current = $("#" + selector + "_current");

        var maxLength = $field.attr("maxlength");
        $max.text(maxLength);

        $current.text(maxLength - $field.val().length);

        //        $field.change(function() {
        //            if (+$field.val().length <= +maxLength) {
        //                $field.val($field.val().substr(0, maxLength));
        //            }
        //        });
        //
        //        $field.keypress(function(e) {
        //            var keyCode = e.which;
        //            var allowedCodes = new Array(8, 37, 38, 39, 40, 46);
        //
        //            var allowedCode;
        //            for (allowedCode in allowedCodes) {
        //                if (+keyCode == +allowedCode) { return true; }
        //            }
        //
        //            if (+$field.val().length <= +maxLength) {return true; }
        //
        //            return false;
        //        });

        $field.keyup(function() {
            //$field.val($field.val().substr(0, maxLength));

            $current.text(maxLength - $field.val().length);
        });
    }

    var fieldToLowerCase = function(fieldId) {
        var $field = $("#" + fieldId);
        $field.val($field.val().toLowerCase());
    }

    var initLimits = function() {
        setMaxLenght("otherPlace");
        setMaxLenght("headline");
        setMaxLenght("resume");
        setMaxLenght("infoPage");
        setMaxLenght("participants");
    }

    $(function() {
        $("#eventPlace").change(function() {
            var $otherPlace = $("#otherPlaceConteiner");

            if ( $("#eventPlace :selected").text().toLowerCase().indexOf("annan") >= 0 ) {
                $otherPlace.show();
            } else {
                $otherPlace.hide();
            }
        });

        $("#eventPlace, #secondEventPlace").trigger('change');

        initLimits();

        $("#actFormSubmit").click(function() {
            initLimits();

            $("#actForm").submit();
        });

        $("#headlineToLowerCase").click(function() {
            fieldToLowerCase("headline");
        });

        $("#resumeToLowerCase").click(function() {
            fieldToLowerCase("resume");
        });
    });
}

var initDenyContainer = function() {
    var enterDenyMessage = function() {
        $("#denyContainer" ).show();
    }

    $(function() {
        $("#deny").click(function(){
            enterDenyMessage();
        });
    });
}

var initCompareEvent = function() {

    $(function() {
        initDenyContainer();
    });
}

var initFormEngine = function() {
    $(function() {
        $("#formEngineForm").append("<input type=\"hidden\" value=\"" +$("#metaId").val() + "\" name=\"pageMetaId\"/>");
        $("#formEngineForm").append("<input type=\"hidden\" value=\"" +$("#surveyPageId").val() + "\" name=\"surveyPageId\"/>");
        $("#formEngineForm").append("<input type=\"hidden\" value=\"" +$("#journalistPageId").val() + "\" name=\"journalistPageId\"/>");

        if ($("#metaId").val() == $("#surveyPageId").val()) {
            $("#formEngineText > div > a").each(function() {
                if ($(this).attr('href') == "javascript:history.back();") {
                     $(this).attr('href', "javascript:window.location=\"" + common.contextPath +
                        "/event/user\"");
                }
            });
        }
    });
}