//General functions
var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;
function jsddm_open() {
  jsddm_canceltimer();
  jsddm_close();
  ddmenuitem = $(this).find('ul').css('visibility', 'visible');
  $(this).children(':first').addClass('hover');
}
function jsddm_close(){
  if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');
  $('a').removeClass('hover');
}
function jsddm_timer(){
  closetimer = window.setTimeout(jsddm_close, timeout);
}
function jsddm_canceltimer(){
  if(closetimer){
    window.clearTimeout(closetimer);
    closetimer = null;
  }
}
$(document).ready(function () {  
  
  var promoSpeed = 7500;
  $('#promo-items li').not(':first').css({opacity: 0.25});
  $('#promo-items li:first').addClass('selected');
  $('#promo-selected a').not(':first').css({opacity: 0});
  $('#promo-selected a:first').addClass('selected');
  
  $.fn.automatePromos = function(){
    //Tabs
    var $selected = $('#promo-items li.selected');
    if ($selected.length == 0) $selected = $('#promo-items li:last');
    var $next =  $selected.next().length ? $selected.next() : $('#promo-items li:first');
    $selected.addClass('last-active');
    $('#promo-items li').css('opacity','0.25');
    $next.css({opacity: 1}).addClass('selected');
    $selected.removeClass('selected last-active');
    //Banners
    var $selectedBanner = $('#promo-selected a.selected');
    if ($selectedBanner.length == 0) $selectedBanner = $('#promo-selected a:last');
    var $nextBanner =  $selectedBanner.next().length ? $selectedBanner.next() : $('#promo-selected a:first');
    $selectedBanner.addClass('last-active');
    $('#promo-selected a').css('opacity','0');
    $nextBanner.css({opacity: 1}).addClass('selected');
    $selectedBanner.removeClass('selected last-active');
   
  }

  setInterval(function () {
    $('body').automatePromos();
  }, promoSpeed); 
  
  
  $('.new').attr('target','_blank');
  
  $('#search-input').focus(function () {
    if ($(this).attr('value') == 'Search site') {
      $(this).attr('value', '');
    }
  });
  $('#search-input').blur(function () {
    if ($(this).attr('value') == '') {
      $(this).attr('value', 'Search site');
    }
  });
  $('#email').focus(function () {
    if ($(this).attr('value') == 'Your email address') {
      $(this).attr('value', '');
    }
  });
  $('#email').blur(function () {
    if ($(this).attr('value') == '') {
      $(this).attr('value', 'Your email address');
    }
  });

  $('#nav > li').bind('mouseover', jsddm_open);
  $('#nav > li').bind('mouseout', jsddm_timer);
  document.onclick = jsddm_close;


  //Promo items
  $('#promo-items li').mouseover(function(){
    $('#promo-items li').css('opacity','0.25');
    $(this).css('opacity','1');
    var promoItemSelected = $(this).index();
    $('#promo-selected a').css('opacity','0');
    $('#promo-selected a:eq('+promoItemSelected+')').css('opacity','1');
  });


  //Resource downloads
  $('.resource-downloads h2 a').click(function(){
    $(this).toggleClass('selected');
    $(this).parent().nextAll('.downloads').slideToggle(500);
    return false;
  });

  //Resource category fade effect
  $('h3.resource').mouseover(function(){
    $('h3.resource').not($(this)).stop().animate({opacity: 0.25},200);
  }).mouseout(function(){
    $('h3.resource').stop().animate({opacity: 1},200);
  });


  //Carousel
  $('.carousel').jCarouselLite({
    auto: 3800,
    speed: 1000
  });


  //Event details pop-up
  $('table.sm_calendar td').hover(function(){
    $(this).children('div.events_today').fadeIn('fast');
  },function(){
    $(this).children('div.events_today').fadeOut('fast');
  });


  //Modals
  $('.iframe').colorbox({iframe:true, width:'520px', height:'380px'});

});
