var Base64 = {
  // private property
  _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

  // public method for encoding
  encode : function (input) {
    var output = "";
    var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
    var i = 0;

    input = Base64._utf8_encode(input);

    while (i < input.length) {

      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) {
        enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
        enc4 = 64;
      }

      output = output +
      this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
      this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
    }
 
    return output;
  },

  // public method for decoding
  decode : function (input) {
    var output = "";
    var chr1, chr2, chr3;
    var enc1, enc2, enc3, enc4;
    var i = 0;

    input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
 
    while (i < input.length) {

      enc1 = this._keyStr.indexOf(input.charAt(i++));
      enc2 = this._keyStr.indexOf(input.charAt(i++));
      enc3 = this._keyStr.indexOf(input.charAt(i++));
      enc4 = this._keyStr.indexOf(input.charAt(i++));

      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;

      output = output + String.fromCharCode(chr1);

      if (enc3 != 64) {
        output = output + String.fromCharCode(chr2);
      }
      if (enc4 != 64) {
        output = output + String.fromCharCode(chr3);
      }

    }

    output = Base64._utf8_decode(output);

    return output;

  },
 
  // private method for UTF-8 encoding
  _utf8_encode : function (string) {
    string = string.replace(/\r\n/g,"\n");
    var utftext = "";

    for (var n = 0; n < string.length; n++) {

      var c = string.charCodeAt(n);

      if (c < 128) {
        utftext += String.fromCharCode(c);
      }
      else if((c > 127) && (c < 2048)) {
        utftext += String.fromCharCode((c >> 6) | 192);
        utftext += String.fromCharCode((c & 63) | 128);
      }
      else {
        utftext += String.fromCharCode((c >> 12) | 224);
        utftext += String.fromCharCode(((c >> 6) & 63) | 128);
        utftext += String.fromCharCode((c & 63) | 128);
      }

    }

    return utftext;
  },

  // private method for UTF-8 decoding
  _utf8_decode : function (utftext) {
    var string = "";
    var i = 0;
    var c = c1 = c2 = 0;

    while ( i < utftext.length ) {
      c = utftext.charCodeAt(i);

      if (c < 128) {
        string += String.fromCharCode(c);
        i++;
      } else if((c > 191) && (c < 224)) {
        c2 = utftext.charCodeAt(i+1);
        string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
        i += 2;
      } else {
        c2 = utftext.charCodeAt(i+1);
        c3 = utftext.charCodeAt(i+2);
        string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
        i += 3;
      }

    }

    return string;
  }
}

jQuery(function($) {
  isIE6 = function() {
    return ($.browser.msie && $.browser.version.substr(0,1) == 6);
  }

  isIE7 = function() {
    return ($.browser.msie && $.browser.version.substr(0,1) == 7);
  }

  /*
   * Fade the kid in and out
   */
  var fadeDuration = 6000;
  var fadeSpeed = 2000;
  var kidFadeTimer = null;
  var fadeIn = function() {
    kidFadeTimer = null;

    $('#portfolio-path-link img').animate({ opacity: 1 }, fadeSpeed, function() {
      kidFadeTimer = setTimeout(fadeOut, fadeDuration);
    });
  };
  var fadeOut = function() {
    kidFadeTimer = null;

    $('#portfolio-path-link img').animate({ opacity: 0 }, fadeSpeed, function() {
      kidFadeTimer = setTimeout(fadeIn, fadeDuration);
    });
  };
  
  $('#portfolio-path-link').hover(function() {
    // stop the animation and hide the kid
    if (kidFadeTimer) { clearTimeout(kidFadeTimer); }

    // stop all animations
    $('#portfolio-path-link img').stop();

    // hide the kid
    $('#portfolio-path-link img').css("opacity", 0);
  }, function() {
    // start up the timer again
    kidFadeTimer = setTimeout(fadeIn, fadeDuration);
  });

  // set the initial opacity
  $('#portfolio-path-link img').css("opacity", 0);
  $('#portfolio-path-link img').css("display", "block");
  kidFadeTimer = setTimeout(fadeIn, fadeDuration);

  /*
   * Product Dropdown -- IE < 9 doesn't support :hover on non-anchor elements
   * use jquery to 
   */
  if ($.browser.msie && $.browser.version < 9) {
    $("#products-tab").hover(function() {
      $("#products-menu").show();
    }, function() {
      $("#products-menu").hide();      
    });
  }

  /*
   * Product Accordion
   */
  $('#product-accordion .section .header .bar').click(function(event) {
    var $bar = $(this);
    var $content = $(this).parents(".section").find(".content");

    if ($content.css('display') == 'none') {
      // show
      // if ($.browser.msie) {
        // $content.toggle();
      // } else {
        $content.toggle("fast");
      // }
      $bar.addClass("bar-open")
    } else {
      // hide
      // if ($.browser.msie) {
        // $content.toggle();
      // } else {
        $content.toggle("fast");
      // }
      $bar.removeClass("bar-open")
    }
  });

  if (! isIE6()) {
    $('.contact-us').hover(function(event) {
      var $c = $(this).find('.small-circle');
      if ($.browser.msie) {
        $c.show();
      } else {
        $c.fadeIn('fast');
      }
    }, function(event) {
      var $c = $(this).find('.small-circle');
      if ($.browser.msie) {
        $c.hide();
      } else {
        $c.fadeOut('fast');
      }
    });
  }

  /*
   * Tabs & dropdown menus
   */
  var menuTimer = null;
  var currentMenu = null;

  var startTimer = function() {
    // if the current menu has a dropdown, start the timer, else close right away
    if (currentMenu.find('.submenu').size() > 0) 
      menuTimer = window.setTimeout(closeMenu, 500);
    else
      closeMenu();
  };
  var cancelTimer = function() {
    if (menuTimer) {
      window.clearTimeout(menuTimer);
      menuTimer = null;
    }
  };
  var openMenu = function(event) {
    cancelTimer();
    closeMenu();

    currentMenu = $(this).parents('.tab')

    currentMenu.find('.submenu').show();

    currentMenu.addClass('open-menu');
  };
  var closeMenu = function() {
    if (currentMenu) {
      $(currentMenu).find('.submenu').hide();
      currentMenu.removeClass('open-menu');
    }
  };

  $('#top-nav .tab-link, #top-nav .submenu').mouseover(openMenu);
  $('#top-nav .tab-link, #top-nav .submenu').mouseout(startTimer);
  $(document).click(function() {
    closeMenu();
  });

  /*
   * Driving directions tabs
   */
  $('table.directions-tabs td').hover(function(event) {
    $(this).addClass('hover');
  }, function(event) {
    $(this).removeClass('hover');
  }).click(function(event) {
    var tab = $(this).attr('-data-tab');
    var $tab = $(this);
    var $content = $('.direction-content[-data-tab=' + tab + ']');

    $('.direction-content').each(function() {
      $(this).css('display', 'none');
      $('table.directions-tabs td').removeClass('selected');
      $tab.addClass('selected');
    });

    $content.css('display', 'block');

    // if the map isn't placed on the screen, do that
    if ($content.find('iframe').size() == 0) {
      $content.html(Base64.decode($content.attr('-data-map-code')));
    }
  });

  $('.directions-tabs td:first').click();

  /*
   * General page tabs
   */
  $('table.tabs td').hover(function(event) {
    $(this).addClass('hover');
  }, function(event) {
    $(this).removeClass('hover');
  })
});
