function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}
function viewHQ()
{
	player_options['format_id'] = '2';
	if (is_pl == '2')
	{
		playItem(currentItem);
	}
	else
	{
		loadPlayer();
	}
}

function viewLQ()
{
	player_options['format_id'] = '3';
	if (is_pl == '2')
	{
		playItem(currentItem);
	}
	else
	{
		loadPlayer();
	}
}
function downloadFile(id)
{
	// Create an IFRAME.
	var iframe = document.createElement("iframe");
	
	// Point the IFRAME to GenerateFile, with the
	//   desired region as a querystring argument.
	iframe.src = "/download/"+id+".avi";
	
	// This makes the IFRAME invisible to the user.
	iframe.style.display = "none";
	
	// Add the IFRAME to the page.  This will trigger
	//   a request to GenerateFile now.
	document.body.appendChild(iframe);
}

function loadPlayer(autoplay)
{
    //alert(autoplay);
    if ( typeof(autoplay) == 'undefined' )
    autoplay='true';
    else
    autoplay='false';   
    
    
    
    
    
    
    
    var swf = new SWFObject("/player4/btplayer.swf","player4/btplayer","100%","100%","9", "#000000");
	swf.useExpressInstall("/playerProductInstall.swf");
	swf.setAttribute( "xiRedirectUrl", window.location.href );
	for (var key in player_options)
	{
		swf.addVariable(key, player_options[key]);
	}
	swf.addVariable("video_id", video_id);
	swf.addVariable("vars", 'on');
	swf.addVariable("id", '1');
	swf.addVariable("synckey", '1');
	swf.addVariable("rc", '1');
    swf.addVariable("auto", autoplay);
    swf.addVariable("player", 'bigtubewww');
	swf.addVariable("seek_to", seek_to);
	swf.addParam("quality", 'high');
	swf.addParam("align", 'middle');
	swf.addParam("play", 'true');
	swf.addParam("loop", 'true');
	swf.addParam("scale", 'showall');
	swf.addParam("wmode", 'window');
	swf.addParam("devicefont", 'false');
	swf.addParam("id", 'player4/btplayer');
	swf.addParam("name", 'player4/btplayer');
    swf.addParam("menu", 'true');
	swf.addParam("allowFullScreen", 'true');
	swf.addParam("allowScriptAccess", 'always');
	swf.addParam("movie", 'player4/btplayer');
	swf.write("defaultDiv");
}

function loadPreviewPlayer(autoplay)
{
    if (!autoplay) 
    autoplay="true";
	var swf = new SWFObject("../player4/btplayer.swf","player4/btplayer","100%","100%","9", "#000000");
	swf.useExpressInstall("/playerProductInstall.swf");
	swf.setAttribute( "xiRedirectUrl", window.location.href );
	for (var key in player_options)
	{
		swf.addVariable(key, player_options[key]);
	}
	swf.addVariable("video_id", video_id);
	swf.addVariable("vars", 'on');
	swf.addVariable("id", '1');
	swf.addVariable("synckey", '1');
	swf.addVariable("rc", '1');
    swf.addVariable("auto", autoplay);
	swf.addVariable("player", 'bigtubewww');
	swf.addParam("quality", 'high');
	swf.addParam("align", 'middle');
	swf.addParam("play", 'true');
    swf.addParam("loop", 'true');
	swf.addParam("scale", 'showall');
	swf.addParam("wmode", 'window');
	swf.addParam("devicefont", 'false');
	swf.addParam("id", 'player4/btplayer');
	swf.addParam("name", 'player4/btplayer');
	swf.addParam("menu", 'true');
	swf.addParam("allowFullScreen", 'true');
	swf.addParam("allowScriptAccess", 'always');
	swf.addParam("movie", 'player4/btplayer');
	swf.write("defaultDiv");
}
function viewHQP()
{
	player_options['fd'] = '2';
	loadPreviewPlayer();
}
function viewLQP()
{
	player_options['fd'] = '3';
	loadPreviewPlayer();
}

