munky = {
	
	list : null,
	filter : null,
	order : 9999,
	
	init : function(){
		/*----- Mimic standard lightbox behaviour -----*/
		$('a[rel=lightbox]').lightBox();
		
		/*----- Start menu hovering -----*/
		
		$('#links span').mouseover(function(){
												$(this).children('div').show();
												$(this).children('>a').addClass('hovering');
											});
											
		$('#links span').mouseout(function(){
												$(this).children('div').hide();
												$(this).children('>a').removeClass('hovering');
											});
											
		var email = $('#user input').eq(0);
		var emailDefault = email.attr('value');
		email.focus(function(){
									if($(this).attr('value')==emailDefault){
										$(this).attr('value', '');
									}
								});
								
		email.blur(function(){
									if($(this).attr('value')==''){
										$(this).attr('value', emailDefault);
									}
								});
											
		/*----- End menu hovering -----*/
		
		/*----- Give lists the ability to be sorted -----*/
		
		munky.initList();
	},
	
	calendarElem : new Array(),	

	calendar : function(elem){
		munky.calendarElem.push($(elem));
		$(elem).append(' <a class="calendar" href="#" rel="'+(munky.calendarElem.length-1)+'"><img src="http://munky.com.au/images/icons/calendar.png" /></a>');
		
		$(elem).children('a.calendar').click(function(){
																	$(this).blur();
																	var day, month, year;
																	var low = 10000;
																	var high = 0;
																	$(elem).children('select').each(function(i){
																		if(i>=3) return; //allow extra select boxes to be added after the date (see /admin/events/new)
																		var name = $(this).attr('name').match(/^.*?\[(d|M|Y)\]$/);
																		switch(name[1]){
																			case "Y":
																				$(this).children('option').each(function(){
																					var val = $(this).attr('value');
																					if(val.match(/^\d+$/)){
																						if(val<low) low = val;
																						if(val>high) high = val;
																					}
																				});
																				year = $(this).val();
																				break;
																			case "M":
																				month = $(this).val();
																				break;
																			case "d":
																				day = $(this).val();
																				break;
																		}
																	});
																	window.open("http://munky.com.au/calendar/?elem="+($(this).attr('rel'))+"&day="+day+"&month="+month+"&year="+year+"&low="+low+"&high="+high, "calendar", 'height=300,width=300,left=100,top=100,directories=0,fullscreen=0,location=0,menubar=0,resizable=0,scrollbars=0,status=0,titlebar=0,toolbar=0');
																	return false;
																});
	},
	
	returnDate : function(day, month, year, elemIndex){
		var elem = munky.calendarElem[elemIndex];
		var setTo = new Date();
		setTo.setFullYear(year, month-1, day);
		munky.setDate(elem, setTo);
	},
	
	setDate : function(elem, date){
		$(elem).children('select').each(function(i){
			if(i>=3) return; //allow extra select boxes to be added after the date (see /admin/events/new)
			var type = $(this).attr('name').match(/^.*?\[(d|M|Y)\]$/)[1];
			var val;
			switch(type){
				case "d":
					val = date.getDate();
					break;
				case "M":
					val = date.getMonth()+1;
					break;
				case "Y":
					val = date.getFullYear();
					break;
			}
			$(this).val(val);
		});
	},
	
	getDate : function(elem){
		var selected = new Date();
		var day;
		var month;
		var year;
		elem.children('select').each(function(i){
			if(i>=3) return; //allow extra select boxes to be added after the date (see /admin/events/new)
			var type = $(this).attr('name').match(/^.*?\[(d|M|Y)\]$/)[1];
			switch(type){
				case "d":
					day = $(this).val();
					break;
				case "M":
					month = $(this).val()-1;
					break;
				case "Y":
					year = $(this).val();
					break;
			}
		});
		
		selected.setFullYear(year, month, day);
		return selected;
	},
	
	initList : function(){
		if($('div.list').length==0){
			return;
		}	
	
		$('#listFilter').show();		
		
		var filter = $('#listFilter input').eq(0);
		munky.filter = filter;
		var list = $('div.list');	
		munky.list = list;	
		
		filter.keyup(function(){
			munky.filterList();
		});
		
		/*----- Make titles sortable -----*/
		
		var titles = $('div.list.title');
		
		titles.children('>div').each(function(i){
			$(this).html('<a href="#" rel="'+i+'">'+$(this).html()+"</a>");
		});
		
		munky.idList();		
		
		titles.children('div').children('a').click(function(){
			$(this).blur();
			munky.sortList($(this).attr('rel'));
			return false;
		});
	},
	
	/*----- Add a rel ID to each of the list items so it can be used for sorting-----*/
	idList : function(){
		munky.list.each(function(i){
			$(this).attr('rel', i);
		});
	},
	
	filterList : function(){
		var list = munky.list;
		var alt = true;
		var val = $(munky.filter).val();

		var regEx = new RegExp(val, "i");
		$('div.list').each(function(){
			if($(this).hasClass("title")){
				return;
			}
			
			if(regEx.test($(this).html().replace(/\<(.*?)\>/g, ""))){
				$(this).show();
				if(alt){
					$(this).addClass("alt");
				}
				else {
					$(this).removeClass("alt");
				}
				alt = !alt;
			}
			else {
				$(this).hide();
			}
		});
	},
	
	sortList : function(by){

		var order = Math.abs(by)+1;
	
		if(Math.abs(munky.order)==order){
			var reverse = munky.order==order;
		}
		munky.order = order * (reverse ? -1 : 1);
	
		var values = new Array();
		
		munky.list.each(function(){
			if($(this).hasClass("title")){
				$(this).children('div').children('a').each(function(i){
					//$(this).html($(this).html().replace(/(\^|v)/, '')+(i==by ? (reverse ? '^' : 'v') : ''));
				});
				return;
			}
			values.push($(this).children('>div').eq(by).html().replace(/\<(.*?)\>/g, "")+'_'+$(this).attr('rel'));
		});
		
		values.sort(munky.sortBy);
		if(!reverse){ //set as !reverse instead of just reverse because the insertAfter method essentially reverses elements
			values.reverse();
		}	
		
		var current = $('div.list.title').eq(0);		
		
		for(var key in values){
			var val = values[key];
			var rel = val.match(/_(\d+)$/)[1];
			var move = $('div.list[rel='+rel+']').eq(0);
			move.insertAfter(current);
		}
		
		munky.filterList();
	},
	
	sortBy : function(a, b){
		a = a.match(/^(.*?)_\d+$/)[1];
		b = b.match(/^(.*?)_\d+$/)[1];	
	
		var aNum = munky.isNumForSort(a);
		var bNum = munky.isNumForSort(b);
		
		if(aNum===false && bNum===false){
			return a<b;
		}
		else if(aNum!==false && bNum!==false){
			return aNum<bNum;
		}
		else { //one must be a number so return numberes first
			return aNum!==false;
		}
	},
	
	isNumForSort : function(a){
		var m;
		if(m=a.match(/^\$?(\d+)$/)){
			return m[1] | 0; //bitwise casts as float
		}
		else if(m=a.match(/^(\d+):(\d{2})$/)){ //time
			return (m[1] | 0) + (m[2] | 0)/60;
		}
		else {
			return false;
		}
	},
	
	noPx : function(val){
		var num = val.match(/^(-?\d+)px$/);
		if(num){
			return num[1] | 0; //bitwise or will cast as an integer
		}
		else {
			return val;
		}
	}

}

$(function(){ munky.init(); });