$(document).ready (function () {

  $("div.twitter").tweet ({
    avatar_size: 32,
    count: 1,
    username: "hdkvaliteta",
    template: "<p>{text}</p><small>{time}</small>",
    auto_join_text_default: "we said,",
    auto_join_text_ed: "we",
    auto_join_text_ing: "we were",
    auto_join_text_reply: "we replied to",
    auto_join_text_url: "we were checking out",
    loading_text: "loading tweets..."
  }).bind("loaded", function(){
    $(this).find("a.tweet_action").click(function(ev) {
      window.open(this.href, "Retweet",
                  'menubar=0,resizable=0,width=550,height=420,top=200,left=400');
      ev.preventDefault();
  });
  });

  var $hovHeads = $('div.mainContent > div.column > h2');
  if ($hovHeads.length) {
    // Handle hover state for headings
    $hovHeads.hover (
      // Hover in
      function () {
        $(this).data ('hover', true);
        showExtraContent ($(this).parents ('div.column')[0]);
      },
      // Hover out
      function () {
        $(this).data ('hover', false);
      }
    );

    /*var config = {
      over: function () {
        $(this).data ('hover', true);
        showExtraContent ($(this).parents ('div.column')[0]);
      },
      timeout: 300,
      sensitivity: 1,
      out: function () {
        $(this).data ('hover', false);
      }
    };

    $hovHeads.hoverIntent (config);*/

    // Handle hovering over column content
    var $cols = $('div.mainContent > div.column');
    if ($cols.length) {
      $cols.hover (
        // Hover in
        function () {
          showExtraContent (this);
        },
        // Hover out
        function () {
          $(this).removeClass ('hover');
          showOtherContent ();
        }
      );

      /*var configCols = {
        over: function () {
          showExtraContent (this);
        },
        timeout: 300,
        sensitivity: 1,
        out: function () {
          $(this).removeClass ('hover');
          showOtherContent ();
        }
      };

      $cols.hoverIntent (configCols);*/
    }
  }

  /* Gallery show */

  $('div.gallery div.button, div.gallery > img').click (openGallery);

  /* Chosen plugin */

  var $selects = $('form.searchForm select');
  if ($selects.length) {
    $selects.chosen ();
  }

});

var galleryConf = {
  speed: 1000,
  slideInSpeed: null,
  slideOutSpeed: null
};
galleryConf.slideInSpeed = galleryConf.speed * 0.333
galleryConf.slideOutSpeed = galleryConf.speed * 0.667

function openGallery (eventObject) {
  var $button = $(eventObject.target).parent ('div.gallery').find ('div.button');
  var $gallery = $button.parents ('div.gallery');
  $button
    .unbind ('click')
    .fadeOut (100);
  $('div.gallery div.button').unbind ('click');
  $gallery.animate (
    {width: 0, height: $gallery.height (), marginRight: '10px'},
    galleryConf.slideInSpeed,
    'swing',
    function () {
      $gallery.find ('div.flexslider').css ({height: '350px'});
      $gallery.find ('div.flexslider').css ({width: 0, display: 'block'});
      $gallery.css ({overflow: 'visible'});
      $gallery.children ('img').css ({display: 'none'});
      startUpSlider ($gallery);
      $gallery.find ('ul.flex-direction-nav').css ({display: 'none'});
      centerNavControls (false);
      $gallery.find ('ul.flex-direction-nav').fadeIn (galleryConf.slideOutSpeed);
      $gallery.find ('div.flexslider').animate (
        {width: '910px'},
        galleryConf.slideOutSpeed,
        'swing',
        function () {
          $gallery.find ('ul.slides').css ({width: (910 * $gallery.find ('ul.slides li').length) + 'px'});
          $button
            .addClass ('active')
            .fadeIn (100);
        }
      );
    }
  );
  // Hide content
  var $contentToHide = $('ul.nav, div.pageLead, div.footerContent, div.pageContent');
  $contentToHide = $contentToHide.add ($gallery.siblings ());
  $contentToHide.animate (
    {opacity: 0.2},
    galleryConf.speed,
    'swing'
  );
  $button.click (closeGallery);
}

