function init(){
  //gSiteBaseRef = "http://localhost:8080/opencms/opencms";
  gSiteMap = getSiteMap();
  gSectionPageRef =  getSectionPageRef();
  gSectionName    = util.safeGetProp(gSiteMap, ["ref", gSectionPageRef, "section"], "");
  gSubSectionName = util.safeGetProp(gSiteMap, ["ref", gSectionPageRef, "subSection"], "");
  if ($("#fontsizer").length){
    $("#fontsizer").fontsizer();
  }
  
  if ($("div.tabbox").length){
    /*$("div.tabbox").tabbox();*/
	  $("div.tabbox").tabbox({equalHeights: false});
  }
  
  if ($(".treeview").length){
	    $(".treeview").treeview();
	  }
  
  if (("#searchbox #keywords").length){
    $("#searchbox #keywords").one("click", function(){
      $(this).val("");
    });
  }
  
  initNav();
}

$(function(){
  init();
});
/*
function getSiteMap(){
  var tNav = {};
  tNav["needs"] = {};
  tNav["solutions"] = {};
  tNav["solutions"]["protection"] = {name:"Protection", link:"protection.html"};
  tNav["solutions"]["savings"] = {name:"Savings", link:"savings.html"};
  tNav["solutions"]["retirement"] = {name:"Retirement", link:"retirement.html"};
  tNav["solutions"]["education"] = {name:"Child's Education", link:"education.html"};
  tNav["solutions"]["investment"] = {name:"Investment", link:"investment.html"};
  tNav["solutions"]["viewall"] = {name:"View All", link:"viewall.html"};
  tNav["prudential"] = {};
  tNav["prudential"]["promotions"] = {name:"Latest Promotions", link:"promotions.html"};
  tNav["prudential"]["fund"] = {name:"Fund Prices", link:"fund.html"};
  tNav["prudential"]["claim"] = {name:"Make a Claim", link:"claim.html"};
  tNav["prudential"]["directory"] = {name:"Consultant Directory", link:"directory.html"};
  
  var tRef = {};
  tRef["home"] = {section:"home", subSection:""};
  tRef["nat"] = {section:"needs", subSection:""};
  tRef["solutions"] = {section:"solutions", subSection:""};
  tRef["protection_life_01"] = {section:"solutions", subSection:"protection"};
  tRef["protection_life_02"] = {section:"solutions", subSection:"protection"};
  tRef["savings"] = {section:"solutions", subSection:"savings"};
  tRef["retirement"] = {section:"solutions", subSection:"retirement"};
  tRef["investment"] = {section:"solutions", subSection:"investment"};
  tRef["viewall"] = {section:"solutions", subSection:"viewall"};
  tRef["myprudential"] = {section:"prudential", subSection:"promotions"};
  tRef["latest_promotions_01"] = {section:"prudential", subSection:"promotions"};
  tRef["latest_promotions_02"] = {section:"prudential", subSection:"promotions"};
  tRef["fund"] = {section:"prudential", subSection:"fund"};
  tRef["claim"] = {section:"prudential", subSection:"claim"};
  tRef["claim_download"] = {section:"prudential", subSection:"claim"};
  tRef["claim_how"] = {section:"prudential", subSection:"claim"};
  tRef["directory"] = {section:"prudential", subSection:"directory"};
  tRef["career_staff"] = {section:"prudential", subSection:""};
  tRef["faq"] = {section:"prudential", subSection:""};
  tRef["search_01"] = {section:"prudential", subSection:""};
  tRef["search_02"] = {section:"prudential", subSection:""};
  tRef["about"] = {section:"home", subSection:""};
  tRef["about_history"] = {section:"home", subSection:""};
  tRef["about_logo"] = {section:"home", subSection:""};
  tRef["career"] = {section:"home", subSection:""};
  tRef["career_consultants_01"] = {section:"home", subSection:""};
  tRef["career_consultants_02"] = {section:"home", subSection:""};
  tRef["career_consultants_03"] = {section:"home", subSection:""};
  tRef["career_consultants_04"] = {section:"home", subSection:""};
  tRef["contact_global"] = {section:"home", subSection:""};
  tRef["contact_hk"] = {section:"home", subSection:""};
  tRef["press_execbios"] = {section:"home", subSection:""};
  tRef["press_medialibrary_01"] = {section:"home", subSection:""};
  tRef["press_medialibrary_02"] = {section:"home", subSection:""};
  tRef["press_medialibrary_03"] = {section:"home", subSection:""};
  tRef["pressreleases_01"] = {section:"home", subSection:""};
  tRef["pressreleases_02"] = {section:"home", subSection:""};
  tRef["pressreleases_03"] = {section:"home", subSection:""};
  
  return {nav:tNav, ref:tRef};
}
*/
function getSectionPageRef(){
  var tRef = getSubPath(document.location.href, 5);
  var tIndex = tRef.indexOf(".html");
  if (tIndex >= 0){
    return tRef.substring(0,tIndex).toLowerCase();
  } else{
    return tRef.toLowerCase();
  }
}

