﻿var global = {
    cur_section : '',
    filtersArray : [],
    cur_page : '',
    news_filter  : false,
    
    setTopNav : function(){
        
        if(this.cur_page){
           $$('.cur_section').each(function(f){
		        if($(f).getParent().id == global.cur_page + "_link"){
		        
		            //Kill Tool Tip
		            $(f).getParent().getFirst().removeProperty('class');
		            $(f).getParent().getFirst().removeProperty('title');
		            $(f).getParent().getFirst().removeProperty('href');
		        
		            $(f).addClass('cur_section_selected');
		            
		            var curDescription  = $(f).getParent().getFirst().getFirst().alt.split(':');
		            $('section_title').setHTML(curDescription[0]);
		            $('section_discription').setHTML(curDescription[1]);
		        }
		        /*Remove un-wanted alt tags from DOM*/
		        $(f).getParent().getFirst().getFirst().setAttribute('alt','');
		    });
        }
        
        /* Set up Tool Tips IN Header */
        this.makeTips({x:-225,y:-110},'.header_link','header-tool');
        
     },
    
    scrollBar : function(argScroll){
    
        $(argScroll).scroll = new Fx.Scroll($(argScroll+'_container'));
	    $(argScroll).scroll_container = $(argScroll+'_scrollarea');
	    $(argScroll).scroll_contents = $(argScroll+'_container');
	    $(argScroll).scroll_height = $(argScroll).scroll_container.getSize().size.y
	    $(argScroll).scroll_thumb = $(argScroll+'_scrollthumb');

	    $(argScroll).scroll_thumb_height = $(argScroll).scroll_thumb.getSize().size.y
	    $(argScroll).scroll_thumb.makeDraggable(
		    {
			    limit: {x: [0, 0], y: [0, $(argScroll).scroll_height - $(argScroll).scroll_thumb.getSize().size.y]},
			    onDrag:function(){
				    $(argScroll).percent_scrolled = ($(argScroll).scroll_thumb.getTop() - $(argScroll).scroll_container.getTop()) / ($(argScroll).scroll_height - $(argScroll).scroll_thumb_height);
				    $(argScroll).scroll_contents.setStyle('top', Math.round(0 - $(argScroll).scroll_height - (($(argScroll).scroll_contents.getSize().size.y - $(argScroll).scroll_height) * $(argScroll).percent_scrolled)) +'px');
			    }			
		    }
	    );
	    $(argScroll).scroll_thumb.setStyle('top',0);
	    $(argScroll).scroll_thumb.setStyle('left',0);
    	
	    // After we build it turn off scroller if we dont need it.
        this.checkScrollHeight(argScroll);
    
    },
    
    checkScrollHeight : function(argScroll){
      
        if($(argScroll).scroll_contents.getSize().size.y > $(argScroll).scroll_height){
			
            $(argScroll + '_scrollarea').setStyle("visibility","visible");
            $(argScroll + '_scrollthumb').setStyle("visibility","visible");
        }else{
			
            $(argScroll + '_scrollarea').setStyle("visibility","hidden");
            $(argScroll + '_scrollthumb').setStyle("visibility","hidden");
        }
    },
    
    setFilter : function(argFilter) {
		
	    var optionFilter = argFilter;
	    if(optionFilter != '' && optionFilter != 'undefined' && optionFilter != null){
	        if(optionFilter == "all" || optionFilter == "news"){
	            this.clearFilter();
	        }else{
	            this.toggleFilter(optionFilter);
	        }
	     }
	},
	
    toggleFilter : function(argFilter) {
    
	    var isFilterActive = false;
	    var filterIndex = "";
	   
	    if(this.filtersArray.length != 0){
	        for(i=0; this.filtersArray.length > i; i++){
	            if(argFilter == this.filtersArray[i]){
	                isFilterActive = true;
	                filterIndex = i;
	            }
	        }
	    }
	    
	    if(this.news_filter){
            if(!isFilterActive){
                this.filtersArray.push(argFilter);
            }else{
                this.filtersArray.splice(filterIndex, 1);
            }
	    }else{
	        if(!isFilterActive){
	            this.clearFilter();
	            this.filtersArray.push(argFilter);
	        }
	    }
	  
	},
	
	clearFilter : function() {
		
	    this.filtersArray = new Array(); 
	},
	
    makeTips : function(browserOffset,eleClass,tipClassName){
        if (window.ie6 || navigator.appVersion.indexOf("Mac")!=-1){
            var HeaderTips = new Tips($$(eleClass), {className: tipClassName,offsets: browserOffset});
        }else{
             var HeaderTips = new Tips($$(eleClass), {
             className: tipClassName,
             offsets: browserOffset,
             initialize:function(){
             this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 500, wait: false}).set(0);},
             onShow: function(toolTip) {this.fx.start(1);},
             onHide: function(toolTip) {this.fx.start(0);}});
        }
    },
    
    initAccordion : function(name, content_selector, toggler_selector, show) {
        //Create accordion object if it doesn't exist
        if(typeof(accordions) != 'object') {
            accordions = [];
        }
        
        //Create a single accordion control, including empty objects and properties
        accordions[name] = new global.newAccordion()
        acc = accordions[name];
        
        //Define the content items of the accordion
        acc.content = $$(content_selector);
	    acc.content.each(function(item){
		    item.setStyles({'height': '0', 'overflow': 'hidden'});
	    });

	    //Define the togglers of the accordion
	    acc.togglers = $$(toggler_selector);
	    this.setTogglerEvents(name,acc.togglers);
	    
	       
	    if(!show){
	        acc.myAccordion = new Fx.Accordion(acc.togglers, acc.content, {show: -1, alwaysHide: true, opacity: false, start: false, transition: Fx.Transitions.quadOut});
	    }else{
	        acc.myAccordion = new Fx.Accordion(acc.togglers, acc.content, {alwaysHide: false, opacity: false, start: false, transition: Fx.Transitions.quadOut});
	    }
	    
	    
    },
    
    setTogglerEvents : function(name, toggler){
         var curItem = "";
         
         if(name == "share"){
	            toggler.each(function(item){
                var isOpen = false;
                    item.addEvent('click', function(){
                        isOpen ? isOpen = false:isOpen = true;
                        isOpen ? item.setHTML('CLOSE&nbsp;&nbsp;X'):item.setHTML('Share');
                        isOpen ? item.removeClass('shareToggler'):item.addClass('shareToggler');
                        isOpen ? item.addClass('shareToggler_open'):item.removeClass('shareToggler_open');
                    });
               });
	        }
	        
         if(name == "swag"){
            acc.togglers.each(function(item){
               
                 //item.getParent().getFirst().removeClass('item');
                 //item.getParent().getFirst().addClass('item_at');
                 curItem = "";
                
                item.addEvent('click', function(){
                     //Turn off default link
                     
                     if(curItem != ""){
                         curItem.removeClass('item_at');
                         curItem.addClass('item');
                     }
                     
                     if(this != curItem){
                         //Turn on current
                         item.removeClass('item');
                         item.removeClass('item_on');
                         item.addClass('item_at');
                         curItem = this;
                     }else{
                        item.removeClass('item_at');
                        item.addClass('item');
                        curItem = "";
                     }
                    
                });
                
                item.addEvent('mouseenter', function(){
                    if(curItem != this){
	                   item.removeClass('item');
                       item.addClass('item_on');
	                }
                 });
                
                item.addEvent('mouseleave', function(){
                    if(curItem != this){
                         item.removeClass('item_on');
                         item.addClass('item');
	                }else{
                          item.removeClass('item_on');
                          item.addClass('item_at');
	                }
                 });
          
           });
         }
         
        if(name == "feed_accordions"){
        
             acc.togglers.each(function(item){
               
                 item.getParent().getFirst().removeClass('news_item');
                 item.getParent().getFirst().addClass('news_item_at');
                 curItem = item.getParent().getFirst();
                
                item.addEvent('click', function(){
                     //Turn off default link
                     curItem.removeClass('news_item_at');
                     curItem.addClass('news_item');
                     
                     //Turn on current
                     item.removeClass('news_item');
                     item.removeClass('news_item_on');
                     item.addClass('news_item_at');
                     curItem = this;
                });
                
                item.addEvent('mouseenter', function(){
                    if(curItem != this){
		               item.removeClass('news_item');
                       item.addClass('news_item_on');
		            }
                 });
                
                item.addEvent('mouseleave', function(){
                    if(curItem != this){
	                     item.removeClass('news_item_on');
                         item.addClass('news_item');
		            }else{
                          item.removeClass('news_item_on');
                          item.addClass('news_item_at');
		            }
                 });
          
           });
	    }
    },
    
    newAccordion : function(name) {
        //Create an empty object to hold accordion properties
        this.content = [];
        this.togglers = [];
    }
}

//onload_register('global.setFilter(global.cur_page)');
//onload_register('global.setTopNav()');
//if (window.ie6){onload_register('iePngFix();');}


 
