﻿MyNewID = "";
window.onload = function() {
  try {
    load();
  }
   catch(err) {
   //Handle errors here
  }
}
 
 
 
// OnLoadActions
$(function(){
  
  // Top Menu
  $("#TopMenu ul ul li").pngfix({sizingMethod: "scale"});
  $("#TopMenu #nav ul").css({display: "none"}); // Opera Fix
  $("#TopMenu #nav li").hover(function(){
    $(this).find('ul:first').css({visibility: "visible",display: "none"}).show(10);
  },function(){
    $(this).find('ul:first').css({visibility: "hidden"});
  });
  
  // Right Column Tabs
  $("#ContentRight .tab:not(:first)").slideUp();
  $("#ContentRight .tab:first").slideDown();
  $('#ContentRight .tabNavigation  a:last').addClass('activeTab');
  $("#ContentRight .tabNavigation a").click(function(){
    stringref = $(this).attr("rel").split('#')[1];
    $('#ContentRight .tabNavigation  a').removeClass('activeTab');
    $(this).addClass('activeTab'); 
    $('#ContentRight .tab:not(#'+stringref+')').slideUp();
    $('#ContentRight .tab#' + stringref).slideDown();
  });
  
  // eCommerce actions
  // Activate Product Horizontal Bar Selection
  $("#jQueryProductList .h2").click(function() {
    $(this).addClass('activeArrow').next("div.ProductList").slideToggle(300, function() {
      if ($(this).children(".MainGroupDescription").html() != "" && $(this).children(".MainGroupDescription").height() != "0") {
        DescriptionHeight = parseInt($(this).children(".MainGroupDescription").height());
        $(this).children(".MainGroupDescription").css("height", DescriptionHeight + "px");
        $(this).children(".MenuProduct").css("padding-top", "20px");
        $(this).children(".GroupLinks").css("padding-top", "20px");
      }
    }).siblings("div.ProductList").slideUp("slow");
    $(this).siblings().removeClass('activeArrow');
  });
  
  // CSS management for Product/Categories Vertical menus
  $(".MenuProduct ul.VerticalMenu li").click(function() {
    $(this).addClass('activeTab');
    $(this).siblings().removeClass('activeTab');
  });
  
  // Sort Categories and Products
  $(".VerticalMenu").each(function(i) {
    TheId = $(this).parent().attr("id");
    $("#" + TheId + " li.Group").tsort({attr:'sort'});
  });
  
  // Product sheet tabs
  $("#ProductTabs td:last").css("border-right-width", "1px");
  
  // Other specific pages actions
  // Activate History Selection
  $('div.ShowHideContentor').hide();
  if ($("#psHistoria").length > 0){
    $('#TopGraphic').slideUp("slow");
  }
  $("#psHistoria .h2").click(function() { 
    $(this).next("div.ShowHideContentor").slideToggle(300).siblings("div.ShowHideContentor").slideUp("slow");
    $(this).addClass('triangleopened');
    $(this).siblings().removeClass('triangleopened');
    if(MyNewID != ""){
      $("#" + MyNewID + " a").css({'color' : '#666666'});
    }
    MyNewID = $(this).attr('id');
    ImgSrc = $("#psHistoria #HistoryImage" + MyNewID + " img").attr('src');
    $("#TopGraphic").slideDown("slow");
    $("#TopGraphic img").attr({'src':ImgSrc});
    $("#" + MyNewID + " a").css({'color' : '#000000'});
  });
    
  // Correct Reference Constructions spacing
  $(".ContentGrey ~ br").css({"line-height":"20px"});
  
  // Put Id's in input file types
  $("input[type='file']").each(function(i) {
    $(this).attr("id", "FileInput" + i);
  });
  
  // Forms module manipulation
  CheckForFormsModule();
  $(".T11-B11_White2 .T11-B11_text").css({"width":$(".T11-B11_White2").parent().width() + "px", "height":$(".T11-B11_White2").parent().height() + "px"});
  
  // IE 6 PNG fix
  $("img, div").pngfix();
  
  // Check for Newsletter Cancel
  TheCancelEmail = $.getURLParam("NewsletterCancel");
  if (TheCancelEmail != null) {
    ReloadNewsletter(TheCancelEmail);
  }
});
 
 
 
