﻿/// <reference path="jquery-1.4.2.min-vsdoc.js" />

var imageURL = "", imageBackground = "", imageAlternate = "ODE", imageDescription = "", backgroundRepeat = "no-repeat",
  backColor1 = "", backColor2 = "";
var padding = "", textAlign = "";
var mins, secs, TimerRunning, TimerID, listItem;
TimerRunning = false;
/**/
/****************    JQuery Document Ready Init   *********************/
$(document).ready(function() {
  $(".searchBox").focus();
  (function($) {
    $(function() { //on DOM ready
      $(".scroller").simplyScroll({
        className: 'custom',
        autoMode: 'loop',
        pauseOnHover: true,
        frameRate: 25,
        speed: 1,
        horizontal: false
      });
    });
  })(jQuery);

  //Show Banner
  $(".main_image .desc").show(); //Show Banner
  $(".main_image .block").animate({ opacity: 0.85 }, 1); //Set Opacity

  //Click and Hover events for Tator navigation
  $(".imageNavigation ul li:first").addClass('active');
  $(".imageNavigation ul li").click(function() {
    if (TimerRunning)
      StopTimer();
    //Set Variables
    var imgAlt = $(this).find('img').attr("alt"); //Get Alt Tag of Image
    var imgTitle = $(this).find('a').attr("href"); //Get Main Image URL
    var imgDesc = $(this).find('.block').html(); 	//Get HTML of block
    var imgDescHeight = $(".main_image").find('.block').height(); //Calculate height of block	

    if ($(this).is(".active")) {  //If it's already active, then...
      InitTimer();
      return false; // Don't click through
    } else {
      var currentIndex = new Number(this.id);
      rotatorIndex.value = (currentIndex - 1);
      GetServerRotatorObject();
      //Animate the Teaser
      $(".main_image .block").animate({ opacity: 0, marginBottom: -imgDescHeight }, 250, function() {
        ShowRotator();
      });
    }

    $(".imageNavigation ul li").removeClass('active'); //Remove class of 'active' on all lists
    $(this).addClass('active');  //add class of 'active' on this list only
    InitTimer();
    return false;

  }).hover(function() {
    $(this).addClass('hover');
  }, function() {
    $(this).removeClass('hover');
  });

  $(".imageNavigation ul li:first").removeClass('active');
  $(".imageNavigation ul li:first").click();
  InitTimer();
});

