// JavaScript Document

//these arrays must match in length, could be populate from external data
//this array is a list of the icon id's
var worldicons = ["worldicon_brisbane", "worldicon_fremantle", "worldicon_houston", "worldicon_dubai", "worldicon_kuala_lumpur", "worldicon_tokyo", "worldicon_qingdao", "worldicon_hong_kong", "worldicon_jakarta", "worldicon_singapore", "worldicon_angeles_city"];
//time zones relative to GMT for the locations in the array above
var worldzones = [10, 8, -6, 4, 8, 9, 8, 8, 7, 8, 8];

var myDays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]

//pads strings left
function lpad(padString, length) {
    var str = padString;
    while (str.length < length)
        str = '0' + str;
    return str;
}

function initMenus() {

    // DROPDOWN MENUS
    /////////////////
    //setup dropdown animation.  hoverintent used to prevent unwanted activations when passing over
    $('.dropdown').each(function () {
        $(this).parent().eq(0).hoverIntent({
            timeout: 100,
            over: function () {
                var current = $('.dropdown:eq(0)', this);
                current.slideDown(100);
            },
            out: function () {
                var current = $('.dropdown:eq(0)', this);
                current.fadeOut(200);
            }
        });
    });

    //select animation for dropdown items, comment out to disable
    $('.dropdown a').hover(function () {
        $(this).stop(true).animate({ paddingLeft: '10px' }, { speed: 100, easing: 'easeOutBack' });
    }, function () {
        $(this).stop(true).animate({ paddingLeft: '0' }, { speed: 100, easing: 'easeOutBounce' });
    });

    //added to this is a quick replacement of outstanding pngs for ie6 that are not covered in the sytlesheet variant
    if ($.browser.msie && $.browser.version == "6.0") {
        $('#footer_arrow_white_med').attr("src", "images/arrow_white_med.gif");
        $('#newstoggleprev_img').attr("src", "images/arrow_white_toggle_left.gif");
        $('#newstogglenext_img').attr("src", "images/arrow_white_toggle_right.gif");
    }
}

function initNews() {
    // NEWS SLIDER
    ////////////////
    //create a slider for the news, auto rotating every 3 sec
    var slider = $('#newsslider').bxSlider({ easing: 'easeOutBounce', controls: false, auto: true, duration: 3000 });

    //assign the prev and next buttons for the news slider
    $('#newstoggleprev').click(function () {
        slider.goToPreviousSlide();
        return false;
    });
    $('#newstogglenext').click(function () {
        slider.goToNextSlide();
        return false;
    });
}

function initMap(size) {
    // COMPUTE MAP TIMES
    ////////////////////
    //set the open/close icons
    //opening times assumed 9am-5pm Mon-Fri
    //a further refinement could be made to store opening and closing times for each location to another array.
    var d = new Date();
    var localeday;
    var localehour;

    for (i = 0; i < worldicons.length; i++) {
        localehour = d.getUTCHours() + worldzones[i]; //calculate the locale hour/day
        localeday = d.getUTCDay(); //reset
        if (localehour > 24) { localehour -= 24; localeday += 1; if (localeday > 6) localeday = 0; }
        if (localehour < 0) { localehour += 24; localeday -= 1; if (localeday < 0) localeday = 6; }

        //update the mouse overs to also show the time
        $('#' + worldicons[i] + '_time').html("Local time: " + myDays[localeday] + ' ' + lpad(localehour.toString(), 2) + ':' + lpad(d.getMinutes().toString(), 2));

        //only need to change the image if close as open is loaded by default
        if (((localehour < 9) || (localehour >= 17)) || (localeday == 0) || (localeday == 6)) {
            $("#" + worldicons[i]).attr("src", '../images/icon_office_closed' + size + '.png');
        }

    }

    // WORLDMAP TOOLTIPS
    ////////////////////
    $("#worldmap" + size + " a").tooltip({ relative: true, effect: 'slide', offset: [10, 0], opacity: 1, predelay: 200 });
}

function initMapSmall() {
    initMap("_small");
}

function initMapLarge() {
    initMap("_large");
}

function MM_openBrWindow(theURL, winName, features) { //v2.0
    myWindow = window.open(theURL, winName, features);
    myWindow.moveTo(0, 0);
    var width = window.screen.availWidth;
    var height = window.screen.availHeight;
    myWindow.resizeTo(width, height);
}

function getPageFileName() {
    //this gets the full url
    var url = document.location.href;
//    //this removes the anchor at the end, if there is one
//    url = url.substring(0, (url.indexOf("#") == -1) ? url.length : url.indexOf("#"));
//    //this removes the query after the file name, if there is one
    //    url = url.substring(0, (url.indexOf("?") == -1) ? url.length : url.indexOf("?"));

    //this removes everything after '.aspx'
    //this removes everything before the last slash in the path
    url = url.substring(0, (url.indexOf(".aspx") == -1) ? url.length : url.indexOf(".aspx"));
    url = url.substring(url.lastIndexOf("/") + 1, url.length);
    //return
    return url;
}

