/* Author:

*/
$(function() {

        var footersections = $.cookie('footersections');

        if($('#sitemapsection').length > 0){
            $('#sitemapsection').masonry({ itemSelector: 'dl'/*, isAnimated: true, animationOptions: { duration: 200 } */}); // sitemapsection

            callback('#sitemapsection', function() { // masonry callback fix
                    $('#sitemapsection').parents('.show:eq(0)').removeClass('show');
            });
        }

        $('footer .infoBlock > li').each(function(){
                var li = $(this);
                var full = li.find('.full');
                li.find('.door').toggle(function(){
                        full.css('opacity',0);
                        full.stop().slideDown(350,'easeOutBack',function(){
                                li.addClass('show');
                                $(window).resize();
                                full.animate({'opacity':1},100, function(){
                                        if (full.hasClass('sitemapsection')) { 
                                            $('#sitemapsection').masonry({ itemSelector: 'dl'/*, isAnimated: true, animationOptions: { duration: 200 } */}); // sitemapsection
                                        }
                                        if (full.hasClass('moscowsection')) { 
                                            var mos = $('#moscowsection');
                                            if (!mos.hasClass('jcarousel-list')) createCarousel(mos);
                                        }
                                        if (full.hasClass('russiasection')) { 
                                            var rus = $('#russiasection');
                                            if (!rus.hasClass('jcarousel-list')) createCarousel(rus);
                                        }
                                        saveDataToCookie();
                                        andResize();
                                });
                        });
                    },function(){
                        full.stop().slideUp(250,'easeInBack',function(){
                                if (full.hasClass('masonry')) $('#sitemapsection').masonry('destroy');
                                li.removeClass('show');
                                saveDataToCookie();
                                andResize();
                        });
                });
                li.removeClass('show');
        });


        if (footersections && footersections.length > 0) {
            footersections = footersections.split('|');
            for (var i=0; i<footersections.length; i++) {
                if (footersections[i] > 0) $('footer .infoBlock > li:eq('+i+')').find('.door').click();
            }
        }


        function createCarousel(obj){
            obj.each(function(){ // moscowsection && russiasection
                    var carousel = $(this);
                    var tmr;
                    carousel.jcarousel({
                            wrap: 'circular',
                            initCallback: function(){
                                var crsl = this, jcarouselcontainer = carousel.parents('.jcarousel-clip:eq(0)');
                                var LI = carousel.parents('LI');
                                var W = 0, li = carousel.find('LI'), full = LI.find('.full'), liH = 0;
                                li.each(function(){
                                        W += $(this).width();
                                        liH = Math.max($(this).height(), liH);
                                });
                                li.height(liH); carousel.height(liH);
                                W = W/li.length;
                                jcarouselcontainer.css('max-width','9999em');
                                var cnt = Math.floor((jcarouselcontainer.width()) / W);
                                cnt = (cnt<1)? 1 : cnt;
                                var scr = (cnt > 3) ? 3 : cnt-1;
                                crsl.scroll = (scr > 0) ? scr : 1;
                                crsl.visible = cnt;
                                jcarouselcontainer.css({'max-width': cnt * W, 'margin-left':'auto', 'margin-right':'auto' });
                                $(window).resize(function(){
                                        window.clearTimeout(tmr);
                                        tmr = window.setTimeout(function(){
                                                window.clearTimeout(tmr);
                                                li.width('');
                                                crsl.initCallback(crsl,'reset');
                                            }, 10);
                                });
                                andResize();
                            }
                    });
            });
        }

        function andResize() {
            post('resize_frame_footer_123',{h:($('body').height()),w:0});
        }

        function saveDataToCookie() {
            var newcookie = '';
            $('footer .infoBlock > li').each(function(i){
                    if (i > 0) newcookie += '|';
                    newcookie += ($(this).hasClass('show'))? '1':'0';
            });
            $.cookie('footersections', newcookie,{path:'/',expires:3652});
        }

}); // DOM loaded

var callback = function(el, cb) {
    if (!$(el).data('masonry')) {
        setTimeout(callback(el, cb), 300);
    } else {
        cb();
    }
}


$('footer').addClass('foo');
/* for mobile */
var mobile = (/iphone|ipad|ipod|android|blackberry|mini|windows\sce|palm/i.test(navigator.userAgent.toLowerCase()));
if(mobile){
    $('footer').addClass('ios');
}
/* /for mobile */