/****************    Rotator Handlers   *********************/
function ShowRotator() {
  $(".main_image .block").html(imageDescription).animate({ opacity: 0.85, marginBottom: "0" }, 500).slideToggle(500);

  $(".main_image").css({ "background-repeat": backgroundRepeat, "background-color": backColor1,
    "background-image": "url(" + imageBackground + ")"
  }).animate({ opacity: 1 }, 500);
  // get handle to element that wraps the image and make it semi-transparent
  return;
}
/*$(".main_image .block").css({ "text-align": textAlign, "padding": padding });*/
function HideRotatorText() {
  $(".main_image .block").slideToggle(1000);
  $(".main_image").animate({ opacity: 0 }, 1000);
  return;
}
function GetServerRotatorObject() {
  var currentIndex = (rotatorIndex.value -1);
  if (TimerRunning)
    StopTimer();
  HideRotatorText();
  PageMethods.GetRotatorObject(currentIndex, OnRotatorSuccess, OnRotatorFailure);
  return;
}
function OnRotatorSuccess(result) {
  imageURL = "";
  backgroundRepeat = "";
  textAlign = "";
  padding = "";
  imageDescription = "";

  imageURL = new String(result.HImage);
  imageBackground = result.HImageBG;
  backgroundRepeat = result.HImageBGRepeat;
  backColor1 = result.HBackground1;
  backColor2 = result.HBackground2;

  padding = result.HPadTop + "px " + result.HPadRight + "px " +
            result.HPadBottom + "px " + result.HPadLeft + "px";

  $("#tatorLink").attr("href", result.HLink);    
  if (result.HHeader.toLowerCase() != "fullimage") {
    if (imageURL.length > 0 && imageURL.indexOf("spacer") < 0)
      imageDescription = "<img src='" + imageURL.valueOf() + "' border='0' alt='' align='" + result.HImageAlign + "'" +
      "hspace='" + result.HImageHSpace + "' vspace='" + result.HImageVSpace + "' />";
    else if (imageURL.indexOf("spacer") >= 0)
      imageDescription += "<img src='" + imageURL.valueOf() + "' border='0' alt='' align='" + result.HImageAlign + "'" +
      "width='" + result.HImageHSpace + "' height='" + result.HImageVSpace + "' />";
    
    imageDescription += "<div align='" + result.HTextAlign + "' style='padding:" + padding + ";'>";

    imageDescription += "<b style='font-size:" + result.HHeaderSize + "px;'>" +
    "<a style='color:" + result.HHeaderColor + ";'" +
    " href='" + result.HLink + "' target='_top'>" + result.HHeader + "</a></b><br />";
    imageDescription += "<div style='color:" + result.HDetailColor + "; font-size:" + result.HDetailSize + "px;font-style:" + result.HDetailStyle + ";font-weight:" + result.HDetailWeight + "'>" +
    result.HDetails + "</div></div>";
    window.status = result.HImageBG;
  }
  else {

    imageDescription = "<div style='padding:" + padding + "; '>";
    imageDescription += "<a style='color:" + result.HHeaderColor + ";'" + " href='" +
    result.HLink + "' target='_top'>" ;

    if (imageURL.length > 0)
      imageDescription += "<img src='" + imageURL.valueOf() + "' border='0' alt='' " +
        "width='" + result.HImageHSpace + "' height='" + result.HImageVSpace + "' />";

    imageDescription += "</a></div>";
  }


  rotatorIndex.value = result.Index;
  //  debugText.value = imageDescription + "\r\n Current Index: " + rotatorIndex.value + 
  //    "\r\n" + imageURL + "\r\n" + imageBackground;

  return false;
}
function OnRotatorFailure(error) {
  //alert(error);
  return;
}

