﻿
function pageFocus() {
    $("#lang").hide();
    $("#menu").hide();
    $("#bar").hide();
    $("#footerBar").hide();
    $("#footerMask").height(0);
    $('#page').mouseenter(function () {
        $('#page').unbind('mouseenter');
        $("#lang").fadeIn(1000);
        $("#menu").fadeIn(1000);
        $("#bar").fadeIn(1000);
        $("#footerBar").fadeIn(1000, function () {
            $("#footerMask").animate({ height: 25 }, 1000)
        });
    });
}

function homeInit() {
    $("#heropanes").hide();
    $("#heroFrame").hide();
    $("#mainContent1").hide();
    $("#mainContent2").hide();

    loadBG(function () {
        var img1 = $("#heropanes .item img").eq(0);
        img1.hide();
        $("#heroFrame").slideDown(2000, function () {
            $("#heropanes").show();
            setTimeout(function () {
                img1.fadeIn(2000, function () {
                    img1.removeAttr("style");
                    LoadHero();
                });
            }, 800);
        });
    });
}


function teamInit() {
    $(".teamHeader").hide();
    $("#members span").hide();
    $("#members .name").hide();
    $("div.description").hide();

    loadBG(function () {
        $(".teamHeader").slideDown(1000, function () {
            $("#member1").fadeIn(1300, function () {
                $("#member2").fadeIn(1300, function () {
                    $("#member3").fadeIn(1300, function () {
                        $("#members .name").slideDown(1300);
                        $("#tad").fadeIn(1300, function () {
                            $('html,body').animate({ scrollTop: 180 }, 800);
                            $("#members").tabs("div.description", {
                                event: 'mouseover'
                            });
                        });
                    });
                });
            });
        });
    });
}

function approachInit() {
    $(".apBar").height(50).css("top", 0);
    $("#approachMask").hide().height(0);
    $(".approachs .item").hide();
    $(".aContent .item").hide();
    $("#approachTabs a").hide();
    $("#approachBar").fadeTo(0, 0);

    $.tools.tabs.addEffect("approachEffect", function (tabIndex, done) {
        if ($.browser.mozilla) {
            $('html').focus();
        } else {
            $('body').focus();
        }

        var currentIndex = this.getIndex();

        var panes = this.getPanes();
        panes.eq(currentIndex).fadeOut(this.fadeOutSpeed);
        panes.eq(tabIndex).fadeIn(this.fadeInSpeed);

        var currentTab = this.getCurrentTab();
        var content = $('.aContent .item');
        content.eq(currentIndex).hide();
        content.eq(tabIndex).show();

        $("#approachTabs #t" + tabIndex + 1).animate({ lineHeight: 73 }, 800, function () {

        });
        currentTab.animate({ lineHeight: 50 }, 800, function () {
            currentTab.removeAttr("style");
        });

        if (getScrollTop() < 130) {
            $('html,body').animate({ scrollTop: 130 }, 1000);
        }
        else {
            $('html,body').scrollTop(130);
        }

        if (tabIndex == 1) $('#top').show(); else $('#top').hide();

        done.call();
    });

    loadBG(function () {
        $("#approachBar").fadeTo(800, 0.65, function () {
            $("#approachTabs a").fadeIn(800, function () {
                if (this.id != 't1') return;
                $("#approachTabs").animate({ top: 288 }, 800);
                $("#approachBar").animate({ top: 313, height: 25 }, 800, function () {
                    $("#approachMask").animate({ height: 25 }, 2000);
                    $("#approachTabs").tabs(".approachs .item", {
                        tabs: 'a', effect: 'approachEffect', rotate: true,
                        fadeOutSpeed: 1000,
                        fadeInSpeed: 1200
                    });
                    $("#nextApproach").show();
                });
            });
        });
    });

    $("#nextApproach").click(nextApproachClick);
}

