﻿var _index = null;
var hideTopStory = !($jQuery.browser.msie && $jQuery.browser.version < 8);
var COLLAPSED_HEIGHT = "87px";

function ShowTopStory(index) {
  //Register the menu click with Google Analytics
  if (_index != null) {
    var caseStoryIndex = index + 1;
    trackClick("/casestory/" + caseStoryIndex);
  }

  _index = index;
  $jQuery("#CaseInfoBar").fadeOut("fast", hideTopStoryCallback);
  $jQuery("#TopStoryImage").fadeOut("fast", hideTopStoryCallback);
}

function hideTopStoryCallback() {
  if (topStoryItems[_index] == null)
    return;

  $jQuery(".LanguageSwitcher").css("background-color", topStoryItems[_index].colorValue)

  $jQuery(".TopStory").css("visibility", "visible");
  $jQuery(".TopStory #CaseStoryRepeater a").removeClass("Selected");
  $jQuery(".TopStory #CaseStoryRepeater a").eq(_index).addClass("Selected");
  $jQuery(".TopStory .InnerBox").css("background-color", topStoryItems[_index].colorValue)
  $jQuery(".TopStory .Ornament").css("background-color", topStoryItems[_index].colorValue)
  $jQuery(".TopStory .CaseStripe").css("background-color", topStoryItems[_index].colorValue)
  $jQuery(".TopStory #CaseInfoBar").css("background-color", topStoryItems[_index].colorValue)
  $jQuery(".TopStory .Links").css("color", topStoryItems[_index].colorValue)
  $jQuery(".TopStory #CaseStoryRepeater a").css("border-color", topStoryItems[_index].colorValue).css("color", topStoryItems[_index].colorValue).css("background-color", "");
  $jQuery(".TopStory #CaseStoryRepeater a.Selected").css("background-color", topStoryItems[_index].colorValue)
                                              .css("color", "");
  $jQuery(".TopStory .CaseToggle a").css("border-color", topStoryItems[_index].colorValue).css("color", topStoryItems[_index].colorValue).css("background-color", "");
  

  $jQuery("#CaseInfoBar").html(topStoryItems[_index].caseInfoText + "<a href=\"" + topStoryItems[_index].linkUrl + "\">" + topStoryItems[_index].caseInfoLinkText + "</a>");

  $jQuery("#TopStoryLink").attr("href", topStoryItems[_index].linkUrl);
  $jQuery("#TopStoryImage").attr("src", topStoryItems[_index].imageUrl);
  $jQuery("#TopStoryImage").attr("alt", topStoryItems[_index].altText);

  $jQuery("#CaseStoryText a").attr("href", topStoryItems[_index].linkUrl);
  $jQuery("#CaseStoryText a").html(topStoryItems[_index].altText);
  $jQuery("#CaseStoryText a").css("background-color", topStoryItems[_index].colorValue)

  $jQuery("#CaseInfoBar").fadeIn("slow");
  $jQuery("#TopStoryImage").fadeIn("slow");
  
  $jQuery.cookie('currentTopStoryItem', _index, {path: '/'})
}

var isTopStoryVisible = true;
var topStoryHeight = null;

function ToggleTopStory() {
  var topStory = $jQuery(".TopStory");
  var caseToggle = $jQuery(".TopStory .CaseToggle");
  var caseStoryText = $jQuery("#CaseStoryText");
  
  if (isTopStoryVisible) {
    topStory.animate({ height: COLLAPSED_HEIGHT });
    caseStoryText.fadeIn();
  }
  else {
    topStory.animate({ height: topStoryHeight });
    caseStoryText.fadeOut();
  }

  isTopStoryVisible = !isTopStoryVisible;
}

function InitializeTopStory() {
  var topStory = $jQuery(".TopStory");
  var caseToggle = $jQuery(".TopStory .CaseToggle");
  var caseStoryText = $jQuery("#CaseStoryText");

  topStoryHeight = topStory.innerHeight();

  if (document.location.pathname != "" && document.location.pathname != "/" && document.location.pathname != "/default.aspx" && document.location.pathname != "/Default.aspx" && hideTopStory) {
    topStory.css("height", COLLAPSED_HEIGHT);
    caseStoryText.show();
    caseToggle.show();
    isTopStoryVisible = false;
  }
}

$jQuery(document).ready(function() 
{
  ShowTopStory(currentTopStoryItem);

  InitializeTopStory();
});
