var currentState = "NONE"; 
var previousState = "NONE"; 

var vid_id = "NONE"; 
var duration = "NONE"; 
var img_preview = "NONE"; 
var pre_time = "NONE"; 
var back_to_account = "NONE";
 
var player = null;
var replay = 0;

function playerReady(thePlayer) {
	player = document.getElementById(thePlayer.id);
	addListeners();
	player.addModelListener('TIME', 'timeMonitor');
	
}
 
 var time = null;
function timeMonitor(obj)
{
        time = obj.position;
		
			if (time>pre_time && currentState=='PLAYING' && replay==0) {				
				player.sendEvent('PLAY');
				replay = 1;
				if (back_to_account==1) {return_account();}
				var goDiv = document.getElementById('buynow');
				goDiv.style.display=''; // show
				//stop_watching();
     		 } else if (time>pre_time && currentState=='PLAYING' && replay==1) {
			 	replay = 0;
				if (back_to_account==1) {return_account();}
				var goDiv = document.getElementById('buynow');
				goDiv.style.display=''; // show
				player.sendEvent('STOP');
				//stop_watching();
			 };
}
 
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; 
		//alert(currentState);
 	if (currentState == "PLAYING" || currentState == "IDLE") {
		var goDiv = document.getElementById('buynow');
		goDiv.style.display='none'; // show
	}
}

function createPlayer(thePlaceholder, thePlayerId, theFile) {
	
	var flashvars =
      {
		//'file':                                  'http://localhost/ewisdomtv/_site_contents/contents/vid_preview/'+theFile,
		//'file':                                  'http://qualityclix.com/ewisdomtv/_site_contents/contents/vid_preview/'+theFile,
		'file':                                  theFile,
		'image':								 img_preview,
        'autostart':                             'true',
		'duration':								 duration
      };
        var params = {
                allowfullscreen:"true", 
                allowscriptaccess:"always",
				wmode:"transparent"
        }
        var attributes = {
                id:thePlayerId,  
                name:thePlayerId
        }
        swfobject.embedSWF("includes/player.swf", thePlaceholder, "408", "295", "9.0.115", false, flashvars, params, attributes);
}

function deletePlayer(theWrapper, thePlaceholder, thePlayerId) { 
        swfobject.removeSWF(thePlayerId);
        var tmp=document.getElementById(theWrapper);
        if (tmp) { tmp.innerHTML = "<div id=" + thePlaceholder + "></div>"; }
}

function initPlayer(theFile) { 
        deletePlayer('wrapper', 'placeholder1', 'player1'); 
        createPlayer('placeholder1', 'player1', theFile);
}

function stop_player () {
	if (player) {
		player.sendEvent('STOP');
	}
}

function setValues (a_img_preview,a_duration,a_pre_time,a_vid_id,a_back_to_account) {
duration = a_duration; 
img_preview = a_img_preview; 
pre_time = a_pre_time; 
vid_id = a_vid_id;
back_to_account = a_back_to_account;
}

function return_account () {
window.location = '?page=myaccount';
}