var homeNewsIndex = 0;
var homeNewsCount = 1;
var homeNewsCycleTimeout;
var overlayStep = 0;

$(function(){
  if(typeof Touch != "object") {
    $("#main-nav.home li.top-level").hover(function(){
      $(this).children('ul').stop().animate({width:180},250);
    },
    function(){
      $(this).children('ul').stop().animate({width:0},150);
    });
  }

  $("#top-nav li.drop").hover(function(){
	if(!$(this).hasClass("account-login") && document.activeElement.tagName.toLowerCase() == "iframe") {
      return;
    }
    $(this).css('background-color', '#FFF');
    $(this).children('ul, div.drop').show().stop().animate({opacity:1},150);
  },
  function(){
	if(document.activeElement.tagName.toLowerCase() == "iframe") {
      return;
    }
    $(this).css('background-color', 'transparent');
    $(this).children('ul, div.drop').stop().animate({opacity:0},150).hide();
  });

  $(".slider-handle").click(function(){
    if(!$(this).prev().hasClass('open')) {
      $(this).prev().slideDown(200).addClass('open');
      $(this).addClass('open');
    } else {
      $(this).prev().slideUp(200).removeClass('open');
      $(this).removeClass('open');
    }
  });

  $(document).placeholder();

  homeNewsCount = $('#home-media-room .article').length;

  $('#home-media-room .article').each(function(i, e) {
    var ione = i + 1;
    $(this).addClass('article-' + ione);
  });

  homeNewsCycleTimeout = setTimeout('rotateHomeNews(1)', 7000);

  $("#prev-control").click(function(){
    if(homeNewsIndex != 0) {
      rotateHomeNews(-1);
    }
    return false;
  });
  $("#next-control").click(function(){
    if(homeNewsIndex + 1 < homeNewsCount) {
      rotateHomeNews(1);
    }
    return false;
  });

  $("#checking-options td.note").hover(function(){
    var note = $(this).find('span').html();
    var popup = $('<div class="note-popup"><div class="popup-arrow"></div><p>' + note + '</p></div>');
    var xpos = $(this).width() + $(this).offset().left + 10;
    var ypos = $(this).offset().top;
    popup.css('top', ypos);
    popup.css('left', xpos);
    $("body").append(popup);
  },function(){
    $(".note-popup").remove();
  });
  
  if ($.browser.msie && $.browser.version.substr(0,1)==7) {
    var zIndexNumber = 1000;
    $('div').each(function() {
      $(this).css('zIndex', zIndexNumber);
      zIndexNumber -= 10;
    });
  }

  // compliance for outgoing links
  $('a[href="http://www.bankatfirst.com/investor"],a[href="http://www.snl.com/irweblinkx/corporateprofile.aspx?IID=100255"],a[href="http://info.netteller.com/go/preventingfraud/"],a[href="http://www.idtheft.gov/probono/index.html"],a[href="https://www.ftccomplaintassistant.gov/"],a[href="http://www.ftc.gov/bcp/edu/microsites/idtheft/tools"]').click(function(){
    var l = confirm("You have clicked on an external link and are leaving the First Financial Bank website. \nLinked web pages are not under the control of First Financial Bank, its affiliates or \nsubsidiaries. First Financial provides such links as a convenience and is not responsible \nfor the content or security of any linked web page.");
    return l;
  });
  
  // handle sizes for javascript updates for media queries
  $(window).resize(checkSize);
  checkSize();
});

function checkSize() {
  var width = $(window).width();
  if(width <= 480) {
    if($("#top-nav-select").length == 0) {
      var login = $('#top-nav > li > a[href*="account-access"]:eq(0)').clone(false);
      login.attr('id', 'top-login-button');
      $("#header").append(login);
      var navitems = $('#top-nav > li > a:not([href*="account-access"])');
      var holder = $('<div id="top-select-holder"></div>');
      var select = $('<ul id="top-nav-select"></ul>');
      select.append(
        $("<a></a>").
        attr("href", "#").
        attr('id', 'main-navigation-link').
        text("menu")
      ); 
      $.each(navitems, function(key, value)
      {   
        select.append(
          $("<a></a>").
          attr("href", value.href).
          text(value.innerText)
        );
      });
      holder.append(select);
      $("#header").append(holder);
      $("#main-navigation-link").click(function(){
        if(!select.hasClass('open')) {
          select.addClass('open');
          holder.stop().animate({height: 187});
        } else {
          holder.stop().animate({height: 24}, function(){
            select.removeClass('open');
          });
        }
        return false;
      });
      var search = $('<a id="top-search-button" href="#">Search</a>');
      $("#header").append(search);
      $('html').click(function(){
        holder.stop().animate({height: 24}, function(){
          select.removeClass('open');
        })
      })
      search.click(function(){
        $("#search").fadeIn();  
        return false;
      });
      if($("#sub-nav-select").length == 0 && $("#sidebar-navigation").length != 0) {
        var subnavitems = $('#sidebar-navigation > li a');
        var subselect = $('<select id="sub-nav-select"></select>');
        var ptitle = $(document).attr('title');
        ptitle = ptitle.replace(' l First Financial Bank', '').replace(' | First Financial Bank', '');
        ptitle = ptitle.split(',')[0];
        subselect.append(
          $("<option></option>").
          attr("value", "null").
          text(ptitle + " Menu")
        );
        $.each(subnavitems, function(key, value)
        {   
          subselect.append(
            $("<option></option>").
            attr("value", value.href).
            text(value.innerText)
          );
        });
        $("#three-col-nav").append(subselect);
        subselect.change(function(){
          var val = $(this).find("option:selected").val();
          if(val != "null") {
            window.location = val;
          }
        });
      } 
      $('#search-text').wrap('<span class="deleteicon" />').after($('<span/>').click(function() {
        $("#search").fadeOut();
      }));
    }
  } else {
    $('#top-nav-select').remove();
    $('#top-login-button').remove();
    $('#top-search-button').remove();
    $('#sub-nav-select').remove();
    $('.customStyleSelectBox').remove();
    if($(".deleteicon").length > 0) {
      $("#search").fadeIn();
      $('#search-text').unwrap("<span></span>").siblings("span").remove();
    }
    $('html').unbind('click');
  }

  if(width > 480 && width <= 980) {
    if($("#home-content").length > 0) {
      var containerWidth = $("#home-content").width();
      $("#home-features").width(containerWidth - 250);
    }
  } else {
    $("#home-features").attr('style', '');
  }
}

