﻿/// <reference path="../jquery/jquery-1.4.3.min.js" />

(function ($) {
    var userAgent = navigator.userAgent.toLowerCase();

    $.browser = {
        version: (userAgent.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/) || [0, '0'])[1],
        safari: /webkit/.test(userAgent),
        opera: /opera/.test(userAgent),
        msie: /msie/.test(userAgent) && !/opera/.test(userAgent),
        mozilla: /mozilla/.test(userAgent) && !/(compatible|webkit)/.test(userAgent),
        chrome: /chrome/.test(userAgent)
    };

})(jQuery);

(function ($) {
    var cache = [];
    // Arguments are image paths relative to the current page.
    $.preLoadImages = function () {
        var args_len = arguments.length;
        for (var i = args_len; i--; ) {
            var cacheImage = document.createElement('img');
            cacheImage.src = arguments[i];
            cache.push(cacheImage);
        }
    }
})(jQuery);

jQuery.preLoadImages();
var cboLoginStatus1;
var t;
var tick=new Array();
var time = 10000;
$(document).ready(function () {
    $('#navHomeBut').unbind('click');
    $('#navHomeBut').bind('click', function () { window.top.location = 'default.aspx'; });
    var strPos = (($.browser.chrome) || ($.browser.mozilla) || (($.browser.msie) && (navigator.userAgent.split('MSIE ')[1].substr(0, 1) == "9"))) ? 5 : 1;
    $.ajax({
        type: "GET",
        url: "http://feeds1.mcgbfa.com/engine.asmx/getPriceData?SubFeed=4&newfeed=true&CompanyKey=200833+(WBHO)",
        dataType: "xml",
        success: function (xml) {
            var sprices = $($.parseXML(xml.responseText)).find("record");
            //var temp = $($(xml.responseText)[strPos]).text().split(' ');
            for (var i = 0, counter = 1; i < sprices.length; i++) {
                if ($(sprices[i]).find('Ticker').text() == 'ZAR/AUD') {
                    $('#ticker' + counter + 'Name').html($(sprices[i]).find('Ticker').text());
                    $('#ticker' + counter + 'Price').html($(sprices[i]).find('Price').text());
                    $('#ticker' + counter + '1Name').html($(sprices[i]).find('Ticker').text());
                    $('#ticker' + counter + '1Price').html($(sprices[i]).find('Price').text());
                    counter++;
                }
                if ($(sprices[i]).find('Ticker').text() == 'ZAR/USD') {
                    $('#ticker' + counter + 'Name').html($(sprices[i]).find('Ticker').text());
                    $('#ticker' + counter + 'Price').html($(sprices[i]).find('Price').text());
                    $('#ticker' + counter + '1Name').html($(sprices[i]).find('Ticker').text());
                    $('#ticker' + counter + '1Price').html($(sprices[i]).find('Price').text());
                    counter++;
                }
                if ($(sprices[i]).find('Ticker').text() == 'ZAR/EUR') {
                    $('#ticker' + counter + 'Name').html($(sprices[i]).find('Ticker').text());
                    $('#ticker' + counter + 'Price').html($(sprices[i]).find('Price').text());
                    $('#ticker' + counter + '1Name').html($(sprices[i]).find('Ticker').text());
                    $('#ticker' + counter + '1Price').html($(sprices[i]).find('Price').text());
                    counter++;
                }
            }
        }
    });
    $.ajax({
        type: "GET",
        url: "http://feeds1.mcgbfa.com/engine.asmx/getPriceData?SubFeed=3&newfeed=true&CompanyKey=200833+(WBHO)",
        dataType: "xml",
        success: function (xml) {
            var sprice = $($.parseXML(xml.responseText)).find("Price").text();
            //var temp = $($(xml.responseText)[strPos]).text().split(' ');
            $('#sharePriceValue').html(sprice);
            $('#sharePriceValue1').html(sprice);
        }
    });
    for (var i = 0; i < tick.length; i++) {
        clearTimeout(tick[i]);
    }
    $('.ticker').animate({ left: '-650px' }, 15000, 'linear', function () {
        ticktime();
    });
    $('.ticker2').delay(7500).animate({ left: '-650px' }, 15000, 'linear', function () {
        ticktime2();
    });
    function ticktime() {
        $('.ticker').css('left', '839px');
        $('.ticker').animate({ left: '-650px' }, 15000, 'linear', function () {
            for (var i = 0; i < tick.length; i++) {
                clearTimeout(tick[i]);
            }
            tick = new Array();
            tick[tick.length] = window.setTimeout(function () {
                ticktime();
            }, 10);
        });
    }
    function ticktime2() {
        $('.ticker2').css('left', '839px');
        $('.ticker2').animate({ left: '-650px' }, 15000, 'linear', function () {
            for (var i = 0; i < tick.length; i++) {
                clearTimeout(tick[i]);
            }
            tick = new Array();
            tick[tick.length] = window.setTimeout(function () {
                ticktime2();
            }, 10);
        });
    }
    Services.WBHOService.CheckLoggedIn(onCheckLoggedIn, onError);
    Services.WBHOService.GetHomeProj(onGetHomeProj, onError);

    switch ($.urlParam('extLink')) {
        case 'EmailDisclaimer':
            $('#right').val('QL');
            $('#page').val('legdocl');
            $('#rightlist').val('LegDoc');
            $('#curQL').val('1');
            getPage('#content', 'Template2.aspx', 'Scripts/pages/template2.js', 'legdocl');
            break;
        case 'TCO':
            $('#right').val('QL');
            $('#page').val('legdocl');
            $('#rightlist').val('LegDoc');
            $('#curQL').val('3');
            getPage('#content', 'Template2.aspx', 'Scripts/pages/template2.js', 'legdocl');
            break;
        default:
            if ($('#content').text() == "") { getPage('#content', 'Home.aspx', 'Scripts/pages/home.js', 'home') }
    }

    $('#TopLogo').click(function () { getPage('#content', 'Home.aspx', 'Scripts/pages/home.js', 'home') });
    $('#NavAbout').click(function () { $('#right').val(''); $('#rightlist').val(''); $('#curQL').val(''); getPage('#content', 'Template2.aspx', 'Scripts/pages/template2.js', 'about', this, 'FootAbout') });
    $('.NavAboutd').click(function () { $('#right').val(''); $('#rightlist').val(''); $('#curQL').val(''); getPage('#content', 'Template2.aspx', 'Scripts/pages/template2.js', 'about', this, 'FootAbout') });
    $('.NavHome').click(function () { $('#right').val(''); $('#rightlist').val(''); $('#curQL').val(''); window.top.location = 'default.aspx'; });
    $('#NavInvRel').click(function () { $('#right').val('QL'); $('#rightlist').val('InvRel'); $('#curQL').val('0'); getPage('#content', 'Template2.aspx', 'Scripts/pages/template2.js', 'invrel', this, 'FootInvRel') });
    $('#NavSusAble').click(function () { $('#right').val('QL'); $('#rightlist').val('SusAble'); $('#curQL').val('0'); getPage('#content', 'Template2.aspx', 'Scripts/pages/template2.js', 'sustain', this, 'FootSusAble') });
    $('#NavCorGov').click(function () { $('#right').val('QL'); $('#rightlist').val('CorGov'); $('#curQL').val('0'); getPage('#content', 'Template2.aspx', 'Scripts/pages/template2.js', 'corgov', this, 'FootCorGov') });
    $('#NavAssSubs').click(function () { $('#right').val(''); $('#rightlist').val(''); $('#curQL').val(''); getPage('#content', 'Template2.aspx', 'Scripts/pages/template2.js', 'asssub', this, 'FootAssSubs') });
    $('#NavContact').click(function () { getPage('#content', 'Template1.aspx', 'Scripts/pages/template1.js', 'contact', this, 'FootContact') });
    $('.a2').click(function () { $('#catid').val('1'); getPage('#content', 'Template3.aspx', 'Scripts/pages/template3.js', 'buildcons', $('#NavPort'), 'FootPort') });
    $('.a3').click(function () { $('#catid').val('2'); getPage('#content', 'Template3.aspx', 'Scripts/pages/template3.js', 'civeng', $('#NavPort'), 'FootPort') });
    $('.a4').click(function () { $('#catid').val('3'); getPage('#content', 'Template3.aspx', 'Scripts/pages/template3.js', 'rande', $('#NavPort'), 'FootPort') });
    $('.a1').click(function () { getPage('#content', 'Template3.aspx', 'Scripts/pages/template3.js', 't10', $('#NavPort'), 'FootPort') });
    $('#topnav_InvRel a').click(function () { $('#right').val('QL'); $('#rightlist').val('InvRel'); $('#curQL').val($(this).attr('pos')); getPage('#content', 'Template2.aspx', 'Scripts/pages/template2.js', 'invrel', this, 'FootInvRel') });
    $('#topnav_SusAble a').click(function () { $('#right').val('QL'); $('#rightlist').val('SusAble'); $('#curQL').val($(this).attr('pos')); getPage('#content', 'Template2.aspx', 'Scripts/pages/template2.js', 'sustain', this, 'FootSusAble') });
    $('#topnav_CorGov a').click(function () { $('#right').val('QL'); $('#rightlist').val('CorGov'); $('#curQL').val($(this).attr('pos')); getPage('#content', 'Template2.aspx', 'Scripts/pages/template2.js', 'corgov', this, 'FootCorGov') });
    $('#NavPort').click(function () { $('#catid').val(''); getPage('#content', 'Template3.aspx', 'Scripts/pages/template3.js', 't10', $('#NavPort'), 'FootPort') });
    //    $('.loginButton').colorbox({ inline: true, href: "#Login" });
    $('.loginButton').colorbox({ inline: false, iframe: true, href: "IntraLogin.aspx", width: '500px', height: '300px' });
});

