﻿

$(document).ready(function() {

  $("body").height($(document).height() + 1);
  $("#home").addClass("delink");
  $("#tutorial").addClass("act");

  subMenuImageAttributes();

  $(".link").click(function() {
    var url;

    $("#thirdLevelMenu").animate({
      opacity: 0.4
    });

    $(".link").each(function(i, element) {

      allMenuItems(i, element, "delink");

    });
    $(this).addClass("delink");

    if (location.href.toLowerCase().match("wordml") != null)
      url = "/WordML/" + $(this).attr("id") + "Partial";
    if (location.href.toLowerCase().match("spreadsheetml") != null)
      url = "/SpreadSheetML/" + $(this).attr("id") + "Partial";
    if (location.href.toLowerCase().match("presentationml") != null)
      url = "/PresentationML/" + $(this).attr("id") + "Partial";

    $.ajax({
      url: url,
      type: "post",
      data: "",
      success: function(result) {

        $("#thirdLevelMenus").html(result);
        $("thirdLevelMenus").animate({
          opacity: 1
        });

        subMenuImageAttributes();
        subMenuClickEventBinding();

      },
      error: function() {
        //alert("Error");
      }
    });


  });

  $("#video").click(function() {

    $(".right_container").animate({
      opacity: 0.4
    });

    $(".inactive").each(function(i, element) {
      allMenuItems(i, element, "act");
    });

    $(".right_container").height($(".right_container").height());

    $(this).addClass("act");

    $.ajax({
      url: "/Menu/VideoPartial",
      type: "post",
      data: "",
      success: function(result) {

        $(".right_container").html(result);
        $(".right_container").height('100%');
        $(".right_container").animate({
          opacity: 1
        });
      },
      error: function(result) {
        //alert(result.status);
      }
    });

  });

  $("#samples").click(function() {

    $(".right_container").animate({
      opacity: 0.4
    });

    $(".inactive").each(function(i, element) {
      allMenuItems(i, element, "act");
    });

    $(this).addClass("act");

    $.ajax({
      url: "/Menu/SamplesPartial",
      type: "post",
      data: "",
      success: function(result) {

        $(".right_container").html(result);
        $(".right_container").height('100%');
        $(".right_container").animate({
          opacity: 1
        });

      },
      error: function(result) {
        //alert(result.status);
      }
    });

  });

  $(".tutorials").click(function() {
    //alert("tutorials click");

    $(".inactive").each(function(i, element) {
      allMenuItems(i, element, "act");
    });

    $(this).addClass("act");

    $.ajax({
      url: "/Menu/TutorialPartial",
      type: "post",
      data: "thirdLevelMenuId=" + $(this).attr("thirdLevelMenuId"),
      success: function(result) {

        $(".right_container").html(result);
        $(".right_container").height('100%');
        $(".right_container").animate({
          opacity: 1
        });

      },
      error: function(result) {
        //alert(result.status);
      }
    });

  });

  $("#forum").click(function() {

    $(".right_container").animate({
      opacity: 0.4
    });

    $(".inactive").each(function(i, element) {
      allMenuItems(i, element, "act");
    });

    $(this).addClass("act");

    $.ajax({
      url: "/Menu/VideoPartial",
      type: "post",
      data: "",
      success: function(result) {

        $(".right_container").html(result);
        $(".right_container").height('100%');
        $(".right_container").animate({
          opacity: 1
        });

      },
      error: function(result) {
        //alert(result.status);
      }
    });

  });

  $(".menuItem").click(function() {
    //Load top menu

    $.ajax({
      url: "/wordml/index",
      type: "post",
      data: "",
      success: function(result) {

        //alert(result);
        $("#second_lev_menu_container").html(result);

      },
      error: function() {
        //alert("Error");
      }
    });

    // Load sub menu

    $.ajax({
      url: "/WordML/HomePartial",
      type: "post",
      data: "",
      success: function(result) {

        //alert(result);
        $("#thirdLevelMenu").html(result);

      },
      error: function() {
        //alert("Error");
      }
    });

    // Load left links

    // Load right content

  });

  $(".subMenu").click(function(object) {
    //alert("submenu");
    subMenuImageAttributes();
    subMenuItemClick(this);
  });



  $(".tabLinkBtn").click(function() {
    $(".content").each(function() {
      $(this).css({ 'display': 'none' });
    });
    //alert("hhh");

    var divToShow = $(this).attr("title");
    $("#" + divToShow).html(divToShow);
    $("#" + divToShow).fadeIn("slow");
    $(".edit").attr({ 'title': divToShow });
    //        alert($("#" + divToShow).text());
    //        $(divToShow).html("ricky");
    //        $("#mainContent").slideUp("slow");
    //        $("#mainContent").html("");
    //        $("#mainContent").html($("#" + divToShow).innerHTML);
    //        $("#mainContent").fadeIn("slow");

  });

  $(".smallIcon").mouseover(function() {
    if ($(this).attr("clickState") == "true")
      return;
    var src = $(this).attr("src").replace("_normal", "_over");
    $(this).attr({ "src": src });

  });

  $(".smallIcon").mouseout(function() {
    if ($(this).attr("clickState") == "true")
      return;
    var src = $(this).attr("src").replace("_over", "_normal");
    $(this).attr({ "src": src });

  });

  $(".firstLevelMenu").click(function() {

    var firstLevelMenuId = $(this).attr("childMenuId");
    //alert("firstLevelMenu");
    //    $.ajax({
    //      url: "/Content/GetSecondLevelMenus",
    //      type: "post",
    //      data: "firstLevelMenuId=" + firstLevelMenuId,
    //      success: function(result) {

    //alert(result);

    if ($(this).attr("childNodeShowing") == "false") {
      $("#secondLevelMenu-" + firstLevelMenuId).css({ "display": "block" });
      $(this).attr({ "childNodeShowing": "true" });
    } else {
      $("#secondLevelMenu-" + firstLevelMenuId).css({ "display": "none" });
      $(this).attr({ "childNodeShowing": "false" });
    }

    //      },
    //      error: function() {
    //        alert("Error");
    //      }
    //    });

  });

  $(".secondLevelMenu").click(function() {
    //alert("Hillo 1");
    var thirdLevelMenuId = $(this).attr("thirdLevelMenuId");
    var secondLevelMenuId = $(this).attr("firstLevelMenuId");
    //alert(secondLevelMenuId);
    //  $.ajax({
    //    url: "/Content/GetThirdLevelMenus",
    //    type: "post",
    //    data: "secondLevelMenuId=" + secondLevelMenuId,
    //    success: function(result) {

    //      //alert(result);
    if ($(this).attr("childNodeShowing") == "false") {

      $("#secondLevelMenu-" + secondLevelMenuId).css({ "display": "block" });
      $("#thirdLevelMenu-" + thirdLevelMenuId).css({ "display": "block" });
      $(this).attr({ "childNodeShowing": "true" });
    }
    else {
      $("#thirdLevelMenu-" + thirdLevelMenuId).css({ "display": "none" });
      $(this).attr({ "childNodeShowing": "false" });
    }

    //    },
    //    error: function() {
    //      alert("Error");
    //    }
    //  });

  });

  $(".thirdLevelMenu").click(function(event) {
    //alert($(this).attr("id"));
    var thirdLevelMenuId = $(this).attr("secondLevelMenuId");
    var secondLevelMenuId = $(this).attr("firstLevelMenuId");

    if ($(this).attr("childNodeShowing") == "false") {
      $("#secondLevelMenu-" + secondLevelMenuId).css({ "display": "block" });
      $("#thirdLevelMenu-" + thirdLevelMenuId).css({ "display": "block" });
      $("#thirdLevelMenu-" + $(this).attr("id")).css({ "display": "block" });
      $("#options-" + $(this).attr("id")).css({ "display": "block" });

      $("#" + $(this).attr("id") + "_Tutorial").click();

      $(this).attr({ "childNodeShowing": "true" });

      if ($(".selectedIcon").attr("src") != undefined) {
        var selectedIconNewSrc = $(".selectedIcon").attr("src").replace("_over", "_normal");
        $(".selectedIcon").attr({ "src": selectedIconNewSrc });
        $(".selectedIcon").removeClass("selectedIcon");
      }
      var thirdLevelMenuIconSrc = $("#thirdLevelMenuIcon-" + $(this).attr("thirdLevelMenuId")).attr("src").replace("_normal", "_over");
      $("#selectedIcon").css({ "top": $("#thirdLevelMenuIcon-" + $(this).attr("thirdLevelMenuId")).position().top });
      $("#selectedIcon").css({ "left": $("#thirdLevelMenuIcon-" + $(this).attr("thirdLevelMenuId")).position().left });
      $("#selectedIconImage").attr({ "src": thirdLevelMenuIconSrc });
      $("#selectedIcon").fadeIn(5);

      $("#thirdLevelMenuIcon-" + $(this).attr("thirdLevelMenuId")).attr({ "src": thirdLevelMenuIconSrc });
      $("#thirdLevelMenuIcon-" + $(this).attr("thirdLevelMenuId")).addClass("selectedIcon");

      $("#selectedIcon").fadeOut(5);

    }
    else {
      if (event.button != 0)
        return;
      //alert($(this).attr("id"));
      //alert("else fired !");
      $("#options-" + $(this).attr("id")).css({ "display": "none" });
      $(this).attr({ "childNodeShowing": "false" });
    }

  });

  $(".smallIcon").click(function() {
    //alert(" asdf ");
    var menuItemId = $(this).attr("targetButtonId").replace("_Tutorial", "");
    //alert(menuItemId);
    $("#" + menuItemId).click();
    $("#" + $(this).attr("targetButtonId")).click();
  });

});