function closeGallery (eventObject) {
  var $button = $(eventObject.target);
  var $gallery = $button.parents ('div.gallery');
  $button.fadeOut (100);
  $('div.gallery div.button').unbind ('click');
  var $contentToHide = $('ul.nav, div.pageLead, div.footerContent, div.pageContent');
  $contentToHide = $contentToHide.add ($gallery.siblings ());
  $contentToHide.animate (
    {opacity: 1},
    galleryConf.speed,
    'swing'
  );
  $gallery.find ('ul.flex-direction-nav').fadeOut (galleryConf.slideInSpeed);
  $gallery.find ('div.flexslider').animate (
    {width: 0},
    galleryConf.slideOutSpeed,
    'swing',
    function () {
      //$gallery.find ('ul').css ({display: 'none'});
      $gallery.find ('div.flexslider').css ({width: 0});
      $gallery.css ({overflow: 'hidden'});
      $gallery.children ('img').css ({display: 'block'});
      $gallery.animate (
        {width: '300px'},
        galleryConf.slideInSpeed,
        'swing',
        function () {
          $button
            .removeClass ('active')
            .fadeIn (100);
        }
      );
    }
  );
  $('div.gallery div.button').click (openGallery);
}

function startUpSlider ($context) {
  $('.flexslider', $context).flexslider (
    {
      animation: "slide",              //Select your animation type (fade/slide)
      slideshow: false,                //Should the slider animate automatically by default? (true/false)
      slideshowSpeed: 4000,           //Set the speed of the slideshow cycling, in milliseconds
      animationDuration: 300,         //Set the speed of animations, in milliseconds
      directionNav: $('div.gallery ul.slides li').length > 1 ? true : false,             //Create navigation for previous/next navigation? (true/false)
      controlNav: false,               //Create navigation for paging control of each clide? (true/false)
      keyboardNav: false,              //Allow for keyboard navigation using left/right keys (true/false)
      touchSwipe: false,
      animationLoop: false,
      start: startSlideCallback,
      after: afterSlideCallback
    }
  );
}

function startSlideCallback () {
  // Add hover handle
  var $button = $('ul.flex-direction-nav li a');
  $button.hover (
    function () {$button.data ('hover', true);},
    function () {$button.data ('hover', false);}
  );
  centerNavControls (false);
}

function afterSlideCallback () {
  var $button = $('ul.flex-direction-nav li a');
  $button.unbind ('mouseout', centerNavControls);
  if ($button.data ('hover')) {
    $button.mouseout (centerNavControls);
  }
  else {
    centerNavControls (false);
  }
}

function centerNavControls (animate) {
  if ($('ul.flex-direction-nav').length) {
    var $navControls = $('ul.flex-direction-nav');
    if (animate == undefined) {
      animate = true;
    }
    var top = $('ul.flex-direction-nav').position ().top;
    if (animate) {
      $navControls.css ({top: '50%'});
      // Get new top
      var newTop = $('ul.flex-direction-nav').position ().top;
      $navControls.css ({top: top + 'px'});
      $navControls.animate (
        {top: newTop + 'px'},
        150,
        'swing'
      );
    }
    else {
      $navControls.css ({ top: $('div.flexslider').height () / 2 + 'px' });
      //top = $('ul.flex-direction-nav').position ().top;
      //$navControls.css ({top: top + 'px'});
    }
  }
}

function showExtraContent (container) {
  var $h2 = $(container).find ('h2');
  if ($h2.data ('hover')) {
    $(container).addClass ('hover');
    hideOtherContent ();
  }
}

function hideOtherContent () {
  $('div.footerContent, img.infoGraphic')
    .stop (true, false)
    .animate (
      {opacity : 0.2},
      400,
      'swing'
  );
}

function showOtherContent () {
  $('div.footerContent, img.infoGraphic')
    .stop (true, true)
    .animate (
      {opacity : 1},
      300,
      'swing',
      function () {$(this).removeAttr ('style');}
  );
}
