//*********************************************
// vars
//*********************************************
var init = true;
var header_offset = 20; // see css for definition
var news_cont_closed_pos = $('#news_container').width() - $('#header').width() + header_offset;
var news_cont_opened_pos = $('#header').width() - header_offset;
var subnav_cont_closed_pos = $('#navigation > li > ul').width() - $('#header').width() + header_offset;
var subnav_cont_opened_pos = news_cont_opened_pos;

// document ready
$(document).ready(function() {
  //toggleNewsContainer();
  if($('#content').hasClass('portfolio')) setActivePortfolioSubpage(); // has to be BEFORE toggleSubNavigation()
  toggleSubNavigation();
  if($('.overlay').length) initOverlay();
  if($('#video_panel').length) initVideoPanelScrollable();
  if($('.toggle').length) togglePeopleDesc();
  var el = $('.clickable');
  if(el.length) selectEntireElement(el);
  positionSubNavigation();
  positionHeaderBg();
  disableRootLinksNav();
  if($('.handle-focus').length) handleInputFocus();
  if($('#subForm').length) pulsateNewsletterField();
	$("#awards_listing .award").tooltip({position:'bottom center', relative:true, effect:'slide', direction:'down'}); // init tooltips of awards on portfolio item pages
});

// window load
$(window).load(function() {
  resize();
  showInvisibles();
  if($('#content').hasClass('portfolio')) positionContentPortfolio();
  // set init to false as last action of the initialization
  init = false;
});

// window resize
$(window).resize(function() {
  positionSubNavigation();
  positionHeaderBg();
  resize();
});



//*********************************************
// functions
//*********************************************

// positions the header background so that the background is not behind the navigation but below
function positionHeaderBg() {
  var top = $('#logo').outerHeight() + $('#navigation').height() + $('#header_plane').height() - 3; // solution for ipad subpixel rendering issues: -5px and remove 5px of shadow from element above
  var h = $(document).height() - top;

  $('#header_bg').height(h).css('top', top);
}

function toggleNewsContainer() {
  // shows newsContainer onLoad: if uncommented change the functions order in the toggle function
  // note: in order to toggle the newsContainer when a subNavigation is opened, the subNavigation has to be closed because there is no way to place the subNavigation (as a part of the header) behind the newsContainer
  $('#nav_news').hover(
    function() {
      // animate newsContainer
      $(this).children('#news_container').stop().animate({
        left: news_cont_opened_pos
      }, 400, "easeOutExpo");
        $(this).addClass("current");
    },
    function() {
      // animate newsContainer
      $(this).children('#news_container').stop().animate({
        left: -news_cont_closed_pos
      }, 550, "easeOutQuad");
      $(this).removeClass("current");
    }
  );
}

// toggles subNavgiation onHover without delay, otherwise use hoverIntent plugin
function toggleSubNavigation() {
  // show current subNavigation
  if($('#navigation ul li').hasClass('current-menu-item')) {
    $('#navigation ul li.current-menu-item').parent().css('visibility', 'visible');
    $('#navigation ul').css('z-index', '-30');
    $('#navigation ul li.current-menu-item').addClass('arrow').parent().css('left', subnav_cont_opened_pos).css('z-index', '-200').siblings('a').addClass('current');
  }
  // hoverToggle
  $('#navigation > li').hover(
    function() {
      if($(this).children('ul').children('li').hasClass('current-menu-item')) {
        $(this).children('ul').stop().animate({
          left: subnav_cont_opened_pos
        }, 300, "easeOutExpo");
        $(this).siblings().children('a').removeClass("current");
        $(this).children('a').addClass("current");
      }
      else {
        $(this).children('ul').stop().animate({
          left: subnav_cont_opened_pos
        }, 300, "easeOutExpo");  
        $('#navigation').find('.current-menu-item').removeClass('arrow');
        $(this).siblings().children('a').removeClass("current");
        $(this).children('a').addClass("current");
        
        $('#navigation ul li.current-menu-item').parent().stop().animate({left: -subnav_cont_closed_pos}, 250, "easeOutQuad");
      }
    },
    function() {
      if($(this).children('ul').children('li').hasClass('current-menu-item')) {
      }
      else {
        $(this).children('ul').stop().animate({
          left: -subnav_cont_closed_pos
        }, 250, "easeOutQuad");
        $('#navigation').find('.current-menu-item').addClass('arrow');
        $(this).siblings().children('ul').children('.current-menu-item').parent().siblings('a').addClass("current");
        $(this).children('a').removeClass("current");
        
        $('#navigation ul li.current-menu-item').parent().stop().animate({left: subnav_cont_opened_pos}, 300, "easeOutExpo");
      }
    }
  );
}

// set the position of the SubNavigation to vertically align with the parent navigation item
// add arrow indicating the currently acitve subNavigation entry
// resize height to fill out the entire content height
function positionSubNavigation() {
  // set the position to align top with the active main nav entry
  $('#navigation > li > ul').map(function() {
    $(this).css('padding-top', $(this).parent().position().top + 5);
    $(this).height($(document).height());    
  });
}