function getSubPath(aURL, aStartIndex, aEndIndex){
  var tList = String(aURL).split("/");
  if (String(aURL).substr(0,4) == "http"){
    var tBaseIndex = 3;
  } else{
    var tBaseIndex = 1;
  }
  var tStart = tBaseIndex + aStartIndex;
  if (aEndIndex == null){
    var tEnd = tList.length;
  } else{
    var tEnd = tBaseIndex + aEndIndex + 1;
  }
  if (tStart < 0) tStart = 0;
  if (tStart > tList.length - 1) tStart = tList.length - 1;
  if (tEnd > tList.length) tEnd = tList.length;
  if (tEnd <= tStart) tEnd = tStart + 1;
  var tPartList = tList.slice(tStart, tEnd);
  return tPartList.join("/");
}

function initNav(){
  $("#subnav_hint_container").css("display", "none").html(getSubNavHintContent()).find(".subnav").css({"opacity": 0});
  $("#nav a").bind("mouseenter", function(){
    $("#subnav_hint_container").clearQueue().stop().fadeTo("", 1);
    var tIndex = $("#nav a").index(this);
    $("#subnav_hint_container .subnav").not($("#subnav_hint_container .subnav:eq(" + tIndex  + ")").stop().fadeTo("", 1, function(){
      $(this).css("filter", "");
    })).stop().fadeOut();
  }).bind("mouseleave", function(){
    $("#subnav_hint_container").clearQueue().delay(1000).fadeOut(function(){
      $(".subnav", this).css({"opacity": 0});
    });
  });
  $("#subnav_hint_container").bind("mouseenter", function(){
    $("#subnav_hint_container").clearQueue().stop().fadeTo("", 1);
  }).bind("mouseleave", function(){
    $("#subnav_hint_container").clearQueue().stop().fadeOut(function(){
      $(".subnav", this).css({"opacity": 0});
    });
  });
}

function getSubNavHintContent(){
  var t = "";
  for (var i in gSiteMap.nav){
    t += "<div class=\"subnav_hint\"><ul class=\"subnav\">\n" + getSubNavContent(i) + "</ul></div>";
  }
  return t;
}

function getSubNavContent(aSectionName){
  var tSect = gSiteMap["nav"][aSectionName];
  var t = "";
  var j = 0;
  for (var i in tSect){
    var tName = tSect[i]["name"];
    var tLink = String(tSect[i]["link"]);
    if (tLink.indexOf("http") == 0) {
      tLink = tLink + "\" target=\"_blank";
    } else{
      //tLink = gSiteBaseRef + "/" + tLink;
    }
    
    if (i == gSubSectionName && aSectionName == gSectionName){
      var tClass = " class=\"selected\"";
      if (j == 0){
        tClass = " class=\"first selected\"";
      }
    } else{
      var tClass = "";
      if (j == 0){
        tClass = " class=\"first\"";
      }
    }
    t += "<li" + tClass + "><div class=\"bg\"><div class=\"borderleft\"><a href=\"" + tLink + "\" title=\"" + tName + "\" class=\"borderright\">" + tName + "</a></div></div></li>\n";
    j = j + 1;
  }
  return t;
}

function showEnquiryForm(){
	$("#enquiryform_panel").show();
}

function openUrl(url) 
{
    window.location = url;
}

function openWin(url, width, height) {
	  var top = (screen.height) ? (screen.height - height)/2 : 0;
	  var left = (screen.width) ? (screen.width - width)/2 : 0;
	  return window.open(url, 'win', 'width=' + width + ',height=' + height + ',top=' + top + ',left=' + left + ',resizable=0,scrollbars=0,menubar=0');
}

function openWin(url, width, height, bars) {
	  var top = (screen.height) ? (screen.height - height)/2 : 0;
	  var left = (screen.width) ? (screen.width - width)/2 : 0;
	  return window.open(url, 'win', 'width=' + width + ',height=' + height + ',top=' + top + ',left=' + left + ','+ bars);
}
