//vars
var lastIndex;
var timer;
var mpane;
var bgroup;
var mpane_testimonials;
var timer_testimonials;

function timer_onTriggerHandler(instanceName){
  var instance = jqready;
  
  //alert(instanceName);
  
  if(instanceName=='timer'){
  
    timer.trigger();
    
    if(typeof(mpane)!='undefined'){
      
      //reset to 0 first
      if(mpane.curIndex >= mpane.numChildren-1){
        $('#mPane .mp_container').css({
          'left':'0px'
        });
        mpane.advance(1);
      }
      mpane.advance(1);
      
    }
  }
  
  if(instanceName=='timer_testimonials'){
    timer_testimonials.trigger();
    mpane_testimonials.advance(1);
  }
}

function timer_testimonials_onTriggerHandler(instanceName){
  var instance = jqready;
  //alert(instanceName);
  
  alert('x');
  timer_testimonials.trigger();
  
  if(typeof(mpane_testimonials)!='undefined'){
    mpane_testimonials.advance(1);
  }
}

//########################################################################################

$(document).ready(function() {
  
  //.ready vars
  jqready = this;
  
  function setup_homePage() {
    //duplicate first multipane item before creating
    $('#mPane .item').eq(0).clone().appendTo('#mPane .mp_container');
    
  
    mpane = new MultiPane({
      target:'#mPane',
      item:'.item',
      type:'slider',
      speed:1000,
      onActivate:function(){
        if(typeof(bgroup)!='undefined'){
          bgroup.update(this.curIndex);
          
          //if index past 'true' max, set to 0
          if(this.curIndex > this.numChildren-2){
            bgroup.update(0);
          }
        }
        if(typeof(scontrol)!='undefined'){
          scontrol.update(this.curPercent);
        }
      }
    });
    
    bgroup = new ButtonGroup({
      target:'#bGroup',
      trigger:'click',
      active:'none',
      onActivate:function(){
        if(typeof(mpane)!='undefined'){
          mpane.activate(bgroup.curIndex);
        }
      },
      onMouseDown:function() {
        if(typeof(timer)!='undefined'){
          //timer.reset();
          timer.stop();
        }
      }
    });
    bgroup.activate(0);
    
    //stop timer btn
    $('#bGroup a.btn_stop').bind('mousedown',function(){
      timer.stop();
    })
    $('#bGroup a.btn_stop').bind('click',function(){
      return false; //prevent browser default
    })
    
    timerSpeed = $('#home_rotator_speed').val();
    timer = new Timer({
      time:timerSpeed,
      instanceName:'timer',
      repeat:true,
      onTrigger:timer_onTriggerHandler
    });
  }
  
  function setup_contentPage() {
    
    //format services table rows
    /*
    $('.servicesTable_2 tr:even:not(.div) td').each(function(){
      $(this).css('background','#eee');
    });
    */
    
    //services table accordion
        $('#servicesTable h4.accordion_head').bind('click',function(){
          var index = $(this).index('.accordion_head');
          openPane(index);
        });
        
        //close all panes
        $('#servicesTable .accordion_pane').slideUp(0);
        
        function openPane(index) {
          //alert('openPane '+index);
          
          var speed = '';
          
          //open/close selected pane & activate head
          var paneState = $('#servicesTable h4.accordion_head').eq(index).hasClass('accordion_head_active');
          if(paneState==false){
            $('#servicesTable .accordion_pane').eq(index).slideDown(speed);
            $('#servicesTable h4.accordion_head').eq(index).addClass('accordion_head_active');
          } else {
            $('#servicesTable .accordion_pane').eq(index).slideUp(speed);
            $('#servicesTable h4.accordion_head').eq(index).removeClass('accordion_head_active');
          }
        }
        
        //openPane(0); //open first pane
    
    //testimonials
        mpane_testimonials = new MultiPane({
          target:'#mPane_testimonials',
          item:'.item',
          type:'toggle',
          animation:'fade',
          speed:500
        });
        
        //select random testimonial
        var randNum = Math.floor(Math.random()*mpane_testimonials.numChildren);
        mpane_testimonials.activate(randNum);
        
        var timerSpeed_testimonials = $('#testimonials_speed').val();
        timer_testimonials = new Timer({
          time:timerSpeed_testimonials,
          instanceName:'timer_testimonials',
          repeat:true,
          onTrigger:timer_onTriggerHandler
        });
  }
  
  function setup_universalContent() {
    
    $('#username,#pass').attr('autocomplete', 'off');
    
    //reset username and pass fields when you focus on them with their default values
    $('#username,#pass').bind('focus',function(){
      var val = $(this).val();
      if(val=='Client Login ID'){
        $(this).val('');
      }
      if(val=='********'){
        $(this).val('');
      }
    })
    $('#username,#pass').bind('blur',function(){ //restore to defaults if left blank
      var val = $(this).val();
      var id = $(this).attr('id');
      if(id=='username'){
        if(val==''){
          $(this).val('Client Login ID');
        }
      }
      if(id=='pass'){
        if(val==''){
          $(this).val('********');
        }
      }
    });
    
    //reset search field
    $('#search').bind('focus',function(){
      var val = $(this).val();
      if(val=='Search...'){
        $(this).val('');
      }
    })
    $('#search').bind('blur',function(){ //restore to defaults if left blank
      var val = $(this).val();
      if(val==''){
        $(this).val('Search...');
      }
    })
  }
  
  
  
  //call different function depending on page
  if( $('div.homepage').length ) {
    setup_homePage();
  } else {
    setup_contentPage();
  }
  
  //call universal function
  setup_universalContent();
  
});
