﻿$(document).ready(function() {



//$("#leftcol_submenu8").removeClass('hide');

//site redirection
$("#jumpMenu").change(function() {
    location = $(this).val();
});

/*-------------- search box toogle text handling ------------------------*/
$('.text_search').each(function() {
    var default_value = this.value;
    $(this).focus(function() {
        if (this.value == default_value) {
            this.value = '';
            $(this).css('color', '#333');
        }
    });
    $(this).blur(function() {
        if (this.value == '') {
            this.value = default_value;
            $(this).css('color', '#666');
        }
    });
});



    /*-------- for handling the image map in our-business-what-we-do.aspx and our-business-who-we-serve.aspx--*/
    var href = document.location.href ? document.location.href : document.location;
    var value = getParameterValue("projectCategoryId", href);
    var src = "images/our-business/wheel-cat-" + value + ".jpg";

    $('.sideimage img').attr("src", src);

    /*-------- for handling the image map in our-business-what-we-do.aspx and our-business-who-we-serve.aspx--*/



    $(".indextextboxbody").click(function() {
        $(".indextextboxbody").attr("value", "");
    });


    /** ---------multi-level main menu------------- **/
    $("ul.mainmenu li").hover(function() {

        $(this).addClass("hover");
        $('.submenu_container', this).css('visibility', 'visible');

    }, function() {
        $(this).removeClass("hover");
        $('.submenu_container', this).css('visibility', 'hidden');
    });
    $("ul.dropdown li ul li:has(ul)").find("a:first").append(" &raquo; ");



    var path = document.location.href ? document.location.href : document.location;
    var currentPage = extractPageName(path);
    var currentPageWithoutExt = extractPageNameWithouExt(path);

    var mainMenuIndex = getMainMenuCategoryIndex(currentPageWithoutExt);
    if (mainMenuIndex == 0) { //hide home button in the index page
        $("#btn_mainmenu_home").addClass("hide");
    }


    if (mainMenuIndex == 3 || mainMenuIndex == 2) {//project and our business
        setActiveLeftColLinkItem(currentPage); // dynamic item
    } else {
        setActiveLeftColLeveOneMenuItem(currentPageWithoutExt); //static item
    }


    var leftColSubMenuIndex = -1;
    if (mainMenuIndex >= 0)
        leftColSubMenuIndex = getLeftColSubMenuIndex(mainMenuIndex, currentPageWithoutExt);


    setActiveMainLevelOneMenuItem(currentPageWithoutExt);

    setContentHeader(mainMenuIndex);

    setContentTitile(leftColSubMenuIndex, mainMenuIndex);

});



// toggler!
function toggle() {
    //var el = $('#lefcol_submenu');
    // el = document.getElementById("leftcol_submenu");
    $('ul#leftcol_submenu').removeClass('hide');
    //el.style.display = (el.style.display != 'none' ? 'none' : '');
}

function onError(obj) {
    $(obj).addClass('hide');
    //$('.sideimage img').addClass("hide");
}

//extract the page name,  i.e. /abc/index.com -> index.com
function extractPageName(fullHrefString) {
    var arr = fullHrefString.split('/'); //split by slash
    if (arr.length >1)
        return arr[arr.length - 1];
    return "";
}
//extract the page name,  i.e. /abc/index.com -> index.com
function extractPageNameWithouExt(fullHrefString) {
    var arr = fullHrefString.split('/'); //split by slash
    var pageName = arr[arr.length - 1]; //split by slash
    arr = pageName.split('.');
    if (arr.length >1)
        return arr[0];
    return "";
}


function getMainMenuCategoryIndex(pageNameWithOutExt) {
    var currentPage = pageNameWithOutExt;
   
    var subMenuIndex = -1;
    var homePageItems = ["index"];
    var aboutUsPageItems = ["company-profile", "aim-and-core-values", "key-sectors", "management-team-executive-team", "milestones", "where-we-operate"];
    var ourBusinessPageItems = ["our-business", "our-business-who-we-serve", "our-business-what-we-do"];
    var projectPageItems = ["projects","project-by-location","project-jec-sector","project-by-solution"];
    var responsibilityPageItems = ["responsibility", "group-ehs-vision", "health-and-safety-policy", "environmental-policy", "jec-in-the-community"];
    var newsAndPublicationsPageItems = ["news-and-events", "jec-magazine", "company-brochure", "jec-environmental-report", "solution-brochure", "news-details", ];
    var careersPageItems = ["careers","career-development", "career-opportunities"];
    var footerItems = ["disclaimer", "sitemap", "contact-us"];
    var others = ["search-results"];

    var menuItems = [homePageItems, aboutUsPageItems, ourBusinessPageItems, projectPageItems, responsibilityPageItems, newsAndPublicationsPageItems, careersPageItems, footerItems, others];

    //  0=home, 1=about us ... 6= careers
    for (var i = 0; i < menuItems.length; i++) {
        subMenuIndex = menuItems[i].toString().toLowerCase().indexOf(currentPage.toLowerCase());
        if (subMenuIndex != -1) {
    
            return i;
        }

    }
    return -1;
}

