window.onload = function () {
    var videotag = document.getElementById('video-tag')
    var spinner  = document.getElementById('video-spinner')
    
    if(!videotag || !videotag.play) {
        return;
    }
    
    if(navigator.userAgent.indexOf('AppleWebKit/531') != -1) {
        videotag.poster = null
    }
    
    if(!videotag.loop) {
        // fake it
        videotag.addEventListener("ended", function () {
            videotag.currentTime = 0.0;
            videotag.play();
        }, false)
    }
    
    //alert(videotag.readyState)
    if(videotag.readyState != 4) {                
        spinner.style.visibility = "visible";
    }
    
    videotag.addEventListener("loadstart", function () {
        //alert("loadstart");
        if(videotag.readyState != 4) {                
            spinner.style.visibility = "visible";
        }
    }, false)
    
    videotag.addEventListener("waiting", function () {
        //alert("waiting")
        if(videotag.readyState != 4) {                
            spinner.style.visibility = "visible";
        }
    }, false)
    
    videotag.addEventListener("playing", function () {
        //alert("playing")
        spinner.style.visibility = "hidden";
    }, false)
    
    // firefox 3.5 doesn't understand the events properly
    if(navigator.userAgent.match(/rv:1\.9\.1.*Gecko/)) {
        videotag.addEventListener("play", function () {
            spinner.style.visibility = "hidden";
        }, false)
    }
    
    videotag.addEventListener("play", function () {
        videotag.controls = false;
    }, false)
    
    videotag.addEventListener("click", function () {
        if(!videotag.paused) {
            videotag.controls = true
            videotag.pause();
        }
    }, true)
}
