var search_clear = false;

$(function() {
  
  // This person has JS: set cookie for PHP to remember this
  js_cookie();
  
  // Should we show the scrollbar?
  resize();
  $(window).resize(function() {
    resize();
  });
  
  // Get width of search box
  searchwidth = ($('#search').width())-42;
  clicked = 0;
  
  // Set opacity of the "meet the team" area to 0
  $('#team').css('opacity', 0).show();
  
  // Add fake password box into login area
  if('#login-box-fakepass') {
    $('#login-box-fakepass').html('<input type="text" id="login-pass-fake" class="form-text form-text-none" value="Password" />');
    
    //$('#login-box').click(function() {
    //  return false;
    //});
  }
  
  // Fade the pointer in
  if($('#pointer')) {
    $('#pointer').hide().fadeIn(1000);
  }
  
  // Make sure default input text is displayed on load
  if($('#login-box').is("form") == true) {
    hide_input('login-user');
    hide_input('login-pass');
  }
  
  if($('#join-search-name').is("input") == true) {
    hide_input('join-search-name');
  }
  
  // Show/hide default input text
  $('#login-user,#login-pass,#join-search-name,#search-box').keyup(function() {
    hide_input($(this).attr('id'));
  });
  
  // Toggle the "meet the team" area on mouseover, mouseout
  $('#team').hover(function() {
    $(this).stop().animate({opacity: 1}, 250);
  },
  function() {
    $(this).stop().animate({opacity: 0}, 250);
  });
  
  // Show the search box when clicking "Find a yearbook"
  $('#search').click(function() {
    
    search_show();
    
  });
  
  // Hide the search box when leaving
  $('#search-box').blur(function() {
    search_clear = setTimeout(search_blur, 300);
  });
  
  // Focus into main field when creating a yearbook
  if($('#join-search-name')) {
    $('#join-search-name').focus();
  }
  
  // Login/country menu
  $('#login,#country').click(function() {
    //console.debug('Clicked login or country button.');
    
    clicked = $(this).attr('id');
    
    if(clicked == 'login') {
      
      if ($('#login-box').is(':visible')) {
        clicked = 0;
      }
      
      login_box_width = calc_width();
      $('#login-box').css({'width': login_box_width+'px'}).slideToggle("fast", function() {
        $('#login').toggleClass("t-a-l-sel");
        if($('#login-box').is("form") == true) {
          $('#login-user').focus();
        }
      });
      $('#search').toggleClass("t-a-s-sel");
      
      if ($('#country-box').is(':visible')) {
        $('#country-box').slideUp("fast", function() {
          //console.debug('Slid up country box.');
          $('#country').removeClass("t-a-c-sel");
        });
      }
      
    }
    
    else {
      
      distance_from_right = calc_width();
      
      $('#country-box').css('right',(distance_from_right-22)+'px');
      
      $('#country-box').slideToggle(300, function() {
        //console.debug('Slid up country box.');
        $('#country').toggleClass("t-a-c-sel");
      });
      
      if ($('#login-box').is(':visible')) {
        $('#login-box').slideUp("fast", function() {
          //console.debug('Slid up login box.');
          $('#login').removeClass("t-a-l-sel");
          $('#search').removeClass("t-a-s-sel");
        });
      }
      
    }
    
  });
  
  // Hide the login/country menus when clicking elsewhere on the page
  $('body *:not(#login):not(#country):not(.t-a):not(#login > h2)').click(function() {
    if (clicked != 0
        && $(this).closest('#'+clicked+'-box').size() == 0) {
      //console.debug('Clicked somewhere outside of login area.');
      if($('#country-box').is(':visible')) {
        $('#country-box').slideUp("fast", function() {
          //console.debug('Slid up country box.');
          $('#country').removeClass("t-a-c-sel");
        });
      }
      
      else if ($('#login-box').is(':visible')) {
        $('#login-box').slideUp("fast", function() {
          //console.debug('Slid up login box.');
          $('#login').removeClass("t-a-l-sel");
          $('#search').removeClass("t-a-s-sel");
        });
      }
    }
  });
  
  // Hide the login/country menus when mouse pointer hovers outside of them for a few seconds
  // REMOVED
  //$('.t-a, #login-box').hover(reset_login_timer, start_login_timer);
  
  $('#login-box input').focusin(login_focusin);
  $('#login-box').click(login_focusin);
  
  $('#login-box input').focusout(function() {
    //console.debug('Focus out.');
    $(this).removeClass('has-focus');
    $('#login-box, #login-box input').removeClass('has-focus');
    start_login_timer.apply($('.t-a'));
  });
  
});