function onGetHomeProj(o) {
    var oHomeProjs = eval('(' + o + ')');
    $('#imagefullhome').data('Projects', oHomeProjs);
    $('#imagefullhome').find('.image:gt(0)').remove();
    for (var j = 0; j < oHomeProjs.length; j++) {
        var img = $('#imagefullhome').find('.image:first').clone(true);
        img.insertAfter($('#imagefullhome').find('.image:last'));
        img.attr('src', oHomeProjs[j].homeslideimage);
        img.attr('index', j);
        img.attr('projid', oHomeProjs[j].ID);
        img.attr('projname', oHomeProjs[j].project_name);
        img.attr('show', 'false');
        img.hide();
        if (j == 0) {
            img.unbind('load');
            img.load(function () {
                $('#imagefullhome').show();
                $('#imagefullhome').css('height', $(this).height() + 'px');
                $('#goNextPrev').css('left', $('.homeTopImageWrap').position().left + 'px');
                $('#goNextHome').css('left', $('#imagefullhome').width() - 48 + 'px');
                if ($.browser.version == '7.0') {
                    $('#goProj').css('left', $('.homeTopImageWrap').position().left + 140 + 'px');
                    $('#goPort').css('left', $('.homeTopImageWrap').position().left + $('#imagefullhome').width() - $('#goPort').width() + 3 + 'px');
                    $('#goProj').css('top', $('.homeTopImageWrap').position().top + $('#imagefullhome').height() - $('#goProj').height() - 23 + 'px');
                    $('#goPort').css('top', $('.homeTopImageWrap').position().top + $('#imagefullhome').height() - $('#goPort').height() - 23 + 'px');
                } else {
                    if ($.browser.safari) {
                        $('#goProj').css('left', $('.homeTopImageWrap').offset().left + 140 + 'px');
                        $('#goPort').css('left', $('.homeTopImageWrap').offset().left + $('#imagefullhome').width() - $('#goPort').width() + 3 + 'px');
                        $('#goProj').css('top', $('.homeTopImageWrap').position().top + $('#imagefullhome').height() - $('#goProj').height() - 23 + 'px');
                        $('#goPort').css('top', $('.homeTopImageWrap').position().top + $('#imagefullhome').height() - $('#goPort').height() - 23 + 'px');
                    } else {
                        if ($.browser.version == '9.0') {
                            $('#goProj').css('left', $('.homeTopImageWrap').offset().left + 140 + 'px');
                            $('#goPort').css('left', $('.homeTopImageWrap').offset().left + $('#imagefullhome').width() - $('#goPort').width() + 3 + 'px');
                            $('#goProj').css('top', $('.homeTopImageWrap').position().top + $('#imagefullhome').height() - $('#goProj').height() - 23 + 'px');
                            $('#goPort').css('top', $('.homeTopImageWrap').position().top + $('#imagefullhome').height() - $('#goPort').height() - 23 + 'px');
                        } else {
                            $('#goProj').css('left', $('.homeTopImageWrap').position().left + 140 + 'px');
                            $('#goPort').css('left', $('.homeTopImageWrap').position().left + $('#imagefullhome').width() - $('#goPort').width() + 3 + 'px');
                            $('#goProj').css('top', $('.homeTopImageWrap').position().top + $('#imagefullhome').height() - $('#goProj').height() - 23 + 'px');
                            $('#goPort').css('top', $('.homeTopImageWrap').position().top + $('#imagefullhome').height() - $('#goPort').height() - 23 + 'px');
                        }
                    }
                }
                t1 = window.setTimeout(function () {
                    goNext()
                }, time);
                $('.homeTopImageWrap').unmask();
            });
        }
    }
    $('#imagefullhome').find('.image:first').remove();
    $('#imagefullhome').find('.image:first').show();
    $('#imagefullhome').find('.image:first').attr('show', 'true');
    $('#imagefullhome').find('.image:first').attr('pos', 'first');
    $('#imagefullhome').find('.image:last').attr('pos', 'last');
    $('#projName').html($('#imagefullhome').find('.image:first').attr('projname'));
    $('#goPort').unbind('click');
    $('#goPort').click(function () { $('#catid').val(''); getPage('#content', 'Template3.aspx', 'Scripts/pages/template3.js', 't10', $('#NavPort'), 'FootPort') });
    $('#goProj').unbind('click');
    $('#goProj').click(function () {
        $('#curProjID').val($('#imagefullhome').find('.image[show="true"]').attr('projid'));
        $.fn.colorbox({
            //inline: true,
            overlayClose: false,
            href: "TemplatePortfolio.aspx",
            open: true,
            width: "998px",
            height: "550px"
        });
    });

    $('#goNextHome').unbind('click');
    $('#goNextHome').click(function () {
        goNext();
    });

    $('#goPrevHome').unbind('click');
    $('#goPrevHome').click(function () {
        clearTimeout(t1);
        var curSlide = $('#imagefullhome').find('.image[show="true"]');
        $(curSlide).fadeOut('slow', function () {
            $(this).attr('show', 'false');
        });
        if ($(curSlide).attr('pos') == 'first') {
            $('#projName').fadeOut('slow', function () {
                $(this).html($('#imagefullhome').find('.image:last').attr('projname'));
            });
            $('#imagefullhome').find('.image:last').fadeIn('slow', function () {
                $(this).attr('show', 'true');
            });
        } else {
            $('#projName').fadeOut('slow', function () {
                $(this).html($(curSlide).prev().attr('projname'));
            });
            $(curSlide).prev().fadeIn('slow', function () {
                $(this).attr('show', 'true');
            });
        }
        $('#projName').fadeIn('slow');
        t1 = window.setTimeout(function () {
            goNext()
        }, time);
    });
}

