// Utilizing MooTools Library http://www.mootools.net

// Custom event handler to prevent conflict between libraries using window.addEvent 
var AC = {
	addEvent: function (el, type, fn) {
		if (el.attachEvent) {
			el['e'+type+fn] = fn;
			el[type+fn] = function(){el['e'+type+fn](window.event);}
			el.attachEvent('on'+type,el[type+fn]);
		} else {
			el.addEventListener(type,fn,false);
		}
	},
	removeEvent: function (el, type, fn) {
		if (el.detachEvent) {
			el.detachEvent('on'+type,el[type+fn]);
			el[type+fn] = null;
		} else {
			el.removeEventListener(type,fn,false);
		}
	}
};


AC.addEvent(window, 'load', setup);

function setup(){

	new SelectBoxMenu({ menus: $$('.AC-Jump') });
	
	if($chk($('AC-PhotoList'))){ var PhotoThumb = new ACThumbnailer('AC-PhotoList','AC-PhotoThumbs','AC-PhotoImage','AC-NewsStory'); }
	
	if($chk($('AC-TodoList'))){ var TodoList = new ACTodoList(); }

	var accordion = new Accordion('div.toggleHead', 'div.toggleBody', {opacity: false,show: -1,alwaysHide: true}, $('AC-Accordion'));

	if($$('.AC-Glance').length > 0){var glance = new ACGlance('.AC-Glance',{imageLocation:'.AC-BlockImage',descriptors:'.AC-BlockText',JSONLocation:'/glance.js'});}

	if($chk($$('.AC-DYK2')[0])){ alert('yes3');var didYouKnow = new ACDidYouKnow('.AC-DYK2',{descriptors:'.AC-BlockText',JSONLocation:'/dykJSON.js'}); 	}

}


// Follows a link attached to the value of an option in a select box of class AC-Jump.  No other intervention is required.
var SelectBoxMenu = new Class({
	options: {
		menus: []
	}, 
	initialize: function(options){
		this.setOptions(options)
		this.menus = [];
		this.addMenus(this.options.menus);
	},
	addMenus: function(menus) {
		$$(menus).each(function(menu){
			this.menus.include($(menu));
			menu.addEvent('change', function(){
				location.href=menu.get('value');
			});
		}, this);
	} 
	
});

SelectBoxMenu.implement(new Options, new Events);


// TodoList Class
var ACTodoList = new Class({
	initialize: function(){
		this.header = $$('.AC-TodoHeader')[0];
		this.footer = $$('.AC-TodoFooter')[0];
		this.content = $$('.AC-TodoContent')[0];
		this.todoOpen = false;
		this.heightChange = new Fx.Tween(this.content,{property: 'height', duration:700}, Fx.Transitions.Sine.easeInOut);
		this.header.getElements('a')[0].removeProperty('href');
		this.header.addEvent('click', function() { this.todoToggle() }.bind(this) );
		this.footer.addEvent('click', function() { this.todoToggle() }.bind(this) );
	},
	todoToggle: function() {
		if(this.todoOpen == false) {
			this.heightChange.cancel();
			this.header.addClass('AC-TodoOpen');
			this.heightChange.start(this.content.getScrollSize().y);
			this.todoOpen = true;
		} else {
			var main = this;
			this.heightChange.cancel();
			this.heightChange.start(0).chain(function(){
				if(main.content.getStyle('height').toInt() == 0) {
					main.header.removeClass('AC-TodoOpen');
				}
			});
			this.todoOpen = false;
		}
	}		
});

// Special DID YOU KNOW rotator class, for use with all pages //
var ACDidYouKnow = new Class({
    initialize: function(element,options) {
        this.options = Object.extend({
            descriptors: '.AC-BlockText',
            JSONLocation: '/dykJSON.js'
        }, options || {});
   
        this.element = element;
		this.jsonObject;
        this.currentItem = 0;
        if(this.element.length > 0){
            this.viewerSetup();
        }       
       
    },          
    viewerSetup: function(){
        $$(this.element + ' ' + this.options.descriptors)[0].empty();   
         var jsonKnow = new Request.JSON({url: this.options.JSONLocation, onComplete: function(json){
			this.loadJSONObj(json.didyouknows);
		  }.bind(this)
	    }).get();
    },
    loadJSONObj: function(jsonObj){
		this.jsonObject = jsonObj;
        $$(this.element + ' ' + this.options.descriptors)[0].set('html',jsonObj[0].text.toString());
		pageRight = new Element('div',{'class':'AC-BlockCtrl'}).set('html','<span>Read Another Fact</span>').addEvent('click',this.loadNext.bind(this)).injectInside($$(this.element)[0]);
    },
    loadNext: function(){
        this.currentItem = (this.currentItem == this.jsonObject.length - 1)? 0 : this.currentItem + 1;
         $$(this.element + ' ' + this.options.descriptors)[0].set('html',this.jsonObject[this.currentItem].text.toString());

    }
});