


// THIS FILE IS DEAD PLEASE MIGRATE AWAY FROM IT AND USE UPDATED VERSIONS FOUND IN js/slidecom/lib/








////////////////////////////////////////
// Mu is a container of general, reusable functions and objects requiring jquery

var Mu = {};
Mu.log = function(message){
	if (self.console) {
	    console.log(message);
	}
};
Mu.refresh = function(){
	window.location.reload(true);
}
Mu.relocate = function( destination, escape_iframe ){
	if( escape_iframe && parent ){ window.parent.location = destination; }
	else        { window.location        = destination; }
};
Mu.nocache_uri = function( uri ){
	return Mu.update_query_string(uri, {'nocache' : ((new Date()).getTime())});
}
Mu.update_query_string = function( uri, new_query_hash ){
	var new_query_string = "";
	var old_query_hash   = {};
	var split_uri = uri.split("?");
	var path      = split_uri[0]  ;
	if( split_uri.length === 2 ){
		old_query_hash = this.parse_query_string(split_uri[1]);
	}
	var combined_query_hash   = jQuery.extend(old_query_hash, new_query_hash);
	var combined_query_string = this.generate_query_string(combined_query_hash);
	if( combined_query_string.length ){
		combined_query_string = "?" + combined_query_string;
	}
	return path + combined_query_string;
};
// query string must be well formed (with no leading question mark and an equals sign for each variable)
Mu.parse_query_string = function( query_string ){
	var query_hash = {};
	var query_array = query_string.split("&");
	jQuery.each(query_array, function(each_var){
		var split_var    = each_var.split("=");
		var name         = split_var[0];
		var value        = split_var[1];
		query_hash[name] = unescape(value);
	});
	return query_hash;
};
// note: this will urlencode values
Mu.generate_query_string = function( vars_hash ){
	var vars_array = [];
	jQuery.each(vars_hash, function(which_var, each_var){
		vars_array.push(which_var + "=" + escape(each_var));
	});
	return vars_array.join("&");
};


Mu.datestring = function(time) {
	var day_threshold = 86400;
	var now = new Date();
	var nows = Date.parse(now.toString())/1000;
	var then = new Date();
	var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']
	var months = ['January','February','March','April','May','June','July','August','September','October','November','December']
	var datestr = "";
    then.setTime(time*1000);

	var hours = then.getHours();
	var suffix = (hours<12) ? 'am' : 'pm';
	if (hours == 0) {
	    hours = 12;
	} else if (hours>12){
	    hours = hours-12;
	}
	var minutes = then.getMinutes();
	if (minutes < 10) {
	   minutes = "0" + minutes;
	}
	var timestr = hours + ':' + minutes + suffix

	if ((nows-time) < day_threshold && now.getDate() == then.getDate()) {
	    datestr = 'at ' + timestr;
	} else if ((nows - time) < (day_threshold*3)) {
        datestr = 'on ' + days[then.getDay()] + ' at ' + timestr
	} else {
	    datestr = 'on ' + months[then.getMonth()] + ' ' + then.getDate() + ' ' + timestr
	}

	return datestr;
}

Mu.deep_transform = function( item_or_list, transformation_function ){
	if( Mu.type_of(item_or_list) === 'array' ){
		return jQuery.each( item_or_list, function( each_item_or_list ){
			return Mu.deep_transform(each_item_or_list);
		});
	}else{
		return transformation_function( item_or_list );
	}
};

Mu.type_of = function( value ){
	var type = typeof value;
	if( type === 'object' ){
		if( ! value ){
			return 'null';
		}else if( typeof value.length === 'number' && !(value.propertyIsEnumerable('length')) && typeof value.splice === 'function' ){
			return 'array';
		}
	}
	return type;
};
Mu.is_empty = function( container ){
	var which_element, element;
	if( Mu.type_of(container) === 'string' ){ return container.length === 0; }
	if( Mu.type_of(container) === 'array'  ){ return container.length === 0; }
	if( Mu.type_of(container) === 'object' ){
		for(which_element in container){
			element = container[which_element];
			if( element !== undefined && Mu.type_of(element) !== 'function' ){
				return false;
			}
		}
	}
	return true;
};
Mu.size_of = function( container ){
	var which_element, element, count=0;
	if( Mu.type_of(container) === 'string' ){ return container.length; }
	if( Mu.type_of(container) === 'array'  ){ return container.length; }
	if( Mu.type_of(container) === 'object' ){
		for(which_element in container){
			element = container[which_element];
			if( element !== undefined ){
				count++;
			}
		}
	}
	return count;
};



Mu.append_to_onbeforeunload = function( new_callback ){
	var existing_callback = window.onbeforeunload;
	if( Mu.type_of(existing_callback) === 'function' ){
		new_callback = function(){existing_callback(); new_callback();};
	};
	window.onbeforeunload = new_callback;
};

Mu.browser = Object();
Mu.browser.does_support_onbeforeunload = function(){return true;} //todo: fill out this stub function

Mu.text_field_hint = function(text_field, hint, hint_style_class){
	var text_field = $(text_field);
	var does_contain_hint         = false;
	var does_contain_user_content = function(){
		return( ! does_contain_hint && text_field.val() !== '' );
	};
	var show_hint = function(){
		text_field.addClass(hint_style_class);
		text_field.val(hint);
		does_contain_hint = true;
	}
	var hide_hint = function(){
		//if( does_contain_hint ){
		text_field.removeClass(hint_style_class);
		text_field.val('');
		does_contain_hint = false;
	};
	var update_field = function( under_scrutiny ){
		if( ! does_contain_user_content() ){
			if( under_scrutiny ){
				hide_hint();
			}else{
				show_hint();
			}
		}
	};
	text_field.focus(      function(){update_field(true );} );
	text_field.blur(       function(){update_field(false);} );
	if( Mu.browser.does_support_onbeforeunload() ){
		Mu.append_to_onbeforeunload( function(){update_field(true );} );
	}else{
		$(window).unload(      function(){update_field(true );} );
	}
	update_field(false);
	return update_field;
}



slide = Object();
slide.restfully = function( action, resource, parameters, callback ){
	jQuery.post('/'+resource, jQuery.extend(parameters, {'xaction' : action}), callback);
};



ieAttrBug = / data[a-zA-Z]+="null"/g
var xmlEscape = function(x) {
	x = x || '';
	return x.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/%/g, "&#37;");
};
var flashvarEscape = function(s) {
    s = s || '';
    s = s.replace(ieAttrBug, '');
	s = s.replace(/&/g, '%26');
	s = s.replace(/\+/g, '%2B');
	return s;
};


Mu.validate_email = function(email){
  var email_matcher = /^[-0-9a-z._+&]+@([-0-9a-z]+\.)+[a-z]{2,6}$/i;
  return email_matcher.test(email) && email.length <= 64;
};



