﻿/// <reference path="jquery-1.3.2.js" />

var _iCategoryShowCaseTimeoutId;
var _oCategoryShowCaseToLoadAsync = new Array();

function initCategoryShowCase() {
  $j(document).ready(function()
  {
    var oImagesToCache = new Array();
    var sIdCategoryFirstToShow;

    $j(document).find("a").click(function() { cancelAjaxRequests(); });
    $j(window).unload(function() { cancelAjaxRequests(); });

    $j(".categoryShowCaseSelector").each(function(i)
    {

      $j("#" + this.id).mouseenter(function() { if ($j("#" + this.id).attr("src") != $j("#" + this.id).attr("ImageUrlSelected")) { categoryShowCaseShowCategoryAfterDelay(this.id, 200); } });
      $j("#" + this.id).mouseleave(function() { if (_iCategoryShowCaseTimeoutId) { clearTimeout(_iCategoryShowCaseTimeoutId); } });

      if (i == 0)
      { /* Load the first category */
        setTimeout("categoryShowCaseShowCategory('" + this.id + "');", 50);
      }
      else
      { /* Add other categories in a queue to preload them later */
        _oCategoryShowCaseToLoadAsync.push(this.id);
        //setTimeout("loadCategoryShowCaseItemAsync('" + this.id + "');", 50);
      }

      oImagesToCache.push(this.getAttribute("ImageUrlSelected"));
    });

    $j.cacheImage(oImagesToCache);
    $j(".categoryShowCaseTitleImage").cacheImage();
  });
}

function categoryShowCaseAjaxRequestEnd(sender, eventArgs)
{
  // Preload next category
  if (_oCategoryShowCaseToLoadAsync.length > 0)
  {
    setTimeout("loadCategoryShowCaseItemAsync('" + _oCategoryShowCaseToLoadAsync.pop() + "');", 50);
  }
}

function cancelAjaxRequests()
{
  var oPageRequestManager = Sys.WebForms.PageRequestManager.getInstance();

  if (oPageRequestManager != null && oPageRequestManager != null && oPageRequestManager.get_isInAsyncPostBack())
  {
    oPageRequestManager.abortPostBack();
  }
}

function categoryShowCaseShowCategoryAfterDelay(vsIdCategoryImage, viDelay) {
  if (_iCategoryShowCaseTimeoutId) {
    clearTimeout(_iCategoryShowCaseTimeoutId);
  }

  _iCategoryShowCaseTimeoutId = setTimeout(function() { categoryShowCaseShowCategory(vsIdCategoryImage); }, viDelay);
}

function categoryShowCaseShowCategory(vsIdCategoryImage) {
  var oCategoryImage = $j("#" + vsIdCategoryImage);

  hideAllCategoryShowCaseItems();

  oCategoryImage.attr("src", oCategoryImage.attr("ImageUrlSelected"));

  $j("#" + oCategoryImage.attr("idTitleImage")).fadeIn(400);

  loadCategoryShowCaseItemAsync(vsIdCategoryImage);

  $j("#" + oCategoryImage.attr("idDivItem")).show();
  $j("#" + oCategoryImage.attr("idDivItem")).find(".categoryShowCaseFadeContent").fadeIn(400);
}

function hideAllCategoryShowCaseItems()
{
  $j(".categoryShowCaseItem").each(function(i) {
    $j("#" + this.id).find(".categoryShowCaseFadeContent").hide();
    $j("#" + this.id).hide();
  });

  $j(".categoryShowCaseSelector").each(function(i) {
    var oCategoryImage = $j("#" + this.id);
    oCategoryImage.attr("src", oCategoryImage.attr("ImageUrlUnSelected"));
    $j("#" + oCategoryImage.attr("idTitleImage")).hide();
  });
}

function loadCategoryShowCaseItemAsync(vsIdCategoryImage) {
  var oCategoryImage = $j("#" + vsIdCategoryImage);
  var oDivItem = $j("#" + oCategoryImage.attr("idDivItem"));
  var oAjaxManager;

  if (oDivItem.find("#divItemNotLoaded").length > 0) {
    oAjaxManager = getRadAjaxManager();
    
    if (oAjaxManager && oDivItem) {
      oAjaxManager.ajaxRequestWithTarget(oDivItem.attr("idCmdLoad"), "");
    }
  }  
}