function resize() {
  
  if($(window).width() >= 974) {
    $('body').css({'overflow-x': 'hidden'});
  }
  else {
    $('body').css({'overflow-x': 'auto'});
  }
  
}

function hide_input(id) {
  
  //console.debug(id);
  if($('#'+id).val().length < 1) {
    //console.debug('Showing '+id+'-fake');
    $('#'+id+'-fake').css({ color: '#9a9a9a' });
  }
  else {
    //console.debug('Hiding '+id+'-fake');
    $('#'+id+'-fake').css({ color: '#fff' });
  }
  
}

function reset_login_timer() {
  
  //console.debug('Reset timer.');
  // reset mouseout close login box timer
  clearTimeout($(this).data('hover_close'));
  
}

function start_login_timer() {
  
  if ($('#'+clicked+'-box').is(':visible')) {
    //console.debug('Set timer.');
    $('.t-a').data('hover_close', setTimeout(function() {
      //console.debug('Timer!');
      
      if(clicked == 'country'
        || !$('#login-box input').hasClass('has-focus')) {
        $('#'+clicked+'-box').slideUp("fast", function() {
          //console.debug('Slid up login/country box.');
          $('#'+clicked).removeClass("t-a-l-sel");
          $('#search').removeClass("t-a-s-sel");
        });
      }
    }, 15000));
  }
  
}

function login_focusin() {
  
  //console.debug('Focus in.');
  $(this).addClass('has-focus');
  reset_login_timer();
  
}

function form_focus(element, title) {
  
  if(element.value==title) {
    element.value = "";
  }
  
}

function form_blur(element, title) {
  
  if(!element.value) {
    element.value = title;
  }
  
}

function calc_width() {
  
  // Why 73?
  // Login/search padding + borders - login box padding/border
  
  width = 71+$('#login').width()+searchwidth+18;
  return width;

}

function search_show() {
  
  // Nicely show the search box
  
  clearTimeout(search_clear);
  
  if($('#search-box').is(':hidden')) {
    
    $('#search-text').hide();
    
    $('#search').stop().animate({ 'min-width': '195px' }, 250);
    $('#search-box').stop().css({ width: searchwidth, opacity: 0 }).show().animate({ width: '150px', opacity: 1 }, 250, function() {
      
      $('#search-box-fake').css({ position: 'relative' }).show();
      $('#search-box').css({ position: 'absolute', top: '4px' });
      $('#search').addClass('t-a-s-b');
      $('#search-box').select();
      
    });
    
    return false;
  
  }
  
  return true;
  
}

function search_blur() {
  
  $('#search-box-fake').css({ position: 'absolute' }).hide();
  $('#search-box').stop().css({ position: 'relative', top: '0px' });
  
  $('#search').stop().animate({ 'min-width': '146px' }, 250);
  $('#search-box').animate({ width: searchwidth, opacity: 0 }, 250, function() {
    
    $('#search-text').show();
    $('#search').removeClass('t-a-s-b', function() {
      
      $('#login-box').css({'width': login_box_width+'px'});
      
    });
    
    $('#search-box').hide();
    
  });
  
}

function js_cookie() {
  
  var today = new Date();
  var expire = new Date();
  expire.setTime(today.getTime() + 3600000*24*30);
  
  document.cookie = "hasjs=1;path=/;expires="+expire.toGMTString();
  
}
