var rotate_index = 0;
var casestudy_index = 0;
var caseStudyArray;
var newsArray;

function loadAlternateContent() {
    $.ajax({
        type: "POST",
        url: "/webservices/homepageservice.asmx/Test",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(result) {
            // case studies
            caseStudyArray = result.d.CaseStudies;

            if (caseStudyArray.length > 0) {
            var client = '';

                client = caseStudyArray[0].ClientName + ", " + caseStudyArray[0].ClientType + " - " + caseStudyArray[0].AUM;

                $("#case-study-client").html(client);
            $("#case-study-overview").html(caseStudyArray[0].Description);
            $("#download-case-study").click(function() {
            window.location.href = caseStudyArray[0].USFile;
            return false;
            }); 
            }
            
            // events
            if (result.d.Events != null) {
            // left side event
            $("#left-event").html("<a href='" + result.d.Events[0].Url + "'><img src='" + result.d.Events[0].ThumbnailPath + "'/></a>");
            // top right side event
            $("#top-right-event").html("<div class='event-title'>" + result.d.Events[1].HomepageEventTitle + "</div><div class='event-description'>" + result.d.Events[1].HomepageEventDesc + "</div>");
            $('#top-right-event').click(function() {
            window.location.href = result.d.Events[1].Url;
            return false;
            });
            // bottom right side event
            $("#bottom-right-event").html("<div class='event-title'>" + result.d.Events[2].HomepageEventTitle + "</div><div class='event-description'>" + result.d.Events[2].HomepageEventDesc + "</div>");
            $('#bottom-right-event').click(function() {
            window.location.href = result.d.Events[2].Url;
            return false;
            });
            }
            
            // more news link
            $('#more-events').click(function() {
            window.location.href = '/about/events'
            return false;
            });
            

            // news

            // store news items in global array
            newsArray = result.d.News;
            // make sure there are news items
            if (newsArray.length > 0) {
            // populate news panel
            var story = '';
            for (var i = 0; i <= 2; i++) {
            story = ".story" + (i + 1);
            $(story).html("<a href='" + newsArray[i].Url + "'>" + newsArray[i].Title + "</a>");
            }
            // initialize the news rotator
            $("#news-rotate-container").html(newsArray[rotate_index].Title);
            rotate_index++;
            // start the rotation timer
            $(document).everyTime(3000, "news", rotateNews, 0);
            }
            // more news link
            $('#more-news').click(function() {
            window.location.href = '/about/news/press_releases'
            return false;
            });
        },
        error: function(xhr, textStatus, errorThrown) {
            alert(textStatus);
            alert(xhr.statusText + ' ' + xhr.status + ' ' + errorThrown);
        }
    });
  
  var config = {    
       over: makeTall, // function = onMouseOver callback (REQUIRED)    
       timeout: 200, // number = milliseconds delay before onMouseOut    
       out: makeShort // function = onMouseOut callback (REQUIRED)    
  };
  // case study navigation
  // back
  $('#case-study-nav-back').click(function() {
      if (casestudy_index == 0) {
          casestudy_index = caseStudyArray.length - 1;
      }
      else {
          //decrement casestudy_index by 1
          casestudy_index--;
      }

      // update view
      var client = '';
      client = caseStudyArray[casestudy_index].ClientName + ", " + caseStudyArray[casestudy_index].ClientType + " - " + caseStudyArray[casestudy_index].AUM;

      $("#case-study-client").html(client);
      $("#case-study-overview").html(caseStudyArray[casestudy_index].Description);
      $("#download-case-study").click(function() {
          window.location.href = caseStudyArray[casestudy_index].USFile;
          return false;
      });
      return false;
  });

  // next
  $('#case-study-nav-next').click(function() {
      if (casestudy_index == caseStudyArray.length - 1) {
          casestudy_index = 0;
      }
      else {
          //increment casestudy_index by 1
          casestudy_index++;
      }

      // update view
      var client = '';
      client = caseStudyArray[casestudy_index].ClientName + ", " + caseStudyArray[casestudy_index].ClientType + " - " + caseStudyArray[casestudy_index].AUM;

      $("#case-study-client").html(client);
      $("#case-study-overview").html(caseStudyArray[casestudy_index].Description);
      $("#download-case-study").click(function() {
          window.location.href = caseStudyArray[casestudy_index].USFile;
          return false;
      });  
      return false;
  });

  // all case studies link
  $('.see-all-case-studies').click(function() {
      window.location.href = '/resources/case-studies/'
      return false;
  });
  // init hover intent
  $(".header").hoverIntent(config);
  // init swap image
  $.swapImage(".swapImage");
  // product hover animation
  $(".product").hover(
      function () {
        $(this).animate({ backgroundColor: "#3a7daf" }, 200);
      }, 
      function () {
        $(this).animate({ backgroundColor: "#0a446c" }, 200);
      }
  );
  // event hover animation
  $(".event").hover(
      function() {
          $(this).animate({ backgroundColor: "#4b96ef" }, 200);
      },
      function() {
          $(this).animate({ backgroundColor: "#88b6eb" }, 200);
      }
  );
  // news hover animation
  $(".news-item").hover(
      function() {
        $(this).animate({ backgroundColor: "#7fe25b" }, 200);
      },
      function() {
        $(this).animate({ backgroundColor: "#52a81b" }, 200);
      }
  );
  
}

function rotateNews()
{
	// make sure we are still in range
	if (rotate_index > newsArray.length - 1 | rotate_index > 2)
		rotate_index = 0;
	// fade out rotate container
	$("#news-rotate-container").fadeOut("fast", function () 
		{
  			$("#news-rotate-container").html(newsArray[rotate_index].Title);
  			// increment index
			rotate_index++;
  		}
  	);
	// fade in rotate container
	$("#news-rotate-container").fadeIn("fast");
	
}

function makeTall()
{
	// Perform panel specific functions
	var class_name = $(this).attr('class');
	
	switch (class_name)
	{
	    case "header clients":
	        $(this).animate({ "height": 300 }, 300);
			$(this).find(".client-fade-container").fadeIn("fast");
			break;
		case "header news":
		    $(this).animate({ "height": 130 }, 300);
			$(document).stopTime("news");
			$(this).find("#news-rotate-container").fadeOut("fast", function () 
				{
			  		$("#news-item-float-container").fadeIn("fast");
			  	}
  			);
			break;
		case "header events":
		    $(this).animate({ "height": 205 }, 300);
		    break;
		default:
		    $(this).animate({ "height": 300 }, 300);
		    break;
					
	}
}
		
function makeShort()
{ 
	$(this).animate({"height":25},300);
	
	// Perform panel specific functions
	var class_name = $(this).attr('class');
		
	switch (class_name)
	{
		case "header clients":
			$(this).find(".client-fade-container").fadeOut("fast");
			break;
		case "header news":
			$(this).find("#news-item-float-container").fadeOut("fast", function () 
				{
					$("#news-rotate-container").fadeIn("fast");
				}
  			);
			rotate_index = 0;
			$(document).everyTime(3000, "news", rotateNews, 0);
			break;
	}
}