function getThirdLevelMenuItems(object) {
  //alert("Hillo !");
  var thirdLevelMenuId = $(object).attr("thirdLevelMenuId");
  var secondLevelMenuId = $(object).attr("firstLevelMenuId");
  //alert(secondLevelMenuId);
  //  $.ajax({
  //    url: "/Content/GetThirdLevelMenus",
  //    type: "post",
  //    data: "secondLevelMenuId=" + secondLevelMenuId,
  //    success: function(result) {

  //      //alert(result);
  $("#secondLevelMenu-" + secondLevelMenuId).css({ "display": "block" });
  $("#thirdLevelMenu-" + thirdLevelMenuId).fadeIn("slow");


  //    },
  //    error: function() {
  //      alert("Error");
  //    }
  //  });

}

function thirdLevelMenuItemClick(object) {
  //alert("thirdLevel");
  if ($(".selectedIcon").attr("src") != undefined) {
    var selectedIconNewSrc = $(".selectedIcon").attr("src").replace("_over", "_normal");
    $(".selectedIcon").attr({ "src": selectedIconNewSrc });
    $(".selectedIcon").removeClass("selectedIcon");
  }
  var thirdLevelMenuIconSrc = $("#thirdLevelMenuIcon-" + $(object).attr("thirdLevelMenuId")).attr("src").replace("_normal", "_over");
  $("#selectedIcon").css({ "top": $("#thirdLevelMenuIcon-" + $(object).attr("thirdLevelMenuId")).position().top });
  $("#selectedIcon").css({ "left": $("#thirdLevelMenuIcon-" + $(object).attr("thirdLevelMenuId")).position().left });
  $("#selectedIconImage").attr({ "src": thirdLevelMenuIconSrc });
  $("#tblSecondLevelMenu").animate({
    opacity: 0.2
  },
    { duration: 5000
    });
  $("#selectedIcon").fadeIn(5000);
  //alert("Top - " + $("#thirdLevelMenuIcon-" + $(object).attr("id")).position().top + " left - " + $("#thirdLevelMenuIcon-" + $(object).attr("id")).position().left);

  $("#thirdLevelMenuIcon-" + $(object).attr("thirdLevelMenuId")).attr({ "src": thirdLevelMenuIconSrc });
  $("#thirdLevelMenuIcon-" + $(object).attr("thirdLevelMenuId")).addClass("selectedIcon");

  $("#selectedIcon").fadeOut(5000);
  $("#tblSecondLevelMenu").animate(
  { opacity: 1 },
  { duration: 5000 });
}