var originalViewLeft = "";
var originalViewTop = "";
var originalViewWidth = "";
var originalViewHeight = "";
var isPlayerCentered = false;
var resizeInit = false;

function fs()
{
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' )
	{
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	}
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
	{
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	}
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
	{
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	isPlayerCentered = false;
	returnObjById("defaultDiv").style.top = "0px";
	returnObjById("defaultDiv").style.left = "0px";
	resizePlayer(myWidth, myHeight);
}

function findPosLeft(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		curleft = obj.offsetLeft;
		while (obj=obj.offsetParent)
		{
			curleft += obj.offsetLeft;
		}
	}
	return curleft;
}

function findPosTop(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		curtop = obj.offsetTop;
		while (obj=obj.offsetParent)
		{
			curtop += obj.offsetTop;
		}
	}
	return curtop;
}

function returnObjById(id)
{
	if (document.getElementById)
	{
		var returnVar = document.getElementById(id);
	}
	else if (document.all)
	{
		var returnVar = document.all[id];
	}
	else if (document.layers)
	{
		var returnVar = document.layers[id];
	}
	return returnVar;
}

function resizePlayer(x, y)
{
	if (originalViewLeft == "")
	{
		originalViewLeft = findPosLeft(returnObjById("defaultDiv"));
		originalViewTop = findPosTop(returnObjById("defaultDiv"));
		originalViewWidth = returnObjById("defaultDiv").style.width;
		originalViewHeight = returnObjById("defaultDiv").style.height;
	}
	returnObjById("defaultDiv").style.width = (x)+"px";
	returnObjById("defaultDiv").style.height = (y)+"px";
}

function movePlayer(x, y)
{
	if (originalViewLeft == "")
	{
		originalViewLeft = findPosLeft(returnObjById("defaultDiv"));
		originalViewTop = findPosTop(returnObjById("defaultDiv"));
		originalViewWidth = returnObjById("defaultDiv").style.width;
		originalViewHeight = returnObjById("defaultDiv").style.height;
	}
	var newTop = returnObjById("defaultDiv").style.top.substring(0, returnObjById("defaultDiv").style.top.length - 2);
	newTop = eval(newTop) + y;
	var newLeft = returnObjById("defaultDiv").style.left.substring(0, returnObjById("defaultDiv").style.left.length - 2);
	newLeft = eval(newLeft) + x;
	//alert (newTop+" "+y+" "+newLeft+" "+x);
	returnObjById("defaultDiv").style.top = newTop+"px";
	returnObjById("defaultDiv").style.left = newLeft+"px";
	/*returnObjById("defaultDiv").style.left =  -findPosLeft(returnObjById("playerwrap"))+ eval(x)+"px";
	returnObjById("defaultDiv").style.top =  30+-findPosTop(returnObjById("playerwrap"))+eval(y)+"px";*/
}

function moveNormalSize()
{
	isPlayerCentered = false;
	returnObjById("defaultDiv").style.width ="640px";
	returnObjById("defaultDiv").style.height = "606px"; 
	returnObjById("defaultDiv").style.top = "0px";
	returnObjById("defaultDiv").style.left = "0px";
}

function zoomPlayer(percent)
{
	var width = (640*percent)/100;
	var height = ((480*percent)/100) + 126;
	resizePlayer(width, height);
}