function loadTrackRecordServices() {
    var servicesLinks = [{ "name": "Auction Services", "url": "../Services/AuctionServices.aspx" }, //1
                            { "name": "Bonded & Free Storage", "url": "../Services/BondedStorage.aspx" }, //2
                            { "name": "Container Packs/UnPacks", "url": "../Services/ContainerPacking.aspx" }, //3 
                            { "name": "Customs Clearance", "url": "../Services/CustomsClearance.aspx" }, //4
                            { "name": "Customs Consultancy", "url": "../Services/CustomsConsultancy.aspx" }, //5
                            { "name": "Freight Forwarding", "url": "../Services/FreightForwarding.aspx" }, //6
                            { "name": "Global Logistics", "url": "../Services/GlobalLogistics.aspx" }, //7
                            { "name": "Heavy Haulage", "url": "../Services/HeavyHaulage.aspx" }, //8
                            { "name": "Marine Insurance", "url": "../Services/MarineInsurance.aspx" }, //9
                            { "name": "Pre-Delivery Processing", "url": "../Services/PreDeliveryProcessing.aspx" }, //10
                            { "name": "Project Logistics", "url": "../Services/ProjectLogistics.aspx" }, //11
                            { "name": "Biosecurity Clearance", "url": "../Services/QuarantineClearance.aspx" }, //12
                            { "name": "Steam Cleaning", "url": "../Services/SteamCleaning.aspx"}]; //13

    var indices;

    switch (getPageFileName().toLowerCase()) {
        case "energylogistics":
            indices = [4, 6, 7, 8, 9, 11, 12];
            break;
        case "energylogistics_kogancreek":
            indices = [4, 6, 7, 8, 9, 11, 12];
            break;
        case "energylogistics_orica":
            indices = [4, 6, 7, 8, 9, 11];
            break;
        case "majorinfrastructure":
            indices = [4, 6, 7, 8, 11, 13];
            break;
        case "majorinfrastructure_railgrinders":
            indices = [4, 6, 7, 8, 11];
            break;
        case "majorinfrastructure_tbm":
            indices = [8, 11, 13];
            break;
        case "latestprojects":
            indices = [4, 6, 7, 8, 9, 11, 12, 13];
            break;
        case "auctionlogistics":
            indices = [];
            break;
        default:
            indices = [2, 3, 4, 6, 7, 8, 9, 10, 11, 12, 13];
    }

    if (indices.length > 0) {
        loadLeftNavLinks(servicesLinks, indices);
    }
}

function loadServiceTrackRecords() {

    var trackRecordsLinks = [{ "name": "Kogan Creek Power Station", "url": "../TrackRecord/EnergyLogistics_KoganCreek.aspx" },
                            { "name": "Orica Nitric Acid Plant", "url": "../TrackRecord/EnergyLogistics_Orica.aspx" },
                            { "name": "Tunnel Boring Machine", "url": "../TrackRecord/MajorInfrastructure_TBM.aspx" },
                            { "name": "Rail Grinder", "url": "../TrackRecord/MajorInfrastructure_RailGrinder.aspx"}];
            
    var indices;

    switch (getPageFileName().toLowerCase()) {
        case "freightforwarding":
            indices = [1, 2, 3];
            break;
        case "globallogistics":            
            indices = [1, 2, 3];
            break;
        case "projectlogistics":
            indices = [1, 2, 3, 4];
            break;
        case "quarantineclearance":
            indices = [1];
            break;
        case "customsclearance":
            indices = [1, 2, 3];
            break;
        case "customsconsultancy":
            indices = [1, 2, 3];
            break;
        case "steamcleaning":
            indices = [4];
            break;
        case "predeliveryprocessing":
            indices = [];
            break;
        case "bondedstorage":
            indices = [];
            break;
        case "containerpacking":
            indices = [];
            break;
        case "heavyhaulage":
            indices = [1, 2, 3, 4];
            break;
        case "marineinsurance":
            indices = [1, 2];
            break;
        case "jacking":
            indices = [1, 2, 3, 4];
            break;
        case "craning":
            indices = [1, 2, 3, 4];
            break;
        case "auctionservices":
            indices = [];
            break;
        default:
            indices = [1, 2, 3, 4];
    }

    if (indices.length > 0) {
        loadLeftNavLinks(trackRecordsLinks, indices);
    }
}

function loadLeftNavLinks(links, indices) {
    
    var ihtml = "<ul class='levtnavlist'>";

    for (i = 0; i < indices.length; i++) {
        ihtml = ihtml + "<li><a href='" + links[indices[i] - 1].url + "'>" + links[indices[i] - 1].name + "</a></li>";
    }
    ihtml=ihtml + "</ul>";

    $("#leftnavlistmenus").html(ihtml);
}

function submitnewsubscription(formname) {
    
    if ($("#email").val() == "Email Address") {
        $("#email").val("");
    }

    if ($("#email_subscribe").val() == "Email Address") {
        $("#email_subscribe").val("");
    }
    
    $("#" + formname).submit();
}
