﻿var global = {
    cur_section : '',
    filtersArray : [],
    cur_page : '',
    cur_sub_page : '',
    news_filter  : false,
    cur_Item : '',
    cur_scroller : '',
    
    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 */
		$$('.header_link').each(function(link) {
			link.removeProperty('title');
		});
		
		if(global.cur_sub_page != ''){
		    $(global.cur_sub_page).addClass('sub_nav_on');
		}
	
        
     },
    
    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);

    },
    
    setScrollBar : function(){
        
        var item = global.cur_Item;
        var argScroll = global.cur_scroller;
  
        temp_y = item.getPosition().y - $(argScroll+'_container').getPosition().y;
        
	    if($(argScroll).scroll_contents.getSize().size.y >= $(argScroll).scroll_height){
		   global.moveScrollBar(argScroll,temp_y,400);
		   $(argScroll).scroll_container.setStyle('visibility', 'visible');
	    }else{
	       global.moveScrollBar(argScroll,temp_y,400);
	       $(argScroll).scroll_container.setStyle('visibility', 'hidden');
	    }
    
    },
    
    moveScrollBar: function(argScroll,argY,argDuration){
        
        temp_y = argY;

        var dest_y = (temp_y > $(argScroll).scroll_contents.getSize().size.y - $(argScroll).scroll_height) ? $(argScroll).scroll_contents.getSize().size.y - $(argScroll).scroll_height : temp_y;
        var percent_y = (temp_y > $(argScroll).scroll_contents.getSize().size.y - $(argScroll).scroll_height) ? 1 : temp_y / ($(argScroll).scroll_contents.getSize().size.y - $(argScroll).scroll_height);

        if($(argScroll).scroll_contents.getSize().size.y >= $(argScroll).scroll_height){
        
            var xTopScrollThumb = new Fx.Style($(argScroll).scroll_thumb, 'top', {duration:400,transition:Fx.Transitions.Sine.easeOut});
            xTopScrollThumb.start(Math.round(($(argScroll).scroll_height - $(argScroll).scroll_thumb.getSize().size.y) * percent_y));
        	    
            var xTopContents = new Fx.Style($(argScroll).scroll_contents, 'top', {duration:400,transition:Fx.Transitions.Sine.easeOut});
            xTopContents.start(Math.round(0 - $(argScroll).scroll_height - dest_y));
            
        }else{
            
            var xTopScrollThumb = new Fx.Style($(argScroll).scroll_thumb, 'top', {duration:400,transition:Fx.Transitions.Sine.easeOut});
            xTopScrollThumb.start(Math.round(($(argScroll).scroll_height - $(argScroll).scroll_thumb.getSize().size.y) * percent_y));
            
            var xTopContents = new Fx.Style($(argScroll).scroll_contents, 'top', {duration:400,transition:Fx.Transitions.Sine.easeOut});
            xTopContents.start(Math.round(0 - $(argScroll).scroll_height));
        }
        
   },
    

    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.ie || 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, argShow, argAlwaysHide, myFunction) {
		// -1 is the default don't-show-anything value
		argShow = argShow ? 0 : -1;
		
		argAlwaysHide = argAlwaysHide || false;
		myFunction = myFunction || function() {};
		
		
        //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);
	
		acc.myAccordion = new Fx.Accordion(acc.togglers, acc.content, {display: argShow, alwaysHide: argAlwaysHide, opacity: false, start: false, transition: Fx.Transitions.quadOut, wait: false, onComplete:eval(myFunction)});
    },
    
	setTogglerEvents : function(name, toggler){
		var curItem = "";

		if (name == 'events') {
			var curItem = '';
			
			toggler.each(function(item) {
			
				item.addEvent('click', function() {
				
					if (this != curItem) {
					
						item.addClass('open');
						item.getNext().addClass('open');

						//Scrollbar stuff
						global.cur_Item = item;
                        global.cur_scroller = 'events';
                        
						if(curItem != ''){
						    curItem.removeClass('open');
						    curItem.getNext().removeClass('open');
						}
					
						curItem = this;
					}
					
				});

				item.addEvent('mouseenter', function() {
					item.addClass('hover');
				});

				item.addEvent('mouseleave', function() {
					item.removeClass('hover');
				});
			});
		}

		else 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');
				});
			});
		}

		else if(name == "swag" || name == 'tags') {
			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');
					}
				});

			});
		}

		else 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('getUserData();');
onload_register('global.setFilter(global.cur_page)');
onload_register('global.setTopNav()');
if (window.ie6){onload_register('iePngFix();');}
 