function getLeftColSubMenuIndex(mainMenuIndex, pageNameWithOutExt) 
{

    var currentPage = pageNameWithOutExt;
    //alert(currentPage);
  //  var mainMenuIndex = -1; // where 0=home, 1=about us ... 6= careers
    var subMenuIndex = -1;
    var homePageItems = ["index"];
    var aboutUsPageItems = ["company-profile", "aim-and-core-values", "key-sectors", "management-team-executive-team", "milestones", "where-we-operate"];
    var ourBusinessPageItems = ["our-business", "our-business-who-we-serve", "our-business-what-we-do"];
    var projectPageItems = ["projects","project-jec-sector","project-by-solution","project-by-location"];
    var responsibilityPageItems = ["responsibility", "group-ehs-vision", "health-and-safety-policy", "environmental-policy", "jec-in-the-community"];
    var newsAndPublicationsPageItems = ["news-and-events", "JEC-magazine", "company-brochure", "JEC-environmental-report", "solution-brochure", "news-details"];
    var careersPageItems = ["careers", "career-development", "career-opportunities"];
    var footerItems = ["disclaimer", "sitemap", "contact-us"];
    var otherItems = ["search-results"];
    var menuItems = [homePageItems, aboutUsPageItems, ourBusinessPageItems, projectPageItems, responsibilityPageItems, newsAndPublicationsPageItems, careersPageItems, footerItems, otherItems];

    var leftColSubItems = menuItems[mainMenuIndex];

    for (var i = 0; i < leftColSubItems.length; i++) {
        if (currentPage.toLowerCase() == leftColSubItems[i].toLowerCase()) {
            return i;
        }
    }
    return -1;
}

function setActiveLeftColLeveOneMenuItem(pageNameWithOutExt) {

    if (pageNameWithOutExt != "") {
        $('#sitesubmenu a[href$="' + pageNameWithOutExt + '.aspx"] img').attr('class', 'subnavigation_sel');

        if (pageNameWithOutExt == "news-details") {
            $('#sitesubmenu a[href$="news-and-events.aspx"] img').attr('class', 'subnavigation_sel');
        } else if (pageNameWithOutExt == "jec-environmental-report") {//difference in the button height
            $('#sitesubmenu a[href$="jec-environmental-report.aspx"] img').attr('class', 'subnavigation2_sel');
        }/* else if (pageNameWithOutExt == "where-we-operate") {
            //$('#sitesubmenu a[href$="contact-us.aspx"]').removeClass('hide');
        }*/
    }

}

function setActiveLeftColLinkItem(pageFullName) {

    $('#sitesubmenu a[href$="' + pageFullName + '"]').removeClass('subnavigation_link'); //.attr('class', 'subnavigation_link_sel');
    $('#sitesubmenu a[href$="' + pageFullName + '"]').addClass('subnavigation_link_sel');

    $('#leftcol_submenu8 a[href$="' + pageFullName + '"]').parent().parent().siblings('a').removeClass('subnavigation_link'); //.attr('class', 'subnavigation_link_sel');
    $('#leftcol_submenu8 a[href$="' + pageFullName + '"]').parent().parent().siblings('a').addClass('subnavigation_link_sel'); //.attr('class', 'subnavigation_link_sel');
   // $('#leftcol_submenu8 a[href$="' + pageFullName + '"]').parent().parent().siblings('a').attr('class', 'subnavigation_link_sel ');
    
}


function setActiveMainLevelOneMenuItem(pageNameWithOutExt) 
{

    if (pageNameWithOutExt != "") {

        switch (pageNameWithOutExt) {
            case "company-profile":
            case "aim-and-core-values":
            case "key-sectors":
            case "management-team-executive-team":
            case "milestones":
            case "where-we-operate":
                $('#sitemainmenu a[href$="company-profile.aspx"] img').attr('class', 'btn_mainnavigation_sel');
                break;

            case "our-business":
            case "our-business-who-we-serve":
            case "our-business-what-we-do":
                $('#sitemainmenu a[href$="our-business.aspx"] img').attr('class', 'btn_mainnavigation_sel');
                break;

            case "projects":
            case "project-by-location":
            case "project-jec-sector":
            case "project-by-solution":
                $('#sitemainmenu a[href$="projects.aspx"] img').attr('class', 'btn_mainnavigation_sel');
                break;

            case "responsibility":
            case "jec-in-the-community":
            case "group-ehs-vision":
            case "health-and-safety-policy":
            case "environmental-policy":
                $('#sitemainmenu a[href$="responsibility.aspx"] img').attr('class', 'btn_mainnavigation_sel');
                break;

            case "news-and-events":
            case "news-details":
            case "jec-magazine":
            case "jec-environmental-report":
            case "solution-brochure":
            case "company-brochure":
                $('#sitemainmenu a[href$="news-and-events.aspx"] img').attr('class', 'btn_mainnavigation_sel');
                break;

            case "careers":
            case "career-development":
            case "career-opportunities":
                $('#sitemainmenu a[href$="careers.aspx"] img').attr('class', 'btn_mainnavigation_sel');
                break;

            case "contact-us":
            
                $('#sitemainmenu a[href$="contact-us.aspx"] img').attr('class', 'btn_mainnavigation_sel');
                break;
            case "index":
                $('#sitemainmenu a[href$="index.aspx"] img').attr('class', 'btn_mainnavigation_sel');
            default:
                $('#sitemainmenu a img').attr('class', 'btn_mainnavigation');
                break;
        }
    }

}