function allMenuItems(index, domElement, className) {
  $(domElement).removeClass(className);
}

function subMenuItemClick(object) {

  var newSrc;

  newSrc = $(object).attr("src").replace("normal", "over");

  $(object).attr("src", newSrc);
  $(object).addClass("subMenuSelected");

  $(object).mouseover(function() {
  });

  $(object).mouseout(function() {
    newSrc = $(this).attr("src").replace("normal", "over");
    $(this).attr("src", newSrc);
  });

  var officeDocument;

  if (location.href.toLowerCase().match("wordml") != null)
    officeDocument = "WordML";
  if (location.href.toLowerCase().match("spreadsheetml") != null)
    officeDocument = "SpreadSheetML";
  if (location.href.toLowerCase().match("presentationml") != null)
    officeDocument = "PresentationML";

  var firstLevelMenu = $(".delink").attr("id");
  var secondLevelMenu = $(object).attr("alt");

  var data = "officeDocument=" + officeDocument + " &firstLevelMenu=" + firstLevelMenu + " &secondLevelMenu=" + secondLevelMenu;

  //alert(data);
  $("#visiblePagePath").val(data);

  $.ajax({
    url: "/Content/GuidePartial",
    type: "post",
    data: data,
    success: function(result) {
      $(".content").each(function() {
        $(this).css({ 'display': 'none' });
      });
      $("#document").fadeOut("slow");
      $("#document").html(result);
      $("#document").fadeIn("slow");

      //alert(result);
      //alert(result);

    },
    error: function(result) {
      //alert(result.status);
    }
  });
}

function subMenuImageAttributes() {

  $(".subMenu").each(function(i, element) {
    var newSrc;
    newSrc = $(element).attr("src").replace("over", "normal");
    $(element).attr("src", newSrc);

    $(element).mouseover(function() {
      newSrc = $(this).attr("src").replace("normal", "over");
      $(this).attr("src", newSrc);
    });

    $(element).mouseout(function() {
      newSrc = $(this).attr("src").replace("over", "normal");
      $(this).attr("src", newSrc);
    });
  });

}

function subMenuClickEventBinding() {

  $(".subMenu").each(function(i, element) {

    $(element).click(function() {
      subMenuImageAttributes();
      subMenuItemClick(element);
    });
  });

}


