function onYouTubePlayerReady(playerId) {
    console.log(playerId);
    if (playerId == 'yvideo1') {
        var ytplayer1 = document.getElementById("player1");

    }
    else if (playerId == 'yvideo2') {
        var ytplayer2 = document.getElementById("player2");
    }
}

$(document).ready(function() {


    $("#banner-items").jshowoff({ speed:4000, changeSpeed:1000,  links:false, controls:false });

    $('#today > .header').click(function() {
        if (!$(this).hasClass('toggled')) {
            $('#today > .header').toggleClass('toggled');
            $(this).parent().toggleClass('toggled');
            $('.slides-togglable').toggle();
        }
    });

    $('.today-nav a').click(function(e) {
        e.preventDefault();
        $(this).parent().parent().find('.today-img').attr('src', $(this).attr('rel'));
        $(this).parent().find('a').removeClass('active')
        $(this).addClass('active')
    });

    $('#project-complectation').click(function(e) {
        e.preventDefault();
        $('#project-detail-complectation').slideToggle();
    });

    $('#project-detail-thumbs a').click(function (e) {
        e.preventDefault();
        $('#project-image img').attr('src', $(this).attr('href'));
        $('#project-image a').attr('href', $(this).attr('big'));
        $('#project-detail-thumbs a').removeClass('active-thumb');
        $(this).addClass('active-thumb');
    });

    var current_video = $('#video-first a').attr('rel');
    var current_video_title = $('#video-first a').attr('title');

    function top_video_nav() {
        $('#video-first a').click(function (e) {
            e.preventDefault();
            current_video = $(this).attr('rel');
            current_video_title = $(this).attr('title')
            document.getElementById("player1").loadVideoById(current_video);
//        ytplayer1.loadVideoById($(this).attr('rel'));
            $(this).parent().parent().find('.video-block').removeClass('active');
            $(this).parent().addClass('active');
            $('#video-first a').removeClass('running');
            $(this).addClass('running');
        });

    }

    function bottom_video_nav() {
        $('#second-video-bottom a').click(function (e) {
            e.preventDefault();
            document.getElementById("player2").loadVideoById($(this).attr('rel'));

//        ytplayer2.loadVideoById($(this).attr('rel'));
            $('#video-header').text(
                $(this).attr('title')
            );
            $('.bottom-video-block').removeClass('active');
            $(this).parent().addClass('active')
        });

        $('#right-video').click(function(e) {
            e.preventDefault();
            next = $('.bottom-video-block.active').next()
            if (next.length != 0) {
                $('.bottom-video-block').removeClass('active');
                next.addClass('active');
                document.getElementById("player2").loadVideoById(next.find('a').attr('rel'));
                $('#video-header').text(next.find('a').attr('title'));
            }
        });

        $('#left-video').click(function(e) {
            e.preventDefault();
            next = $('.bottom-video-block.active').prev()
            if (next.length != 0) {
                $('.bottom-video-block').removeClass('active');
                next.addClass('active');
                document.getElementById("player2").loadVideoById(next.find('a').attr('rel'));
                $('#video-header').text(next.find('a').attr('title'));
            }
        });

    }

    top_video_nav();
    bottom_video_nav();

    var one;
    var two = 'start';
    var blocks;
    var video_height = 214;

    $('#more-video').funcToggle('click', function(e) {
            e.preventDefault();
            video_open = true;

            $('#center-wrapper').animate({height:'880px'});
            $('#video').animate({height:'524px'}, function() {
                $('#more-video').addClass('less');
                $('#more-video').text('Свернуть');
            });
//        if (today_open){
//            height_news = '1124px';
//            height_news_inner = '1070px';
//        } else {
//             height_news = '854px'
//             height_news_inner = '810px'
//        }
//            $('.news-block\.hidden').show();
//            $('#news-wrapper').animate({height:height_news});
//            $('#news-inner').animate({height:height_news_inner});

            add_height($('#center-wrapper'), video_height);
            add_height($('#news-wrapper'), video_height);
            add_height($('#news-inner'), video_height);
            $('.news-block\.hidden').show();


            one = $('#player1-wrapper').clone();
            $('#player1-wrapper').remove();
            blocks = $('#video-blocks').clone();
            $('#video-blocks').remove();
            if (two == 'start') {
                $('#second-video').show();
                var params = { allowScriptAccess: "always", wmode:'opaque'};
                var atts = { id: "player2" };
                swfobject.embedSWF("http://www.youtube.com/e/" +
                    $('.bottom-video-block.active a').attr('rel') + "?enablejsapi=1&playerapiid=yvideo2",
                    "video2e", "460", "270", "9", null, null, params, atts);

            } else {
                two.appendTo('#video');
                bottom_video_nav();
            }

        }, function(e) {
            e.preventDefault();
            video_open = false;

            $('#video').animate({height:'310px'}, function() {
                $('#more-video').removeClass('less');
                $('#more-video').text('Еще видео');
            });


//           if (today_open){
//            height_news = '910px';
//           height_news_inner='850px';
//        } else {
//             height_news = '640px';
//             height_center = '660px';
//             height_news_inner='580px';
//        }

            add_height($('#center-wrapper'), -video_height);
            add_height($('#news-wrapper'), -video_height);
            add_height($('#news-inner'), -video_height);
            $('.news-block\.hidden').hide();

//            $('#center-wrapper').animate({height: height_center});
//            $('#news-wrapper').animate({height:height_news}, function(){
////                $('.news-block\.hidden').hide();
//            });
//            $('#news-inner').animate({height:height_news_inner});
            two = $('#second-video').clone();
            $('#second-video').remove();

            blocks.appendTo('#video');
            one.appendTo('#video');
            top_video_nav();

        }
    );

      $('.more-news').click( function(e) {
          e.preventDefault();
          if ($('.news-block.open').length < 5) {
              $('.news-block').slideDown().addClass('open');
          } else {
          $('.news-block.open').first().slideUp().removeClass('open');
          }
          
      });
//    $('.more-news').funcToggle('click', function(e) {
//            e.preventDefault();
//            video_open = true;
//            if ($('#news-wrapper').height() > 1060) {
//                $(this).css({'background-image':'url("/static/img/more_news.png")'});
//
//                $('#close-today').click();
//
//                $('#video').animate({height:'310px'});
//                $('#center-wrapper').animate({height:'660px'});
//                $('#news-wrapper').animate({height:'640px'});
//                $('#news-inner').animate({height:'580px'});
//                $('.news-block\.hidden').hide();
//
//
//                video_open = false;
//
//            $('#video').animate({height:'310px'}, function() {
//                $('#more-video').removeClass('less');
//            });
//
//
//            two = $('#second-video').clone();
//            $('#second-video').remove();
//
//            blocks.appendTo('#video');
//            one.appendTo('#video');
//            top_video_nav();
//
//
//            } else {
//
//                add_height($('#center-wrapper'), 200);
//                add_height($('#news-wrapper'), 200);
//                add_height($('#news-inner'), 200);
//                $('.news-block\.hidden').show();
//                    console.log($('#news-wrapper').height());
//                if ($('#news-wrapper').height() > 1030) {
//                    console.log($('#news-wrapper').height());
//                    console.log($(this));
//                  $(this).css({'background-image':'url("/static/img/less_news.png")'});
//                }
//            }
//        }
//    );


    $('#today-slides').jshow_mod({
        links:false, controls:false, autoPlay:false,
        hoverPause:false, effect:'slideLeft' });
    $('#today-slides2').jshow_mod({
        links:false, controls:false, autoPlay:false,
        hoverPause:false, effect:'slideLeft' });

    var lbParams = {
        txtImage:'Изображение',
        txtOf:'из',
        imageLoading:            '/static/lightbox/images/lightbox-ico-loading.gif',        // (string) Path and the name of the loading icon
        imageBtnPrev:            '/static/lightbox/images/lightbox-btn-prev.gif',            // (string) Path and the name of the prev button image
        imageBtnNext:            '/static/lightbox/images/lightbox-btn-next.gif',            // (string) Path and the name of the next button image
        imageBtnClose:            '/static/lightbox/images/lightbox-btn-close.gif',        // (string) Path and the name of the close btn
        imageBlank:                '/static/lightbox/images/lightbox-blank.gif'
    };

    $('#project-image a').lightBox(lbParams);
    $('#news-images a').lightBox(lbParams);
    $('#released-project-images a').lightBox(lbParams);
    $('a.lightboxed').lightBox(lbParams);
    $('.extra-site-pictures a').lightBox(lbParams);

    $("#features a").click(function(e) {
        e.preventDefault();
        var o = $(this).next();

        //console.log($(o).attr("display"));
        $(o).slideToggle();
    });

//    $('.extra-site-side input[type="submit"]').click(function (e) {
//        e.preventDefault();
//        $('#result1').text(parseInt($('#square1').val())*120000);
//    });
//    $('.extra-site-side input[type="text"]').keyup(function (e) {
//        $(this).next().text(parseInt($(this).val())*120000 || '');
//    })

    $('#calculate').click(function(e){
        e.preventDefault();
        var sum =0;
        $('input[type="checkbox"]:checked:enabled').each(function(){
            sum += parseInt($(this).attr('data-price'));
//            console.log($(this).val());
        });
        sum = parseInt($('#square').val())*sum || '';
        if (sum) { sum += ' р.'};
        $('#result span').text(sum);
    })
});




