function onYouTubePlayerReady(playerId) {
    ytplayer = document.getElementById("ytPlayer");
    ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
}

function onytplayerStateChange(newState) {
    if(newState!=-1){
        clearInterval(timer);
        timeroff = true;
    }
}
var hintover = false;
var timeroff = false;

var timer = setInterval(function(){
    nextitem();
},4000);

function nextitem(){
    var current = $('.bcknewslist:visible li.bckactive');
    var next;
    if(current.next().length){
        next = current.next();
    }
    else{
        next =  $('.bcknewslist:visible li:first');
    }
    
    if(next.is('.bckvideoref')){
        playVideo(next.find('a').attr('rel'));
    }
    else{
        changeSlide(next.find('a').attr('rel'));
    }
    current.removeClass('bckactive');
    next.addClass('bckactive');
}

function changeSlide(slideId){
    var slider = $('.bcknewsrotator:visible');
    slider.find('#ytPlayer').remove();
    slider.find('.bckitem:visible').hide();
    slider.find('.bckitem[id="'+slideId+'"]').show();
}

function playVideo(video){
    var videoref = video.split('|')[0].split('/');
    var videoID = videoref[videoref.length - 1];
    $('.bcknewsrotator:visible .bckitem').hide();
    if(!$('.bcknewsrotator:visible #ytPlayer').length){
        $('.bcknewsrotator:visible').append("<div id='bckvideocontainer'>&nbsp;</div>");
    
   
        var params = {
            allowScriptAccess: "always",
			wmode:"opaque"
        };
        var atts = {
            id: "ytPlayer"
        };
        swfobject.embedSWF("http://www.youtube.com/v/" + videoID + 
            "?version=3&enablejsapi=1&playerapiid=player1", 
            "bckvideocontainer", "440", "268", "9", null, null, params, atts);
    }
    else{
        ytplayer = document.getElementById("ytPlayer");
        if(ytplayer) {
            ytplayer.cueVideoById(videoID);
        }
    }
}

var loaded = false;

