/*
 * File 'javascript/js_home.js'.
 *
 * Handles the featured content area for the homepage
 *
 * @copyright  Copyright (c) 2011, Hocking College
 * @version    February 24th, 2011
 * @category   javascript
 */

var strReadMore = "<br /><br /><span style='font-size:14px'>[read more]</span>";
var strLink = "";
var arrContentBg = "";
var arrThumbDiv = "";
var arrThumbImage = "";

intHighlight = 0;
intPointer = 0;

$(document).ready( function() { _initHome() } );

/*
 * Function '_init()' is the class constructor.
 *
 * @access  public
 * @since   February 24th, 2011
 */
function _initHome()
{
   strLink = strBaseUrl + "application/images/" + strTemplate + "/";
   arrContentBg = new Array(strLink + "Widgets/FeaturedContent/content1.png", strLink + "Widgets/FeaturedContent/content2.png", strLink + "Widgets/FeaturedContent/content3.png");
   arrThumbDiv = new Array("#thumbsContainer #one", "#thumbsContainer #two", "#thumbsContainer #three");
   arrThumbImage = new Array(strLink + "Widgets/FeaturedContent/thumbOn1.png", strLink + "Widgets/FeaturedContent/thumbOn2.png", strLink + "Widgets/FeaturedContent/thumbOn3.png", strLink + "Widgets/FeaturedContent/thumbOff.png");

   // Functionality for more than 3 featured items currently not being used
//   if (title.length > 3) 
//   {
//      $("#leftArrow").attr({src : strBaseUrl + "application/images/" + strTemplate + "/Widgets/FeaturedContent/leftArrow.png"});
//      $("#rightArrow").attr({src : strBaseUrl + "application/images/" + strTemplate + "/Widgets/FeaturedContent/rightArrow.png"});
//      $('.featuredArrow').show();
//      $("#one").css( {'left' : '45px','width' : '205px'});
//      $("#two").css( {'left' : '250px','width' : '205px'});
//      $("#three").css( {'left' : '450px','width' : '205px'});
//      $(".title").css( {'width' : '105px'});
//      arrThumbImage = new Array("/Widgets/FeaturedContent/thumbOnSmall1.png", "/Widgets/FeaturedContent/thumbOnSmall2.png", "/Widgets/FeaturedContent/thumbOnSmall3.png",  "/Widgets/FeaturedContent/thumbOffSmall.png");
//   }
   $("#image").attr( {src : image[0],alt : alt[0]});
   $("#thumbImgOne").attr( {src : image[0],alt : alt[0]});
   $("#thumbImgTwo").attr( {src : image[1],alt : alt[1]});
   $("#thumbImgThree").attr( {src : image[2],alt : alt[2]});
   $("#one .title").text(title[0]);
   $("#two .title").text(title[1]);
   $("#three .title").text(title[2]);
   if(url[0] == "")
   {
      $("#blurb").html(blurb[0]);
   }
   else
   {
      if(url[0].indexOf("http://") == -1 || url[0].indexOf("hocking.edu") != -1)
      {
         $("#blurb").html("<a href='" + url[0] + "'>" + blurb[0] + strReadMore + "</a>");
      }
      else
      {         
         $("#blurb").html("<a href='" + url[0] + "' target='_blank'>" + blurb[0] + strReadMore + "</a>");
      }
   }
   $("#two, #three").css({"background-image" : "url(" + arrThumbImage[3] + ")"});
   $("#content").css({"background-image" : "url(" + arrContentBg[0] + ")"});
   $("#one").css({"background-image" : "url(" + arrThumbImage[0] + ")"});
   centerObjectsVertical("#content", "#blurb");
   centerObjectsVertical("#one", "#one .title", 10);
   centerObjectsVertical("#one", "#two .title", 10);
   centerObjectsVertical("#one", "#three .title", 10);   
   
   $('#one').hover(function() 
   {
      changeFeaturedItem(0);
   });
   $('#two').hover(function() 
   {
      changeFeaturedItem(1);
   });
   $('#three').hover(function() 
   {
      changeFeaturedItem(2);
   });
   $('#one').click(function() 
   {
      if(url[0 + intPointer] != "")
      {
         location.href = url[0 + intPointer];
      }
   });
   $('#two').click(function() 
   {      
      if(url[1 + intPointer] != "")
      {
         location.href = url[1 + intPointer];
      }
   });
   $('#three').click(function() 
   {
      if(url[2 + intPointer] != "")
      {
         location.href = url[2 + intPointer];
      }
   });
   $('#rightArrow').click(function() 
   {
      if (image.length > (intPointer + 3)) 
      {
         intPointer++;
         shiftContent();
      }
   });
   $('#leftArrow').click(function() 
   {
      if (intPointer > 0) 
      {
         intPointer--;
         shiftContent();
      }
   });
}

