﻿

function openCloseDiv(num) {
    var div = $('div_' + num);
    if (div) {
        if (div.getStyle('height').toInt() == div.getScrollSize().y) {
            div.morph({ 'height': 0 });
        }
        else if (div.getStyle('height').toInt() == 0) {
            div.morph({ 'height': div.getScrollSize().y });
        }
    }
}

window.addEvent('domready', function() {
    var video = 'http://www.youtube.com/apiplayer?enablejsapi=1&playerapiid=ytplayer';
    var obj = new Swiff(video, {
        id: 'myytplayer',
        width: 380,
        height: 284,
        container: $('div_video'),
        params: {
            bgcolor: '#cccccc',
            allowScriptAccess: "always"
        }
    });
    $('video_controls').setStyle('opacity', '0.6');
    var divVideo = $('div_full_video');
    if (divVideo) {
        divVideo.addEvents({
            'mouseenter': function() {
                showControlsVideo();
            },
            'mouseleave': function() {
                NoshowControlsVideo();
            }
        });
    }

    var btnPause = $('pause');
    if (btnPause) {
        btnPause.setStyle('display', 'none');
    }

    var vtnOff = $("sound");
    if (vtnOff) {
        vtnOff.setStyle('display', 'none');
    }

});

function showControlsVideo() {
    var divCtr = $('video_controls');
    divCtr.setStyle('display', 'block');
}

function NoshowControlsVideo() {
    var divCtr = $('video_controls');
    divCtr.setStyle('display', 'none');
}

function SplitIdVideo() {
    ids = document.getElementById("ctl00_ContentMain_hiddVid_ID").value;
    idsSplit = ids.split(';');
    return idsSplit;
}

var ytplayer;
function onYouTubePlayerReady(playerId) {
    ytplayer = document.getElementById("myytplayer");
    ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
    ytplayer.addEventListener("onError", "onytplayerStateChange");

    loadNewVideo(SplitIdVideo()[0], 0);
}

function loadNewVideo(id, startSeconds) {
    if (ytplayer) {
        ytplayer.cueVideoById(id, parseInt(startSeconds), 'default');
    }
}

var i = 0;
function onytplayerStateChange(newState) {
    if (newState == 0) {
        if (i < SplitIdVideo().length - 1) {
            i++;
        }
        else {
            i=0;
        }
        //play();
        if (SplitIdVideo()[i].trim() == "" || SplitIdVideo()[i].trim() == " ") {
            loadNewVideo(SplitIdVideo()[0], 0);
        }
        else {
            loadNewVideo(SplitIdVideo()[i], 0);
        }
        play();
    }
    else if (newState == 1) {
        var btnPause = $('pause');
        var btnPlay = $('play');
        if (btnPause) {
            btnPause.setStyle('display', '');
        }
        if (btnPlay) {
            btnPlay.setStyle('display', 'none');
        }
    }
    else if (newState == 2) {
        var btnPause = $('pause');
        var btnPlay = $('play');
        if (btnPause) {
            btnPause.setStyle('display', 'none');
        }
        if (btnPlay) {
            btnPlay.setStyle('display', '');
        }
    }
}

function onytplayerError(code) {
    alert(code);
}

function getDuration() {
    if (ytplayer) {
        return ytplayer.getDuration();
    }
}

function getCurrentTime() {
    if (ytplayer) {
        var currentTime = ytplayer.getCurrentTime();
        return roundNumber(currentTime, 3);
    }
}

function roundNumber(number, decimalPlaces) {
    decimalPlaces = (!decimalPlaces ? 2 : decimalPlaces);
    return Math.round(number * Math.pow(10, decimalPlaces)) /
            Math.pow(10, decimalPlaces);
}

setInterval(updateElapsed, 100);
function updateElapsed() {
    var tot = getDuration();
    var curr = getCurrentTime();

    var max_width = 290;
    var width = max_width * (curr / tot);

    $('progress_bar').setStyle('width', width);
}


function play() {
    if (ytplayer) {
        ytplayer.playVideo();

        var btnPause = $('pause');
        var btnPlay = $('play');
        if (btnPause) {
            btnPause.setStyle('display', '');
        }
        if (btnPlay) {
            btnPlay.setStyle('display', 'none');
        }

        var playButonRed = $('play1');
        if (playButonRed) {
            playButonRed.setStyle('display', 'none');
        
        }
    }
}

function mute() {
    if (ytplayer) {
        ytplayer.mute();

        var vtnOff = $("sound");
        if (vtnOff) {
            vtnOff.setStyle('display', '');
        }

        var vtnOn = $("mute");
        if (vtnOn) {
            vtnOn.setStyle('display', 'none');

        }
    }
}

function unMute() {
    if (ytplayer) {
        ytplayer.unMute();

        var vtnOff = $("mute");
        if (vtnOff) {
            vtnOff.setStyle('display', '');
        }

        var vtnOn = $("sound");
        if (vtnOn) {
            vtnOn.setStyle('display', 'none');
        }

    }
}

function pause() {
    if (ytplayer) {
        ytplayer.pauseVideo();

        var btnPause = $('pause');
        var btnPlay = $('play');
        if (btnPlay) {
            btnPlay.setStyle('display', '');
        }
        if (btnPause) {
            btnPause.setStyle('display', 'none');
        }
    }
}

function stop() {
    if (ytplayer) {
        ytplayer.stopVideo();
        ytplayer.stopVideo();
    }
}