function projectsInit() {
    var preloadimg = new Image();
    $(preloadimg).attr('src', '/images/singapore.png');

    $("#mapTabs a").click(function (e) {
        selectPorfolio(this);
    });
    $("#country").click(function (e) {
        restPorfolio(this);
    });
    $(document).keydown(function (e) {
        if (e.keyCode == 27) { restPorfolio(); }
    });
}

function selectPorfolio(select) {
    $(".item#bgItem").animate({ width: 900 }, 1500, function () {
        $("#country").text(select.title);
    });
    $("#country").show().attr("title", select.id);
    $("#portfolio div:not(." + select.id + ")").animate({ marginLeft: 180, height: 0, opacity: 0 }, 1500,
        function () {
            $(this).hide();
        }
    );
}

function restPorfolio(select) {
    if ($("#country").css("display") == "none") return;

    $(".item#bgItem").animate({ width: 400 }, 1500, function () { });
    $("#country").hide();
    $("#country").text("");
    $("#portfolio div:not(." + select.title + ")").animate({ marginLeft: 0, height: 32, opacity: 1 }, 1500,
        function () {
            $(this).removeAttr("style");
        }
    );
}


function galleryInit() {
    $("#gallery .item").hide();
    loadBG(function () {
        $("#galleryItems").tabs("#gallery .item", {
            effect: 'fade',
            fadeOutSpeed: 2000,
            fadeInSpeed: 2000,
            rotate: true
        }).slideshow({ autoplay: true, interval: 8000 });
    });
}

function showContact(done) {
    $("#contact").show();
    $("#footerMask").hide();
    $("#contactMask").fadeTo(1200, 0.87);
    $("#footer").fadeTo(0, 1);
    $("#contact .innerBox").animate({ marginTop: 0, marginLeft: 0 }, 1000, done);
}

function hideContact(done) {
    $("#contactMask").fadeTo(500, 0, function () {
        $("#contactMask").removeAttr("style"); $("#footerMask").removeAttr("style");
    });
    $("#contact .innerBox").animate({ marginTop: 240, marginLeft: 350 }, 1000, function () {
        $("#contact").hide();
        $("#footer").fadeTo(800, 0.8, function () { if (done != null) done.call(); });
    });
}

function footerInit() {
    $("#contactUS").removeAttr("href");
    $(document).ready(function () {
        // adds an effect called "myEffect" to tooltip 
        $.tools.tooltip.addEffect("contactEffect", showContact, hideContact);

        // initialize tooltip
        $("#contactUS").tooltip({
            cancelDefault:false,
            predelay: 500,
            tip: '#contact',
            effect: 'contactEffect',
            events: {
                tooltip: "mouseover"
            }
        });
    });
    $(document).click(function (e) {
        var el = $(e.target)
        if ($("#contact:visible").length && !el.is("#contact") && !el.is("#contactUS") && !el.is(".innerBox")) {
            hideContact();
        }
    });
    $(document).keydown(function (e) {
        if (e.keyCode == 27) { hideContact(); }
    });
}

function pauseAutoscroll(id) {
    $(id).trigger('mouseover');
}

function LoadHero() {
    $("#heropanes").scrollable({ size: 1, speed: 1500 })
    .autoscroll({ autoplay: true, autopause: true, interval: 6000 })
    .circular()
    .navigator({
        navi: "#herotabs",
        naviItem: 'a',
        activeClass: 'current'
    });
}

function loadBG(load) {
    
    var img = new Image();
    $(document).ready(function () {
        $(img).load(load).attr('src', '/images/background2.jpg');
    });
}

function getScrollTop() {
    var ScrollTop = document.body.scrollTop;

    if (ScrollTop == 0) {

        if (window.pageYOffset)

            ScrollTop = window.pageYOffset;

        else

            ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;

    }

    return ScrollTop;
}

function nextApproachClick() {
    $("#approachTabs").tabs({ api: true }).next();
}