function eComParagraphSetupLoaded() {
  ProductID = $.getURLParam("ShowProduct");
  if (ProductID != null) {
    ShowProduct2(ProductID);
  }
  
  GroupID = $.getURLParam("ShowGroup");
  if (GroupID != null) {
    ShowGroup2(GroupID);
   }
}
 
 
 
// Search function
function GetSearchTag(errorMessage,ThePageId) {
  var MySearchTag = document.getElementById("SearchBox").value;
  if(MySearchTag != ""){
    window.location = "Default.aspx?ID=" + ThePageId + "&eComSearch=1&ProductName=" + MySearchTag;
  }
  else {
    alert(errorMessage);
  }
}
 
 
 
// Change History Image
function ChangeImage(imgID, imgSrc) {
 $("#MenuImg" + imgID).attr({'src':imgSrc});
}
 
 
 
// eCom functions 
function CheckGroupLevel(GroupId, PageId, GroupCatalogPDF, GroupIpaperId) {
  CurrentGroup = $('.GroupLevel1 li[GroupID="' + GroupId + '"]');
  GroupLevel = $(CurrentGroup).attr("GroupLevel");
  if (GroupLevel == 2) LoadLevel2Group(GroupId, PageId, GroupCatalogPDF, GroupIpaperId);
  else if (GroupLevel == 3) LoadLevel3Group(GroupId);
}
 
function LoadLevel2Group(GroupId, PageId, GroupCatalogPDF, GroupIpaperId) {
  if ($("#jQueryProductList").length == 0) {
    $("#Content .ParagraphProducts").prepend('<div id="jQueryProductList"></div>');
  }
  $("#jQueryProductList").append('<h2 class="h2 ParagraphProductHeader" id="Header_' + GroupId + '">' + $(CurrentGroup).attr("GroupName") + '</h2>');
  $("#jQueryProductList").append('<div class="ProductList" id="'+ GroupId + '_Content"></div>');
  $("#" + GroupId + "_Content").append('<div id="' + GroupId + '_MainDescription" class="MainGroupDescription">'+$("#Description_" + GroupId).html()+'</div>');
  $("#" + GroupId + "_Content").append('<div class="MenuProduct" id="' + GroupId + '_Menu"><ul class="VerticalMenu"></ul></div>');
  if (GroupCatalogPDF != undefined || GroupIpaperId != undefined) {
    $("#" + GroupId + "_Content").append('<div class="GroupLinks" id="' + GroupId + '_Links"></div>');
    if (GroupCatalogPDF != undefined && GroupCatalogPDF != "") $("#" + GroupId + "_Links").append('<div class="GroupCatalog" id="' + GroupId  + '_GroupCatalog"><a href="Admin/Public/Download.aspx?File=' + GroupCatalogPDF + '">Download do Catálogo</a></div>');
    if (GroupIpaperId != undefined && GroupIpaperId != "") {
      $("#" + GroupId + "_Links").append('<div class="iPaperCatalog" id="' + GroupId  + '_iPaperCatalog"><a href="javascript:showIpaper(' + GroupIpaperId + ');">Ver Catálogo</a></div>');
    }
  }
  $("#" + GroupId + "_Content").append('<div style="clear:both;height:1px;overflow:hidden;"></div>');
  $("#" + GroupId + "_Content").append('<div id="' + GroupId + '_Description" class="GroupDescription"></div>');
  $("#" + GroupId + "_Content").append('<div id="' + GroupId + '_Select3" class="GroupSelect"></div>');
  $("#" + GroupId + "_Content").append('<div id="' + GroupId + '_Select4" class="GroupSelect"></div>');
  $("#" + GroupId + "_Content").append('<div id="' + GroupId + '_Select5" class="GroupSelect"></div>');
  $("#" + GroupId + "_Content").append('<div id="' + GroupId + '_ProductIframe" class="GroupProductIframe"><iframe frameborder="0" height="0" scrolling="no" width="100%"></iframe></div>');
}
 
function CheckProductGroup(ProductId) {
  ProductGroup = $('#Li_' + ProductId).attr('groupId');
  ProductGroupIdLevel = $('.GroupLevel1 li[GroupID="' + ProductGroup + '"]').attr("GroupLevel");
  if (ProductGroupIdLevel == 2) LoadLevel2Product(ProductId, ProductGroup);
}
 
