/* broutek js - events and behaviour */

/* --valuePacks
 sets up the value-packs interface 
-- */

valuePacks = function() {
    
     if (!$('value-packs')) return;
     $ES('li', 'value-packs').each(function(obj){
        obj.addEvent('mouseover', function(){    
            var targetTR = "tr." + this.id;
            $ES(targetTR, 'value-packs-table').addClass('over');
        });  
        obj.addEvent('mouseout', function(){    
            var targetTR = "tr." + this.id;        
            $ES(targetTR, 'value-packs-table').removeClass('over');

        });          
        });

}
    
/* -- showtips
 tooltips - add the class tool-tips to an element, along with a title tag and pass an array of objects to the function
 eg: showtips("li") where the argument is the element
 make sure to add the classname "tool-tip"
-- */

var showtips = function(element) {
    var els = [];
    $ES(element).each(function(obj){
        if (obj.getAttribute('title')) els.push(obj);
    });
    
    new Tips(els, {maxOpacity: 0.9, maxTitleChars: 25});
}


/* -- expandList
 faq accordian list
-- */

var expandList = function() {

    var headings = $$('dl.faqs dt');

    $$('dl.faqs dd').addClass('hide');

    headings.addClass('pointer');
    
    headings.each(function(obj){
    
        obj.addEvent('click', function(){ 

               
               var ddElem = this.getNext();
               if(this.hasClass('current')) {
                    $ES('dt',this.getParent()).removeClass('current');
                    $ES('dd',this.getParent()).addClass('hide');
                    ddElem.addClass('hide');
                    this.removeClass('current');               
               } else {
                    $ES('dt',this.getParent()).removeClass('current');
                    $ES('dd',this.getParent()).addClass('hide');               
                   ddElem.removeClass('hide');
                    this.addClass('current');  
               }

        });
        
    });
}


/* -- navBar
sets up current page and ensures the navbar works in all browsers
-- */

navBar = function() {
    if (!$('navigation')) return;
    $ES('li','navigation').addEvent('mouseenter', function(){
       this.addClass('hover');
    });
    $ES('li','navigation').addEvent('mouseleave', function(){
       this.removeClass('hover');
    });    
}

/* -- clearSearch
clears inputs that have default values
-- */

clearSearch = function(id) {
    if (!$(id)) return;
    var originalvalue = $(id).value;
    $(id).addEvent('focus', function(){
        this.addClass("darktext");
        if (this.value == originalvalue) {this.value = ""};
    });    
    
    $(id).addEvent('blur', function(){
        if (this.value == "") {this.value = originalvalue};
        this.removeClass("darktext");
    });      
}


/* -- homePromos
provides rollover functionality for home promos
-- */

var homePromos = function(id) {
    if (!$(id)) return;
    $ES('li',id).each(function(obj){
        obj.style.cursor = "pointer";
        obj.addEvent('click', function(){
            window.location.href=$E('a',this).getProperty('href');           
            return false;
        });  
        });
}


/* -- homePromos
where sub headers have numbers, this inserts them automatically for your convenience
-- */
addListNumbers = function() {    
    $$('.sub-promo li').each(function(obj, index){
            index+=1;
            var newSpan = new Element('span');
            newSpan.appendText(index);
            newSpan.injectTop(obj);
        });  
    
}




/* -- load 
add functions that you want to load
-- */

window.addEvent('domready', function(){
  clearSearch("query");
  navBar();
  //homePromos("home-promos");
  addListNumbers();
  //valuePacks();
  //showtips("li");
  expandList();
});