﻿var Tooio4Pymes = function() {
    var that = {};
    var popups = {};
    var columns = {};
    var main = {};
    var checks = {};
    var page = {};

    var showModal = function showModal(elementId, style) {
        if (style === undefined) {
            $("#" + elementId).modal();
        }
        else {
            $("#" + elementId).modal(style);
        }
    };

    var equalHeight = function equalHeight(group) {
        tallest = 0;

        group.each(
            function() {
                thisHeight = $(this).height();

                if (thisHeight > tallest) {
                    tallest = thisHeight;
                }
            }
        );

        group.height(tallest);
    };

    var bottom = function bottom(parentId, elementId) {
        var parentHeight = $(parentId).height();
        var parentTop = $(parentId).position().top;
        var elementHeight = $(elementId).outerHeight(true);
        var elementTop = $(elementId).position().top;

        $(elementId).css('top', (parentHeight - elementHeight) - (elementTop - parentTop));
    };

    var setHeight = function setHeight(headerId, footerId) {
        var mainElement = $("#main");
        var headerElement = $("#" + headerId);
        var footerElement = $("#" + footerId + "> p");

        if (mainElement.attr("style") !== undefined) {
            mainElement.removeAttr("style");
        }

        var windowHeight = $(window).height();
        var mainHeight = mainElement.outerHeight(true);
        var headerHeight = headerElement.outerHeight(true);
        var footerHeight = footerElement.outerHeight(true);
        var mainPaddingTop = parseInt(mainElement.css("padding-top").replace('px', ''), 0);
        var mainPaddingBottom = parseInt(mainElement.css("padding-bottom").replace('px', ''), 0);

        var mainAvailableHeight = windowHeight - headerHeight - mainHeight + mainElement.innerHeight() - mainPaddingTop - mainPaddingBottom;

        if (mainHeight < mainAvailableHeight) {
            mainElement.css('height', mainAvailableHeight);
        }
    };

    var backgroundColor = function backgroundColor(color) {
        $("#main").css({ 'background-color': color });
    };

    var change = function change() {
        setHeight("header", "footer");
    };

    var toggle = function toggle(elementId) {
        /*
        if ($("#" + elementId).is(':checked')) {
        $("#" + elementId).attr('checked', false);
        }
        else {
        $("#" + elementId).attr('checked', true);
        }
        */

        $("#" + elementId).click();
    };

    var moveTop = function moveTop(elementId) {
        location.href = "#" + elementId;
    };

    var submitEnter = function(resultSet) {

        if (resultSet.length == 0)
            return false;
        var theForm = null;

        var findForm = function(resultSet) {
            if (resultSet.find("form").length > 0) {
                theForm = resultSet.find("form");
                return false;
            } else
                findForm(resultSet.parent());
        }

        findForm(resultSet);

        if (theForm.length > 0) {
            resultSet.bind("keyup", function(evt) {
                var testElement = evt.target;
                if (evt.keyCode == 13) {
                    if (evt.target.id == "password") {
                        if (theForm.attr("onsubmit")) {
                            theForm.bind("submit", function() {
                                eval(theForm.attr("onsubmit"));
                                return false;
                            });
                        }
                        theForm.trigger("submit");
                    }
                    if (evt.target.id == "username") {
                        $("input#password").focus();
                    }
                }
            });
        }
        return false;
    };

    popups.showModal = showModal;
    columns.equalHeight = equalHeight;
    columns.bottom = bottom;
    main.backgroundColor = backgroundColor;
    main.setHeight = setHeight;
    main.change = change;
    checks.toggle = toggle;
    page.moveTop = moveTop;

    that.Popups = popups;
    that.Columns = columns;
    that.Main = main;
    that.Checks = checks;
    that.Page = page;

    that.Main.SubmitEnter = submitEnter;

    that.addObject = function(name, obj) { that[name] = obj; };

    return that;
};

var tooio4Pymes = Tooio4Pymes();

$(document).ready(function() {
    tooio4Pymes.Main.setHeight("header", "footer");
    tooio4Pymes.Main.SubmitEnter($("input.submitEnter"));
});