﻿$(document).ready(function () {

    /* Slice Menu
    -------------------------------------------------*/

    //    $("h1 a").click(function () {
    //        $("#slice-menu").slideToggle();
    //        return false;
    //    });

    /* Navigation colour transform
    -------------------------------------------------*/

    $("header#global-header nav#global-nav ul li a").hover(function () {
        $(this).stop().animate({ color: "#A60037" }, 500);
    }, function () {
        $(this).stop().animate({ color: "#393939" }, 500);
    });

    

    /* Subnav Slide Down
    -------------------------------------------------*/

    $("aside.subnav nav ul:first li a").click(function (e) {
        var submenu = $(this).next("ul");
        if (submenu.length != 0) {
            $(this).hasClass("selected") ? $(this).removeClass("selected") : $(this).addClass("selected");
            submenu.slideToggle();
            e.preventDefault();
        }
    });

    /* Refine Menu Slide Down
    -------------------------------------------------*/

    $("a.refine-show-hide").click(function (e) {
        $(this).prev("div.refine-inner").slideToggle();
        $(this).hasClass("show") ? $(this).removeClass("show").addClass("hide").text("Hide") : $(this).removeClass("hide").addClass("show").text("Expand");
        e.preventDefault();
    });

    /* Select All / Deselect All
    -------------------------------------------------*/

    $("a.select-all-button").click(function (e) {
        $(this).parent().next("ul").children("li").children("input[type=checkbox]").each(function () {
            $(this).attr("checked", "checked");
        });
        e.preventDefault();
    });

    $("a.deselect-all-button").click(function (e) {
        $(this).parent().next("ul").children("li").children("input[type=checkbox]").each(function () {
            $(this).attr("checked", "");
        });
        e.preventDefault();
    });

    /* List item colour
    -------------------------------------------------*/

    $("article.main-content div.article-content-inner li").not("ul.pagination li, div.white-paper-detail ul li").each(function () {
        if ($(this).children("ul").length == 0) {
            var contents = $(this).html();
            $(this).html("<span>" + contents + "</span>");
        }
    });

    /* Fix nth-child
    -------------------------------------------------*/

    /*
    nav#global-nav ul li:last-child a { background: none; padding-right: 0; }
    aside.subnav nav ul li:last-child a { border-bottom: none; }
    ul#contact-nav li:nth-child(2) a { background: none; }
    #industries section.industry-section:nth-child(2n) { margin-right: 0; }
    ul.breadcrumbs li:first-child { margin: 0; padding: 0 20px 0 0; }
    ul.breadcrumbs li:last-child { background: none; }
    footer#global-footer ul.terms li:last-child a { border: none; }
    #design aside.service-block:last-child { margin-right: 0; }
    */

    $("ul#contact-nav li:nth-child(2) a").css("background", "none");

    $("footer#global-footer ul.terms li:last a").css("border", "none");

    $("nav#global-nav ul li:last a").css({
        "background": "none",
        "padding-right": "0"
    });

    $("#services div.service-row").each(function () {
        $(this).children("article.service-block:last").css("margin-right", "0");
    });

    $("ul.breadcrumbs li:last").css("background", "none");

    $("section.our-knowledge article.blog-excerpt:last, section.our-knowledge article.article-excerpt:last").css({
        "border": "none"
    });

    $("aside.latest-info").each(function () {
        $(this).children("article.news-excerpt:last").css({
            "border": "none",
            "padding-bottom": "0"
        });
    });

    $("section.knowledge-section").each(function () {
        $(this).children("article.blog-excerpt:last").css({
            "border": "none"
        });
    });

    $("section.knowledge-section:eq(2n)").css({
        "margin-right": "0"
    });

//    $(".key-project:nth-child(3n)").css({
//        "margin-right": "0"
//    });

    /* Blog tag cloud
    -------------------------------------------------*/

    /* Causes an error in IE7. Line 90. */

    /*if (!$('#blog-tags').tagcanvas({
    textColour: '#ffffff',
    outlineThickness: 0,
    outlineColour: "#a8114d",
    minSpeed: 0,
    maxSpeed: 0.05,
    depth: 0.75,

    })) {
    $('#blog-tags').hide();
    }*/

    /* Image gallery
    -------------------------------------------------*/

    $('#gallery-scroller-ex3').MGGalleryScroller({ thumbContainer: '.scroller', scrollerLeft: '.ctrl-prev', scrollerRight: '.ctrl-next' });
    $('#carousel-ex2').MGCarousel({ banner_nav: '#gallery-scroller-ex3 .scroller', banner_nav_items: '#gallery-scroller-ex3 .scroller-container li', animType: 'slide', animSpeed: 500, rotationMilli: 3000, rotationResumeMilli: 6000, rotate: false });

    /* Tabs
    -------------------------------------------------*/

    $("div#tabs").tabs();

    // Tab Prev/next (not dynamic)
    $("a.go-tab1").click(function (e) {
        $("div#tabs").tabs('select', 0);
        e.preventDefault;
    });
    $("a.go-tab2").click(function (e) {
        $("div#tabs").tabs('select', 1);
        e.preventDefault;
    });
    $("a.go-tab3").click(function (e) {
        $("div#tabs").tabs('select', 2);
        e.preventDefault;
    });
    $("a.go-tab4").click(function (e) {
        $("div#tabs").tabs('select', 3);
        e.preventDefault;
    });

    /* Homepage Tabs
    -------------------------------------------------*/

    function getTabs(selector) { // Selector is UL element containing tab list
        var tabArray = [];
        $(selector).children("li").children("a").each(function () {
            tabArray.push($(this).attr("href"));
        });
        return tabArray;
    }

    function hideAllTabs(selector) { // Selector is UL element containing tab list
        var tabArray = getTabs(selector);
        for (var i = 0; i < tabArray.length; i++) {
            $(tabArray[i]).fadeOut("fast");
        }
    }

    function showFirstTab(selector) { // Finds tab that has the class selected to initialise
        var firstTab = $(selector).children("li").children("a.selected").attr("href");
        $(firstTab).show();
    }

    function showTab(anchor, selector) { // Selector is UL element containing tab list
        hideAllTabs(selector);
        var tabToShow = $(anchor).attr("href");
        $(tabToShow).fadeIn("slow");
    }

    function highlightTab(anchor, selector) { // Selector is UL element containing tab list
        $(selector).children("li").children("a").removeClass("selected");
        $(anchor).addClass("selected");
    }

    /*function init(selector) {
    //hideAllTabs(selector);
    showFirstTab(selector);
    }*/

    if ($("section#home-carousel").length != 0) {

        //init(".home-carousel-nav");

        $(".home-carousel-nav li a").click(function (e) {
            showTab($(this), ".home-carousel-nav");
            highlightTab($(this), ".home-carousel-nav");
            e.preventDefault();
        });

    }

    /* Input clearing
    -------------------------------------------------*/

    $("input[type=text]").focus(function () {
        $(this).val() == $(this).attr("title") ? $(this).val("").css("color", "#333") : null;
    }).blur(function () {
        $(this).val() == "" ? $(this).val($(this).attr("title")).css("color", "#525252") : null;
    }).each(function () {
        $(this).css("color", "#525252");
    });

    /* Homepage background positioning
    -------------------------------------------------*/

    function setBottomBackground() {
        var winWidth = $(window).width();
        var bgPos = (winWidth - 920) / 2 + 602;
        $("#home div.bottom-section").css("background-position", bgPos + "px  0");
    }
    setBottomBackground();

    // Background top section on homepage
    function setTopBackground() {
        var winWidth = $(window).width();
        var bgPos = (winWidth - 4096) / 2;
        if (winWidth <= 1180) {
            var diff = 1180 - winWidth;
            bgPos = ((winWidth - 4096) + diff) / 2;
        }
        $("section#home-carousel").css("background-position", bgPos + "px 0");
    }
    setTopBackground();

    $(window).resize(function () {
        setBottomBackground();
        setTopBackground();
    });

    /* Min-height for pages with spotlights
    -------------------------------------------------*/

    if ($("aside.spotlight").length != 0) {
        var spotHeight = $("aside.spotlight").height();
        var articleHeight = $("div.article-content-container").height();
        if (articleHeight < spotHeight) {
            $("div.article-content-container").height(spotHeight + 20);
        }
    }

    /* Google map init
    -------------------------------------------------*/

    if ($("section#projects-map").length != 0) {
        initialize();
    }

    /* Industry refiner
    -------------------------------------------------*/

    $('.refine-results a.select-all').click(function () {

        $(this).parent().parent().find('ul li input').each(function () {
            $(this).attr('checked', 'checked')
        });

        return false;
    });

    $('.refine-results a.select-none').click(function () {

        $(this).parent().parent().find('ul li input').each(function () {
            $(this).removeAttr('checked')
        });

        return false;
    });



    /************************************************ COLORBOX POPUP ************************************************/
    $(window).load(function () {

        //colorbox
        $('.colorbox').colorbox();

        //directions submit
        $('.directions-submit').live('click', function () {
            $.fn.colorbox({ title: "View Directions", icon: "event", href: $(this).attr('href'), iframe: true, width: "550px", height: "650px", open: true });
            return false;
        });

        //gallery submit
        $('.gallery-submit').live('click', function () {
            $.fn.colorbox({ title: "Project Gallery", icon: "event", href: $(this).attr('href'), iframe: true, width: "550px", height: "650px", open: true });
            return false;
        });

    });

    /* Print Window
    -------------------------------------------------*/

    $(".print").click(function (e) {
        window.print();
        e.preventDefault();
    });

    /* Email Popup
    -------------------------------------------------*/

    $(".email").colorbox({
        'iframe': true,
        'width': 400,
        'height': 440
    });
});




function addBookmark(title, url) {

if (window.sidebar) { // firefox

    window.sidebar.addPanel(title, url,"");

} else if( document.all ) { //MSIE

    window.external.AddFavorite(url, title);

} else if (window.opera) { //Opera

    window.sidebar.addPanel(title, url, "");

} else {

    alert("Sorry, your browser doesn't support this");

}

}
