/*
In house helper plugins
*/
jQuery.fn.extend({
    loadPage: function(id, url) {
        //aplopay.tracking.track.page(url);
        $('#'+id).load(url);
    },
    
    liveLoadPage: function(elem, id_override) {
        var url = $(elem).attr("href");
        var id = id_override || $(elem).attr("rel");
        $().loadPage(id, url);
    }
    
});

(function() {
aplopay =
{
    exists: true,                       // Allows future modules to validate that the core system is loaded
    has_console: true,                  // Does current browser env support console logging?
    
    debug:  function(str, pass_fail) {
        if($o.show_debug) {
            if($o.has_console) {
                if(pass_fail === false) {
                    console.warn("FAILED: " + str);
                } else {
                    console.log(str);
                }
            }
        }
    },
    
    exists: function(obj) {
        var does_exist = (eval('typeof '+obj) != "undefined");
        $a.debug(obj + " exists", does_exist);
        return does_exist;
    },
    
    // function standard_error_handler
	// Runs the standard error handler which merely logs errors to the console. Typically called via catch{} blocks.
	// PARAMS
	//     error: [required] The thrown Error() object or a string describing the error
	standard_error_handler: function(error) {
		// If a string was provided, simply log the error string
		if (typeof(error) == "string" || typeof(error) == "STRING") {
			var error_msg = 'error: '+error;
		}
		// If an Error() was thrown, log its name and message.
		// If the browser provides it, also log the file name and line number of the error.
		else {
			var error_msg = "error ("+error.name;
			if (error.lineNumber) {
				error_msg += ", line "+error.lineNumber+", "+error.fileName;
			}
			error_msg += "): "+error.message;
		}
		$a.debug(error_msg);
	},
    
    init: function() {
        //Ensures that a "console.log" func exists, to avoid breakage during debuging
        try { console.log('...'); } catch(e) { console = { log: function() {} }; $a.has_console = false; }
		
        /*
            JQuery live functions
        */
        //ensure all live load pages operate properly
        $('.live') 
    	    .live('click', function(e) {
    	        e.preventDefault();
    	        $().liveLoadPage(this);
    			return false;
    	    });

        // submit containing form
        $('.submitForm')
            .live('click', function(e) {
                e.preventDefault();
                console.log(this);
                console.log($(this).closest('form'))
                $(this).closest('form').submit();
        	});
            
        // ajax form submission
        $('.ajaxForm')
            .bind('submit', function() {
                $(this).ajaxSubmit({
                    target: '#'+$(this).attr('rel')
                });
                return false;
            });
        

    }
}

$a = aplopay = window.aplopay;
$(document).ready(function(){aplopay.init()});
})();