function rotateHomeNews(dir) {
  clearTimeout(homeNewsCycleTimeout);
  var topHeight = 0;
  if(dir == 1) {
    topHeight = $("#home-media-room .article-1").height() + 25;
  } else {
    topHeight = $("#home-media-room .article-0").height() + 25;
  }

  homeNewsIndex += dir;
  if(homeNewsIndex >= homeNewsCount) {
    homeNewsIndex = 0;
  }
  if(homeNewsIndex == 0 && dir == 1) {
    $("#home-media-slider").fadeOut('fast', function(){
      $(this).css('top', 0).fadeIn('fast');
    });
  } else {
    if(dir == 1) {
      $("#home-media-slider").animate({top: '-=' + topHeight}, 'slow'); 
    } else {
      $("#home-media-slider").animate({top: '+=' + topHeight}, 'slow'); 
    }
    
  }
  $('#home-media-room .article').each(function(i, e) {
    var ione = i - homeNewsIndex + 1;
    $(this).attr('class', 'article article-' + ione);
  });
  homeNewsCycleTimeout = setTimeout('rotateHomeNews(1)', 7000);
}

function showCalc(link) {
	var pWidth = (((parseInt(screen.width) / 2)) - 350)
	var pHeight = (((parseInt(screen.height) / 2)) -300)
	window.open(link.href,"calc","width=610,height=570,left=" + pWidth + ",top=" + pHeight + ",resizable=1,scrollbars=1");
	return false;
}

jQuery.fn.placeholder = function() 
{
  $('textarea').each(function()
  {
    var placeholder = $(this).attr("placeholder");
    $(this).html(placeholder);
    
    $(this).css({color:'#999999'});
    
    $(this).bind('focus', function()
    {
      if($(this).html()==placeholder)
      {
        $(this).html("");
        $(this).css({color:'#000'});
      }
    }
    )
    
    $(this).bind('blur', function()
    {
      if($(this).html()=="")
      {
        $(this).html(placeholder);
        $(this).css({color:'#999999'});
      }
    }
    )
  }
  );
  
  var counter=0;
  
  $('input').each(function()
  {
    var id = $(this).attr("id");
    var placeholder = $(this).attr("placeholder");
    
    if($(this).attr("type")=="password")
    {
      var thisdummy = 'dummy' + counter;
      
      $(this).hide();
      $(this).after('<input type="text" id="' + thisdummy + '"/>');
      $('#' + thisdummy).val(placeholder);
      $('#' + thisdummy).css({color:'#999999'});
      
      counter++;
      
      $('#' + thisdummy).bind('focus', function()
      {
        $("#" + id).show().val("");
        $("#" + id).show().focus();
        $(this).hide();
      }
      )
      
      $(this).bind('blur', function()
      {
        if($(this).val()=="")
        {
          $(this).hide();
          $("#" + thisdummy).show();
        };
      }
      )
    }
    
    if($(this).attr("type")=="text")
    {
      $(this).val(placeholder);
      $(this).css({color:'#999999'});
      
      $(this).bind('focus', function()
      {
        if($(this).val()==placeholder)
        {
          $(this).val("");
          $(this).css({color:'#000'});
        }
      }
      )
      
      $(this).bind('blur', function()
      {
        if($(this).val()=="")
        {
          $(this).val(placeholder);
          $(this).css({color:'#999999'});
        }
      }
      )
    }
  }
  );
};

/**
 * Get the value of a cookie with the given name.
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