// important: only one link inside element allowed. the link attribute href is used to redirect per js inside the click function
function selectEntireElement(el) {
  el.hover(
    function() {
      $(this).addClass("hover");
    },
    function() {
      $(this).removeClass("hover");
    }).click(function() { window.location = $(this).find("a").attr("href"); });
}

// resize height to fill out the entire content height
// resize content and bg_lines
function resize() {
  // reset height to default value
  $('#content').height('auto');
  $('#page_bg_lines').height('auto');
  $('#header_bg').height('auto');
  
  var h = $(document).height();
  $('#content').height(h);
  $('#page_bg_lines').height(h);
  $('#header_bg').height(h);
	$('#portfolio_container').height(h);
}

function initVideoPanelScrollable() {
  var api = $("#video_panel").scrollable().navigator().data("scrollable");
  
  var i = api.getIndex();
  var cur_el = $('#video_panel .items > div').eq(i);
  var h = cur_el.height() - 5; // -5px correction
  $('#video_panel').animate({height: h}, 250);
  
  api.onSeek(function() {
    var i = api.getIndex();
    var cur_el = $('#video_panel .items > div').eq(i);
    var h = cur_el.height() - 5; // -5px correction
    $('#video_panel').animate({height: h}, 250);
  });
}

function disableRootLinksNav() {
  $('#navigation > li:not(:first-child)').children('a').click(function() {
    return false;
  });
}

function initOverlay() {
  $('.use_overlay').hover(
  function(){
    $(this).find('.overlay').stop(true, true).fadeIn(200);
  },
  function() {
    $(this).find('.overlay').stop(true, true).fadeOut(200);
  });
}

function pulsateNewsletterField() {
  $('#pulsar').effect("pulsate", { times:5, mode:'hide' }, 2000);
}

// clear input field onfocus and reset original content onblur
function handleInputFocus() {
  var sel = $(".handle-focus");
  sel.each(function(i) {
    var inputValue = $(this).attr('value');
    $(this).focus(function(){
      $(this).stop(true, true);
      if($(this).attr('value') == inputValue) $(this).attr('value', '');
      $(this).parent().addClass("input-focus").removeClass("error");
    }).blur(function(){
      if($(this).attr('value') == '') {
        $(this).attr('value', inputValue);
        $(this).parent().removeClass("input-focus");
      }
    });
  });
}

// set the currently active subpage within portfolio page (called inline in tf-portfolio-navigation.php)
function setActivePortfolioSubpage() {
  var urlParams = {};
  getQueryStrings(urlParams);
  if(urlParams['art'] == 'Broadcast Design') $('#menu-item-3295').addClass('current-menu-item');
  else if(urlParams['art'] == 'Corporate') $('#menu-item-3296').addClass('current-menu-item');
  else if(urlParams['art'] == 'Commercials') $('#menu-item-3297').addClass('current-menu-item');
  else $('#menu-item-3290').addClass('current-menu-item');
}

function getQueryStrings(urlParams) {
  var e,
      a = /\+/g,  // Regex for replacing addition symbol with a space
      r = /([^&=]+)=?([^&]*)/g,
      d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
      q = window.location.search.substring(1);

  while (e = r.exec(q))
     urlParams[d(e[1])] = d(e[2]);
}

// toggle people description on people page
function togglePeopleDesc() {
  $('.person .toggle').toggle(
  function() {
    $(this).parent().prev().css('height','auto');
    $(this).html('schliessen');
    // resize website in case the content becomes too tall
    resize();
  },
  function() {
    $(this).parent().prev().css('height','59px');
    $(this).html('weiterlesen');
    // resize website in case the content becomes too short
    resize();
  });
}

// show invisible elements after loading, e.g., menu items
function showInvisibles() {
  $('#navigation li ul').css('visibility','visible');
}

// position #scroll_top_container to align with currently hooked preview image
function positionContentPortfolio() {
	if(window.location.hash.indexOf('#') != -1) {
		var doc_h = $(document).height();
		var prev_t = $(window.location.hash).position().top;
		var cont_h = 962;
		
		var isiPad = navigator.userAgent.match(/iPad/i) != null;
	
		if (doc_h - prev_t < cont_h) { // if the content is too far down, set it back up
			if ($(window).height() < cont_h + 29) {// if the content is bigger as the window height, set it up back the height of the content
				$('#scroll_top_container').css('top', doc_h - cont_h);
				if (isiPad) $('.tooltip').css('margin-top', doc_h - cont_h + 170);
			} else { // set it back up to the top of the window
				$('#scroll_top_container').css('top', doc_h - $(window).height());
				if (isiPad) $('.tooltip').css('margin-top', doc_h - $(window).height());
			}
		} else {
			$('#scroll_top_container').css('top', prev_t);
			if (isiPad) $('.tooltip').css('margin-top', prev_t);
		}
	}
}
