var SWF_path = "http://lib.ewisdomtv.com/jwplayer/player.swf"; 

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://qc-8/ewisdomtv/_site_contents/contents/vid_preview/3_How_to_Shoot_A_video.mov',
		'file':                                  'http://www.ewisdomtv.com/_site_contents/contents/'+theFile,
		'image':								 img_preview,
        'autostart':                             'true',
		'duration':								 duration,
		'bufferlength':						     'null'
      };
        var params = {
                allowfullscreen:"true", 
                allowscriptaccess:"always",
				wmode:"transparent"
        }
        var attributes = {
                id:thePlayerId,  
                name:thePlayerId
        }
        //swfobject.embedSWF("jwplayer/player.swf", thePlaceholder, "356", "256", "9.0.115", false, flashvars, params, attributes);
		swfobject.embedSWF(SWF_path, thePlaceholder, "356", "260", "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_watching () {
makeRequestSim("http://www.ewisdomtv.com/_site_contents/contents/ajax_stop_watching.php?vid_id="+vid_id,"none");
}

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';
}