var checkBufferInterval = null;
var checkPlayerIsStoppedInterval = null;

var clipStartedPlaying = 0;

function onFlowPlayerReady() {

	ponyfilm.movie.moviePlayerElm = document.getElementById(ponyfilm.movie.moviePlayerID);

	ponyfilm.movie.showPlayer();
}

function onClipDone(clip) {

	if (ponyfilm.movie.movieDuration > 0) {

		ponyfilm.movie.onMovieEnd();
	}
}

function onMetaData(metadataObj) {

	ponyfilm.movie.movieDuration = metadataObj.duration;
}

function onStartBuffering(clip) {

	if (!ponyfilm.movie.movieIsClosed) {

		checkBufferInterval = window.setInterval("ponyfilm.movie.checkBuffer()", 500);
	}
}

function onPlay(clip) {

	clipStartedPlaying++;

	if (clipStartedPlaying == 2) {

		YAHOO.util.Event.addListener(document.getElementsByTagName('body')[0], "click", function(e) {

			var target = YAHOO.util.Event.getTarget(e);

			if (target.tagName && target.tagName.toUpperCase() != "OBJECT") {

				ponyfilm.movie.closeVideoPlayer();
			}
		});

		clipStartedPlaying = 0;
	}
}

var ponyfilm = {

    author: 'Stephan Bothur',
    copyright: 'freilicht.net',

    YUL: YAHOO.lang,
    YUD: YAHOO.util.Dom,
    YUA: YAHOO.util.Anim,
    YUE: YAHOO.util.Easing,

    system: {

    	openWindow: function(url, name, options) {

		var newWindow = window.open(url, name, options + ', location=no, toolbar=no, scrollbars=yes, menubar=yes, resizable=yes, status=yes');

		newWindow.focus();
	}
    },
    
    animate: {
        
        easeOut: function(animObj, animParams, callback) {
            
            var animation = new ponyfilm.YUA(animObj, animParams, 2, ponyfilm.YUE.easeOut);
            
            animation.onComplete.subscribe(callback);
            animation.animate();
        }
    },

    movie: {

        moviePlayerID: 'moviePlayer',
	moviePlayerElm: null,
	movieDuration: 0,
	movieIsClosed: false,
        
        playMovie: function(movieParams) {

            this.createVideoPlayer(movieParams.URL, movieParams.splashImage, movieParams.coord);
        },
        
        createVideoPlayer: function(movieURL, movieSplashImage, movieCoord) {

            var movieContainer = ponyfilm.YUD.get("movieContainer");

	    movieContainer.style.top = eval(movieCoord.y) + "px";
	    movieContainer.style.left = eval(movieCoord.x) + "px";

	    swfobject.embedSWF("swf/FlowPlayerDark.swf", this.moviePlayerID, "480", "360", "9.0.0", "swf/expressInstall.swf", { }, { movie: "swf/FlowPlayerDark.swf", flashVars: "config= {videoFile: 'http://ponyfilm.com/" + movieURL + "', splashImageFile: 'http://ponyfilm.com/" + movieSplashImage + "', configFileName: 'js/playerConfig.js' }", allowfullscreen: false }, { id: this.moviePlayerID, name: this.moviePlayerID });

	    return true;
        },

	checkBuffer: function() {

		var percentLoaded = ponyfilm.movie.moviePlayerElm.getPercentLoaded();

		if (Math.round(percentLoaded) > 30) {

			ponyfilm.movie.doPlay();

			window.clearInterval(checkBufferInterval);
		}
	},

	checkPlayerIsStopped: function() {

		if (ponyfilm.movie.moviePlayerElm.getIsPaused()) {

			window.clearInterval(checkPlayerIsStoppedInterval);
		}

		else {
			ponyfilm.movie.moviePlayerElm.Reset();
		}
	},

	showPlayer: function() {

		ponyfilm.movie.movieIsClosed = false;

		ponyfilm.movie.moviePlayerElm.style.visibility = "visible";
	},

	closeVideoPlayer: function() {

		ponyfilm.movie.movieDuration = 0;
                ponyfilm.movie.movieIsClosed = true;

		YAHOO.util.Event.removeListener(document.getElementsByTagName('body')[0], "click");

		ponyfilm.movie.moviePlayerElm.style.visibility = "hidden";
		ponyfilm.movie.moviePlayerElm.style.width = "0px";
		ponyfilm.movie.moviePlayerElm.style.height = "0px";

		checkPlayerIsStoppedInterval = window.setInterval("ponyfilm.movie.checkPlayerIsStopped()", 200);

		return true;
	},

	onMovieEnd: function() {

		if (Math.round(ponyfilm.movie.moviePlayerElm.getTime()) == Math.round(ponyfilm.movie.movieDuration)) {

			this.closeVideoPlayer();

			return true;
		}

		return false;
	},

	doPlay: function() {

		if (!ponyfilm.movie.movieIsClosed) {

			ponyfilm.movie.moviePlayerElm.DoPlay();
		}

		return true;
	}
    }
}
