// JavaScript Document
var currentState = "NONE"; 
var previousState = "NONE"; 
var player = null;
var oldWrap = new Object();

function playerReady(thePlayer) {
	player = document.getElementById(thePlayer.id);
	addListeners();
}
function addListeners() {
	if (player) { 
		player.addModelListener("STATE", "stateListener");
	} else {
		setTimeout("addListeners()",100);
	}
}
function stateListener(obj) { //IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
	currentState = obj.newstate; 
	previousState = obj.oldstate; 
	if ((currentState == "COMPLETED")&&(previousState == "PLAYING")) {
		hideWrap("vidwrapper");
		Element.show("videoad");
	}
	if ((currentState == "PLAYING")) {
		Element.hide("videoad");
		restoreWrap("vidwrapper");
	}
	if ((currentState == "PAUSED")) {
		hideWrap("vidwrapper");
		Element.show("videoad");
	}
}
function storePosition(theWrapper) { 
            var tmp = document.getElementById(theWrapper);
                if (tmp) {
                    oldWrap.left = parseInt(tmp.style.left); 
                    oldWrap.top = parseInt(tmp.style.top);   
                }
        }


        function storeSize(theWrapper) { 
            var tmp = document.getElementById(theWrapper);
                if (tmp) { 
                    oldWrap.width = parseInt(tmp.style.width); 
                    oldWrap.height = parseInt(tmp.style.height);
                }
        }


        function storeWrap(theWrapper) { storeSize(theWrapper); storePosition(theWrapper); }


        function positionWrap(theWrapper, theLeft, theTop, unitStr) { 
            var tmp = document.getElementById(theWrapper);
                if (tmp) { 
                    if (! unitStr) { var unitStr = "px"; }
                    tmp.style.left = parseInt(theLeft)+unitStr; 
	    tmp.style.top = parseInt(theTop)+unitStr;
                }
        }


        function sizeWrap(theWrapper, theWidth, theHeight, unitStr) { 
            var tmp = document.getElementById(theWrapper);
                if (tmp) { 
                    if (! unitStr) { var unitStr = "px"; }
                    tmp.style.width = parseInt(theWidth)+unitStr; 
                    tmp.style.height = parseInt(theHeight)+unitStr; 
                }
        }


        function showWrap(theWrapper, theLeft, theTop, theWidth, theHeight, unitStr) { 
                    if (! unitStr) { var unitStr = "px"; }
                    positionWrap(theWrapper, theLeft, theTop, unitStr);
                    sizeWrap(theWrapper, theWidth, theHeight, unitStr);
        }


        function hideWrap(theWrapper) { storeWrap(theWrapper);  showWrap(theWrapper, 0, 0, 0, 0); }

        function restoreWrap(theWrapper) { showWrap(theWrapper, oldWrap.left, oldWrap.top, oldWrap.width, oldWrap.height); }

function createPlayer(url,thumb,token) {
	var flashvars = {
		autostart:"false",
		file:url, 
		fullscreen:"true",
		image:thumb, 
		skin:"http://www.sexytimez.net/flvplayer/modieus.swf",
		streamer:"lighttpd",
		token:token
	}
	var params = {
		allowfullscreen:"true", 
		allowscriptaccess:"always",
		wmode:"transparent"
}
	var attributes = {
		id:"videoplayer",  
		name:"videoplayer"
	}
	swfobject.embedSWF("http://www.sexytimez.net/flvplayer/mediaplayer.swf", "placeholder1", "100%", "100%", "9.0.115", false, flashvars, params, attributes);
}
function loadImage(thumb){
	var obj = {image:thumb};
	player.sendEvent("LOAD",obj);	
}
