
var gCurSceneId = 0;

function showTrailer(id, play)
{
	if (play=="on")
	{
		gCurSceneId = id;
		
		var url = "?action=scene_viewed_count&guimode=ajax&site_id=" + g_SITE_ID;
		new Ajax(url, {method: 'get'}).request();
		
	   	var container_id = "tr_" + id;   
		document.getElementById(container_id).innerHTML = "";    
		document.getElementById(container_id).innerHTML = "<div class=\"main_trailer\" id=\"main_trailer" +"\"><div id=\"porn_" + id + "\"></div></div>" +
														  "<div class=\"play_trailer_hover\" id=\"trailer_hover\"> </div>" ; 
		                                                  
		document.getElementById('main_trailer').style.backgroundColor = "#000000";
						
		var s1 = new SWFObject(
		"http://static.brazzers.com/embed/flvplayer_test.swf",
		"pornplayer",
		"640",
		"480",
		"9");
			
		s1.addParam("allowfullscreen","true");
		s1.addParam("allowscriptaccess","always");
		s1.addParam("scalemode","showall");
		s1.addVariable("width","640");
		s1.addVariable("height","480");
		s1.addVariable("displayheight","480");
		//s1.addVariable("file", site_url + "/updates/" + folder + "/trailer.flv");
		s1.addVariable("file", "http://static.brazzers.com/scenes/" + id + "/trailer.flv");
		s1.addVariable("image","http://static.brazzers.com/player/wait.jpg");
		//s1.addVariable("image", "img/area/scene/flv_has_preview_bg.jpg")
		s1.addVariable("nats","");
		s1.addVariable("enablejs",true);
		s1.addVariable("showdownload",false);
		s1.addVariable("usekeys",true);
		s1.addVariable("repeat","list");
		s1.addVariable("captionsScale", 0.3);
		//s1.addVariable("usecaptions","false");
		s1.addVariable("backcolor","0xCDCDCD");
		s1.addVariable("frontcolor","0xFFFFFF");
		s1.addVariable("lightcolor","0xFFDD15");
		s1.addVariable("autostart", true);
		s1.addVariable("trailer",true);
		s1.addVariable("display", "block"); //test
		//s1.addVariable("overstretch", "fit");
		s1.write("porn_" + id);
		
		//update views counter, gets called after the movie starts playing in order not to black the last preview
		//alert('check 1');
		viewsLeft = viewsLeft - 1;
		//alert('check 2');
		updatePreviewCount(viewsLeft)
		
	}
}

function trailerEnd()
{
	//viewsLeft = viewsLeft - 1; //now gets updated when play button gets clicked
	
	document.getElementById('main_trailer').style.visibility = "hidden";
		
	var cont_id = 'tr_'+ gCurSceneId;
	
	if(viewsLeft > 0 && viewsLeft < viewsAllowed)
	{
		document.getElementById(cont_id).innerHTML = '<img src="http://static.brazzers.com/site_images/tours/bblib/tour/img/trailer_bg_home.jpg" alt="" />' + 
													 '<div class="trailer_after">' + 
														 '<div class="num_previews_remaining">You Have ' + viewsLeft + ' Previews Remaining</div>' + 
														 '<div class="join_iframe"><iframe name="join3" src="http://enter.bigbuttslikeitbig.com/signup/signup.php?nats=' + nats + '&step=2&qualify=1&tpl=join3" width="322" height="291" scrolling="no" frameborder="0" allowtransparency="true" marginheight="0" marginwidth="0"></iframe></div>' + 
														 '<div class="bottom-strip">' + 
															 '<span onclick="playTrailer('+ gCurSceneId +');" class="button">&nbsp;&nbsp;&nbsp;&nbsp;</span>' + 
															 '<span class="click2watch"><a onclick="playTrailer('+ gCurSceneId +');">Click Here</a> To Play This Trailer Again</a></span>' + 
														 '</div>' + 
													 '</div>';
	}
	else
	{
		document.getElementById(cont_id).innerHTML = '<img src="http://static.brazzers.com/site_images/tours/bblib/tour/img/trailer_bg_home.jpg" alt="" />' + 
													 '<div class="trailer_after">' + 
														 '<div class="num_previews_remaining">You Have No Previews Remaining</div>' + 
														 '<div class="join_iframe"><iframe name="join3" src="http://enter.bigbuttslikeitbig.com/signup/signup.php?nats=' + nats + '&step=2&qualify=1&tpl=join3" width="322" height="291" scrolling="no" frameborder="0" allowtransparency="true" marginheight="0" marginwidth="0"></iframe></div>' + 
														 '<div class="bottom-strip">' + 
															 '<span onclick="location.href=\'' + joinUrl + '\'" class="button">&nbsp;&nbsp;&nbsp;&nbsp;</span>' + 
															 '<span class="click2watch"><a onclick="location.href=\'' + joinUrl + '\'">Click Here</a> To Join Now!</a></span>' + 
														 '</div>' + 
													 '</div>';
	}
	
	//update preview counter displayed on trailer
	updatePreviewCount(viewsLeft);
}