function goNext() {
    clearTimeout(t1);
    var curSlide = $('#imagefullhome').find('.image[show="true"]');
    $(curSlide).fadeOut('slow', function () {
        $(this).attr('show', 'false');
    });
    if ($(curSlide).attr('pos') == 'last') {
        $('#projName').fadeOut('slow', function () {
            $(this).html($('#imagefullhome').find('.image:first').attr('projname'));
        });
        $('#imagefullhome').find('.image:first').fadeIn('slow', function () {
            $(this).attr('show', 'true');
        });
    } else {
        $('#projName').fadeOut('slow', function () {
            $(this).html($(curSlide).next().attr('projname'));
        });
        $(curSlide).next().fadeIn('slow', function () {
            $(this).attr('show', 'true');
        });
    }
    $('#projName').fadeIn('slow');
    t1 = window.setTimeout(function () {
        goNext()
    }, time);
}

function onCheckLoggedIn(o) {
    $('#header').data('isAuthenticated', o);

    if (o) {
        //$('#login').html('CMS Mode');
        $('.forgotPassword').hide();
        $('.LoginStatus1').show();
        //loadjscssfile('/Scripts/tiny_mce/jquery.tinymce.js');
    }
}

function getPage(c, p, s, a, h, hb) {
    $('#content').mask('loading...'); 
    removejscssfile();
    $('#page').val(a);
    $(c).find('div[class!="loadmask"][class!="loadmask-msg"][class!="loadmask-inner"]').remove();
    $(c).find('*[class!="loadmask"][class!="loadmask-msg"][class!="loadmask-inner"]').remove();
    $(c).load(p, function (response, status, xhr) {
        if (status == "error") {
            var msg = "Sorry but there was an error: ";
            alert(msg + xhr.status + " " + xhr.statusText);
        }
        loadjscssfile(s);
        if (h != undefined) {
            $('#topnav,#BottomNav').find('a').each(function () {
                $(this).removeClass($(this).attr('id') + 'Hov');
            });
            $(h).addClass($(h).attr('id') + 'Hov');
            $('#' + hb).addClass(hb + 'Hov');
        }
    });
}

function loadjscssfile(filename) {
    var fileref = document.createElement('script');
    fileref.setAttribute("type", "text/javascript");
    fileref.setAttribute("src", filename);
    fileref.setAttribute('rel', 'Dyn');
    if (typeof fileref != "undefined") document.getElementsByTagName("head")[0].appendChild(fileref);
}

function removejscssfile() {
    var targetelement = "script";
    var targetattr = "rel";
    var allsuspects = $('script'); //document.getElementsByTagName(targetelement);
    for (var i = allsuspects.length; i >= 0; i--) { //search backwards within nodelist for matching elements to remove
        //if (allsuspects[i] && allsuspects[i].getAttribute(targetattr) != null && (allsuspects[i].getAttribute(targetattr) == 'Dyn' || allsuspects[i].getAttribute(targetattr) == 'Port'))
        if (allsuspects[i] && $(allsuspects[i]).attr('rel') != null && ($(allsuspects[i]).attr('rel') == 'Dyn' || $(allsuspects[i]).attr('rel') == 'Port'))
            $(allsuspects[i]).parent().find($(allsuspects[i])).remove();  //remove element by calling parentNode.removeChild()
    }
}

function onError(o) {
    alert(o._message);try {$('#content').unmask();}catch(e){alert(e);}
}
