/*jslint white: true, onevar: true, undef: true, newcap: true, nomen: true, regexp: true,
plusplus: true, bitwise: true, strict: true, devel: true, maxerr: 50, maxlen: 120, indent: 4 */
/*globals $, window, navigator*/

$(function () {
    'use strict';
    /* make all icons gray */
    function resetIcons() {
        $('#footer ul li a').each(function () {
            $(this).addClass($(this).attr('rel') + '_icon').removeClass($(this).attr('rel') + '_icon_hover');
        });
    }

    /* main nav */
    if (navigator.platform === "iPad") {
        $('#nav li a').bind('touchend', function () {
            window.location.href = $(this).attr('href');
            $('#nav li').hover(function () {
                $(this).find('img.menu').hide();
                $(this).find('img.menuHover').show();
            }, function () {
                if (!$(this).hasClass('active')) {
                    $(this).find('img.menu').show();
                    $(this).find('img.menuHover').hide();
                }
            });
        });

        $('#main').bind('touchend', function () {
            $("#footer .hover_container .hover").hide();
            resetIcons();
        });
    }
    else {
        $('#nav li').hover(function () {
            $(this).find('img.menu').hide();
            $(this).find('img.menuHover').show();
        }, function () {
            if (!$(this).hasClass('active')) {
                $(this).find('img.menu').show();
                $(this).find('img.menuHover').hide();
            }
        });
    }
    /* show popup */
    $('#footer>ul>li:not(#sitemapHover, #joinTheTeamHover)>a').mouseenter(function () {
        $("#footer .hover_container .hover").hide();
        resetIcons();
        $('.' + $(this).attr('rel')).fadeIn();
        $(this).removeClass($(this).attr('rel') + '_icon').addClass($(this).attr('rel') + '_icon_hover');
    });

    $('#footer').mouseleave(function () {
        resetIcons();
        $('#footer .hover:not(#sitemapHover, #joinTheTeamHover)').fadeOut();
    });

    $('#footer ul li.sitemap span').click(function () {
        if (navigator.platform !== "iPad") {
            $.blockUI({
                message: null,
                overlayCSS: {
                    cursor: 'default'
                }
            });
        }
        $('#video object').css('visibility', 'hidden');
        $("#footer .hover_container .hover").hide();
        resetIcons();
        $('#sitemapHover').fadeIn();
    });

    $('#footer #sitemapHover .close a').click(function (e) {
        $("#footer .hover_container .hover").hide();
        resetIcons();
        $('#sitemapHover').fadeOut();
        $.unblockUI();
        e.preventDefault();
    });

    /*Join The Team*/
    $('#footer ul li.join span, div#right div.joinUsBlock').css("cursor", "pointer").click(function () {

        $("#footer .hover_container .hover").hide();
        resetIcons();

        if (navigator.platform !== "iPad") {
            $.blockUI({
                message: null,
                overlayCSS: {
                    cursor: 'default'
                }
            });
        }

        $('#video object, #video video').css('visibility', 'hidden');
        $('#joinTheTeamHover').fadeIn('normal', function () {
            var jobFirst = $('#footer #joinTheTeamHover .titleRow').removeClass("active").filter(':first'),
                close = $("#footer #joinTheTeamHover .close a").position(),
                closeOffset = $("#footer #joinTheTeamHover .close a").offset();

            $(jobFirst).addClass('active').parents("div.jobOffer").next("div.jobOffer").fadeOut('fast',
                function () {
                    $(this).load($(jobFirst).children('a').attr("href"), function () {
                        $(this).fadeIn('slow');
                    });
                });
            $(window).scrollTop(parseInt(closeOffset.top - 2 * close.top, 10));
        });

    });

    $('#footer #joinTheTeamHover .close a').click(function (e) {
        $('#video object, #video video').css('visibility', 'visible');
        $('#joinTheTeamHover').fadeOut();


        $.unblockUI();
        $('#footer #joinTheTeamHover div.jobOffer:last').hide();
        e.preventDefault();
    });

    $('#footer #joinTheTeamHover .titleRow a').click(function (e) {

        var aLink = $(this);
        $(this).parents("div.jobOffer").next("div.jobOffer").fadeOut('slow',
            function () {
                $(this).load(aLink.attr("href"), function () {
                    $(this).fadeIn('slow');
                });
            });
        $('#footer #joinTheTeamHover div.jobOffer .titleRow').removeClass("active");
        $(this).parent('div.titleRow').addClass("active");
        e.preventDefault();
    });

    /* disable Twitter links */
    $('.twtr-reply,.twtr-timestamp,#footer a.more').live('click', function (e) {
        e.preventDefault();
    });

    /* remove dot between date and reply link */
    $('a.more').mouseover(function () {
        $('.twtr-tweet-text p em').each(function () {
            var children = $(this).children();
            $(this).text('');
            $(this).append(children);
        });
    });

    /* remove underline from twitter date */
    $('.twtr-reply,.twtr-timestamp').live('mouseover', function () {
        $(this).css('text-decoration', 'none !important');
    });

    /* Offices switch */
    $(".officeImage .thumbnailContainer .thumbnail").css("cursor", "pointer").click(function () {
        var ind = $(this).prevAll().length;
        $(".officeImage .thumbnailContainer .thumbnail img ").removeClass("active");
        $(this).children("img").addClass("active");
        $(".officeImage .imageContainer div.imageRow").removeClass("show").addClass("hide").
            eq(ind).removeClass("hide").addClass("show");
    });

    /* Employees */
    $('#tbscgPeople .employee').hover(function () {
        $(this).addClass('active');
        var balloon = $(this).find('.employeeBalloon'), height = balloon.height();
        balloon.css('margin-top', -235 + (145 - height));
    }, function () {
        $(this).removeClass('active');
    });

    /* Clients */
    $('#tbscgClients .client').hover(function () {
        $(this).addClass('active');
        var balloon = $(this).find('.clientBalloon'), height = balloon.height();
        balloon.css('margin-top', -235 + (145 - height));
    }, function () {
        $(this).removeClass('active');
    });

    /* After work gallery */
    $('.imageThumbnails .afterWork').hover(function () {
        $(this).addClass('active');
        var balloon = $(this).find('.tooltip .balloonContent'), height = balloon.height();
        balloon.parent().css('margin-top', -150 + (34 - height));
    }, function () {
        $(this).removeClass('active');
    });

    /* language switch */
    if (navigator.platform === "iPad") {
        $('.languageswitch a').bind('touchend', function () {
            window.location.href = $(this).attr('href');
            $('.languageswitch span:not(.current)').hover(function () {
                $(this).find('.active').show();
                $(this).find('.inactive').hide();
            }, function () {
                $(this).find('.active').hide();
                $(this).find('.inactive').show();
            });
        });
    }
    else {
        $('.languageswitch span:not(.current)').hover(function () {
            $(this).find('.active').show();
            $(this).find('.inactive').hide();
        }, function () {
            $(this).find('.active').hide();
            $(this).find('.inactive').show();
        });
    }
    /* active showcase category */
    $('.showcaseContent .categories ul li a.active').click(function (e) {
        e.preventDefault();
    });

    if (navigator.userAgent.toLowerCase().search("android") > -1) {
        $('#compVideo').remove();
    }

    $('#footer .tweet').tweet({
        avatar_size: 32,
        count: 2,
        username: "tbscg",
        template: "{text}"
    });
});

$(document).ready(function () {
    $("span.fb_share_no_count").each(function () {
        $(this).removeClass("fb_share_no_count");
        $(".fb_share_count_inner", this).html("0");
    });
});