function setContentTitile(submenuIndex, mainMenuIndex) {
    //The sequence in each array should be consistent to its corresponding sub menu

    var home_TitleSrc = ["images/index/spacer.gif"];
    var aboutUs_TitleSrc = ["images/about-us/title-company-profile.gif", "images/about-us/title-aim-core-value.gif", "images/about-us/title-key-sectors.gif", "images/about-us/title-management-team.gif", "images/about-us/title-milestones.gif", "images/about-us/title-where-we-operate.gif"];
    var ourBusiness_TitleSrc = ["images/our-business/title-our-business.gif", "images/our-business/title-our-business-who-we-serve.gif", "images/our-business/title-our-business-what-we-do.gif"];
    var project_TitleSrc = ["images/project/title-project-highlight.gif", "images/project/title-project-who-we-serve.gif", "images/project/title-project-what-we-do.gif", "images/project/title-project-location.gif"];
    var responsibility_TitleSrc = ["images/responsibility/title-introduction.gif", "images/responsibility/title-group-ehs-vision.gif", "images/responsibility/title-health-safety-policy.gif", "images/responsibility/title-environmental-policy.gif", "images/responsibility/title-jec-in-the-community.gif" ];
    var newsAndPublication_TitleSrc = ["images/news-and-publication/title-news-and-events.gif", "images/news-and-publication/title-JEC-magazine.gif", "images/news-and-publication/title-company-brochure.gif", "images/news-and-publication/title-JEC-environmental-report.gif", "images/news-and-publication/title-solution-brochure.gif", "images/news-and-publication/title-news-and-events.gif"];
    var careers_TitleSrc = ["images/careers/title-career-at-jec.gif", "images/careers/title-career-development.gif", "images/careers/title-current-opportunities.gif"];/* "images/careers/title-opportunities-for-graduates.gif"*/
    var footer_TitleSrc = ["images/footer/title-disclaimer.gif", "images/footer/title-sitemap.gif", "images/footer/title-contact-us.gif"];
    var others_TitleSrc = ["images/index/title-search-result.gif"];

    var titleImgSrc = null;

    switch (mainMenuIndex) {
        
        case 0: titleImgSrc = home_TitleSrc;
        case 1: titleImgSrc = aboutUs_TitleSrc;
            break;
        case 2: titleImgSrc = ourBusiness_TitleSrc;
            break;
        case 3: titleImgSrc = project_TitleSrc;
            break;
        case 4: titleImgSrc = responsibility_TitleSrc;
            break;
        case 5: titleImgSrc = newsAndPublication_TitleSrc;
            break;
        case 6: titleImgSrc = careers_TitleSrc;
            break;
        case 7: titleImgSrc = footer_TitleSrc;
            break;
        default:
            titleImgSrc = others_TitleSrc;
            break;
    }
    if (submenuIndex <= 0) {
        submenuIndex = 0;
    }
    if (mainMenuIndex > 0)
        document.getElementById("contenttitle").setAttribute("src", titleImgSrc[submenuIndex]);
}

function setContentHeader(mainMenuIndex) {
    //The sequence  in the array should be consistent to the index of the menu item
    var contentHeaderSrc = ["images/index/spacer.gif", "images/about-us/header-company-profile.jpg", "images/our-business/header-our-business.jpg", "images/project/header-project.jpg", "images/responsibility/header-responsibility.jpg", "images/news-and-publication/header-news-and-publications.jpg", "images/careers/header-careers.jpg", "images/footer/header.jpg", "images/about-us/header-company-profile.jpg" ];
    if(mainMenuIndex >0 )
        document.getElementById("contentheader").style.backgroundImage = "url(" + contentHeaderSrc[mainMenuIndex] + ")";
}

function getParameterValue(name, href) {//abc.com?name=value, getParameterValue('name')
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(href);
    if (results == null)
        return "";
    else
        return results[1];
}



function disableRightClick(e)
{
  var message = "Right click disabled";
  
  if(!document.rightClickDisabled) // initialize
  {
    if(document.layers) 
    {
      document.captureEvents(Event.MOUSEDOWN);
      document.onmousedown = disableRightClick;
    }
    else document.oncontextmenu = disableRightClick;
    return document.rightClickDisabled = true;
  }
  if(document.layers || (document.getElementById && !document.all))
  {
    if (e.which==2||e.which==3)
    {
      alert(message);
      return false;
    }
  }
  else
  {
    alert(message);
    return false;
  }
}
disableRightClick();

