/* WKD JS
=========*/

Object.extend(Date.prototype, {
  /**
* @param format {String} The string used to format the date.
* Example: new Date().strftime("%A %I:%M %p")
*/
  strftime: function(format) {
    var day = this.getUTCDay(), month = this.getUTCMonth();
    var hours = this.getUTCHours(), minutes = this.getUTCMinutes();
    function pad(num) { return num.toPaddedString(2); };
 
    return format.gsub(/\%([aAbBcdDHiImMpSwyY])/, function(part) {
      switch(part[1]) {
        case 'a': return $w("Sun Mon Tue Wed Thu Fri Sat")[day]; break;
        case 'A': return $w("Sunday Monday Tuesday Wednesday Thursday Friday Saturday")[day]; break;
        case 'b': return $w("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec")[month]; break;
        case 'B': return $w("January February March April May June July August September October November December")[month]; break;
        case 'c': return this.toString(); break;
        case 'd': return this.getUTCDate(); break;
        case 'D': return pad(this.getUTCDate()); break;
        case 'H': return pad(hours); break;
        case 'i': return (hours === 12 || hours === 0) ? 12 : (hours + 12) % 12; break;
        case 'I': return pad((hours === 12 || hours === 0) ? 12 : (hours + 12) % 12); break;
        case 'm': return pad(month + 1); break;
        case 'M': return pad(minutes); break;
        case 'p': return hours > 11 ? 'PM' : 'AM'; break;
        case 'S': return pad(this.getUTCSeconds()); break;
        case 'w': return day; break;
        case 'y': return pad(this.getUTCFullYear() % 100); break;
        case 'Y': return this.getUTCFullYear().toString(); break;
      }
    }.bind(this));
  }
});

var App = window.App||{};

App.Fields = function(){
	var fields = $H({});
	
	function onfocus(){
		if(this.value == this.defaultValue){
		    this.value = '';
		}	
	}
	
	function onblur(){
		if(this.value == ''){
			this.value = this.defaultValue;
		}
	}	
	
	return {
		initialize : function(elements){
			$A(elements).each(function(element){
				element = $(element);
				if(element){
					element.observe('focus', onfocus);
					element.observe('blur', onblur);
				}
			});
		}
	}
}();

function when(obj, fn) {
  if (Object.isString(obj)) obj = /^[\w-]+$/.test(obj) ? $(obj) : $(document.body).down(obj)
  if (Object.isArray(obj) && !obj.length) return
  if (obj) fn(obj)
}

App.DropDown = function(obj){
	obj.observe('mouseover', function(e){
		this.addClassName('hover');
	});
	obj.observe('mouseout', function(e){
		this.removeClassName('hover');
	});	
};

App.MugshotsDropDown = function(o){
    var events = $H();
    var select = $('filterTownId');
    
    var _div = new Element('div', {className: "select"});
    var _select = new Element('select', {name: "data[filter][date]"});
    _div.appendChild(_select);
    select.up('div.select').insert({after: _div});
    
    $A(o).each(function(_o, i){
        events.set(_o[0], $A(_o[1]));
    });
    
    function change(){
        _select.update();
        events.get($F(select)).each(function(_o, i){
            var _d = _o.split('-');
            var date = new Date(parseInt(_d[0]), parseInt(_d[1])-1, parseInt(_d[2])).strftime('%d %B %Y');
            var option = new Element('option', {value: _o}).update(date);
            _select.appendChild(option);            
        });    
    }    
    
    select.observe('change', function(e){
        change();
    });
    change();
};


document.observe("dom:loaded", function(){
	$(document.body).observe('click', function(e){
		var link = Event.findElement(e, 'a');
		if(link){
			if(link.hasClassName('external')){
				Event.stop(e);
				var opener = window.open(link.href, 'external');
				
			}else if(link.hasClassName('popup')){
				Event.stop(e);
				var opener = window.open(link.href, 'popup', 'height=600,width=600,toolbar=0,location=0,directories=0,menubar=0,scrollbars=1');
			}
		}
	});
	
	App.Fields.initialize(['UserEmail', 'UserEmailNav']);
	
	for(flash in App.Flash){
		UFO.create(App.Flash[flash], App.Flash[flash].dom_id);
	}
	
	when(App.Mugshots, App.MugshotsDropDown);
	
	//when('nav-events', App.DropDown);
	//when('nav-downloads', App.DropDown);
	
	
});