/*
 * ASP - A jquery extension that creates event handlers for common asp events.
 *
 * Copyright (c) 2011 Matthew Drake (w) The Foundry Agency
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 *
 */



(function ($) {


    /*define bindable events*/
    //Page_ClientValidate
    //
    $.event.special.aspvalidate = {
        setup: function (data, namespaces, eventHandle) {

        },

        add: function (handleObj) {

        },
        teardown: function (obj) {

        },
        remove: function (obj) {

        }
    };

    $.event.special.afteraspvalidate = {
        setup: function (data, namespaces, eventHandle) {

        },

        add: function (handleObj) {

        },
        teardown: function (obj) {

        },
        remove: function (obj) {

        }
    };

    $.event.special.beforeaspvalidate = {
        setup: function (data, namespaces, eventHandle) {

        },

        add: function (handleObj) {

        },
        teardown: function (obj) {

        },
        remove: function (obj) {

        }
    };

    $.event.special.aspinvalid = {
        setup: function (data, namespaces, eventHandle) {

        },

        add: function (handleObj) {
            var old_handler;
            var new_handler;
            if (handleObj.handler)
                old_handler = handleObj.handler;
            else
                old_handler = handleObj;

            new_handler = function (event) {

                if (event.target == event.currentTarget || (event.currentTarget == document && event.target.nodeName.toLowerCase() == "html")) {

                    old_handler.apply(this, arguments);
                    event.stopPropagation();
                    return false;
                }
            };

            if (handleObj.handler) {
                handleObj.handler = new_handler;
            } else
                handleObj = new_handler;

            return handleObj;
        },
        teardown: function (obj) {

        },
        remove: function (obj) {

        }
    };

    $(document).ready(function () {

        if (window.Page_ClientValidate) {
            window._Page_ClientValidate = window.Page_ClientValidate;
            window.Page_ClientValidate = function () {
                $("*").trigger("beforeaspvalidate");
                //console.log(bf);
                var valid = window._Page_ClientValidate.apply(window, arguments);
                $("*").trigger("aspvalidate");
                if (!valid)
                    $("*").trigger("aspinvalid");
                return valid;
            }
        }
        if (!$.asp("valid")) {
            $("*").trigger("aspinvalid");
        }
    });

    $.expr[':'].aspvalid = function (obj, index, meta, stack) {
        for (i = 0; i < window.Page_Validators.length; i++) {
            val = window.Page_Validators[i];
            if (obj == document.getElementById(val.controltovalidate))
                return val.isvalid;
        }
        return false;
    };
    $.expr[':'].aspinvalid = function (obj, index, meta, stack) {
        for (i = 0; i < window.Page_Validators.length; i++) {
            val = window.Page_Validators[i];
            if (obj == document.getElementById(val.controltovalidate))
                return !val.isvalid;
        }
        return false;
    };

    /*
    * 
    * Define Local EnterFrame
    * 
    */

    var methods = {
        base: function (options) {


            return this;
        },
        cycle: function (options, interval, namespace) {

            return this;
        }
    };


    $.fn.extend(
{
    asp: function (method) {
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method || $.isFunction(method)) {
            return methods.base.apply(this, arguments);
        } else {
            //$.error( 'Method ' +  method + ' does not exist on enterframe' );
            console.log('Method ' + method + ' does not exist on jqasp');
        }
    }
});


    /*
    * 
    * Define Global EnterFrame
    * 
    */


    /* 
    *Set defaults 
    */
    var globalDefaults = {

        paused: false
    };

    /* 
    *Define Global Methods 
    */
    var globalMethods = {
        //recalibrates framerate and resets interval
        base: function (options) {



            return this;
        },
        start: function (options) {



            return this;
        },
        bind: function (data) {

        },
        valid: function (data) {
            return window.Page_IsValid;
        }

    };


    /* 
    *Extend Global Methods 
    */
    $.extend(
	{
	    asp: function (method) {
	        if (globalMethods[method]) {
	            return globalMethods[method].apply(this, Array.prototype.slice.call(arguments, 1));
	        } else if ($.isFunction(method)) {
	            return globalMethods.base.apply(this, arguments);
	        } else if (typeof method === 'object' || !method) {
	            return globalMethods.start.apply(this, arguments);
	        } else {
	            console.log('Method ' + method + ' does not exist on jqasp');
	        }
	    }
	});


})(jQuery);