function updatePreviewCount(viewsLeft)
{
	//set isOn variable to "off" if no previews left
	if(viewsLeft <= 0)
	{
		isOn = 'off';
		hide_playbutton(); //hides all big playback buttons
		//alert('check 3');
		updateDisplayItem('No'); //no previews left
		//alert('check 4');
	}
	else
	{
		//alert('check 5');
		updateDisplayItem(viewsLeft); //indicate number of preview left
	}
}

/**
* updates all preview counter displays(for each episode box) on the page
*/
function updateDisplayItem(value)
{
	if(value == 'No')
	{
		// no previews remaining, must update all episode boxes
		for (var i=0; i < scene_ids.length; i++) 
		{
			if(scene_ids[i] != gCurSceneId)
			{
				/*document.getElementById('tr_' + scene_ids[i]).innerHTML = '<img src="http://static.brazzers.com/scenes/' + scene_ids[i] + '/big2.jpg" />' + 
												'<div class="trailer_before">' +
												'<div class="bottom-strip">' +
													'<span onclick="location.href=' + join_url + '" class="button">&nbsp;&nbsp;&nbsp;&nbsp;</span>' +
													'<span class="click2watch"><a onclick="location.href=' + join_url + '">Click Here</a> To Join Now!</a></span>' +							
													'<span>&nbsp;&nbsp;-&nbsp;&nbsp;</span>' +
													'<span class="preview-count">You Have No Previews Remaining</span>' +
												'</div>' +
											'</div>';
			*/
				
											
				document.getElementById('tr_' + scene_ids[i]).innerHTML = '<img src="http://static.brazzers.com/scenes/' + scene_ids[i] + '/big2.jpg" />' + 
												'<div class="trailer_before">' +
												'<div class="info-panel-join" id="panel_' + scene_ids[i] + '" onclick="location.href=\'' + joinUrl + '\'">' +
												'<span class="click2watch">You Have No Previews Remaining</span>' +
												'</div>' +
												'<div class="bottom-strip">' +
													'<span onclick="location.href=\'' + joinUrl + '\'" class="button">&nbsp;&nbsp;&nbsp;&nbsp;</span>' +
													'<span class="click2watch"><a onclick="location.href=\'' + joinUrl + '\'">Click Here</a> To Join Now!</a></span>' +							
													'<span>&nbsp;&nbsp;-&nbsp;&nbsp;</span>' +
													'<span class="preview-count">You Have No Previews Remaining</span>' +
												'</div>' +
											'</div>';
				
											
			}
		}
	}
	else
	{
		// we still have some previews remaining, update count only
		/*
		var obj = document.getElementsByTagName('span');
		for (var i=0; i <obj.length; i++) 
		{
			if(obj[i].id == 'previewCountDisp')
			{
				//alert('got it');
				obj[i].innerHTML = value;
			}
		}
		*/
	}
	
}

/**
* update previews left counter for all displays, if any
*/
function adjustCounter(value)
{
 	var obj = document.getElementsByTagName('span');
	for (var i=0; i <obj.length; i++) 
	{
		if(obj[i].id == 'previewCountDisp')
		{
			obj[i].innerHTML = value;
		}
	}
} 

/**
* toggles info panel display on/off
*/
function togglePanelDisplay(item, state)
{
	//alert(item + '=' + state);
	var obj = document.getElementById(item);
	if(state == 'on')
	{
		try{
			obj.style.display = 'block';
		}
		catch(e){}
		//update views letf number
		adjustCounter(viewsLeft);
		
	}
	if(state == 'off')
	{
		try{
			obj.style.display = 'none';
		}
		catch(e){}
	}
}

/**
* hides playback button on all episode boxes
*/
function hide_playbutton()
{
	var obj = document.getElementsByTagName('div');
	for (var i=0; i <obj.length; i++) 
	{
		if(obj[i].id.search(/playbutton_.+/) != -1)
		{
			obj[i].style.display = 'none';
		}
	}
} 