function LoadLevel3Group(GroupId) {
  ParentGroupId = $('.GroupLevel1 li[GroupID="' + GroupId + '"]').parent().parent().attr("GroupId");
  ParentSortOrder = $('.GroupLevel1 li[GroupID="' + GroupId + '"]').attr("Sort");
  
  $("#" + ParentGroupId + "_Menu ul").append('<li class="Group" type="group" sort="' + ParentSortOrder + '"><div id="MenuItem' + GroupId + '" class="MenuItem"><a rel="#Li' + GroupId + '">' + $('.GroupLevel1 li[GroupID="' + GroupId + '"]').attr("GroupName") + '</a></div></li>');
  $(function() {
    $("#MenuItem" + GroupId + " a").click(function() {
      CheckMenuItem(GroupId);
    });
  });
}
   
function LoadLevel2Product(ProductId, GroupId) {
  
  $("#" + GroupId + "_Menu ul").prepend('<li class="Product"><div id="MenuItem' + ProductId + '" class="MenuItem"><a rel="#Li' + ProductId + '">' + $('#Li_' + ProductId).attr("name") + '</a></div></li>');
  
  $(function() {
    $("#MenuItem" + ProductId + " a").click(function() {
      CheckMenuItem(ProductId);
    });
  });
}
  
function CheckMenuItem(MenuItemId) {
  if ($(".HiddenProductList li[id='Li_" + MenuItemId + "']").length > 0) {
    ProductGroup = $('#Li_' + MenuItemId).attr('groupId');
    
    TopGroupId = $('.GroupLevel1 li[GroupID="' + MenuItemId + '"]').attr("TopGroupId");
    ClickedLevel = parseInt($('.GroupLevel1 li[GroupID="' + ProductGroup + '"]').attr("GroupLevel")) + 1;
    if (ClickedLevel == 3) {
//      $("#" + TopGroupId + "_Content .GroupSelect").html("");
    }
    if (ClickedLevel == 4) {
//      $("#" + TopGroupId + "_Select4").html("");
    }
    
    LoadProductIframe('Li_' + MenuItemId, ProductGroup);
  }
  else {
    SelectLevel = $('.GroupLevel1 li[GroupID="' + MenuItemId + '"]').attr("GroupLevel");
    ClickedLevel = parseInt(SelectLevel);
    TopGroupId = $('.GroupLevel1 li[GroupID="' + MenuItemId + '"]').attr("TopGroupId");
    if (SelectLevel <= 4) $("#" + TopGroupId + "_Select5").html("");
    if (SelectLevel <= 5) $("#" + TopGroupId + "_Select4").html("");
    $("#" + TopGroupId + "_Select" + SelectLevel).html('<div class="ProductSelectBackground"><select id="GroupsProductsList" name="GroupsProductsList"></select><\/div>');
    $(".HiddenProductList li[groupId='" + MenuItemId + "']").each( function() {
      $("#" + TopGroupId + "_Select" + SelectLevel + " select").prepend('<option type="product" value="' + $(this).attr("id") + '">' + $(this).attr("name") + '</option>');
    });
    $(".GroupLevel1 li[GroupID='" + MenuItemId + "'] li").each( function() {
      $("#" + TopGroupId + "_Select" + SelectLevel + " select").append('<option type="group" value="' + $(this).attr("GroupID") + '">' + $(this).text() + '</option>');
    });
    $("#" + TopGroupId + "_Select" + SelectLevel + " select").prepend('<option value="none" selected="selected">Seleccione uma opção</option>');
    $("#" + TopGroupId + "_Select" + SelectLevel + " select").val('none');
    $("#" + TopGroupId + "_ProductIframe").slideUp("slow");
    
    // Check for description
    if ($('#Description_' + MenuItemId).html() != "") {
      $("#" + TopGroupId + "_Description").css("padding-bottom", "20px");
      $("#" + TopGroupId + "_Description").slideUp("slow", function() {
        $("#" + TopGroupId + "_Description").html($('#Description_' + MenuItemId).html());
        $("#" + TopGroupId + "_Description").slideDown("slow");
      });
    }
    else {
      $("#" + TopGroupId + "_Description").css("padding-bottom", "0px");
    }
    
    // Check for PDF and iPaper
    if ($('#Description_' + MenuItemId).attr("PDF") != "") {
      $("#" + TopGroupId + "_GroupCatalog a").attr("href", "Admin/Public/Download.aspx?File=" + $('#Description_' + MenuItemId).attr("PDF"));
    }
    if ($('#Description_' + MenuItemId).attr("iPaper") != "") {
      iPaperId = $('#Description_' + MenuItemId).attr("iPaper");
      $("#" + TopGroupId + "_iPaperCatalog a").attr("href", "javascript:showIpaper(" + iPaperId + ");");
    }
    
    // Activate Dropdown function
    ActivateOnChange(TopGroupId, SelectLevel);
  }
}
  
