/**
 * @author bluntworks 03/11
 */
//Generic log function
if ($.log != 'function') {
  $.log = function(m){
    if (window && window.console && window.console.log) {
      (typeof(m) === 'object' ) ? window.console.log(JSON.stringify(m)): window.console.log(m);
    }
  };
};

function slugIt(str) {
  return str.replace(/\s+/g, '-').toLowerCase();
}

jQuery(document).ready(function($){
  
  $('.ChimpScribe').click(function(e){ 
    $('.PopUp').show();
  }); 
  
  $('#mc_mv_EMAIL').val('Email Address *');
  $('#mc_mv_FNAME').val('First name');
  $('#mc_mv_LNAME').val('Last Name');
  
  $('.Chimp input').not('#mc_signup_submit').focus(function(e){ 
    $(this).val('');
  }); 
  
  $('input#search').focus(function(e){ 
    $(this).val('');
  });
  
  $('.CForm input').not('#submit').focus(function(e) { 
    $(this).val('');
  }); 
  
  $('.CForm textarea').focus(function(e) { 
    $(this).val('');
  });
  
  
  
  //$.handleChimps();
  
  $.ddfErrors();
  
  
  //START THE BANNER
  $('.Banner').Banner();  
  
  //News/GAllery/Reading List Menu 
  $('.Sub .Nav li a').click(function(e) { 
    var li = $(this).parent();
    var indx = $('.Sub .Nav li').index(li);
    var hash = this.hash.slice(1);
    
    $('.Switchr .Tray').css('left', -(950*indx));
    
    // change .Switchr Hilight nav
    $('.Sub .Nav li a').removeClass(); 
    $(this).addClass('Active'); 
    
    //$.log ( 'hash : '+hash);
    
    //change Archive Nav
    $('.CatLinks a').removeClass('Active'); 
    $('.CatLinks a.'+hash).addClass('Active');

    e.preventDefault(); 
  });
  
  $('.Events.List tr.Row').mouseover(function(e) { 
    $(this).css('background', '#ed1c24');  
    $(this).find('a.BuyNow').css('color', '#fff');
    $(this).find('.GDate').css('color', '#231F20');
  }); 
  
  $('.Events.List tr.Row').mouseout(function(e) { 
    $(this).css('background', 'none');  
    $(this).find('a.BuyNow').css('color', '#ed1c24');
    $(this).find('.GDate').css('color', '#808080');
  });
  
  //Handle Events.List row click
  $('.Events.List tr.Row').click(function(e) { 
    var link = $(this).find('.EventLink').val();
    
    window.location = link;
  });
  
  //Handle EVENTS - GRid/List switching
  $('li.List a').click(function(e) { 
    $('.Events.Grid').hide();
    $('.Events.List').show();
    
    $(this).parent().addClass('Active');
    $('li.Grid').removeClass('Active');
    
    $.fixFoot();
    
    $.cookie('event_view', 'list');
     e.preventDefault();
  }); 
  
  //Handle EVENTS - GRid/List switching
  $('li.Grid a').click(function(e) { 
    $('.Events.List').hide();
    $('.Events.Grid').show();
    
    $(this).parent().addClass('Active');
    $('li.List').removeClass('Active');
    
    $.fixFoot();
    
    $.cookie('event_view', 'grid');
    
    e.preventDefault();
  }); 
  
  if($('.Events.List').length > 0) { 
    var v = $.cookie('event_view'); 
    if(v === 'list') { 
      $('.Events.Grid').hide();
      $('.Events.List').show(); 
      
      $('li.List').addClass('Active'); 
      $('li.Grid').removeClass('Active');
    }
  }
  
  //Home page News and Gallery Rollover
  $('.IBox').mouseover(function(e){
    $('.IBox').not(this).find('.Excerpt').hide('slow');
    $(this).find('.Excerpt').show('slow');
  });
  
  $('.Boxes').mouseleave(function(e) { 
    $('.IBox .Excerpt').hide('slow');
  }); 

  //Fix Footer position on tall screens with little content
  $.fixFoot();
  $(window).resize(function() { $.fixFoot(); });
  
});