/****************    Role Switcher Handlers   *******************/
function SwitchRoles(newRole) {
  var role = new String(newRole.id).substr(3, newRole.id.length);
  //$(".ajax__tab_lulay_cat-theme .ajax__tab_tab").html(role.valueOf());
  if (newRole.className.indexOf("Active") < 0)
    GetRoleContent(role);
  return false;
}
function GetRoleContent(role) {
  PageMethods.SwitchRole(role, OnRoleSwitchSuccess, onRoleSwitchFailure);
}
function OnRoleSwitchSuccess(result) {
  var recomQueue = $(".recommendedLinks");
  recomQueue.queue(
    "linksQueue",
    function(next) {
      $(".recommendedLinks").slideUp(400);
      next();
    });
  recomQueue.delay(450, "linksQueue");

  recomQueue.queue(
    "linksQueue",
    function(next) {
      $(".roleTitle").html(result.Role);
      next();
    });
  recomQueue.delay(300, "linksQueue");

  recomQueue.queue(
    "linksQueue",
    function(next) {
  $(".recommendedLinks").html(result.RecommendedList);
      next();
    });
  recomQueue.delay(200, "linksQueue");

  recomQueue.queue(
    "linksQueue",
    function(next) {
      $(".recommendedLinks").slideDown(400);
      next();
    });
  recomQueue.delay(450, "linksQueue");

  recomQueue.dequeue("linksQueue");

  $("#feedBackTile").fadeOut("fast");
  var scrollerQueue = $(".scroller");
  scrollerQueue.queue(
  "scrollQueue",
  function(next) {
    $(".scroller").fadeOut(400);
    next();
  });
  scrollerQueue.delay(450, "scrollQueue");

  scrollerQueue.queue(
  "scrollQueue",
  function(next) {
    $(".scroller").unwrap();
    $(".simply-scroll-forward").remove();
    $(".simply-scroll-back").remove();
    $(".scroller").unwrap();
    next();
  });
  scrollerQueue.delay(150, "scrollQueue");

  scrollerQueue.queue(
  "scrollQueue",
  function(next) {
    $(".scroller").html(result.ApplicationList);
    next();
  });
  scrollerQueue.delay(150, "scrollQueue");

  scrollerQueue.queue(
  "scrollQueue",
  function(next) {
    $(".scroller").fadeIn(2000);
    next();
  });
  scrollerQueue.delay(450, "scrollQueue");

  scrollerQueue.queue(
  "scrollQueue",
  function(next) {
    $(".scroller").simplyScroll({
      className: 'custom',
      autoMode: 'loop',
      pauseOnHover: true,
      frameRate: 25,
      speed: 2,
      horizontal: false
    });
    next();
  });
  scrollerQueue.delay(150, "scrollQueue");

  scrollerQueue.queue(
  "scrollQueue",
  function(next) {
    $("#feedBackTile").fadeIn("fast");
    next();
  });

  scrollerQueue.dequeue("scrollQueue");
  window.status = result.Role;
  $(".buttonImage").each(
      function() {
        var objButton = this.children[0];
        var buttonName = new String(objButton.id);
        var className = new String(objButton.className);
        var optionName = new String(className.substring(0, className.indexOf("WAI", 0) + 3));
        $(objButton).removeClass(optionName + "Hover");
        $(objButton).removeClass(optionName + "Active");

        if (buttonName.toLowerCase().indexOf(result.Role.toLowerCase()) >= 0)
          $(objButton).addClass(optionName + "Active");
        else
          $(objButton).addClass(optionName + "");
      }
    );

  $(".searchBox").focus();
  return false;
}
function onRoleSwitchFailure(error) {
  return false;
}
/****************    Mouse Event Handlers   *********************/
function MouseEvent(button, action) {
  var className, optionName;
  if (null != button) {
    className = new String(button.className);
    optionName = new String(className.substring(0, className.indexOf("WAI", 0) + 3));
    switch (action.toLowerCase()) {
      case "hover":
        if (className.indexOf("active", 0) < 0) {
          $(button).addClass(optionName + "Hover");
        }
        break;
      case "out":
        if (className.indexOf("active", 0) < 0) {
          $(button).removeClass(optionName + "Hover");
        }
        break;
    }
  }
  return false;
}
function SearchMouseEvent(button, action) {
  var className, optionName;
  if (null != button) {
    className = new String(button.className);
    switch (action.toLowerCase()) {
      case "hover":
        if (className.indexOf("active", 0) < 0) {
          $(button).addClass("searchButton" + "Hover");
        }
        break;
      case "out":
        if (className.indexOf("active", 0) < 0) {
          $(button).removeClass("searchButton" + "Hover");
        }
        break;
    }
  }
  return false;
}
/*-----------------------------------------------------------------------------------------*/
/* Start Timer Functions*/
function InitTimer() //call the InitTimer function when u need to start the timer 
{
  mins = 0;
  secs = 10;
  StopTimer();
  StartTimer();
}
function StopTimer() {
  if (TimerRunning)
    clearTimeout(TimerID);
  TimerRunning = false;
}
function StartTimer() {
  var activeID = "";
  TimerRunning = true;
  TimerID = self.setTimeout("StartTimer()", 2000);

  if (mins == 0 && secs == 0) {
    StopTimer();
    $(".imageNavigation ul li").each(
      function(i) {
        if ($(this).is(".active"))
          activeID = this.id;
      });
    InitSelect(activeID);
  }
  if (secs == 0) {
    mins--;
    secs = 60;
  }
  secs--;
}
function InitSelect(activeID) {
  var nextID = new Number(activeID);
  var pagerCount = new Number(navigationCount);
  var foundControl = false;
  if (null == nextID || nextID == pagerCount)
    nextID = 1;
  else
    nextID += 1;
  if (null != nextID) {
    $(".imageNavigation ul li").each(
      function(i) {
        if (this.id == new String(nextID)) {
          foundControl = true;
          $(this).click();
        }
      });
  }
  if (!foundControl) {
    $(".imageNavigation ul li:first").removeClass('active');
    $(".imageNavigation ul li:first").click();
  }
  return;
}

/* End Timer Functions*/
/*-----------------------------------------------------------------------------------------*/