function ActivateOnChange(TopGroupId, SelectLevel) {
  $(function() {
    $("#" + TopGroupId + "_Select" + SelectLevel + " select").change(function() {
      OptionType = $("#" + TopGroupId + "_Select" + SelectLevel + " select :selected").attr("type");
      if (OptionType == "product") {
        LoadProductIframe($(this).val(), TopGroupId);
      }
      else if (OptionType == "group") {
        $("#" + TopGroupId + "_ProductIframe").slideUp("slow");
        CheckMenuItem($(this).val());
      }
      else {
        $("#" + TopGroupId + "_ProductIframe").slideUp("slow", function() {
          $("#" + TopGroupId + "_ProductIframe").css('height', '0');
          $("#" + TopGroupId + "_ProductIframe").css('overflow', 'hidden');
          $("#" + TopGroupId + "_ProductIframe").css('display', 'block');
        });
      }
    });
  });
}
 
function LoadProductIframe(productId, TopGroupId) {
  ProductGroup = $('#' + productId).attr('groupId');
  ProductGroupIdLevel = $('.GroupLevel1 li[GroupID="' + ProductGroup + '"]').attr("GroupLevel");
//  if (ProductGroupIdLevel < 5) { $("#" + TopGroupId + "_Select5").slideUp("slow"); }
//  if (ProductGroupIdLevel < 4) { $("#" + TopGroupId + "_Select4").slideUp("slow"); }
//  if (ProductGroupIdLevel < 3) { $("#" + TopGroupId + "_Select3").slideUp("slow"); }
  if (ProductGroupIdLevel < 5) { $("#" + TopGroupId + "_Select5").html(""); }
  if (ProductGroupIdLevel < 4) { $("#" + TopGroupId + "_Select4").html(""); }
  if (ProductGroupIdLevel < 3) { $("#" + TopGroupId + "_Select3").html(""); }
  
  productId = productId.substring(3, productId.length);
  $("#" + TopGroupId + "_ProductIframe").slideUp("slow", function() {
    $("#" + TopGroupId + "_ProductIframe").css({'height':'0','overflow':'hidden','display':'block'});
    CheckMyPageID(MyPageId, TopGroupId, productId);
  });
  
  // Check for PDF and iPaper
  if ($('#Li_' + productId).attr("PDF") != "") {
    $("#" + TopGroupId + "_GroupCatalog a").attr("href", "Admin/Public/Download.aspx?File=" + $('#Li_' + productId).attr("PDF"));
  }
  if ($('#Li_' + productId).attr("iPaper") != "") {
    iPaperId = $('#Li_' + productId).attr("iPaper");
    $("#" + TopGroupId + "_iPaperCatalog a").attr("href", "javascript:showIpaper(" + iPaperId + ");");
  }
}
 
function iFrameLoaded() {
  $('.GroupProductIframe').each(function() {
    if ($(this).css('height') == '0px' || $(this).css('height') == '0pt') {
      $(this).css({'display':'none','height':'auto'});
      $(this).slideDown("slow");
    }
  });
}
 
function CheckImportField(FormName) {
  $("form[name='" + FormName + "'] input[type='file']").each(function() {
    TheId = $(this).attr("id");
    if (document.getElementById(TheId).value != "") {
      document[FormName].action = "/Admin/Public/FormMail.aspx?mode=20";
      document[FormName].submit();
    }
    else {
      alert("O campo Curriculum é de preenchimento obrigatório");
    }
  });
}
 
 
 
