var allPreviews = 1;
var show = 1;
var vcontrol = '<div class="allveiw"><a href="javascript:showOnly(\'all\')">Показать все видео</a></div><div class="next"><a href="javascript:plus()"><img src="../../../megatron.ru/templates/megatron/images/spacer.gif" alt="Вперед"/></a></div><div class="prev"><a href="javascript:minus()"><img src="../../../megatron.ru/templates/megatron/images/spacer.gif" alt="Назад"/></a></div>';

function countAll() {
    while ($('#preview'+allPreviews).html() != null) {
        $('#preview'+allPreviews).css('display', 'none');
        allPreviews++;
    }
}

function hideAll() {
    var counter = 1;
    
    while ($('#preview'+counter).html() != null) {
        $('#preview'+counter).css('display', 'none');
        counter++;
    }
}

function showVideo(id) {
    $('#centered').html('<div style="height:347px; width:425px;"><a href="javascript: closeVideoBlock()" style="float: right;"><img src="../../../megatron.ru/templates/megatron/images/close.png" /></a><object width="425" height="325"><param name="movie" value="../../../www.youtube.com/v/' + id + '&hl=ru&fs=1&rel=0"></param><param name="wmode" value="transparent" /><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="../../../www.youtube.com/v/' + id + '&hl=ru&fs=1&rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" wmode="transparent" width="425" height="325"></embed></object></div>').css('display', 'table');
    $('#opacited').css('display' , 'table');
    $('#opacited').css('opacity' , 0).animate({'opacity': '0.8' }, 1000);
}

function closeVideoBlock () {
    $('#opacited').css('opacity' , '0.8').animate({'opacity': 0 }, 1000, function () {
        $('#opacited').css('display' , 'none');
        $('#centered').html('');
    });
   
}

function checkstr(strnum) {
    var cinfirmed = 0;
    var end = (strnum * 6)+1;
    var start = end - 6;
    
    while (start<end) {
        if ($('#preview'+start).html() != null) {
            cinfirmed++;
        }
        start++;
    }
    
    return cinfirmed;
}

function plus () {
    var checkshow = show + 1;
    if(checkstr(checkshow) > 0) {
        show = checkshow;
        hideAll();
        showOnly(checkshow);
    }
}

function minus () {
    var checkshow = show - 1;
    if(checkstr(checkshow) > 0){
        show = checkshow;
        hideAll();
        showOnly(checkshow);
    } 
}

function showOnly (strId) {
    if(strId != 'all') {
        var end = (strId * 6)+1;
        var strId = end - 6;
    } else {
        var strId = 1;
        var end = allPreviews;
    }
    
    for (strId; strId<end; strId++) {
        if($('#preview'+ strId).html() != null) {
            $('#preview'+ strId).css('display', 'table');
            
        }
    }
}

$(document).ready(function () {
    countAll();
    hideAll();
    showOnly(1);
    $('.vcontrl').css('display', 'table');
    $('.vcontrl').html(vcontrol);
});