(function($) {
  $.ddfErrors = function() { 
    var isErr = $('.ddfmerrors');
    var errList = $('.errorlist');
    
    $.log('Checking for errors in signup');
    if(isErr.length ==  0)
      return false;
      
    $.log('there seems to be errors : '+isErr.length);
      
    $('.Cont').find('p').remove();
    $('.Cont').append('<h2>There are errors in your subscription</h2>');
    $('.Cont').append('<p>'+errList.html()+'</p>');
      
    isErr.remove();
    errList.remove();
  };
  
  $.fixFoot = function() { 
    var dbw, inr, win, foot;
      
    inr = $('.Inner'); 
    win = $(window); 
    foot = $('.FootIn');
        
    if(inr.height() < ( win.height()- foot.height() ) )
      inr.css('margin-bottom',  ( win.height() - inr.height() ) - foot.height() );
    
  }
  
  $.handleChimps = function() { 
   var chimp_message =  $('#mc_message').html();
   $('#mc_message').remove(); 
   
   
   if(chimp_message !== '') { 
     
     
     $('.Inner').append('<div class="Chimps">'+chimp_message+'</div>');
     $('.Chimps').css(
      {'position'     : 'absolute',
       'top'          : '10px', 
       'left'         : 0,
       'font-family'  : 'Georgia',
       'color'        : '#ed1c24', 
       'font-size'   : '1.8em',
       'font-style'   : 'italic'
     });
     
     $('.mc_success_msg').css('color', '#ed1c24');
   } 
  }
  
  $.fn.Banner = function(options) { 
  var 
  settings = { 
    'speed'     : 'slow'
   ,'timeOut'   : 6000  
   ,'boxWidth'  : 950
  }
  
  if (options) $.extend(settings, options);
  
  var
  $elf     = $(this)
 ,$numBox  = $('.BNav')
 ,$nums
 ,$pn      = $('.PrevNext')
 ,$tray    = $elf.find('.ImageBox')
 ,$imgs    = $elf.find('img')
 ,iCount   = $imgs.length
 ,max      = iCount-1
 ,curPos   = 0
 ,c        = 0
 ,sId
  
  $tray.css('width',settings.boxWidth*iCount);
  
  //Count the images and create a numeric indicator for each
  buildNums(iCount);
  
  //Set up the click handlers
  enableEvents();
  
  //Kick off the animation
  next();
  
  function next(){
    sId = setTimeout(function() { (c < max )? c++ : c = 0;  doNext(); }, settings.timeOut);
  }
  
  function doNext() { 
     moveTo(c*settings.boxWidth);
     setActiveNum();
  }
  
  function moveTo(newPos) { 
    $tray.stop().animate({left : -newPos}, settings.speed, 
      function() { 
        next();
    });
  }
  
  function setActiveNum() { 
    $nums.removeClass('Active');
    $($nums[c]).addClass('Active');
  }
  
  function buildNums(count){
    var i, O = ''
    
    for (i = 01; i < count+1; i++) {
      O += '<li><a href="#'+(i-1)+'">' + '&nbsp;' + '</a></li>';
    }
    
    $numBox.append(O);
    $nums = $('.BNav li'); 
    
    $($nums[0]).addClass('Active');
  }
  
  function enableEvents()  {
    $('.BNav li a').click(function(e){ 
      clearInterval(sId);
      c =parseInt(this.hash.substring(1),10);
      doNext();     
    }); 
    
    $('.Prev').click(function(e){ 
      clearInterval(sId);
      (c > 0 ) ? c-- : c = max; 
      e.preventDefault();
      doNext();
    }); 
    
    $('.Next').click(function(e){ 
      clearInterval(sId);
      (c < max  ) ? c++ : c = 0; 
      e.preventDefault();
      doNext();
    });
  }
 }
})(jQuery);

