//------------------------------------------------------------------------------
// Espace de Téléchargements 2.2
//------------------------------------------------------------------------------
// Date started: 4 mars 2006
// Writing started by: Fabien
//------------------------------------------------------------------------------

function form_init()
{
	form_choice_tupload();
	form_enable_file_size();
}

function form_choice_tupload()
{
	//Upload
	if( document.getElementById('tupload_1') && document.getElementById('tupload_1').checked == true )
	{
		document.getElementById('tupload-upload').style.display = '';
		if( document.getElementById('tupload-externe') ) document.getElementById('tupload-externe').style.display = 'none';
		if( document.getElementById('tupload-interne') ) document.getElementById('tupload-interne').style.display = 'none';
	}
	//Externe
	else if( document.getElementById('tupload_0') && document.getElementById('tupload_0').checked == true )
	{
		document.getElementById('tupload-externe').style.display = '';
		document.getElementById('tupload-upload').style.display = 'none';
		if( document.getElementById('tupload-interne') ) document.getElementById('tupload-interne').style.display = 'none';
	}
	//Interne
	else if( document.getElementById('tupload_2') && document.getElementById('tupload_2').checked == true )
	{
		document.getElementById('tupload-interne').style.display = '';
		document.getElementById('tupload-upload').style.display = 'none';
		document.getElementById('tupload-externe').style.display = 'none';
	}
}

function form_enable_file_size()
{
	if( document.getElementById('tupload-externe-size') && document.getElementById('file_size_enable').checked == true )
	{
		document.getElementById('tupload-externe-size').style.display = '';
	}
	else if( document.getElementById('tupload-externe-size') && document.getElementById('file_size_enable').checked == false )
	{
		document.getElementById('tupload-externe-size').style.display = 'none';
	}
}

function logs_init()
{
	var th    = '';
	var link  = '';
	var order = '';
	var type  = '';
	var id    = '';
	
	//----------------------------------
	// Using fancy js?
	//----------------------------------
	if ( !use_enhanced_js )
	{
		return false;
	}
	
	//----------------------------------
	// Replace links
	//----------------------------------
	for ( var i = 0 ; i < document.getElementsByTagName('th').length ; i++ )
	{
		if( document.getElementsByTagName('th')[i].id.match(/order/) )
		{
			th = document.getElementById(document.getElementsByTagName('th')[i].id);
			
			link = th.getElementsByTagName("a")[0].href;
			
			order = link.replace( /^(.*)order=(\w+)-(\w+)$/, "$2-$3" );
			
			if( link.search(/view=/) != -1 )
			{
				type = link.replace( /^(.*)view=(\w+)&(.*)$/, "$2" );
			}
			if( link.search(/id=/) != -1 )
			{
				id = link.replace( /^(.*)id=(\d+)&(.*)$/, "$2" );
			}
			
			th.getElementsByTagName("a")[0].href = "javascript:logs_sort('" + order + "','" + type + "','" + id + "');";
		}
	}
}

function logs_sort(order,type,id)
{
	//----------------------------------
	// Using fancy js?
	//----------------------------------
	if ( !use_enhanced_js )
	{
		return false;
	}
	
	do_request_function = function()
	{
		//----------------------------------
		// Ignore unless we're ready to go
		//----------------------------------
		if ( !xmlobj.readystate_ready_and_ok() )
		{
			return;
		}
		
		//----------------------------------
		// INIT
		//----------------------------------
		var html = xmlobj.xmlhandler.responseText;
	
		if ( html != 'error' )
		{
			document.getElementById('result').innerHTML = html;
		}
	}
	
	xmlobj = new ajax_request();
	xmlobj.onreadystatechange( do_request_function );
	xmlobj.process( ipb_var_base_url + 'autocom=FileCP&xmlout=logs&view=' + type + '&order=' + order + '&id=' + id );
}

function quicksearch(qs)
{
	//----------------------------------
	// Init
	//----------------------------------
	var fields = new Array();
	
	//----------------------------------
	// Using fancy js?
	//----------------------------------
	if ( !use_enhanced_js )
	{
		return true;
	}
	
	//----------------------------------
	// f and q not empty?
	//----------------------------------
	if ( qs.elements['q'].value == '' || qs.elements['q'].value == '' )
	{
		return true;
	}
	else
	{
		document.getElementById('catview').innerHTML = "";
	}
	
	//----------------------------------
	// Fields
	//----------------------------------
	fields['f'] = qs.elements['f'].value;
	fields['q'] = qs.elements['q'].value;
	
	do_request_function = function()
	{
		//----------------------------------
		// Ignore unless we're ready to go
		//----------------------------------
		if ( !xmlobj.readystate_ready_and_ok() )
		{
			return;
		}
		
		//----------------------------------
		// INIT
		//----------------------------------
		var html = xmlobj.xmlhandler.responseText;
	
		if ( html != 'error' )
		{
			document.getElementById('catview').innerHTML = html;
		}
	}
	
	xmlobj = new ajax_request();
	xmlobj.onreadystatechange( do_request_function );
	xmlobj.process( ipb_var_base_url + 'autocom=Downloads&xmlout=quicksearch', 'POST', xmlobj.format_for_post(fields) );
	return false;
}