function centerPlayer()
{
	isPlayerCentered = true;
	var isIE;
	if (navigator.appName.indexOf("Internet Explorer")>=0)
	{
		isIE = true;
	}
	if (isIE)
	{
		my_width = document.documentElement.clientWidth;
		my_height = document.documentElement.clientHeight;
		
		if (document.documentElement.clientWidth == 0)
		{
			my_width = document.body.clientWidth;
			my_height = document.body.clientHeight;
		}
		else
		{
			my_width = document.documentElement.clientWidth;
			my_height = document.documentElement.clientHeight;
		}
	}
	else
	{
		var my_width = window.innerWidth;
		var my_height = window.innerHeight;
	}
	var b_player_width = parseInt(returnObjById("defaultDiv").style.width);
	var b_player_height = parseInt(returnObjById("defaultDiv").style.height);
	returnObjById("defaultDiv").style.left =  -findPosLeft(returnObjById("playerwrap"))+ (my_width/2-b_player_width/2)+"px";
	returnObjById("defaultDiv").style.top =  30+-findPosTop(returnObjById("playerwrap"))+(my_height/2-b_player_height/2)+"px";
}

function playNext()
{
	if (is_pl == '2')
	{
		if (currentItem == maxItem)
		{
			var itemToPlay = 0;
		}
		else
		{
			var itemToPlay = currentItem+1;
		}
		playItem(itemToPlay);
	}
	else
	{
		gotoNext();
	}
}

function playItem(num)
{
	var playingItem = items[num];
	
	lastItem = currentItem;
	var swf = new SWFObject("/bigtubeplayer.swf","mpl","100%","100%","9", "#EEEEEE");
	swf.useExpressInstall("/playerProductInstall.swf");
	swf.setAttribute( "xiRedirectUrl", window.location.href );
	for (var key in player_options)
	{
		swf.addVariable(key, player_options[key]);
	}
	swf.addVariable("ht2", playingItem['ht2']);
	swf.addVariable("ht3", playingItem['ht3']);
	swf.addVariable("video_id", playingItem['id']);
	swf.addParam("allowfullscreen",true);
	swf.write("defaultDiv");
	document.getElementById('td'+lastItem).className='videobrowsesmall';
	currentItem = num;
	document.getElementById('td'+num).className='videobrowsesmallplay';
}

function GetElement2(div)
{
	return document.getElementById(div);
}

function getUrlFromPlaylistRow(rowNode)
{
	var links=rowNode.getElementsByTagName('a');
	return links[0].href;
}

function getPlaylistVideoCount()
{
	return parseInt(GetElement2('plistNbVids').innerHTML);
}

function getId(baseId,opt_index)
{
	var id=baseId;
	if(opt_index!=null)
		id+="_"+opt_index;
	return id;
}

function gotoNext()
{
	var firstRow=GetElement2(getId("plistRow",0));
	var nextRow=firstRow;
	var videosInPlaylist=getPlaylistVideoCount();
	for(var i=0;i<videosInPlaylist;i++)
	{
		var currentRow=GetElement2(getId('plistRow',i));
		if(currentRow.className == 'playlisitemcurrent')
		{
			if(i+1<videosInPlaylist)
			{
				nextRow=GetElement2(getId('plistRow',i+1));
			}
			else
			{
				nextRow=null;
			}
			break;
		}
	}
	if(nextRow)
	{
		window.location=getUrlFromPlaylistRow(nextRow);
	}
}

function loadPlaylist(pnum, item)
{
	var xmlHttpReq=getXmlHttpRequest();
	if(!xmlHttpReq)
		return;
	xmlHttpReq.open("POST",'/video_playlist.php',true);
	xmlHttpReq.onreadystatechange=function()
	{
		if(xmlHttpReq.readyState==4 && xmlHttpReq.status==200)
		{
			GetElement('videoPlaylistDisplay').innerHTML = xmlHttpReq.responseText;
			if (GetElement('videoPlaylistDisplay').innerHTML)
			{
				GetElement('videoPlaylistDisplay').style.display = "block";
				var selectedItem = GetElement2(getId("plistRow",item));
				selectedItem.className = 'playlisitemcurrent';
				selectedItem.onmouseover = function() {this.className = 'playlisitemcurrent'};
				selectedItem.onmouseout = function() {this.className = 'playlisitemcurrent'};
			}
		}
	};
	xmlHttpReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlHttpReq.send('&video_id='+video_id+'&p='+pnum);
}