/*
 * Function 'centerObjectsVertical()' Centers an object within its container
 * vertically
 *
 * @access  public
 * @since   February 24th, 2011
 * @param   object   The container to center in
 * @param   object   The child to center within the container
 * @param   integer  Number to offset from the top of the container
 */
function centerObjectsVertical(objContainer, objChild, intOffset) 
{
   if (intOffset == undefined) 
   {
      $(objChild).css({'top' : ($(objContainer).height() - $(objChild).height()) / 2 + "px"});
   } 
   else 
   {
      $(objChild).css({'top' : ($(objContainer).height() + intOffset - $(objChild).height()) / 2 + "px"});
   }
}

/*
 * Function 'changeFeaturedItem()' Changes the featured item that is being 
 * hovered over
 *
 * @access  public
 * @since   February 24th, 2011
 * @param   integer  index of the item is in the input arrays
 */
function changeFeaturedItem(intIndex) 
{
   if (intHighlight != intIndex) 
   {
      $("#image").stop(true, true);
      $("#image").fadeTo(200, 0.25, function() 
      {
         $("#image").attr( {src : image[intIndex + intPointer], alt : alt[intIndex + intPointer]});
      });
      $("#image").fadeTo(400, 1, function() {});
      if(url[intIndex + intPointer] == "")
      {
         $("#blurb").html(blurb[intIndex + intPointer]);
      }
      else
      {         
         if(url[intIndex + intPointer].indexOf("http://") == -1 || url[intIndex + intPointer].indexOf("hocking.edu") != -1)
         {
            $("#blurb").html("<a href='" + url[intIndex + intPointer] + "'>" + blurb[intIndex + intPointer] + strReadMore + "</a>");
         }
         else
         {         
            $("#blurb").html("<a href='" + url[intIndex + intPointer] + "' target='_blank'>" + blurb[intIndex + intPointer] + strReadMore + "</a>");
         }
      }
      centerObjectsVertical("#content", "#blurb");
      $("#content").css({"background-image" : "url(" + arrContentBg[intIndex] + ")"});
      for ( var x in arrThumbDiv) 
      {
         if (intIndex == x) 
         {
            $(arrThumbDiv[x]).css({"background-image" : "url(" + arrThumbImage[x] + ")"});
         } 
         else 
         {
            $(arrThumbDiv[x]).css({"background-image" : "url(" + arrThumbImage[3] + ")"});
         }
      }
   }
   intHighlight = intIndex;
}

/*
 * Function 'shiftContent()' shifts the viewable featured items left or right
 * CURRENTLY NOT IN USE
 *
 * @access  public
 * @since   February 24th, 2011
 */
function shiftContent() 
{
   $("#thumbImgOne").attr( {src : image[intPointer],alt : alt[intPointer]});
   $("#thumbImgTwo").attr( {src : image[intPointer + 1],alt : alt[intPointer + 1]});
   $("#thumbImgThree").attr( {src : image[intPointer + 2],alt : alt[intPointer + 2]});
   $("#one .title").text(title[intPointer]);
   $("#two .title").text(title[intPointer + 1]);
   $("#three .title").text(title[intPointer + 2]);
   if (intHighlight == 0) 
   {
      $("#image").attr( {src : image[intPointer],alt : alt[intPointer]});
      $("#blurb").text(blurb[intPointer]);
   } 
   else 
   {
      if (intHighlight == 1) 
      {
         $("#image").attr( {src : image[intPointer + 1], alt : alt[intPointer + 1]});
         $("#blurb").text(blurb[intPointer + 1]);
      }
      else 
      {
         $("#image").attr( {src : image[intPointer + 2], alt : alt[intPointer + 2]});
         $("#blurb").text(blurb[intPointer + 2]);
      }
   }
   centerObjectsVertical("#content", "#blurb");
   centerObjectsVertical("#one", "#one .title", 10);
   centerObjectsVertical("#one", "#two .title", 10);
   centerObjectsVertical("#one", "#three .title", 10);
}
