﻿// Application specific js

// call back to make a title into a valid permalink in real time.
$.fn.permalinkify = function(target) {
    return this.each(function() {
        $(this).bind("keyup", function() {
            $.ajax(
                {
                    type: "GET",
                    url: "/permalinkify",
                    dataType: "text",
                    data: { e: $(this).val() },
                    success: function(result) { $(target).val(result); }
                });
        });
    });
}

/// Show the login controls in-place
/// Todo - add a redirect if this fails ie an error: function
function ShowLogin(e) {
    e.preventDefault(); // Cancel the href
    $.ajax(
    {
        type: "GET",
        url: "/account/login",
        dataType: "html",
        success: function(result) { $("#login_placeholder").html(result); }
    });
}

// Create empty firebug fn placeholders if Firebug not active.
// This means we can safely call console.log etc
function detectFirebug() {
    
    if (window.console && window.console.firebug != '') {
        // firebug enabled
        // initial call to firebug required to get around a firebug bug..
        console.log("FireBug console found");
    }
    else{
        var names = ["log", "debug", "info", "warn", "error", "assert",
                     "dir", "dirxml", "group", "groupEnd", "time", "timeEnd", 
                     "count", "trace", "profile", "profileEnd"];
        window.console = {};
        for (i in names) {
            window.console[names[i]] = function() { };
        }
    }
}

function ClientExceptionLogger() {

    function Log(message, url, line) {
        try {
            var data = "jsmessage=" + encodeURIComponent(message);
            data += "&jsurl=" + encodeURIComponent(url);
            data += "&jsline=" + encodeURIComponent(line);
            data += "&pageurl=" + encodeURIComponent(location.href);
            data += "&useragent=" + encodeURIComponent(navigator.userAgent);

            $.post(
		          "/site/jserror",
		          {
		              jsmessage: encodeURIComponent(message),
		              jsurl: encodeURIComponent(url),
		              jsline: encodeURIComponent(line),
		              pageurl: encodeURIComponent(location.href),
		              useragent: encodeURIComponent(navigator.userAgent)
		          },
                  function(data) { }
            );

        }
        catch (e) { 
            // ignore logging errors
        }
    };

    this.Log = Log;

    return this;
};

/*
* Check all checkboxes contained within a form
*
* @name     checkCheckboxes
* @param    filter   only check checkboxes matching this expression
* @param    returnChecked   return checkboxes as jQuery object, default false
* @author   Sam Collett (http://www.texotela.co.uk)
* @example  $("#myform").checkCheckboxes();
* @example  $("#myform").checkCheckboxes(".onlyme");
* @example  $("#myform").checkCheckboxes(":not(.notme)");
* @example  $("#myform").checkCheckboxes("*", true);
*
*/

jQuery.fn.checkCheckboxes = function(filter, returnChecked) {
    filter = filter || "*";
    returnChecked = returnChecked || false;
    var returnWhat = jQuery([]);
    this.each(
		function() {
		    var checked = jQuery("input[type=checkbox]", this).filter(filter).each(
				function() {
				    this.checked = true;
				}
			).filter(":checked");
		    returnWhat = checked;
		}
	);
    if (!returnChecked) {
        returnWhat = this;
    }
    return returnWhat;
};

/*
* UnCheck all checkboxes contained within a form
*
* @name     unCheckCheckboxes
* @param    filter   only check checkboxes matching this expression
* @param    returnUnChecked   return unchecked checkboxes as jQuery object, default false
* @author   Sam Collett (http://www.texotela.co.uk)
* @example  $("#myform").unCheckCheckboxes();
* @example  $("#myform").unCheckCheckboxes(".onlyme");
* @example  $("#myform").unCheckCheckboxes(":not(.notme)");
* @example  $("#myform").unCheckCheckboxes("*", true);
*
*/
jQuery.fn.unCheckCheckboxes = function(filter, returnUnChecked) {
    filter = filter || "*";
    returnUnChecked = returnUnChecked || false;
    var returnWhat = jQuery([]);
    this.each(
		function() {
		    var unChecked = jQuery("input[type=checkbox]", this).filter(filter).each(
				function() {
				    this.checked = false;
				}
			).filter(":not(:checked)");
		    returnWhat = unChecked;
		}
	);
    if (!returnUnChecked) {
        returnWhat = this;
    }
    return returnWhat;
};


(function($) {

    /**
    * Initialise input hints on all matched inputs.
    *
    * Usage example:
    *   $('*[hint]').inputHint();
    *
    * Options keys:
    *   hintClass - CSS class to apply to inputs with active hints
    */
    $.fn.inputHint = function(options) {
        options = $.extend({ hintClass: 'hint' }, options || {});

        function showHint() {
            if ($(this).val() == '') {
                $(this).addClass(options.hintClass).val($(this).attr('hint'));
            }
        }

        function removeHint() {
            if ($(this).hasClass(options.hintClass)) $(this).removeClass(options.hintClass).val('');
        }

        this.focus(removeHint).blur(showHint).blur();

        var $form = this.parents('form:eq(0)');
        this.each(function() {
            var self = this;
            $form.submit(function() { removeHint.apply(self); });
        });
    };

})(jQuery);