
/* 	Author: Catalyst Studios 
	url: http://catalyststudios.com 
*/


//
// --- place everything inside a contained function to keep scope issues out of play.
//

var itizen = function() {
    var pub = {};

    pub.inProduction = function() {
        return window.location.toString().toLowerCase().indexOf("itizen.com") > 0;
    };

    pub.resizeCols = function() {
        var $leftCol = $("#main .leftCol");
        var $rightCol = $("#main .rightCol");
        var $footCol = $("footer .leftCol");
        $leftCol.css("min-height", "530px");
        $rightCol.css("min-height", "530px");

        if ($leftCol.innerHeight() > $rightCol.innerHeight()) {
            $rightCol.css("min-height", $leftCol.height() + "px");
        } else {
            $leftCol.css("min-height", $rightCol.height() + "px");
        }

        var heightDiff = $(window).height() - ($("header").outerHeight(true) + $("#main").outerHeight(true));
        $footCol.css("min-height", heightDiff + "px");
    };

    pub.setupSignup = function() {
        $("#TheSignUpForm").submit(function() {
            $("#errors").fadeOut(function() {
                $("#errors").html("");
                if (!$("#checkbox-0").is(":checked")) {
                    $("#errors").append($("<li>").text("You must agree to the Terms of Service and Privacy Policy"));
                    $("#errors").fadeIn();
                }
                else {

                    $.post("proxy.ashx?url=/account/register", $("#TheSignUpForm").serialize(), function(result) {
                        if (result.Success) {
                            window.location = "SignupThanks";
                        }
                        else {
                            for (x in result.Errors) {
                                $("#errors").append($("<li>").text(result.Errors[x]));
                            }
                            $("#errors").fadeIn();
                        }
                    }, "json");

                }
            });
            return false;
        });
    };

    pub.setupHome = function() {
        $(".toggleLink").click(function(e) {
            e.preventDefault();
            $(".toggleWrap").fadeToggle("slow", "linear");
        });

        $("#submit").click(function(e) {
            var validEmail = /^\S+@\S+\.\S+$/;
            if (!validEmail.test($("#myEmail").val())) {
                e.preventDefault();
                alert("Please enter a valid email address.");
            }
        });


        $("#submitCode").click(function(e) {
            e.preventDefault();
            $.post("proxy.ashx?url=/Account/CheckCode", "Code=" + $("#myCode").val(), function(result) {
                if (result.Success) {
                    window.location = "/Signup?Code=" + $("#myCode").val();
                } else {
                    alert("The code you entered is not a valid invite code.");
                }
            }, "json");
        });
    };

    pub.setup = function() {
        //setTimeout(function() { $.fixPlaceHolder(); }, 0);
        $("a:external").addClass('externalLink').attr("target", "_blank"); // check if a link is external to this site and open it in a new window

        pub.resizeCols();

        $(window).resize(function() { pub.resizeCols(); });

    };
    return pub;
} ();

$(function() {
    itizen.setup();
});
