var HQ = {
  nav: null,
  scroller: null,
  slider: null,
  floatingMenu: null,

  wrapper: null,
  sections: null,
  currentSection: null,
  touchCurrent: null,
  touchDirection: null,
  format: null,
  floatingMenuAnim: false,
  scrollingAnim: false,
  currentHash: null,
  sectionContent: {},
  sectionsLoaded: 0,

  init: function (nav, scroller, slider) {
    $.support.touch = (typeof Touch == "object");
    HQ.nav = nav;
    HQ.scroller = scroller;
    HQ.wrapper = HQ.scroller.children('.wrapper').first();
    HQ.slider = slider;
    HQ._preloadImages();
    HQ._initMainNav();
    HQ._initFloatingMenu();
    HQ._initResizeHandler();
    HQ._initScrolling();
    HQ._initHistory();
    HQ.loadSections(true);
    $(document).keyup(HQ._handleKeypress);
  },
  _preloadImages: function () {
    (new Image()).src = 'images/slider-thumb.png';
    (new Image()).src = 'images/welcome-txt-bg.png';
    (new Image()).src = 'images/section-menu-arrow.png';
    (new Image()).src = 'images/menu-work-bullet.png';
    (new Image()).src = 'images/menu-agency-bullet.png';
    (new Image()).src = 'images/menu-blog-bullet.png';
    (new Image()).src = 'images/menu-contact-bullet.png';
  },
  _initResizeHandler: function () {
    $(window).resize(HQ._handleResize);
  },
  _initMainNav: function () {
    $('.nav-link').each( function (index) {
      var href = '#/' + $(this).attr('href');
      $(this).attr('href', href);
    }
    );
  },
  _initInternalLinks: function () {
    // init internal links
    $('a[href^="#"]').each( function (index) {
      $(this).click(HQ._handleLinkClick);
    }
    );
  },
  _initFloatingMenu: function () {
    // init floating menu
    HQ.floatingMenu = $('#floating-menu');
    HQ.floatingMenu.css('top', HQ.scroller.position().top);
    if ($.support.touch) {
      HQ.floatingMenu.bind('touchend', HQ._handleSectionMenuOver);
    } else {
      HQ.floatingMenu.hover(HQ._handleSectionMenuOver, HQ._handleSectionMenuOut);
    }
  },
  _initHistory: function () {
    $(window).hashchange( function () {
      if (window.location.hash != HQ.currentHash) {
        HQ.navTo(window.location.hash);
      }
    }
    );
  },
  _initScrolling: function () {
    HQ.scroller.scroll(HQ._handleScroll);
    if ($.support.touch) {
      // init swipe event
      HQ.wrapper.bind('touchstart', HQ._handleTouchStart);
      HQ.wrapper.bind('gesturestart', function (e) {
        HQ.cancelTouchEvent();
      }
      );
      $(document.documentElement).append("<div id='touch-note'>Swipe across the content to scroll left or right</div>");
    } else {
      HQ.slider.slider({
        value:0,
        min: 0,
        max: 0,
        step: 1,
        slide: function( event, ui ) {
          HQ.scroller.scrollLeft(ui.value);
        },
        stop: function (event, ui) {
          var lastSection = HQ.wrapper.children().last();
          if (ui.value > lastSection.position().left) {
            HQ.scroller.animate({scrollLeft: lastSection.position().left}, 100);
          }
        }
      });
    }
  },
  _calcScrollingSections: function () {
    var wrapperWidth = 0;
    var sectionWidths = [];
    HQ.sections = HQ.wrapper.children();
    HQ.sections.each( function (index) {
      sectionWidths[index] = $(this).width();
      wrapperWidth += $(this).width();
    }
    );
    // init section wrapper
    var netWrapperWidth = wrapperWidth + $(window).width() + (5 * HQ.wrapper.children().length - 1) + 100;
    HQ.wrapper.width(netWrapperWidth);
    if (!$.support.touch) {
      // init slider
      var track = HQ.slider.children('.track').first();
      track.children().each( 
        function (index) {
          $(this).css('width', ((sectionWidths[index] / wrapperWidth) * 100) + '%');
        }
      );
      HQ.slider.slider('option', 'max', wrapperWidth + (5 * HQ.wrapper.children().length - 1));
    }
  },
  _initPeople: function () {
    if ($.support.touch) {
      // init touch on people
      $('#agency_people .people .person').bind('touchend', function () {
        var detail = $(this).children('.detail').first();
        if (detail.hasClass('hover')) {
          detail.animate({top: $(this).height() - 64}, 200);
          detail.removeClass('hover');
        } else {
          detail.animate({top: $(this).height() - detail.height() - 15}, 200);
          detail.addClass('hover');
        }
      }
      );
    } else {
      // init hover on people
      $('#agency_people .people .person').hover( function () {
        var detail = $(this).children('.detail');
        detail.animate({top: $(this).height() - detail.height() - 15}, 200);
        detail.addClass('hover');
      }, function () {
        var detail = $(this).children('.detail');
        detail.animate({top: $(this).height() - 64}, 200);
        detail.removeClass('hover');
      }
      );
    }
  },
  _initLogoLinks: function () {
    $('#client-logos a[href]').hover( function () {
      $(this).css('background-position', '0px -' + $(this).height() + 'px');
    }, function () {
      $(this).css('background-position', '0px 0px');
    }
    );
  },
  _initBlog: function () {
    var maxHeight = HQ.format == 'tall' ? 500 : 350;
    $('.blog-post').each( function (i) {
      if ($(this).height() > maxHeight) {
        HQ._truncateDivText($(this));
      }
    }
    );
  },
  _truncateDivText: function (div) {
    var maxHeight = HQ.format == 'tall' ? 500 : 350;
    var link = div.find('h3 a').eq(0);
    div.children('p, div').each( function (j) {
      var top = $(this).position().top
      if (top > maxHeight) {
        $(this).remove();
        return;
      }
      while (top + $(this).height() > maxHeight) {
        var text = $(this).text();
        if (text == '' || text.length <= 30) {
          $(this).remove();
        }
        var i = text.length - 30;
        var c = null;
        while (c != ' ' && i > 0) {
          i = i - 1;
          c = text[i];
        }
        var newText = text.substring(0, i) + ' ... ';
        if (newText == '') {
          break;
        }
        $(this).text(newText);
      }
    }
    );
  },
  _initMap: function () {
    var style = {
      'Map': [
        {
          featureType: "all",
          elementType: "all",
          stylers: [
            { saturation: -100 }
          ]
        },{
          featureType: "all",
          elementType: "all",
          stylers: [
            { visibility: "simplified" }
          ]
        },{
          featureType: "poi",
          elementType: "labels",
          stylers: [
            { lightness: 0 },
            { visibility: "off" }
          ]
        },{
          featureType: "road",
          elementType: "labels",
          stylers: [
            { lightness: 25 },
            { visibility: "on" }
          ]
        }
      ]
    };
    var myOptions = {
      zoom: 16,
      center: new google.maps.LatLng(39.099335,-84.516216),
      mapTypeId: 'Map',
      mapTypeControl: true,
      mapTypeControlOptions: {
        mapTypeIds: ['Map', google.maps.MapTypeId.SATELLITE],
        style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
        position: google.maps.ControlPosition.TOP_RIGHT
      },
      scaleControl: false,
      scaleControlOptions: {
        position: google.maps.ControlPosition.TOP_LEFT
      },
      panControl: true,
      panControlOptions: {
        position: google.maps.ControlPosition.LEFT_TOP
      },
      zoomControl: true,
      zoomControlOptions: {
        style: google.maps.ZoomControlStyle.LARGE,
        position: google.maps.ControlPosition.LEFT_TOP
      },
      streetViewControl: true,
      streetViewControlOptions: {
        position: google.maps.ControlPosition.LEFT_TOP
      }
    }
    var mapCanvas = document.getElementById("map-canvas");
    $(mapCanvas).css('width', HQ.scroller.width() - 235);
    var map = new google.maps.Map(mapCanvas, myOptions);
    var styledMapType = new google.maps.StyledMapType(style['Map'], {name: 'Map'});
    map.mapTypes.set('Map', styledMapType);
    HQ._initMapMarkers(map);
  },
  _initMapMarkers: function(map) {
    var markers = [];
    markers.push({
      info: '<div id="map-infowindow">' +
            '<img src="images/hq-logo-map.png" alt="Hyperquake" />' +
            '<p>' +
            '205 West Fourth Street<br/>' +
            'Suite 1010<br/>' +
            'Cincinnati, OH 45202' +
            '</p>' +
            '<p>' +
            'p: 513.563.6555<br/>' +
            'f: 513.563.6080' +
            '</p>' +
            '<p style="margin-bottom: 0"><a target="_blank" href="http://www.google.com/maps?f=d&source=s_q&hl=en&geocode=&q=Hyperquake,+Cincinnati,+OH&aq=0&sll=37.0625,-95.677068&sspn=53.87374,114.169922&ie=UTF8&hq=Hyperquake,&hnear=Cincinnati,+Hamilton,+Ohio&t=h&z=16&iwloc=A">Get directions &gt;</a></p>' +
            '</div>',
      coords: new google.maps.LatLng(39.099335,-84.516216),
      type: 'hq'
    });
    markers.push({
      info: '<div id="map-infowindow">' +
            '<h2 class="map-info-header">Starbucks</h2>' +
            '<p>' +
            '401 Vine Street‬<br/>' +
            'Cincinnati, OH 45202' +
            '</p>' +
            '<p><a href="http://www.starbucks.com">www.starbucks.com</a></p>' +
            '<p style="margin-bottom: 0"><a target="_blank" href="http://maps.google.com/maps?q=401+Vine+Street%E2%80%AC,+%E2%80%AACincinnati,+OH+45202%E2%80%AC&hl=en&ie=UTF8&sll=37.09024,-99.931641&sspn=55.806079,76.201172&t=h&z=17">Get directions &gt;</a></p>' +
            '</div>',
      coords: new google.maps.LatLng(39.1000563, -84.5128786),
      type: 'dining'
    });
    markers.push({
      info: '<div id="map-infowindow">' +
            '<h2 class="map-info-header">Great American Ballpark</h2>' +
            '<p>' +
            '100 Joe Nuxhall Way‬<br/>' +
            'Cincinnati, OH 45202' +
            '</p>' +
            '<p><a href="http://reds.mlb.com">reds.mlb.com</a></p>' +
            '<p style="margin-bottom: 0"><a target="_blank" href="http://maps.google.com/maps?q=100+Joe+Nuxhall+Way%E2%80%AC,+%E2%80%AACincinnati,+Ohio+45202%E2%80%AC&hl=en&ie=UTF8&ll=39.097428,-84.508692&spn=0.006761,0.009302&sll=39.100059,-84.512876&sspn=0.006761,0.009302&t=h&z=17">Get directions &gt;</a></p>' +
            '</div>',
      coords: new google.maps.LatLng(39.0974324, -84.5086916),
      type: 'attraction'
    });
    markers.push({
      info: '<div id="map-infowindow">' +
            '<h2 class="map-info-header">Knockback Nat\'s</h2>' +
            '<p>' +
            '10 West 7th Street<br/>' +
            'Cincinnati, OH 45202' +
            '</p>' +
            '<p><a href="http://www.facebook.com/pages/Knockback-Nats-Neighborhood-Bar/146296676362?sk=info#!/pages/Knockback-Nats-Neighborhood-Bar/146296676362?sk=wall">Facebook page</a></p>' +
            '<p style="margin-bottom: 0"><a target="_blank" href="http://maps.google.com/maps?q=10+West+7th+Street,+Cincinnati,+OH&hl=en&ll=39.103731,-84.514132&spn=0.00676,0.009302&sll=39.097428,-84.508692&sspn=0.006761,0.009302&t=h&z=17">Get directions &gt;</a></p>' +
            '</div>',
      coords: new google.maps.LatLng(39.1037334, -84.5141292),
      type: 'night-life'
    });
    markers.push({
      info: '<div id="map-infowindow">' +
            '<h2 class="map-info-header">Coffee Emporium</h2>' +
            '<p>' +
            '110 East Central Parkway‬<br/>' +
            'Cincinnati, OH 45202' +
            '</p>' +
            '<p><a href="http://www.coffee-emporium.com">www.coffee-emporium.com</a></p>' +
            '<p style="margin-bottom: 0"><a target="_blank" href="">Get directions &gt;</a></p>' +
            '</div>',
      coords: new google.maps.LatLng(39.1076866, -84.5125180),
      type: 'dining'
    });
    markers.push({
      info: '<div id="map-infowindow">' +
            '<h2 class="map-info-header">Sung Korean Bistro</h2>' +
            '<p>' +
            '700 Elm Street‬<br/>' +
            'Cincinnati, OH 45202' +
            '</p>' +
            '<p><a href="http://www.sungkoreanbistro.com‬‎">sungkoreanbistro.com‬‎</a></p>' +
            '<p style="margin-bottom: 0"><a target="_blank" href="http://maps.google.com/maps?q=700+Elm+Street%E2%80%AC,+%E2%80%AACincinnati,+OH+45202%E2%80%AC&hl=en&ie=UTF8&ll=39.103332,-84.516728&spn=0.00676,0.009302&sll=39.103731,-84.514132&sspn=0.00676,0.009302&t=h&z=17">Get directions &gt;</a></p>' +
            '</div>',
      coords: new google.maps.LatLng(39.1033286, -84.5167302),
      type: 'dining'
    });
    markers.push({
      info: '<div id="map-infowindow">' +
            '<h2 class="map-info-header">Taqueria Mercado</h2>' +
            '<p>' +
            '100 East 8th Street‬<br/>' +
            'Cincinnati, OH 45202' +
            '</p>' +
            '<p><a href="http://www.taqueriamercado.com‬‎">taqueriamercado.com‬‎</a></p>' +
            '<p style="margin-bottom: 0"><a target="_blank" href="http://maps.google.com/maps?q=100+East+8th+Street%E2%80%AC,+%E2%80%AACincinnati,+OH+45202%E2%80%AC&hl=en&ie=UTF8&ll=39.104855,-84.512082&spn=0.00676,0.009302&sll=39.103332,-84.516728&sspn=0.00676,0.009302&t=h&z=17">Get directions &gt;</a></p>' +
            '</div>',
      coords: new google.maps.LatLng(39.1048513, -84.5120826),
      type: 'dining'
    });
    markers.push({
      info: '<div id="map-infowindow">' +
            '<h2 class="map-info-header">Cincinnati Contemporary Arts Center</h2>' +
            '<p>' +
            '44 E 6th Street<br/>' +
            'Cincinnati, OH 45202' +
            '</p>' +
            '<p><a href="http://www.contemporaryartscenter.org‎">contemporaryartscenter.org‎</a></p>' +
            '<p style="margin-bottom: 0"><a target="_blank" href="http://maps.google.com/maps?q=44+E+6th+Street,+Cincinnati,+OH+45202&hl=en&ll=39.102699,-84.512061&spn=0.006761,0.009302&sll=39.104855,-84.512082&sspn=0.00676,0.009302&t=h&z=17">Get directions &gt;</a></p>' +
            '</div>',
      coords: new google.maps.LatLng(39.1027005, -84.5120601),
      type: 'attraction'
    });
    markers.push({
      info: '<div id="map-infowindow">' +
            '<h2 class="map-info-header">Skyline Chili</h2>' +
            '<p>' +
            '643 Vine Street<br/>' +
            'Cincinnati, OH 45202' +
            '</p>' +
            '<p><a href="http://www.skylinechili.com‎">skylinechili.com‎</a></p>' +
            '<p style="margin-bottom: 0"><a target="_blank" href="http://maps.google.com/maps?q=643+Vine+Street,+Cincinnati,+OH&hl=en&ll=39.10299,-84.513466&spn=0.00676,0.009302&sll=39.102699,-84.512061&sspn=0.006761,0.009302&t=h&z=17">Get directions &gt;</a></p>' +
            '</div>',
      coords: new google.maps.LatLng(39.1029919, -84.5134696),
      type: 'dining'
    });
    markers.push({
      info: '<div id="map-infowindow">' +
            '<h2 class="map-info-header">Skyline Chili</h2>' +
            '<p>' +
            '254 East 4th Street<br/>' +
            'Cincinnati, OH 45202' +
            '</p>' +
            '<p><a href="http://www.skylinechili.com‎">skylinechili.com‎</a></p>' +
            '<p style="margin-bottom: 0"><a target="_blank" href="http://maps.google.com/maps?q=254+East+4th+Street,+Cincinnati,+OH&hl=en&ll=39.100576,-84.508467&spn=0.006761,0.009302&sll=39.10299,-84.513466&sspn=0.00676,0.009302&t=h&z=17">Get directions &gt;</a></p>' +
            '</div>',
      coords: new google.maps.LatLng(39.1005721, -84.5084692),
      type: 'dining'
    });
    markers.push({
      info: '<div id="map-infowindow">' +
            '<h2 class="map-info-header">Skyline Chili</h2>' +
            '<p>' +
            '1001 Vine Street<br/>' +
            'Cincinnati, OH 45202' +
            '</p>' +
            '<p><a href="http://www.skylinechili.com‎">skylinechili.com‎</a></p>' +
            '<p style="margin-bottom: 0"><a target="_blank" href="http://maps.google.com/maps?q=1001+Vine+Street,+Cincinnati,+OH&hl=en&ll=39.106279,-84.514271&spn=0.00676,0.009302&sll=39.100576,-84.508467&sspn=0.006761,0.009302&t=h&z=17">Get directions &gt;</a></p>' +
            '</div>',
      coords: new google.maps.LatLng(39.1062769, -84.5142706),
      type: 'dining'
    });
    markers.push({
      info: '<div id="map-infowindow">' +
            '<h2 class="map-info-header">Fountain News</h2>' +
            '<p>' +
            '101 East 5th Street<br/>' +
            'Cincinnati, OH 45202' +
            '</p>' +
            '<p><a href="http://www.yelp.com/biz/fountain-news-cincinnati-2">Yelp listing</a></p>' +
            '<p style="margin-bottom: 0"><a target="_blank" href="http://maps.google.com/maps?q=101+East+5th+Street,+Cincinnati,+OH&hl=en&ll=39.101217,-84.511138&spn=0.006761,0.009302&sll=39.106279,-84.514271&sspn=0.00676,0.009302&t=h&z=17">Get directions &gt;</a></p>' +
            '</div>',
      coords: new google.maps.LatLng(39.1012146, -84.5111410),
      type: 'shopping'
    });
    markers.push({
      info: '<div id="map-infowindow">' +
            '<h2 class="map-info-header">Mica 12/v</h2>' +
            '<p>' +
            '1201 Vine Street<br/>' +
            'Cincinnati, OH 45202' +
            '</p>' +
            '<p><a href="http://shopmica.com">shopmica.com</a></p>' +
            '<p style="margin-bottom: 0"><a target="_blank" href="http://maps.google.com/maps?q=1201+Vine+Street,+Cincinnati,+OH&hl=en&sll=39.101217,-84.511138&sspn=0.006761,0.009302&t=h&z=17">Get directions &gt;</a></p>' +
            '</div>',
      coords: new google.maps.LatLng(39.1083640, -84.5149360),
      type: 'shopping'
    });
    markers.push({
      info: '<div id="map-infowindow">' +
            '<h2 class="map-info-header">Chick-Fil-A</h2>' +
            '<p>' +
            '28 W 4th St Ste 127<br/>' +
            'Cincinnati, OH 45202' +
            '</p>' +
            '<p><a href="http://chick-fil-a.com‎">chick-fil-a.com‎</a></p>' +
            '<p style="margin-bottom: 0"><a target="_blank" href="http://maps.google.com/maps?q=28+W+4th+St+Ste+127,+Cincinnati,+OH+45202&hl=en&sll=39.108364,-84.514936&sspn=0.00676,0.009302&t=h&z=17">Get directions &gt;</a></p>' +
            '</div>',
      coords: new google.maps.LatLng(39.0997981, -84.5138759),
      type: 'dining'
    });
    markers.push({
      info: '<div id="map-infowindow">' +
            '<h2 class="map-info-header">Koch\'s Sporting Goods</h2>' +
            '<p>' +
            '131 W. 4th Street<br/>' +
            'Cincinnati, OH 45202' +
            '</p>' +
            '<p><a href="http://kochsports.com‎">kochsports.com‎</a></p>' +
            '<p style="margin-bottom: 0"><a target="_blank" href="http://maps.google.com/maps?q=131+W.+4th+Street,+Cincinnati,+OH+45202&hl=en&ll=39.099585,-84.515194&spn=0.006761,0.009302&sll=39.099798,-84.513876&sspn=0.006761,0.009302&t=h&z=17">Get directions &gt;</a></p>' +
            '</div>',
      coords: new google.maps.LatLng(39.0995859, -84.5151944),
      type: 'shopping'
    });
    markers.push({
      info: '<div id="map-infowindow">' +
            '<h2 class="map-info-header">Terry\'s Turf Club</h2>' +
            '<p>' +
            '4618 Eastern Ave<br/>' +
            'Cincinnati, OH 45226' +
            '</p>' +
            '<p><a href="http://yelp.com/biz/terrys-turf-club-cincinnati">Yelp listing</a></p>' +
            '<p style="margin-bottom: 0"><a target="_blank" href="http://maps.google.com/maps?q=4618+Eastern+Ave,+Cincinnati,+OH+45226&hl=en&sll=39.099585,-84.515194&sspn=0.006761,0.009302&t=h&z=17">Get directions &gt;</a></p>' +
            '</div>',
      coords: new google.maps.LatLng(39.1182779, -84.4192640),
      type: 'dining'
    });
    markers.push({
      info: '<div id="map-infowindow">' +
            '<h2 class="map-info-header">Nada</h2>' +
            '<p>' +
            '600 Walnut Street<br/>' +
            'Cincinnati, OH 45202' +
            '</p>' +
            '<p><a href="http://eatdrinknada.com‎">eatdrinknada.com‎</a></p>' +
            '<p style="margin-bottom: 0"><a target="_blank" href="http://maps.google.com/maps?q=600+Walnut+Street,+Cincinnati,+OH+45202&hl=en&sll=39.118278,-84.419264&sspn=0.006759,0.009302&t=h&z=17">Get directions &gt;</a></p>' +
            '</div>',
      coords: new google.maps.LatLng(39.1029690, -84.5114120),
      type: 'dining'
    });
    markers.push({
      info: '<div id="map-infowindow">' +
            '<h2 class="map-info-header">Park + Vine</h2>' +
            '<p>' +
            '1202 Main Street<br/>' +
            'Cincinnati, OH 45202' +
            '</p>' +
            '<p><a href="http://parkandvine.com‎">parkandvine.com‎</a></p>' +
            '<p style="margin-bottom: 0"><a target="_blank" href="http://maps.google.com/maps?q=1202+Main+Street,+Cincinnati,+OH+45202&hl=en&ll=39.109376,-84.511642&spn=0.00676,0.009302&sll=39.102969,-84.511412&sspn=0.00676,0.009302&t=h&z=17">Get directions &gt;</a></p>' +
            '</div>',
      coords: new google.maps.LatLng(39.1093735, -84.5116443),
      type: 'shopping'
    });
    markers.push({
      info: '<div id="map-infowindow">' +
            '<h2 class="map-info-header">Findlay Market</h2>' +
            '<p>' +
            '1801 Race Street<br/>' +
            'Cincinnati, OH 45202' +
            '</p>' +
            '<p><a href="http://findlaymarket.org">findlaymarket.org</a></p>' +
            '<p style="margin-bottom: 0"><a target="_blank" href="http://maps.google.com/maps?q=1801+Race+Street,+Cincinnati,+OH+45202&hl=en&ll=39.115436,-84.518262&spn=0.006759,0.009302&sll=39.109376,-84.511642&sspn=0.00676,0.009302&t=h&z=17">Get directions &gt;</a></p>' +
            '</div>',
      coords: new google.maps.LatLng(39.1154382, -84.5182592),
      type: 'shopping'
    });
    markers.push({
      info: '<div id="map-infowindow">' +
            '<h2 class="map-info-header">Sarah Center</h2>' +
            '<p>' +
            '1618 Vine Street<br/>' +
            'Cincinnati, OH 45202' +
            '</p>' +
            '<p><a href="http://facebook.com/SarahCenter">Facebook page</a></p>' +
            '<p style="margin-bottom: 0"><a target="_blank" href="http://maps.google.com/maps?q=1618+Vine+Street,+Cincinnati,+OH+45202&hl=en&ll=39.11363,-84.515795&spn=0.006759,0.009302&sll=39.115436,-84.518262&sspn=0.006759,0.009302&t=h&z=17">Get directions &gt;</a></p>' +
            '</div>',
      coords: new google.maps.LatLng(39.1136259, -84.5157920),
      type: 'attraction'
    });
    markers.push({
      info: '<div id="map-infowindow">' +
            '<h2 class="map-info-header">Arnold\'s Bar and Grill</h2>' +
            '<p>' +
            '210 East 8th Street<br/>' +
            'Cincinnati, OH 45202' +
            '</p>' +
            '<p><a href="http://arnoldsbarandgrill.com‎">arnoldsbarandgrill.com‎</a></p>' +
            '<p style="margin-bottom: 0"><a target="_blank" href="http://maps.google.com/maps?q=210+East+8th+Street,+Cincinnati,+OH&hl=en&sll=39.11363,-84.515795&sspn=0.006759,0.009302&t=h&z=17">Get directions &gt;</a></p>' +
            '</div>',
      coords: new google.maps.LatLng(39.1051730, -84.5101229),
      type: 'night-life'
    });
    markers.push({
      info: '<div id="map-infowindow">' +
            '<h2 class="map-info-header">Ludlow Bromley Yacht Club</h2>' +
            '<p>' +
            '860 Elm Street<br/>' +
            'Ludlow, KY 41016' +
            '</p>' +
            '<p><a href="http://yelp.com/biz/ludlow-bromley-yacht-club-ludlow">Yelp Listing</a></p>' +
            '<p style="margin-bottom: 0"><a target="_blank" href="http://maps.google.com/maps?q=860+Elm+Street,+Ludlow,+KY&hl=en&sll=39.105173,-84.510123&sspn=0.00676,0.009302&t=h&z=17">Get directions &gt;</a></p>' +
            '</div>',
      coords: new google.maps.LatLng(39.0859090, -84.5601110),
      type: 'night-life'
    });
    markers.push({
      info: '<div id="map-infowindow">' +
            '<h2 class="map-info-header">A Tavola</h2>' +
            '<p>' +
            '1220 Vine Street<br/>' +
            'Cincinnati, OH 45202' +
            '</p>' +
            '<p><a href="http://atavolapizza.com">atavolapizza.com</a></p>' +
            '<p style="margin-bottom: 0"><a target="_blank" href="http://maps.google.com/maps?q=1220+Vine+Street,+Cincinnati,+OH&hl=en&sll=39.085909,-84.560111&sspn=0.006762,0.009302&t=h&z=17">Get directions &gt;</a></p>' +
            '</div>',
      coords: new google.maps.LatLng(39.1088781, -84.5148694),
      type: 'dining'
    });
    markers.push({
      info: '<div id="map-infowindow">' +
            '<h2 class="map-info-header">Chez Nora</h2>' +
            '<p>' +
            '530 Main Street<br/>' +
            'Covington, KY 41011' +
            '</p>' +
            '<p><a href="http://cheznora.com‎">cheznora.com‎</a></p>' +
            '<p style="margin-bottom: 0"><a target="_blank" href="http://maps.google.com/maps?q=530+Main+Street,+Covington,+KY&hl=en&sll=39.108878,-84.514869&sspn=0.00676,0.009302&t=h&z=17">Get directions &gt;</a></p>' +
            '</div>',
      coords: new google.maps.LatLng(39.0844000, -84.5172500),
      type: 'dining'
    });
    markers.push({
      info: '<div id="map-infowindow">' +
            '<h2 class="map-info-header">Bar Louie</h2>' +
            '<p>' +
            '1 Levee Way #3118<br/>' +
            'Newport, KY 41071' +
            '</p>' +
            '<p><a href="http://cbarlouieamerica.com‎">barlouieamerica.com‎</a></p>' +
            '<p style="margin-bottom: 0"><a target="_blank" href="http://maps.google.com/maps?q=1+Levee+Way+%233118,+Newport,+KY&hl=en&sll=39.0844,-84.51725&sspn=0.006762,0.009302&t=h&z=15">Get directions &gt;</a></p>' +
            '</div>',
      coords: new google.maps.LatLng(39.0952960, -84.4958280),
      type: 'night-life'
    });
    markers.push({
      info: '<div id="map-infowindow">' +
            '<h2 class="map-info-header">O\'Malleys In the Alley</h2>' +
            '<p>' +
            '25 Ogden Place<br/>' +
            'Cincinnati, OH 45202' +
            '</p>' +
            '<p><a href="http://yelp.com/biz/omalleys-in-the-alley-cincinnati">Yelp listing</a></p>' +
            '<p style="margin-bottom: 0"><a target="_blank" href="http://maps.google.com/maps?q=25+Ogden+Place,+Cincinnati,+OH+45202&hl=en&sll=39.095296,-84.495828&sspn=0.027045,0.037208&t=h&z=17">Get directions &gt;</a></p>' +
            '</div>',
      coords: new google.maps.LatLng(39.0993841, -84.5114297),
      type: 'night-life'
    });
    markers.push({
      info: '<div id="map-infowindow">' +
            '<h2 class="map-info-header">Neon\'s</h2>' +
            '<p>' +
            '208 E. 12th Street<br/>' +
            'Cincinnati, OH 45202' +
            '</p>' +
            '<p><a href="http://neons-unplugged.com">neons-unplugged.com</a></p>' +
            '<p style="margin-bottom: 0"><a target="_blank" href="http://maps.google.com/maps?q=208+E.+12th+Street,+Cincinnati,+OH+45202&hl=en&sll=39.099384,-84.51143&sspn=0.006761,0.009302&t=h&z=17">Get directions &gt;</a></p>' +
            '</div>',
      coords: new google.maps.LatLng(39.1094160, -84.5111970),
      type: 'night-life'
    });
    markers.push({
      info: '<div id="map-infowindow">' +
            '<h2 class="map-info-header">Fountain Square</h2>' +
            '<p>' +
            '520 Vine Street<br/>' +
            'Cincinnati, OH 45202' +
            '</p>' +
            '<p><a href="http://myfountainsquare.com">myfountainsquare.com</a></p>' +
            '<p style="margin-bottom: 0"><a target="_blank" href="http://maps.google.com/maps?q=520+Vine+Street,+Cincinnati,+OH&hl=en&sll=39.105588,-84.511772&sspn=0.01352,0.018604&t=h&z=17">Get directions &gt;</a></p>' +
            '</div>',
      coords: new google.maps.LatLng(39.1016020, -84.5128070),
      type: 'attraction'
    });
    markers.push({
      info: '<div id="map-infowindow">' +
            '<h2 class="map-info-header">Tina\'s</h2>' +
            '<p>' +
            '350 West 4th Street<br/>' +
            'Cincinnati, OH 45202' +
            '</p>' +
            '<p><a href="http://yelp.com/biz/tinas-cincinnati">Yelp listing</a></p>' +
            '<p style="margin-bottom: 0"><a target="_blank" href="http://maps.google.com/maps?q=350+West+4th+Street,+Cincinnati,+OH&hl=en&ll=39.099077,-84.518466&spn=0.006761,0.009302&sll=39.101602,-84.512807&sspn=0.006761,0.009302&t=h&z=17">Get directions &gt;</a></p>' +
            '</div>',
      coords: new google.maps.LatLng(39.0990770, -84.5184660),
      type: 'night-life'
    });
    markers.push({
      info: '<div id="map-infowindow">' +
            '<h2 class="map-info-header">Lackman</h2>' +
            '<p>' +
            '1237 Vine Street<br/>' +
            'Cincinnati, OH 45202' +
            '</p>' +
            '<p><a href="http://lackmanbar.com">lackmanbar.com</a></p>' +
            '<p style="margin-bottom: 0"><a target="_blank" href="http://maps.google.com/maps?q=1237+Vine+Street,+Cincinnati,+OH&hl=en&sll=39.099077,-84.518466&sspn=0.006761,0.009302&t=h&z=17">Get directions &gt;</a></p>' +
            '</div>',
      coords: new google.maps.LatLng(39.1093100, -84.5153210),
      type: 'night-life'
    });
    markers.push({
      info: '<div id="map-infowindow">' +
            '<h2 class="map-info-header">Japp\'s</h2>' +
            '<p>' +
            '1134 Main Street<br/>' +
            'Cincinnati, OH 45202' +
            '</p>' +
            '<p><a href="http://japps1879.com">japps1879.com</a></p>' +
            '<p style="margin-bottom: 0"><a target="_blank" href="http://maps.google.com/maps?q=1134+Main+Street,+Cincinnati,+OH&hl=en&ll=39.108851,-84.511288&spn=0.00676,0.009302&sll=39.10931,-84.515321&sspn=0.00676,0.009302&t=h&z=17">Get directions &gt;</a></p>' +
            '</div>',
      coords: new google.maps.LatLng(39.1088490, -84.5112920),
      type: 'night-life'
    });
    markers.push({
      info: '<div id="map-infowindow">' +
            '<h2 class="map-info-header">MOTR</h2>' +
            '<p>' +
            '1345 Main Street<br/>' +
            'Cincinnati, OH 45202' +
            '</p>' +
            '<p><a href="http://japps1879.com">japps1879.com</a></p>' +
            '<p style="margin-bottom: 0"><a target="_blank" href="http://maps.google.com/maps?q=1345+Main+Street,+Cincinnati,+OH&hl=en&ll=39.111182,-84.512168&spn=0.00676,0.009302&sll=39.108851,-84.511288&sspn=0.00676,0.009302&t=h&z=17">Get directions &gt;</a></p>' +
            '</div>',
      coords: new google.maps.LatLng(39.1111863, -84.5121668),
      type: 'night-life'
    });
    markers.push({
      info: '<div id="map-infowindow">' +
            '<h2 class="map-info-header">Newport on the Levee</h2>' +
            '<p>' +
            '1 Levee Way<br/>' +
            'Newport, KY 41071' +
            '</p>' +
            '<p><a href="http://newportonthelevee.com">newportonthelevee.com</a></p>' +
            '<p style="margin-bottom: 0"><a target="_blank" href="http://maps.google.com/maps?q=1+Levee+Way,+Newport,+KY+41071&hl=en&sll=39.111182,-84.512168&sspn=0.00676,0.009302&t=h&z=15">Get directions &gt;</a></p>' +
            '</div>',
      coords: new google.maps.LatLng(39.0952960, -84.4958280),
      type: 'attraction'
    });
    markers.push({
      info: '<div id="map-infowindow">' +
            '<h2 class="map-info-header">Senate</h2>' +
            '<p>' +
            '1212 Vine St<br/>' +
            'Cincinnati, OH 45202' +
            '</p>' +
            '<p><a href="http://senatepub.com‎">senatepub.com‎</a></p>' +
            '<p style="margin-bottom: 0"><a target="_blank" href="http://maps.google.com/maps?q=1212+Vine+St,+Cincinnati,+OH&hl=en&sll=39.095296,-84.495828&sspn=0.027045,0.037208&t=h&z=17">Get directions &gt;</a></p>' +
            '</div>',
      coords: new google.maps.LatLng(39.1087420, -84.5145860),
      type: 'dining'
    });
    markers.push({
      info: '<div id="map-infowindow">' +
            '<h2 class="map-info-header">Shanghai Mama\'s</h2>' +
            '<p>' +
            '216 East 6th Street<br/>' +
            'Cincinnati, OH 45202' +
            '</p>' +
            '<p><a href="http://shanghaimamas.com‎">shanghaimamas.com‎</a></p>' +
            '<p style="margin-bottom: 0"><a target="_blank" href="http://maps.google.com/maps?q=216+East+6th+Street,+Cincinnati,+OH&hl=en&ll=39.103265,-84.509422&spn=0.00676,0.009302&sll=39.108742,-84.514586&sspn=0.00676,0.009302&t=h&z=17">Get directions &gt;</a></p>' +
            '</div>',
      coords: new google.maps.LatLng(39.1032670, -84.5094230),
      type: 'dining'
    });
    markers.push({
      info: '<div id="map-infowindow">' +
            '<h2 class="map-info-header">Pho Lang Thang</h2>' +
            '<p>' +
            '114 West Elder Street<br/>' +
            'Cincinnati, OH 45202' +
            '</p>' +
            '<p><a href="http://yelp.com/biz/pho-lang-thang-cincinnati">Yelp listing</a></p>' +
            '<p style="margin-bottom: 0"><a target="_blank" href="http://maps.google.com/maps?q=114+W.+Elder+St.,+Cincinnati,+OH&hl=en&sll=39.103265,-84.509422&sspn=0.00676,0.009302&t=h&z=17">Get directions &gt;</a></p>' +
            '</div>',
      coords: new google.maps.LatLng(39.1154816, -84.5188911),
      type: 'dining'
    });

    var infowindow = new google.maps.InfoWindow({
      content: "holding..."
    });

    var item = null;
    for(var i in markers)
    { 
      item = markers[i];
      var marker = HQ._createMapMarker(map, item.info, item.coords, item.type);
      google.maps.event.addListener(marker, 'click', function() {
        infowindow.setContent(this.html);
        infowindow.open(map, this);
      });
    }
  },
  _createMapMarker: function(map, info, coords, type) {
    var markerImage = "";
    var markerShadow = "images/place-marker-shadow.png"
    switch(type) {
      case 'dining':
        markerImage = "images/place-dining-marker.png";
        break;
      case 'attraction':
        markerImage = "images/place-attraction-marker.png";
        break;
      case 'shopping':
        markerImage = "images/place-shopping-marker.png";
        break;
      case 'night-life':
        markerImage = "images/place-night-life-marker.png";
        break;
      default:
        markerImage = "images/hq-marker.png";
        markerShadow = "images/hq-marker-shadow.png";
    }
    return new google.maps.Marker({
      position: coords,
      map: map,
      shadow: markerShadow,
      icon: markerImage,
      html: info
    });
  },
  navTo: function (href) {
    try {
      var pageTracker = _gat._getTracker("UA-7236318-1");
      pageTracker._trackPageview(href);
    } catch(err) {
    }
    if (href == '#' || href == '#/') {
      HQ.scroller.animate({scrollLeft: 0}, 300);
      window.location.hash = HQ.currentHash = '';
    } else if (href.lastIndexOf('/') > 1) {
      var id = href.replace(/\//g, "_");
      var target = $('#' + id.substr(2));
      var scrollX = $(target).position().left;
      scrollX += $(target).parent().position().left;
      HQ.scroller.animate({scrollLeft: scrollX - 20}, 300);
      window.location.hash = HQ.currentHash = href;
    } else {
      var id = href.replace(/\//g, "_");
      var target = $('#' + id.substr(2));
      var scrollX = $(target).position().left;
      HQ.scroller.animate({scrollLeft: scrollX}, 300);
      window.location.hash = HQ.currentHash = href;
    }
  },
  hideFloatingMenu: function () {
    if (HQ.floatingMenuAnim)
      return;
    HQ.floatingMenuAnim = true;
    HQ.floatingMenu.animate({left: -180}, 100, function () {
      HQ.floatingMenuAnim = false;
    }
    );
    $('#floating-menu-arrow').show();
  },
  showFloatingMenu: function () {
    if (HQ.floatingMenuAnim)
      return;
    HQ.floatingMenuAnim = true;
    HQ.floatingMenu.animate({left: 0}, 100, function () {
      HQ.floatingMenuAnim = false;
    }
    );
    $('#floating-menu-arrow').hide();
  },
  loadSections: function () {
    HQ.format = $(window).height() > 800 ? 'tall' : 'short';
    if(HQ.format == "short") {
    	$('body').addClass('short');
    } else {
    	$('body').removeClass('short');
    }
    HQ.scroller.addClass('loader');
    HQ.floatingMenu.empty();
    HQ.floatingMenu.css('display', 'none');
    HQ.wrapper.empty();
    HQ.hideFloatingMenu
    HQ.sectionsLoaded = 0;
    HQ.loadSection('welcome');
    HQ.loadSection('work');
    HQ.loadSection('agency');
    HQ.loadSection('blog');
    HQ.loadSection('contact');
  },
  loadSection: function (id) {
    var url = '/_' + id + '/include.php';
    $.get(url, {format: HQ.format}, HQ._handleSectionLoaded, 'html');
  },
  _handleSectionLoaded: function (response, status, xhr) {
    HQ.sectionsLoaded++;
    var section = xhr.getResponseHeader('X-HQ-Section');
    HQ.sectionContent[section] = response;
    if (HQ.sectionsLoaded == 5) {
      HQ.wrapper.append(HQ.sectionContent['welcome']);
      HQ.wrapper.append(HQ.sectionContent['work']);
      HQ.wrapper.append(HQ.sectionContent['agency']);
      HQ.wrapper.append(HQ.sectionContent['blog']);
      HQ.wrapper.append(HQ.sectionContent['contact']);
      HQ.sectionContent = {};
      HQ._calcScrollingSections();
      HQ._initInternalLinks();
      HQ._initMap();
      HQ._initPeople();
      //HQ._initLogoLinks();
      $('#main-nav').css('display', 'inline');
      $('#main-nav').fadeIn();
      if (!$.support.touch) {
        $('#slider').fadeIn();
      }
      if (HQ.format == 'tall') {
        $('div.section-menu, div.job').css('height', '500px');
        $('#scroller, #floating-menu, #map-canvas').css('height', '550px');
      } else {
        $('div.section-menu, div.job').css('height', '350px');
        $('#scroller, #floating-menu, #map-canvas').css('height', '400px');
      }
      HQ._initBlog();
      HQ.scroller.removeClass('loader');
      if (window.location.hash != null && window.location.hash != '') {
        HQ.navTo(window.location.hash);
      }
    }
  },
  _handleResize: function () {
    if ($(window).height() > 800 && HQ.format != 'tall') {
      HQ.format = 'tall';
      $('div.section-menu, div.job').css('height', '500px');
      $('#scroller, #floating-menu, #map-canvas').css('height', '550px');
      HQ.loadSections();
    } else if ($(window).height() <= 800 && HQ.format != 'short') {
      HQ.format = 'short';
      $('div.section-menu, div.job').css('height', '350px');
      $('#scroller, #floating-menu, #map-canvas').css('height', '400px');
      HQ.loadSections();
    }
  },
  _handleLinkClick: function () {
    if ($(this).hasClass('ui-slider-handle')) {
      return;
    }
    HQ.navTo($(this).attr('href'));
    HQ.hideFloatingMenu();
    return false;
  },
  _handleScroll: function () {
    var scrollX = Math.ceil(HQ.scroller.scrollLeft());
    if (HQ.slider != null) {
      HQ.slider.slider('value', scrollX);
    }
    HQ.sections.each( function (index) {
      var pos = $(this).position();
      if (scrollX >= Math.floor(pos.left) && scrollX < Math.floor(pos.left) + $(this).width()) {
        if (HQ.currentSection == null || $(this).attr('id') != HQ.currentSection.attr('id')) {
          var oldSection = HQ.currentSection;
          HQ.currentSection = $(this);
          HQ._handleSectionChange(HQ.currentSection, oldSection);
        }
        return false;
      }
    }
    );
    var sectionScrollPos = scrollX - Math.floor(HQ.currentSection.position().left);
    if (HQ.floatingMenu.children().length > 0) {
      if (sectionScrollPos < 180 && HQ.floatingMenu.css('display') != 'none') {
        HQ.floatingMenu.css('display', 'none');
      } else if (sectionScrollPos >= 180 && HQ.floatingMenu.css('display') == 'none') {
        HQ.floatingMenu.css('display', 'block');
      }
      if (sectionScrollPos >= HQ.currentSection.width() - 20) {
        HQ.floatingMenu.css('left', -200 + (HQ.currentSection.width() - sectionScrollPos));
      } else if (sectionScrollPos >= 180 && HQ.floatingMenu.position().left < -180) {
        HQ.floatingMenu.css('left', -180);
      }
    }
  },
  _handleSectionChange: function (newSection, oldSection) {
    // update nav indicator and scroll track
    var newId = newSection.attr('id');
    if (HQ.slider != null) {
      var track = HQ.slider.children('.track').first();
      track.children().each( function (index) {
        $(this).removeClass('selected');
      }
      );
    }
    $('#' + newId + '-link').attr('class', 'selected');
    $('#' + newId + '-section-track').addClass('selected');
    if (oldSection != undefined) {
      var oldId = oldSection.attr('id');
      $('#' + oldId + '-link').attr('class', '');
    }
    window.location.hash = HQ.currentHash = '#/' + newId;

    // change "floating" section menu
    HQ.floatingMenu.empty();
    HQ.floatingMenu.css('display', 'none');
    var sectionMenus = newSection.children('.section-menu');
    if (sectionMenus.length > 0) {
      var sectionMenu = sectionMenus.first();
      if (!sectionMenu.hasClass('no-float')) {
        var clonedMenu = sectionMenu.clone();
        if (HQ.format == 'tall') {
          clonedMenu.css('height', '500px');
        } else {
          clonedMenu.css('height', '350px');
        }
        clonedMenu.find('a[href^="#"]').each( function (index) {
          $(this).click(HQ._handleLinkClick);
        }
        );
        HQ.floatingMenu.append(clonedMenu);
        HQ.floatingMenu.css('display', 'block');
        HQ.floatingMenu.append('<div id="floating-menu-arrow"></div>');
      }
    }
  },
  _handleSectionMenuOver: function () {
    HQ.showFloatingMenu();
  },
  _handleSectionMenuOut: function () {
    HQ.hideFloatingMenu();
  },
  _handleKeypress: function (e) {
    if (HQ.scrollingAnim)
      return;
    var code = (e.keyCode ? e.keyCode : e.which);
    var scrollX = HQ.scroller.scrollLeft();
    if (code == 37) {
      HQ.scrollingAnim = true;
      HQ.scroller.animate({scrollLeft: scrollX - HQ.scroller.width()}, 300, function () {
        HQ.scrollingAnim = false;
        var scrollX = HQ.scroller.scrollLeft();
        var lastSection = HQ.wrapper.children().last();
        if (scrollX > lastSection.position().left) {
          HQ.scroller.animate({scrollLeft: lastSection.position().left}, 100);
        }
      }
      );
    } else if (code == 39) {
      HQ.scrollingAnim = true;
      HQ.scroller.animate({scrollLeft: scrollX + HQ.scroller.width()}, 300, function () {
        HQ.scrollingAnim = false;
        var scrollX = HQ.scroller.scrollLeft();
        var lastSection = HQ.wrapper.children().last();
        if (scrollX > lastSection.position().left) {
          HQ.scroller.animate({scrollLeft: lastSection.position().left}, 100);
        }
      }
      );
    }
  },
  _handleTouchStart: function(e) {
    HQ.wrapper.bind('touchmove', HQ._handleTouchMove);
    HQ.wrapper.bind('touchend', HQ._handleTouchEnd);
    var touch = e.originalEvent.touches[0];
    HQ.touchCurrent = {x: touch.pageX, y: touch.pageY};
    HQ.hideFloatingMenu();
    $('#touch-note').hide();
  },
  cancelTouchEvent: function () {
    HQ.wrapper.unbind('touchmove', HQ._handleTouchMove);
    HQ.wrapper.unbind('touchend', HQ._handleTouchEnd);
  },
  _handleTouchMove: function(e) {
    var touch = e.originalEvent.touches[0],
    moveY = touch.pageY - HQ.touchCurrent.y,
    moveX = touch.pageX - HQ.touchCurrent.x;
    var scrollX = HQ.scroller.scrollLeft();
    HQ.scroller.scrollLeft(scrollX - moveX);
    HQ.touchDirection  = moveX > 0 ? 'left' : 'right';
    HQ.touchCurrent = {x: touch.pageX, y: touch.pageY};
    e.preventDefault();
  },
  _handleTouchEnd: function(e) {
    HQ.wrapper.unbind('touchmove', HQ._handleTouchMove);
    HQ.wrapper.unbind('touchend', HQ._handleTouchEnd);
    var lastSection = HQ.wrapper.children().last();
    var scrollX = HQ.scroller.scrollLeft();
    if (scrollX > lastSection.position().left) {
      HQ.scroller.animate({scrollLeft: lastSection.position().left}, 100);
    } else {
      var fudge = HQ.touchDirection == 'left' ? 40 : -40;
      HQ.scroller.animate({scrollLeft: scrollX - fudge}, 100);
    }
  }
};