function file_rate()
{
	/**
	* Settings
	*/
	this.settings = {
						'allow_rating'           : 0,
						'default_rating'         : 3,
						'img_star_on'            : 'star_filled.gif',
						'img_star_selected'      : 'star_selected.gif',
						'img_star_off'           : 'star_empty.gif',
						'img_base_url'           : '',
						'div_rating_wrapper'     : 'file-rating-wrapper',
						'text_rating_image'      : 'file-rating-img-',
						'file_id'                : 0
	 				};
	
	this.languages = {
						'img_alt_rate'       : '',
						'rate_me'            : '',
						'error_no_perm'      : '',
						'error_already_rate' : ''
					 };
	
	/**
	* INIT rating images
	*/
	this.init_rating_images = function()
	{
		//-----------------------------------------
		// INIT
		//-----------------------------------------
		var html = '';

		//-----------------------------------------
		// Can rate this member?
		//-----------------------------------------
		if ( ! this.settings[ 'allow_rating' ] )
		{
			return false;
		}

		//-----------------------------------------
		// Hide standard rating form
		//-----------------------------------------
		document.getElementById('dlfile-ratingjs').style.display = 'none';
		
		//-----------------------------------------
		// Still here? OK...
		//-----------------------------------------
		for( var i = 1 ; i <= 5 ; i++ )
		{
			var _onmouseover = '';
			var _onmouseout  = '';
			var _onclick     = '';
			var _title       = '';

			_onmouseover = ' onmouseover="this.style.cursor=\'pointer\'; file_rate.show_rating_images(' + i + ', 0)"';
			_onmouseout  = ' onmouseout="file_rate.show_rating_images(-1, 1)"';
			_onclick     = ' onclick="file_rate.send_rating(' + i + ')"';
			_title       = this.languages['img_alt_rate'];

			html += "<img src='" + this.settings['img_base_url'] + '/' + this.settings['img_star_off'] + "' " + _onmouseover + _onmouseout + _onclick + "id='" + this.settings['text_rating_image'] + i + "' alt='-' title='" + _title + "' />";
		}
		
		document.getElementById( this.settings['div_rating_wrapper'] ).innerHTML = this.languages['rate_me'] + ' ' + html;

		//-----------------------------------------
		// Now set the image...
		//-----------------------------------------
		this.show_rating_images( this.settings['default_rating'], 1 );
	};
	
	/**
	* Send rating..
	*/
	this.send_rating = function( rating )
	{
		//-----------------------------------------
		// INIT
		//-----------------------------------------
		rating = rating ? rating : 0;
		
		//-----------------------------------------
		// Got a rating?
		//-----------------------------------------
		if ( rating )
		{
			//----------------------------------
			// INIT
			//----------------------------------
			var url = ipb_var_base_url+'autocom=Downloads&xmlout=rate-file';
			var fields = new Array();
			
			//----------------------------------
			// Data
			//----------------------------------
			fields['file'] = this.settings['file_id'];
			fields['rate'] = rating;
			
			/*--------------------------------------------*/
			// Main function to do on request
			// Must be defined first!!
			/*--------------------------------------------*/
			do_request_function = function()
			{
				//----------------------------------
				// Ignore unless we're ready to go
				//----------------------------------
				if ( ! xmlobj.readystate_ready_and_ok() )
				{
					xmlobj.show_loading( '' );
					return;
				}

				xmlobj.hide_loading();

				//----------------------------------
				// INIT
				//----------------------------------
				var html = xmlobj.xmlhandler.responseText;
				
				if ( html == 'no_permission' )
				{
					alert( file_rate.languages['error_no_perm'] );
				}
				else if ( html == 'already_rate' )
				{
					alert( file_rate.languages['error_already_rate'] );
				}
				else if ( html != 'error' )
				{
					var _result    = html.split(',');
					var _new_value = _result[0];
					var _new_hits  = _result[1];
					var _new_stars = _result[2];
					var _type      = _result[3];
					//alert( 'total : ' + _new_value + ' hits : ' + _new_hits + ' note : ' + _new_stars + ' type : ' + _type );
					
					//-----------------------------------------
					// Now set the image...
					//-----------------------------------------
					file_rate.settings['default_rating'] = parseInt( _new_stars );
				
					file_rate.show_rating_images( file_rate.settings['default_rating'], 1 );
					
					if( _type == 'update' )
					{
						show_inline_messages_instant( 'rating_updated' );
					}
					else
					{
						show_inline_messages_instant( 'rating_added' );
					}
				}
			};

			//----------------------------------
			// LOAD XML
			//----------------------------------
			xmlobj = new ajax_request();
			xmlobj.onreadystatechange( do_request_function );
			
			xmlobj.process( url , 'POST' , xmlobj.format_for_post(fields) );
			
			return false;
		}
	};
	
	/**
	* Show rating images..
	*/
	this.show_rating_images = function( rating, restore_default )
	{
		//-----------------------------------------
		// INIT
		//-----------------------------------------
		rating   = restore_default ? this.settings['default_rating'] : parseInt( rating );
		var star = restore_default ? this.settings['img_star_on'] : this.settings['img_star_selected'];
		
		//-----------------------------------------
		// Set to 0
		//-----------------------------------------
		for( var i = 1 ; i <= 5 ; i++ )
		{
			var _img = document.getElementById( this.settings['text_rating_image'] + i );
			_img.src = this.settings['img_base_url'] + '/' + this.settings['img_star_off'];
		}
		
		//-----------------------------------------
		// Show ones coloured...
		//-----------------------------------------
		for( var i = 1 ; i <= rating ; i++ )
		{
			var _img = document.getElementById( this.settings['text_rating_image'] + i );
			_img.src = this.settings['img_base_url'] + '/' + star;
		}
	};
	
};