//  Product URL params or Href links
function ShowProduct(ProductID) {
  ShowProduct2(ProductID);
}
 
function ShowProduct2(ProductID) {
  ProductGroup = $('#Li_' + ProductID).attr('groupId');
  ProductGroupIdLevel = $('.GroupLevel1 li[GroupID="' + ProductGroup + '"]').attr("GroupLevel");
  
  Group2Open = ProductGroup;
  Group2LevelFlag = ProductGroupIdLevel;
  
  // Find Level 2 group to open
  while (Group2LevelFlag > 2) {
    Group2LevelFlag = $('.GroupLevel1 li[GroupID="' + Group2Open + '"]').parent().parent().attr("GroupLevel");
    Group2Open = $('.GroupLevel1 li[GroupID="' + Group2Open + '"]').parent().parent().attr("GroupID");
  }
  
  // New variables for GroupLevels
  Level2Group = "";
  Level3Group = "";
  Level4Group = "";
  if (ProductGroupIdLevel == 4) {
    Level4Group = ProductGroup;
    Level3Group = $('.GroupLevel1 li[GroupID="' + Level4Group + '"]').parent().parent().attr("GroupID");
    Level2Group = $('.GroupLevel1 li[GroupID="' + Level3Group + '"]').parent().parent().attr("GroupID");
  }
  else if (ProductGroupIdLevel == 3) {
    Level3Group = ProductGroup;
    Level2Group = $('.GroupLevel1 li[GroupID="' + Level3Group + '"]').parent().parent().attr("GroupID");
  }
  else if (ProductGroupIdLevel == 2) {
    Level2Group = ProductGroup;
  }
  // End of new variables
  
  $("#jQueryProductList .h2").siblings().removeClass('activeArrow');
  $("#Header_" + Group2Open ).addClass('activeArrow').next("div.ProductList").slideToggle(300, function() {
    if ($(this).children(".MainGroupDescription").html() != "") {
      DescriptionHeight = parseInt($(this).children(".MainGroupDescription").height()) + 20;
      $(this).children(".MainGroupDescription").css("height", DescriptionHeight + "px");
    }
  }).siblings("div.ProductList").slideUp("slow", function() {
    if (ProductGroupIdLevel == 2) {
      $("a[rel='#Li" + ProductID + "']").parent().parent().addClass('activeTab');
      CheckMenuItem(ProductID);
    }
    else if (ProductGroupIdLevel == 3) {
      $("a[rel='#Li" + Level3Group + "']").parent().parent().addClass('activeTab');
      LoadSelect(3, Level2Group, ProductGroup, ProductID);
    }
    else if (ProductGroupIdLevel == 4) {
      $("a[rel='#Li" + Level3Group + "']").parent().parent().addClass('activeTab');
      LoadSelect(3, Level2Group, Level3Group, Level4Group, true);
      LoadSelect(4, Level2Group, ProductGroup, ProductID);
    }
  });
}
 
function LoadSelect(SelectLevel, TopGroupId, MenuItemId, ProductID, SubCategory) {
  
  $("#" + TopGroupId + "_Select" + SelectLevel).html('<div class="ProductSelectBackground"><select id="GroupsProductsList" name="GroupsProductsList"></select><\/div>');
  $(".HiddenProductList li[groupId='" + MenuItemId + "']").each( function() {
    ProductID_Li = $(this).attr("id");
    $("#" + TopGroupId + "_Select" + SelectLevel + " select").prepend('<option type="product" value="' + $(this).attr("id") + '">' + $(this).attr("name") + '</option>');
  });
  $(".GroupLevel1 li[GroupID='" + MenuItemId + "'] li").each( function() {
    $("#" + TopGroupId + "_Select" + SelectLevel + " select").append('<option type="group" value="' + $(this).attr("GroupID") + '">' + $(this).text() + '</option>');
  });
  $("#" + TopGroupId + "_Select" + SelectLevel + " select").prepend('<option value="none" selected="selected">Seleccione uma opção</option>');
  if (!SubCategory) {
    $("#" + TopGroupId + "_Select" + SelectLevel + " select").val('Li_' + ProductID);
  }
  else {
    $("#" + TopGroupId + "_Select" + SelectLevel + " select").val(ProductID);
  }
  
  if ($('#Description_' + MenuItemId).html() != "") {
    $("#" + TopGroupId + "_Description").slideUp("slow", function() {
      $("#" + TopGroupId + "_Description").html($('#Description_' + MenuItemId).html());
      $("#" + TopGroupId + "_Description").slideDown("slow");
    });
  }
  
  // Activate Dropdown function
  ActivateOnChange(TopGroupId, SelectLevel);
  
  if (!SubCategory) {
    LoadProductIframe("Li_" + ProductID, TopGroupId);
  }
}
 
 
 
