var xmlhttp

if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
 try {
  xmlhttp = new XMLHttpRequest();
 } catch (e) {
  xmlhttp=false
 }
}
function URLEncode(url)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = url;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
}		   
function divapa(id,opt){ 
if(opt=="0"){
$('#'+id).fadeOut('def'); // Pour faire disparaître ton DIV.	
}
if(opt=="1"){  
$('#'+id).fadeIn('slow'); // Pour faire apparaître ton DIV.
}
}			  

///// Advanced search ///////////////////////////
function AdvancedSearchBox()
{
	var asLink = document.getElementById("asLink");
	if (asLink.innerHTML == 'Advanced search')
	{
		document.getElementById('asBox').style.display = "";
		asLink.innerHTML = 'Close';	
	}
	else
	{	
		document.getElementById('asBox').style.display = "none";
		asLink.innerHTML = 'Advanced search';	   
	}
	return false;
}
///// end Advanced search ///////////////////////////
function ReportProblemBox()
{
	var rpLink = document.getElementById("rpLink");
	if (rpLink.innerHTML == 'Report Problem +')
	{
		divapa('rpBox',1);
		rpLink.innerHTML = 'Report Problem -';	
		document.getElementById('rpBox').style.display = ""; 
	}
	else
	{	
		divapa('rpBox',0);
		rpLink.innerHTML = 'Report Problem +';	   
		document.getElementById('rpBox').style.display = "";
	}
	return false;
}

function RepNotes_OnFocus()
{
	if ($('#report_notes').val() == 'notes...')
		$('#report_notes').val('');
}

function RepNotes_OnBlur()
{
	if ($('#report_notes').val() == '')
		$('#report_notes').val('notes...');
}
function SubmitReport(vid){

	var reason = document.getElementById('rpReason').value;
	var notes = URLEncode(document.getElementById('report_notes').value);
	if (notes == 'notes...')
		notes = '';			
	var theURL = "/ajax_req.php?action=report_problem&video="+vid+"&reason="+reason+"&notes="+notes;	  
	document.getElementById('rp_loading').style['display'] = 'block';
	xmlhttp.open('get', theURL);
	xmlhttp.onreadystatechange = handleResponseReport;
	xmlhttp.send(null);	
}

function handleResponseReport()
{
  if(xmlhttp.readyState == 4)
  {
		if (xmlhttp.status == 200)
		{
	        var response = xmlhttp.responseText;
			document.getElementById('rpBox').innerHTML= response;
			document.getElementById('rp_loading').style['display'] = 'none';
		}
    }
}
///// end Reports ///////////////////////////

///// Add Favorites ///////////////////////////
function SubmitFavorite(vid)
{				
	var elemrep = document.getElementById('rep_'+vid);										 	
	var theURL = "/ajax_req.php?action=favorite&video="+vid+"&a=add"; 
	xmlhttp.open('get', theURL);
	xmlhttp.onreadystatechange = function()
{
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
		{
	       var response = xmlhttp.responseText;   
		elemrep.innerHTML= response;
		}
}
	xmlhttp.send(null);	
}  
function SubmitUrl(vid)
{				
	var theURL = "/ajax_req.php?action=out&video="+vid;	 
	xmlhttp.open('get', theURL);
	xmlhttp.send(null);	 
	return true;
}
function SubmitPornstar(id)
{				
	var theURL = "/ajax_req.php?action=pornstar&id="+id;	 
	xmlhttp.open('get', theURL);
	xmlhttp.send(null);	 
	return true;
}
function watch_close(id)
{			 
	var link	 = 'watch_link_'+id;
	var position = 'watch_'+id;  
	document.getElementById(link).innerHTML = '<a href="#" onclick="watch_show('+id+');return false;" class="listing_opt">Watch here</a>';	 
	divapa(position,0);
}
	   
function watch_show(id)
{
	var xhr_object = null;	
	var link	 = 'watch_link_'+id;
	var position = 'watch_'+id;
	if(window.XMLHttpRequest)  xhr_object = new XMLHttpRequest();
	else
	if (window.ActiveXObject)  xhr_object = new ActiveXObject("Microsoft.XMLHTTP"); 
	xhr_object.open("GET", "/listing_show.php?action=watch&video="+id, true);
	xhr_object.onreadystatechange = function(){	 
	if ( xhr_object.readyState != 4 )
	{
	document.getElementById(link).innerHTML = '<a href="#" class="listing_opt">...Wait...</a>';
	}  
	if ( xhr_object.readyState == 4 )
	{
	document.getElementById(link).innerHTML = '<a href="#" onclick="watch_close('+id+');return false;" class="listing_opt">Close player</a>';
	}
	if ( xhr_object.readyState == 4 )
	{						 
	document.getElementById(position).innerHTML = xhr_object.responseText;
	divapa(position,1);
	}
	}
	xhr_object.send(null);
}

function embed_show(id)
{							
	var link	 = document.getElementById('embed_'+id);
	var position = document.getElementById('req_'+id);  
	if(position.innerHTML){
	link.innerHTML = 'Embed this video';  
	position.innerHTML = '';
	}else{
	var xhr_object = null;	
	if(window.XMLHttpRequest)  xhr_object = new XMLHttpRequest();
	else
	if (window.ActiveXObject)  xhr_object = new ActiveXObject("Microsoft.XMLHTTP"); 
	xhr_object.open("GET", "/ajax_req.php?action=embed&video="+id, true);
	xhr_object.onreadystatechange = function(){	 
	if ( xhr_object.readyState != 4 )
	{
	link.innerHTML = 'Please wait';
	}  
	if ( xhr_object.readyState == 4 )
	{
	link.innerHTML = 'Close embed';
	}
	if ( xhr_object.readyState == 4 )
	{			  
	position.innerHTML = xhr_object.responseText;	
	}
	}
	xhr_object.send(null);	
	}
	return false; 
}

function favoris(url,title) {
if ( navigator.appName != 'Microsoft Internet Explorer' )
{ window.sidebar.addPanel(title,url,""); }
else { window.external.AddFavorite(url,title); } }

function showAjaxvideo(id,titre) {
	Lightview.show({
		href: '/ajax_req.php?action=watch&video='+id,
		rel: 'ajax',
		title:titre,
		options: {	
			width: 630, 
      height: 480,
			topclose: true,
			ajax: {
				onComplete: function(){
				}
			}
		}
	});
}
