/*
	__foundation/_js/jquery/jquery.common.js
	purpose: common jquery functions
*/






// - allows binding before existing stack of events
// - we do this by binding the event as we normally would, then moving the last event of that type to the beginning of the stack
jQuery.fn.preBind = function(namespace, fn) {
	
	var type = namespace.split('.')[0];
	this.on(namespace, fn);
	
	
	
	// find the length of the object, as well as the last key
	var last_index = -1
	var last_key;
	for(last_key in this.data('events')[type]) {
		last_index++;	
	}
	
	
	
	
	var new_bindings = [];
	var i = 0;
	var k, k2;
	for(k in this.data('events')[type]) {
		k2 = parseInt(k);
		if(isNaN(k2)) {
			// -- add this key / value pair
			new_bindings[k] = this.data('events')[type][k];	
		} else {
			if(k2 === 0) {
				// -- push the last object first
				new_bindings.push(this.data('events')[type][last_key]);
			}
			
			if(i == (last_index)) {
				break;
			}
			new_bindings.push(this.data('events')[type][k]);			
		}	
		i++;	
	}
	
	//O_O.Bro.da(new_bindings);
	
	this.data('events')[type] = new_bindings;
	return this;
};





// - fixes issue of IE's onChange event waiting for radios to blur
jQuery(function() {
	if(jQuery.browser.msie) {
		jQuery('input:radio').click(function() {
			this.blur();
			this.focus();
		});
	}
});
