$(function () {

    /**/// nadaję klasę body, mówiącą, że jest obsługa js (eng. adding a class to <body> that says if the javascript is working)
    $("body").addClass("js-enabled");

    /**/// css3 selectors for IE lte 8
    if ($.browser.msie && $.browser.version < 9) {
        $("table tbody tr:odd").addClass("odd");
        $(".grid-logotypes li:nth-child(1n)").addClass("even");
        $(".grid-logotypes li:nth-child(2n)").addClass("odd");
        $(".team-hcards li:first-child, .team-hcards li:nth-child(4n+5)").addClass('nth-child')
        $("#content-main > *:last").not(".finances, .fade-buttons-containe, .grid-logotypes, .listing-showcase").addClass("main-content-last-child");
    }

    /**/// piętnuję operę
    if ($.browser.opera) {
        $("body").addClass("opera");
    }

    /**/// add class to content if the image module is present
    $('#content').has('#content-picture').find("#content-main").addClass("no-top");

    /**/// team
    $(".listing-interactive > li").addClass("initial-state"); // initially hiding elements



    /**




    * the images for this module must have some naming convention - can me
    * renamed via CMS mechanism for normal state (violet, small): somename.jpg
    * for hover state (full colours, small): somename-hover.jpg for unfolded
    * state (full colours, big): somename-big.jpg
    */
    $(".listing-interactive h3 a, a.listing-readmore").live("click", function () {
        $(this).offsetParent().toggleClass("initial-state");
        var image = $(this).offsetParent().find('.ls-pic');

        var imagSmall = $(this).offsetParent().find('.ls-pic-Small');
        var imageHover = $(this).offsetParent().find('.ls-pic-Hover');
        var imageBig = $(this).offsetParent().find('.ls-pic-Big');

        var initialSrc = image.attr("src");
        var unfoldedSrc = image.attr("src");
        var hoverSrc = imageHover.attr("src");
        var imageBigSrc = imageBig.attr("src");
        
        if ($(this).offsetParent().hasClass('initial-state')) {
            image.attr('src', hoverSrc);
            $(this).offsetParent().find('a.listing-readmore').show();
        } else {
            image.attr('src', imageBigSrc);
            $(this).offsetParent().find('a.listing-readmore').hide();
        }
    });
    $('.listing-interactive h3 a').live('mouseover', function () {

        var image = $(this).offsetParent().find('.ls-pic');

        var imageHover = $(this).offsetParent().find('.ls-pic-Hover');
        var imageHoverSrc = imageHover.attr("src");

        if ($(this).offsetParent().hasClass('initial-state')) {
            image.attr('src', imageHoverSrc);
        }
    });
    $('.listing-interactive h3 a').live('mouseout', function () {

        var image = $(this).offsetParent().find('.ls-pic');

        var imagSmall = $(this).offsetParent().find('.ls-pic-Small');

        var imagSmallSrc = imagSmall.attr("src");

        if ($(this).offsetParent().hasClass('initial-state')) {

            image.attr('src', imagSmallSrc);
        }
    });

    /**/// requirements
    $('.table-of-content .interact').live('mouseover', function () { // mouse over
        // wysokość nadana wyskakującej warstwie z obrazkiem
        var imgOverlayHeight = $('#content').innerHeight();
        // skopiowanie zawartości węzła i usunięcie klasy
        var copyThis = $(this).clone().removeClass('interact');
        // ukazanie warstwy i wstawienie do niej skopiowanego htmla
        $(this).parent('li').find('div').removeClass('hide').css('height', imgOverlayHeight).append(copyThis);
    });
    $('.table-of-content .interact').live('mouseout', function () {// mouse out
        $(this).parent('li').find('div').addClass('hide'); // ukrycie warstwy
        $(this).parent('li').find('div').find('p').remove(); // usunięcie z niej zbędnego html
    });

    /**/// view map - case studies
    $('.show-map').live('click', function (event) {//ukazanie mapy
        //zapobiegnięcie przejścia do map google, które jest ok w razie wyłączonego js
        event.preventDefault();
        // wysokość nadana wyskakującej warstwie z obrazkiem
        var mapOverlayHeight = $('#content').innerHeight();
        // ukrycie innych map, jeśli były ukazane
        $('.map').not('.hide').addClass('hide');
        //ukazanie mapy
        $(this).parents('li').find('.map').removeClass('hide').css('height', mapOverlayHeight);
        $('html, body').animate({ scrollTop: 0 }, 'slow');
    });
    $('.close-preview').click(function () {//ukazanie mapy
        $(this).parents('.map').addClass('hide');
    });
    $(".enlarge").colorbox({
        opacity: 0.9,
        iframe: true,
        width: 640,
        height: 480
    });

    /**/// tooltip on logotypes
    $(".new-partner").tooltip({
        position: 'bottom center',
        tipClass: "partner-tip",
        events: {
            tooltip: "mouseenter,mouseleave mouseover"
        }
    });

    /**/// tooltip on pins on the grid-map
    $("#map-locations li > a").tooltip({
        position: 'top right',
        offset: [0, -22],
        relative: true,
        delay: 600
    }).dynamic({ bottom: { direction: 'down'} });

    /**///partners hcards
    $(".team-hcards li").hover(function () {
        photo = $(this).find('.ls-pic-Main');


        var imageHover = $(this).find('.ls-pic-Hover');

        var imageHoverSrc = imageHover.attr("src");

        //var imageSmall = $(this).offsetParent().find('.ls-pic-Small');
        //  var imageSmallSrc = imageSmall.attr("src");


        var photoSrc = photo.attr("src")

        //alert(imageHoverSrc);
        var photoReplace = photoSrc.replace(".jpg", "-hover.jpg");
        photo.attr('src', imageHoverSrc);
    }, function () {
        var imageSmall = $(this).find('.ls-pic-Small');
        var imageSmallSrc = imageSmall.attr("src");

        photo.attr('src', imageSmallSrc);
    });

    /**///formularz kontaktowy
    //    $('.frm-contact input[type="text"], .frm-contact textarea').prev('label').addClass("hide");
    //    $('.frm-contact input[type="text"], .frm-contact textarea').map(function () {
    //        var test = $.trim($(this).val()).length;
    //        if (test == 0) {
    //            var newVal = $(this).prev("label").text();
    //            $(this).val(newVal);
    //        };
    //    });
    //    $('.frm-contact input[type="text"], .frm-contact textarea').focus(function () {
    //        var label = $(this).prev("label").text();
    //        var inputVal = $.trim($(this).val());
    //        if (label == inputVal) { $(this).val(''); }
    //    });
    //    $('.frm-contact input[type="text"], .frm-contact textarea').blur(function () {
    //        var label = $(this).prev("label").text();
    //        var inputValLength = $.trim($(this).val()).length;
    //        if (inputValLength == 0) { $(this).val(label); }
    //    });

    /**///pojawianie się i znikanie mapy na kontakt
    $('.map-trigger').live('click', function (event) {
        //zapobiegnięcie przejścia do map google, które jest ok w razie wyłączonego js
        event.preventDefault();
        $('.map-contact').removeClass('hidden'); //ukazanie mapy
        $('.frm-contact').not('.hide').addClass('hide'); //jeśli trzeba ukrycie formularza
    });
    //    $('.team-hcards a').live('click', function (event) {
    //        //zapobiegnięcie otworzniu klienta poczty, które jest ok w razie wyłączonego js
    //        event.preventDefault();
    //        //usunięcie innego (poprzedniego) odbiorcy
    //        $(".frm-contact legend span").remove();
    //        //pobranie nazwiska obecnego odbiorcy
    //        var receiver = $(this).parent('li').find('.fn').text();
    //        //wstawienie nazwiska obecnego odbiorcy
    //        $('.frm-contact legend').append('<span> to ' + receiver + '</span>');
    //        $('.map-contact').not('.hide').addClass('hide'); //jeśli trzeba ukrycie mapy
    //        $('.frm-contact').removeClass('hide'); //ukazanie formularza
    //    })//ukazanie mapy

    /**///homepage nav animation
    $('.pagetype-home #nav-main').hide();

    $('.pagetype-home #content-sub').hide().delay(500).animate({
        height: "show",
        opacity: "show"
    }, 2000, function () { $('.pagetype-home #nav-main').fadeIn(1000) });

    /**///about us, fading buttons
    var delay = 500;
    $('.container.finances li').each(function () {
        $(this).hide().delay(delay).fadeIn(2000);
        delay += 2000;
    });

    /**///accessibility bar
    $("#txt-small").click(function () {
        $('body').not('.typo-small').addClass('typo-small').removeClass('typo-big');
    });
    $("#txt-normal").click(function () {
        $('body').removeClass('typo-small typo-big');
    });
    $("#txt-big").click(function () {
        $('body').not('.typo-big').addClass('typo-big').removeClass('typo-small');
    });

});