// Group URL param or Href links
function ShowGroup(GroupID) {
  ShowGroup2(GroupID);
}
function ShowGroup2(GroupID) {
  GroupIdLevel = $('.GroupLevel1 li[GroupID="' + GroupID + '"]').attr("GroupLevel");
  
  Group2Open = GroupID;
  Group2LevelFlag = GroupIdLevel;
  
  // Find Level 2 group to open
  while (Group2LevelFlag > 2) {
    Group2LevelFlag = $('.GroupLevel1 li[GroupID="' + Group2Open + '"]').parent().parent().attr("GroupLevel");
    Group2Open = $('.GroupLevel1 li[GroupID="' + Group2Open + '"]').parent().parent().attr("GroupID");;
  }
  
  $("#jQueryProductList .h2").siblings().removeClass('activeArrow');
  $("#Header_" + Group2Open ).addClass('activeArrow').next("div.ProductList").slideToggle(300, function() {
    if ($(this).children(".MainGroupDescription").html() != "") {
      DescriptionHeight = parseInt($(this).children(".MainGroupDescription").height()) + 20;
      $(this).children(".MainGroupDescription").css("height", DescriptionHeight + "px");
    }
  }).siblings("div.ProductList").slideUp("slow", function() {
    if (GroupIdLevel == 3) {
      $("a[rel='#Li" + GroupID + "']").parent().parent().addClass('activeTab');
      CheckMenuItem(GroupID);
    }
  });
}
 
 
 
// Check for Forms Module
function CheckForFormsModule() {
  StartSelector = "form[name^='postform']";
  if ($(StartSelector ).length > 0) {
    $(StartSelector + " table").css("width", "423px");
    $(StartSelector + " input, " + StartSelector + " textarea").parent().parent().prev().children("td").css({"text-transform":"uppercase"});
    
    // Text fields
    $(StartSelector + " input[type='text']").css({"border":"none", "background-color":"transparent", "width":"415px"});
    $(StartSelector + " input[type='text']").parent().css({"background":"url('Files/Navigation/Common/FieldBg.gif') no-repeat top left", "height":"21px"});
    
    // Textarea
    $(StartSelector + " textarea").css({"border":"none", "background-color":"transparent", "width":"415px", "height":"72px"});
    $(StartSelector + " textarea").parent().css({"background":"url('Files/Navigation/Common/TextAreaBg.gif') no-repeat top left", "height":"81px"});
    
    // Filetype
    $(StartSelector + " input[type='file']").filestyle({ 
      image: "Files/Navigation/Common/Browse.gif",
      imageheight : 21,
      imagewidth : 87,
      width : 333
    });
    $(StartSelector + " input.file").parent().css({"background":"url('Files/Navigation/Common/FieldMandatoryBg.gif') no-repeat left top", "height":"24px", "position":"relative"});
    $(StartSelector + " input.file ~ div").css({"top":"0px"});
    
    // Mandatory Fields
    $(StartSelector + " img[src$='asterisco.gif']").parent().parent().next().children("td").each(function(i) {
      $(this).css({"background-image":"url('Files/Navigation/Common/FieldMandatoryBg.gif')"});
    });
    $(StartSelector + " img[src$='asterisco.gif']").remove();
    $(StartSelector + " span:contains('*')").parent().css({"background":"url('Files/Navigation/Common/MandatoryLegend.gif') no-repeat top left", "height":"21px", "padding-left":"30px", "text-transform":"uppercase"});
    MandatoryMessage = $(StartSelector + " span:contains('*')").text();
    $(StartSelector + " span:contains('*')").text(MandatoryMessage.substring(2));
  }
}