$(document).ready(function(){
  document.onkeydown = doOnKeyDown;
  getSAHeight();
  
  //  $(".menu a").hover(
  //    function () {
  //      var vis = $('.box:visible').index() - 2;
  //      var indx = $('.menu:eq('+vis+') a').index(this);
  //      $('#slider' + vis).data('nivo:vars').stop = true;
  //      $("#slider" + vis).hide();
  //      var cur_img = $("#slider" + vis + " img").eq(indx).attr('src');
  //      $("#slider" + vis + "-" + vis).html('<img src="' + cur_img + '" alt="" />').animate({'opacity':'show'}, 600);
  //      sliderMenu(vis, indx);
  //    }, 
  //    function () {
  //      var vis = $('.box:visible').index() - 2;
  //      var indx = $('.menu a').index(this);
  //      $('#slider' + vis).data('nivo:vars').stop = false;
  //      $("#slider" + vis).show();
  //      $("#slider" + vis + "-" + vis).hide().html('');
  //      sliderMenu(vis, current_menu_index);
  //    }
  //    );
      
  $(".menu a").mouseenter(
    function () {
      var vis = $('.box:visible').index() - 2;
      var indx = $('.menu:eq('+vis+') a').index(this);
      $('#slider' + vis).data('nivo:vars').stop = true;
      $("#slider" + vis).hide();
      var cur_img = $("#slider" + vis + " img").eq(indx).attr('src');
      if(!$("#slider" + vis + "-" + vis).is(':animated'))
        $("#slider" + vis + "-" + vis).html('<img src="' + cur_img + '" alt="" />').animate({
          'opacity':'show'
        }, 400);
      sliderMenu(vis, indx);
    }).mouseleave(    
    function () {
      var vis = $('.box:visible').index() - 2;
      var indx = $('.menu a').index(this);
      $('#slider' + vis).data('nivo:vars').stop = false;
      $("#slider" + vis).show();
      $("#slider" + vis + "-" + vis).hide().html('');
      sliderMenu(vis, current_menu_index);
    }
    );
});

function playSlider(num, indx) {
  $('.banner .ttl').hide();
  $('.banner .txt').hide();
  $('.box img').show();
  sliderMenu(num, indx);
  indx = 1;
  current_menu_index = indx-1;
  if($(".nivoSlider").length < 2) {
    $('#slider'+num).nivoSlider({
      effect:'random',
      captionOpacity:0,
      directionNav:false,
      controlNav:false,
      pauseTime:250000,
      afterChange: function(){
        sliderMenu(num, indx);
        indx++;
        current_menu_index = indx-1;
      },
      lastSlide: function(){
        indx = 0;
      },
      pauseOnHover:true
    });
  }
}
var current_menu_index = 0;
function sliderMenu(num, i) {
  $('.menu:eq('+num+') a.act').removeClass('act');
  $('.menu:eq('+num+') a').eq(i).delay(200).animate({
    "opacity": "show"
  }, 800 ).addClass('act');
  $('.banner:eq('+num+') .ttl').animate({
    "opacity": "hide"
  }, 800 );
  $('.banner:eq('+num+') .txt').animate({
    "opacity": "hide"
  }, 800 );
  $('.banner:eq('+num+') .ttl').eq(i).animate({
    "opacity": "show"
  }, 1200 );
  $('.banner:eq('+num+') .txt').eq(i).animate({
    "opacity": "show"
  }, 1200 );
}


function getSAHeight() {
  h = (document.body.scrollHeight > document.body.offsetHeight)?document.body.scrollHeight:document.body.offsetHeight;
  document.getElementById('sa').style.height = h + "px";
}
function doOnKeyDown (e) {
  e = e || window.event;
  if (e.keyCode == 27){
    hideRe();
  }
}
function hideRe() {
  $('#sa').hide();
  $('#advance').hide();
}
function putLayer(mydiv, top) {
  var Width = document.documentElement.scrollTop;

  if (document.body.scrollTop == 0) {
    var Y = document.documentElement.scrollTop;
  } else {
    var Y = document.body.scrollTop;
  }
  heightBrowser = top + Y;
  document.getElementById(mydiv).style.top = heightBrowser + "px";
}

function signCheck(mod) {
  if ($("#fname").val()=="") {
    alert( "Please enter Your Name" );
    $("#fname").focus();
    return false ;
  }
  if ($("#lname").val()=="") {
    alert( "Please enter Your Last Name" );
    $("#lname").focus();
    return false ;
  }


  if (mod == "request") {
    if ($("#address1").val()=="") {
      alert( "Please enter Your Address" );
      $("#address1").focus();
      return false ;
    }
    if ($("#city").val()=="") {
      alert( "Please enter Your City" );
      $("#city").focus();
      return false ;
    }
    if ($("#state").val()=="") {
      alert( "Please enter Your State" );
      $("#state").focus();
      return false ;
    }
    if ($("#zip").val()=="") {
      alert( "Please enter Your Zip" );
      $("#zip").focus();
      return false ;
    }
  }


  if ($("#email").val()=="") {
    alert( "Please enter Your Email" );
    $("#email").focus();
    return false ;
  } else {
    if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test($("#email").val()))) {
      alert('Email address you entered appears to be in the wrong format.\nPlease use xxxx@xxx.xxx');
      $("#email").select();
      return false;
    } 
  }

  if (mod != "profile") {
    if ($("#pass").val()=="") {
      alert( "Please enter Password" );
      $("#pass").focus();
      return false ;
    } else {
      if ($("#pass").val() != $("#repass").val()) {
        alert( "The passwords aren't the same!" );
        $("#pass").focus();
        return false ;
      }
    }
  }
}

function getOneMore(id){
  var txt = $('#assoc :selected').text();
  str = "<span id='d"+id+"'>&#151; "+txt+" (<strong class='red go' onclick='deleteAssoc("+id+")'>x</strong>) <input type=hidden name=assoclist[] value="+id+"></span>";

  if ($("#list").text() == "Empty") $("#list").html('');
  $("#list").append(str);
  $("#d"+id).hide();
  $("#d"+id).show("slow");
}
function deleteAssoc(id)
{
  $("#d"+id).animate({
    opacity: "hide", 
    height: "0"
  }, "slow", function(){
    $("#d"+id).remove();
  });
}

function ReloadCaptcha(){
  $('#mycaptcha_img').attr("src", "/inc/captcha/captcha.php?foo="+Math.random());
}

function getPopup(id, prm1, prm2, prm3) {
  $('#advance').html('<div class="top"></div><div class="mid" id="adcontent"></div><div class="btm"></div>');
  $('#sa').show();
  $('#advance').show();
  $('#adcontent').append('<div class="pre"></div>');
  $.ajax({
    url: '/_content/'+id+'.php',
    type: 'POST',
    data: {
      id: id, 
      prm1: prm1, 
      prm2: prm2, 
      prm3: prm3
    },
    success: function(answer) {
      $('#advance').html(answer).animate({
        "opacity": "show"
      }, "slow");
      putLayer('advance', 10);
    }
  });
}
