/**
 * @author gennad
 */
/**
 * This patch removes bug when the page sliding even when the man is watching the video. 
 */


var ytplayer = {};



window.onload = function() {

    // JSAPI get access to youtube video and makes differently playersID for FF, Safari and Chrome:
    $("object").append("<param name='allowScriptAccess' value='always' />");

    var counter = 1;
    $("object").each(function() {
        dataAtr = $(this).attr("data");
        dataAtr = dataAtr.replace("ytplayer", "ytplayer" + counter);
        $(this).attr("data", dataAtr);
        counter++;
    });
    // For IE this was realised by modificated swfobject.js

    if ($('div.slide').length == 1) {
    	$('div.slide').css({'position' : 'absolute' , 'top' : '0pt' , 'left' : '0pt' , 'z-index' : '4' , 'opacity' : '1' , 'display' : 'block' , 'width' : '480px' , 'height' : '450px'});
    }
};


function onYouTubePlayerReady(playerId) {

    if (playerId == "ytplayer1") {
        ytplayer[playerId] = document.getElementById("emvideo-youtube-flash-7");
    }
    if (playerId == "ytplayer2") {
        ytplayer[playerId] = document.getElementById("emvideo-youtube-flash-8");
    }
    if (playerId == "ytplayer3") {
         ytplayer[playerId] = document.getElementById("emvideo-youtube-flash-9");
    }

    ytplayer[playerId].addEventListener("onStateChange", "onytplayerStateChange");
    ytplayer[playerId].addEventListener("onError", "onPlayerError");
}



function onPlayerError(errorCode) {
    alert("An error occured: " + errorCode);
}

function onytplayerStateChange(newState) {

    //alert("ok");

    if ((newState == 1) || (newState == 2) || (newState == 3)) {
        $('.emvideo-youtube').each(function() {
                $('div.slider-inner').each(function() {
                    clearTimeout(this.cycleTimeout);
                });

        });
    }
}