$(document).ready(function(){
    
    
    function itemClick(item){
        if(item.is('.bckvideoref')){
            playVideo(item.find('a').attr('rel'));
        }
        else{
            changeSlide(item.find('a').attr('rel'));
        }
    }

    function oldplayVideo(video){
        $('.bcknewsrotator:visible .bckitem').hide();
        if(!$('.bcknewsrotator:visible #bckvideocontainer_wrapper').length){
            $('.bcknewsrotator:visible').append("<div id='bckvideocontainer'>&nbsp;</div>");
        }
        jwplayer('bckvideocontainer').setup({
            flashplayer: 'addon/tn/player.swf',
            file: video.split('|')[0],
            image:video.split('|')[1],
            height: 265,
            width: 440,
            skin:'addon/tn/player/modieus.zip'
        });
        jwplayer('bckvideocontainer').onPlay(function(){
            clearInterval(timer);
            timeroff = true;
        });
    }
    function topfivenext(){
        var item = $('.bcktopfiveitm:visible');
        item.hide();
        if(item.next().is('.bcktopfiveitm')){
            item.next().show();
        }
        else{
            $('.bcktopfiveitm:first').show();
        }
    }
    function topfiveprev(){
        var item = $('.bcktopfiveitm:visible');
        item.hide();
        if(item.prev().is('.bcktopfiveitm')){
            item.prev().show();
        }
        else{
            $('.bcktopfiveitm:last').show();
        }
    }
    
    
    
    
    /*if($('.bcknewslist:visible li:first').is('.bckvideoref')){
        playVideo($('.bcknewslist:visible li:first a').attr('rel'));
    }*/
	
    if($('.bcknewslist:visible li:first').length){
        if($('.bcknewslist:visible li:first').is('.bckvideoref')){
            playVideo($('.bcknewslist:visible li:first a').attr('rel'));
        }
    }
    
    $('#bcksrchfld input:first').focusin(function(){
        $(this).val("");
    }).focusout(function(){
        $(this).val("Поиск на сайте...");
    });
    
    $('.bckmenuseclvl').each(function () {
        $(this).parent().eq(0).hover(function () {
            $('.bckmenuseclvl:eq(0)', this).slideDown('fast','linear');
        }, function () {
            $('.bckmenuseclvl:eq(0)', this).slideUp('fast','linear');
        });
    });
    
    $('.bcktabs .bckdrktab').live('click', function() {
        if(!$(this).is('.bcknoclick')){
            $(this).parent().find('>div').removeClass('bcklighttab').addClass('bckdrktab');
            $(this).removeClass('bckdrktab').addClass('bcklighttab');
            //$('#bckvideocontainer_wrapper').remove();
            var tabindex = $(this).parent('.bcktabs').find('> div').index($(this)); //index from 0
            var conts = $(this).parent().parent().find('.bcktabs-content:first');
            conts.find('.bckcontent').hide();
            conts.find('.bckcontent').eq(tabindex).show();
        }
    });
    

    
    var topfivetimer = setInterval(function(){
        topfivenext(); 
    },10000);
    
    $('.bcktopfiveitm .bcknext').click(function(){
        topfivenext();
    });
    
    $('.bcktopfiveitm .bckprev').click(function(){
        topfiveprev();
    });
    
    $('.bckonlinelist a').click(function(){
        var rel = $(this).attr('rel');
        $(this).parents('.bckcontent').find('.bckteamcont:visible > .bckgameinfo').hide();
        $(this).parents('.bckcontent').find('.bckteamcont:visible #'+rel+'.bckgameinfo').show();
        $(this).parent().parent().parent().find('tr:hidden').show();
        $(this).parent().parent().hide();
    });
    
    $('#bckclubnews .bcknewslist > li').hover(function() {
        clearInterval(timer);
    }, function(){
        if(!timeroff){
            timer =  setInterval(function(){
                nextitem();
            }, 4000);
        }
    });
    
    $('.bckvideoref').click(function(){
        clearInterval(timer);
        playVideo($('a',this).attr('rel'));
    });
    
    $('#bckclubnews .bcknewslist > li').live('click', function(){
        $('.bcknewslist > li:visible').removeClass('bckactive');
        $(this).addClass('bckactive');
        itemClick($(this));
        if((window.pageYOffset-$('.bcknewsrotator').offset().top)>0){
            window.scrollTo(0,$('.bcknewsrotator').offset().top-40);
        }
        clearInterval(timer);
        timeroff = true;
    });
    
    $('.bckselect').each(function(){
        $(this).find('.bckselected a').text($(this).find('.bckitm:first').text());
    });
    
    $('.bckitm').click(function(){
        $(this).parent().prev().find('.bckselected a').text($(this).text());
    });
    
    $('.bckdroplist').click(function(){
        var thisdroplist = $(this);
        var itemsH = 0;
        var pane = $(this).next('.bckitemlist');
        var pos = $(this).position();
        pane.css({
            'top':pos.top+23,
            'left':pos.left
        });
        $(this).addClass('bckon');
        pane.find('.bckitm').each(function(){
            itemsH+=$(this).outerHeight();
        });
        pane.slideDown(200, function(){
            $('body').bind("click",function(){
                $('body').unbind('click');
                pane.slideUp(200, function(){
                    thisdroplist.removeClass('bckon');
                });
            });
        });
    });
    
    $('.bcknavdate a.bcklft').click(function(){
        var cur = $('.bckmonth-container:visible .bckweeks:visible');
        if(cur.prev().length){
            cur.hide();
            cur.prev().show();
            $(this).parent().find('.bckorangedate').text(cur.prev().find('input:hidden').val());
        }
    });
    
    $('.bcknavdate a.bckrght').click(function(){
        var cur = $('.bckmonth-container:visible .bckweeks:visible');
        if(cur.next().length){
            cur.hide();
            cur.next().show();
            $(this).parent().find('.bckorangedate').text(cur.next().find('input:hidden').val());
        }
    });
    
    $('.bckmatch').each(function(){
        $(this).css('z-index',$('.bckmatch').length - $('.bckmatch').index($(this)));
    });
    $('.bckmatch').mouseover(function(){
        hintover = true;
    });
    $('.bckmatch').mouseout(function(){
        hintover = false;
    });
    $('.bckweekrow .bckmatch').hover(function(){
        $('.bckmatchinfo:visible').css('display','none');
        var rightlimit = $('#bckinfopanel').offset().left + $('#bckinfopanel').outerWidth();
        var pos = $(this).offset().left + 44;
        if((rightlimit - pos)<240){
            $(this).find('.bckmatchinfo').css({
                'left':'auto',
                'right':'0px'
            });
        }
        $(this).find('.bckmatchinfo').css('display','block');
    },function(){
        var $this = $(this);
        var timer = setTimeout(function(){
            if(!hintover){
                $this.find('.bckmatchinfo').css('display','none');
                clearInterval(timer);
            }
        },500);
    });
	
    //$('.bckbigdiv').css('top',$('#bcksiteinfo').offset().top);
	//$('.bckbigdiv').css('top',$('#bcksiteinfo').position().top);
	
	$('.bcknewstab').live('click',function(){
		if(!loaded){
			loaded=true;
			$(this).find('i a').trigger('click');
		}
	});
	
});

/*
$(window).load(function(){
	$('.bckbigdiv').css('top',$('#bcksiteinfo').offset().top);
});*/
