﻿/*onDomReady */
window.addEvent('load', function(){
    Debeers.init();
});
window.addEvent('onunload', function(){
	Debeers = null;
});


/*
* Description : Adds to DOM the jsEnabled class name to indicate javascript availability 
*/
$$('html')[0].addClass('jsAvailable');


/* Classes */
var Debeers = {
	init:function(){	
		Debeers.contentSlider.init();
		Debeers.Gallery.init();
		Debeers.Calendar.init();
		Debeers.Print.init();
	}
}

Debeers.contentSlider = {  

    init : function() {
                        
        if (!$('homepageFeature')) return;
        
        var _this = this;
        this.listItems = $$('#homepageFeature li');                
                
        this.listItems[0].getElement('div.featureContent').addEvent('click', function(e){
            var e = new Event(e);
            e.stopPropagation(); 
        });

        this.listItems.each(function(item){
            //setup transition effect
            //var fx = new Fx.Styles(item, {duration: 500, wait:false, 'unit': 'em'});
            
            item.addEvent('click', function(e){                
                
                var e = new Event(e);
		        e.stop();
		        
		        item.getElement('div.featureContent').addEvent('click', function(e){
                    var e = new Event(e);
                    e.stopPropagation(); 
                });
                
		        _this.resetList();
		        	        
		        /*apply transition effect
		        fx.set({
			        'width': 35			        
		        });*/
		        
		       
                _this.selectItem.call(item);
                return false;                    
            });  
        });
    },
    
    selectItem : function() {            
        this.addClass("selected");
    },
    
    resetList : function(){
        var activeItem = $$('#homepageFeature li.selected');
        
        if (activeItem.length) {                    
            activeItem[0].removeClass('selected');
            /*transition effect
            var activeItemFx = new Fx.Styles(activeItem[0], {duration: 250, wait:false, 'unit': 'em'});
            activeItemFx.start({
		        'width': [35,30.50]		        
            });*/
        }
    }
};    
   

Debeers.Gallery = {
	
	init:function(){
	    
		var hoverEls = $$("div.image");		
		hoverEls.each(function(item){			
			item.addEvent('mouseover', function(e){				
				item.getElement('div.overlay').removeClass('jshidden');				
			});
			
			item.addEvent('mouseout', function(e){				
				item.getElement('div.overlay').addClass('jshidden');
			});			
		});	
	}	
}

Debeers.Calendar = {
	
	calendarModuleClassName: "events_calendar",
	calendarPreviousLinkClassName: "previous_event",
	calendarNextLinkClassName: "next_event",
	
	init:function(){
		//check if calendar exists
		
		if($$('div.' + Debeers.Calendar.calendarModuleClassName).length > 0){
			Debeers.Calendar.addCalendarFunction();
		}
	},
	
	addCalendarFunction:function(){
	
		var eCalendarNodes = $$('div.' + Debeers.Calendar.calendarModuleClassName);
		eCalendarNodes.each(function(element, i) {
            var ePreviousLinks = element.getElements('a.' + Debeers.Calendar.calendarPreviousLinkClassName);

			ePreviousLinks.each(function(plElement, j) {
			    plElement.addEvent('click', function(e){ 
				    new Event(e).stop();
					e = e || window.event;
					target = e.target || e.srcElement;
					
					var eThisPreviousLi = target.parentNode;
					
					while(eThisPreviousLi.tagName != "LI"){
						eThisPreviousLi = eThisPreviousLi.parentNode;
					};
					
					$(eThisPreviousLi).setProperty('class','jshidden');
										
					var ePreviousLi = eThisPreviousLi.previousSibling;
					
					if(ePreviousLi && (ePreviousLi.nodeType !== 1) ) {					    					    
					    while(ePreviousLi && (ePreviousLi.nodeName !== "LI") ){
						    ePreviousLi = ePreviousLi.previousSibling;						    
					    };					    					    
					} 
                    $(ePreviousLi).setProperty('class','current');
					
					return false;
				});
			});
			var eNextLinks = element.getElements('a.' + Debeers.Calendar.calendarNextLinkClassName);
			eNextLinks.each(function(nlElement, k) {
			//for(var k=0; k < eNextLinks.length; k++){
				nlElement.addEvent('click', function(e){ 
				    new Event(e).stop();
					e = e || window.event;
					target = e.target || e.srcElement;
					
					var eThisNextLi = target.parentNode;
					while(eThisNextLi.tagName != "LI"){
						eThisNextLi = eThisNextLi.parentNode;
					};
					
					var eNextLi = eThisNextLi.nextSibling;
					while(eNextLi.nodeType != 1){
						eNextLi = eNextLi.nextSibling;
					};
										
					$(eThisNextLi).setProperty('class','jshidden');
					$(eNextLi).setProperty('class','current');
					
					return false;
				});
			});
		});
	}
}

Debeers.Util = {
	stripOutFlashVarsString:function(_sString){
		//string starts with the format "flashvars="
		//get strip this out
		var flashVars = _sString.substring(_sString.indexOf("="), _sString.length);
		
		return flashVars;
	}
}

Debeers.Print = {
    init: function() {    
        var list;
        if (list = $$('ul.article_buttons')[0]) {             
            if (list.nodeName.toLowerCase() === 'ul') {                
                var innerString = '<a href="#"><img alt="" class="icon" src="/Templates/Public/Images/sitelayout/icons/print.gif" />Print</a>';
                var item = new Element('li');
                item.setHTML(innerString);

                item.getElement('a').addEvent('click', Debeers.Print.printPage);
                list.appendChild(item);
             }
          }
   },
   printPage: function() {
      window.print();
      return false;
   }
}

function inputTextKeyPressed(e, buttonid){

    if(!e) e=window.event;
    key = (e.which)?e.which:window.event.keyCode; 
    if (key == 13){
        document.getElementById(buttonid).click();
        return false;
    }
}