// 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(){

	if($chk($('AC-PhotoList'))){ var PhotoThumb = new ACThumbnailer('AC-PhotoList','AC-PhotoThumbs','AC-PhotoImage','AC-NewsStory'); }
	
	if($chk($$('.AC-TodoHeader')[0])){ var TodoList = new ACTodoList(); }

	var accordion = new Accordion('div.toggleHead', 'div.toggleBody', {opacity: false,show: -1,alwaysHide: true}, $('AC-Accordion'));


	if($chk($$('.AC-Jump')[0])){
				var test = new SelectBoxMenu(); 
	}

	if($chk($('AC-ProgramGrid'))){ 
		var Sorttable =  new SortingTable( 'AC-ProgramGrid',{zebra:false,dont_sort_class: 'nosort', forward_sort_class: 'forward_sort', reverse_sort_class: 'reverse_sort'});
		$$('#AC-ProgramGrid TBODY TR').addEvent('mouseover',function(e){this.toggleClass('highlight');});
		$$('#AC-ProgramGrid TBODY TR').addEvent('mouseout',function(e){this.toggleClass('highlight');});
		
		$('programDD').addEvent('change',function(e){
		    if($chk($('nothingHere'))){$('nothingHere').destroy();}			
			$$('#AC-ProgramGrid TBODY TR').each(function(row){
				if($('programDD').get('value') == 'All'){
					row.removeClass('hiddenRow');
				}else{
				
					if(row.get('programType').indexOf($('programDD').get('value')) == -1){
						row.addClass('hiddenRow');
					}else{
						row.removeClass('hiddenRow');
					}
				}
			});		
			if($$('#AC-ProgramGrid TBODY TR.hiddenRow').length == $$('#AC-ProgramGrid TBODY TR').length){				
				nothingHereTR = new Element('tr',{'class':'nothingHere','id':'nothingHere'}).inject($$('#AC-ProgramGrid TBODY')[0]);
				nothingHereTD = new Element('td',{'html':'<p>There are no areas of study that are in this academic division</p>','colspan':'3'}).inject(nothingHereTR);				
			}
		});	
	}

	if($chk($('AC-DirectoryGrid'))){ 
		var Sorttable= new SortingTable('AC-DirectoryGrid', { zebra: false });
	}


}

//Selectbox Menu Class
var SelectBoxMenu = new Class({
                options: {  }, 
                initialize: function(options){
                                $$('.AC-Jump').each(function(el){
                                                el.selectedIndex = 0;
                        el.addEvent('change', function(e){
		//                                                     location.href = el.get('value');
                                                     window.open(el.get('value'));

                                        });                           
        }, this);
                }
                
});


// Utilizing MooTools for the Admissons Checklist Animation

//window.addEvent('load', function(){

// 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;
		}
	}		
});

window.onload = function(){

 var accordion = new Accordion('div.toggleHead', 'div.toggleBody', {opacity: false, show: -1, alwaysHide: true, start: 'all-closed', duration: 500, onActive: function(tog){tog.addClass('selected');}, onBackground: function(tog){tog.removeClass('selected');}}, $('AC-Accordion'));




    var Alert = {
        initialize:function(){        
            if ($$('#gridCt .AC-Alert').length > 0){
                $$('#gridCt .AC-Alert')[0].innerHTML = '<div id="AC-AlertClose">Close [X]</div>' + $$('#gridCt .AC-Alert')[0].innerHTML;
                $('AC-AlertClose').addEvent('click',this.fadeAlert);
            }


           if (Cookie.read('Alert')) {
                var arrAlerts = Cookie.read('Alert').split('|');
                for(i=0;i<arrAlerts.length;i++){
                  if ($$('#gridCt .AC-Alert').length > 0){
                    $$('#gridCt .AC-Alert')[0].style.display = 'none';
                  } 
                }
            }
        },
        fadeAlert:function(){
            var fadeEffects = new Fx.Tween($$('#gridCt .AC-Alert')[0], {duration: 1450, transition: Fx.Transitions.Sine.easeInOut});
            fadeEffects.start({'opacity': [0.0],'margin-bottom': [0],'height': [0],'padding-top': [0],'padding-bottom': [0]}).chain(function(){$$('#gridCt .AC-Alert')[0].style.display = 'none';});
            Alert.setCookie();     
        },
        setCookie:function(){
            var today = new Date();
            var todayEnd = 24 - today.getHours();
            var foundAlerts
            if (Cookie.read('Alert') != false){
                foundAlerts = Cookie.read('Alert') + '|' + $$('#gridCt .AC-Alert')[0].id;
                Cookie.remove('Alert');
                Cookie.set('Alert',foundAlerts,{duration:(todayEnd/24)})            
            }else{
                Cookie.set('Alert',Cookie.remove('Alert'),{duration:(todayEnd/24)})
            }        
        }
    };
    Alert.initialize();
};