﻿/* Rhino Input-Hint jQuery Plugin*/
/* Author: Corey Mitchell */
(function ($) {
    $.fn.hint = function (options) {
        var opts = $.extend({}, $.fn.hint.defaults, options);

        return this.each(function () {
            var el = $(this);
            el.val(el.attr(opts.attributeName))
            .focus(function () {
                var word = $(this).attr(opts.attributeName);
                if (this.value == word) {
                    this.value = '';
                    $(this).addClass(opts.focusClass);
                }
            }).blur(function () {
                var word = $(this).attr(opts.attributeName);
                if (this.value == '') {
                    this.value = word;
                    $(this).removeClass(opts.focusClass);
                }
            });
        });
    };
    $.fn.hint.defaults = {
        attributeName: "title",
        focusClass: "hint-active"
    };
})(jQuery);
/* Rhino ConstantView jQuery Plugin*/
/* Author: Corey Mitchell */
(function ($) {
    $.fn.constantview = function (options) {
        var settings = {
            "fixedclass": "fixed",
            "bottomelement": "",
            "bottommargin": 0,
            "topmargin": 0
        };

        if (options) {
            $.extend(settings, options);
        }

        var docheight = $(document).height();
        var minFromBottom = settings.bottommargin - settings.topmargin;

        var relocate = function (scrollTop, top, element) {
            if (scrollTop >= top) {
                var pointOfNoReturn = (docheight - minFromBottom) - element.height();
                if (scrollTop >= pointOfNoReturn) {
                    element.css({ "top": pointOfNoReturn - top, "position": "absolute" });
                    element.removeClass(settings.fixedclass);
                }
                else {
                    element.css({ "top": settings.topmargin, "position": "fixed" });
                    element.addClass(settings.fixedclass);
                }
            } else {
                element.css({ "top": "", "position": "absolute" });
                element.removeClass(settings.fixedclass);
            }
        };

        return this.each(function () {

            var element = $(this);

            var top = element.offset().top - parseFloat(element.css("marginTop").replace(/auto/, 0));
            top = top - settings.topmargin;

            element.css({ "position": "absolute" }).parent().css({ "position": "relative" });

            if (settings.bottomelement != "") {
                var stoppingelement = $(settings.bottomelement);
                if (stoppingelement.length > 0)
                    minFromBottom += stoppingelement.height();
            }
            $(window).scroll(function () {
                var arg = this;
                var y = $(arg).scrollTop();
                //in case elements on page change height, lets re-calc doc height
                docheight = $(document).height();

                //console.log("viewport top:" + y + ", element top:" + top);
                relocate(y, top, element);
            });
        });

    };
})(jQuery);

var Rhino = function() { };
Rhino.prototype.Comment = function(selector) {
    var root = this;
    var formPanel = $(selector + " .commentform div.form");
    var progressPanel = $(selector + " .commentform div.progress");
    var commentList = $(selector + " .commentlist");
    var button = $(selector + " .commentform .form input[type='button']");
    var form = $(selector + " .commentform div.form form");

    this.reset = function(e) {
        progressPanel.slideUp();
        formPanel.slideDown();
    };
    this.isValidForm = function() {
        var errors = [];
        form.find(".required", this).each(function() {
            if (this.value.length == 0)
                errors.push("<li>A " + $(this).attr("title") + " is required.");
        });
        if ($("#recaptcha_response_field").val().length == 0)
            errors.push("<li>A Validation Code is required.");

        if (errors.length == 0)
            return true;
        else {
            progressPanel.slideDown("fast", function() {
                progressPanel.html("<ul class=\"errors\">" + errors.join("</li>") + "</li></ul>");
            });
            return false;
        }
    };
    this.postForm = function() {
        formPanel.slideUp("normal", function() {
            progressPanel.html("<h3>Your comment is being processed...</h3>").slideDown("fast", function() {
                //Post comment
                $.post(form.attr("action"), root.serializeForm(form), function(data) {
                    //Display result
                    if (data) {
                        if (data.substring(0, 5) == "error") {
                            progressPanel.html("<h3>" + data + "</h3>");
                        }
                        else {
                            if (data.substring(0, 8) == "moderate") {
                                progressPanel.html(data.substring(8));
                            }
                            else {
                                if (commentList.find(".comment").length == 0)
                                    commentList.html("");
                                commentList.append(data);
                                progressPanel.html("<h3>Your comment has been submitted. Thank you!</h3>");
                            }
                        }
                    } else {
                        progressPanel.html("<h3>An Error occurred</h3>");
                    }

                });

            });
        });
    };
    this.serializeForm = function(form) {
        var inputs = [];
        form.find(":input", this).each(function() {
            inputs.push(this.name + '=' + escape(this.value));
        });
        try {
            inputs.push("challenge" + "=" + Recaptcha.get_challenge());
            inputs.push("response" + "=" + Recaptcha.get_response());
        }
        catch (ex) { }
        return inputs.join("&");
    };
    this.remove = function(element) {
        var anchor = $(element);
        var id = anchor.attr("rel");
        $.post("/comments/post", "action=remove&commentid=" + id, function(data) {
            anchor.parents("div.comment").remove();
        });
    };
    this.approve = function(element) {
        var anchor = $(element);
        var id = anchor.attr("rel");
        var hash = anchor.attr("title");
        $.post("/comments/post", "action=moderate&commentid=" + id + "&hash=" + hash, function(data) {
            anchor.parents("div.comment").replaceWith(data);
        });
    };
    this.init = function() {
        button.click(function(e) {
            e.preventDefault();
            if (root.isValidForm())
                root.postForm();
        });
        commentList.find("a.remove").click(function(e) {
            e.preventDefault();
            root.remove(this);
        });
        commentList.find("a.moderate").click(function(e) {
            e.preventDefault();
            root.approve(this);
        });
    };
};
var Utils = {};
Utils.loadExternalFile = function (filename, filetype) {
    var fileref = null;
    if (filetype == "js") {
        fileref = document.createElement("script");
        fileref.setAttribute("type", "text/javascript");
        fileref.setAttribute("src", filename);
    }
    else if (filetype == "css") {
        fileref = document.createElement("link");
        fileref.setAttribute("rel", "stylesheet");
        fileref.setAttribute("type", "text/css");
        fileref.setAttribute("href", filename);
    }
    if (fileref != null)
        document.getElementsByTagName("head")[0].appendChild(fileref);
};
$(function () {
    $("input[type='text'].hint,input[type='search'].hint").hint();
});
var rhino = new Rhino();
