
var AjaxIMListenerHandler = function( ping_interval )
{
	this.ping_in_process = false;
	
	this.pingServer = function()
	{
		if ( this.ping_in_process )
			return false;
		
		this.ping_in_process = true;
		
		$jq.ajax({
			url: URL_HOME+'ajax_rsp.php?AjaxIM:processNotificationPing()',
			dataType: 'json',
			success: function( r_data ) {
				ajax_im_listener_handler.ping_in_process = false;
				
				if ( r_data )
					ajax_im_listener_handler.drawMessages(r_data);
			}
		});
	}
	
	
	
	
	
	this.container = $('ajax_im_listener_container');
	
	this.invitations = {};
	
	this.drawMessages = function( msgs )
	{
		for ( var id in msgs )
		{
			if ( this.invitations[id] )
				continue;
			
			this.invitations[id] = msgs[id];
			
			$jq('.ajax_im_listener_message:first-child').clone(
				).attr('id', 'ajax_im_listener_message_'+id).appendTo('#ajax_im_listener_container');
			
			var item = $('ajax_im_listener_message_'+id);
			
			$jq('.username', item).append(msgs[id].username);
			$jq('.time', item).append(msgs[id].time);
			$jq('.message', item).append(msgs[id].text+'...');
			
			$jq(item).bind('click', { profile_id: msgs[id].profile_id }, function( event ) {
				clearInterval(this.blink_interval);
				
				openAjaxIM(event.data.profile_id);
				
				$jq(this).remove();
				
				return false;
			});
			
			item.blink_interval = setInterval('$jq("#'+item.id+'").toggleClass("ajax_im_listener_message_blink")', 500);
			
			$jq(item).show(300);
		}
		
	}
	
	this.pingServer();
	
	setInterval( function(){ ajax_im_listener_handler.pingServer() }, ping_interval );
}
var node_opacity = {};
var node_st = {};

function show_node( node_id )
{
	var block = $( node_id );
	
	block.style.display = 'block';
	
	node_opacity[node_id] = 0;
	
	if (navigator.appVersion.indexOf('MSIE') != -1)
		ie_show_div( node_id );
	else
		moz_show_div( node_id )
}

function ie_show_div( node_id )
{
	var block = $( node_id );

	clearTimeout( node_st[node_id] );
	
	if( node_opacity[node_id] < 100 )
	{
		node_st[node_id] = setTimeout( 'ie_show_div(\''+node_id+'\')', 10 );
		
		node_opacity[node_id] +=30;
		
		block.style.filter = 'alpha(opacity='+node_opacity[node_id]+')';
	}
	else
		block.style.filter = 'alpha(opacity=100)';
}

function moz_show_div( node_id )
{
	var block = $( node_id );
	
	clearTimeout( node_st[node_id] );
	
	if( node_opacity[node_id] < 1 )
	{
		node_st[node_id] = setTimeout( 'moz_show_div(\''+node_id+'\')', 30 );
		
		node_opacity[node_id] +=0.1;
		
		block.style.opacity = node_opacity[node_id];
	}
	else
		block.style.opacity = '1';
}

function hide_node( node_id )
{
	if (navigator.appVersion.indexOf('MSIE') != -1)
		ie_hide_node( node_id )
	else
		moz_hide_node( node_id );
}

function moz_hide_node( node_id )
{
	var block = $( node_id );
		
	clearTimeout( node_st[node_id] );
	
	if( node_opacity[node_id] > 0 )
	{
		node_st[node_id] = setTimeout( 'moz_hide_node(\''+node_id+'\')', 30 );
		
		node_opacity[node_id] -=0.1;
		
		block.style.opacity = node_opacity[node_id];
	}
	else
	{
		block.style.opacity = '0';
		block.style.display = 'none';
	}
}

function ie_hide_node( node_id )
{
	var block = $( node_id );
		
	clearTimeout( node_st[node_id] );
	
	if( node_opacity[node_id] > 0 )
	{
		node_st[node_id] = setTimeout( 'ie_hide_node(\''+node_id+'\')', 10 );
		
		node_opacity[node_id] -=30;
		
		block.style.filter = 'alpha(opacity='+node_opacity[node_id]+')';
	}
	else
	{
		block.style.filter = 'alpha(opacity=0)';
		block.style.display = 'none';
	}
}
/* --- component Photo Rate JS --- */

/**
 * Photo Rate javascript class
 */
function JSPhotoRating()
{
	
	/* -- Class constructor -- */
	this._construct = function()
	{
		// integer
		this.curr_photo_id = 0;
		
		// objects
		this.obj_container = $('photo_rating_container');
		
		this.obj_average_score_container = $('photo_rating_average_score_container');
		this.obj_average_score = $('photo_rating_average_score');
		this.obj_average_score_rates = $('photo_rating_average_score_rates');
		
		this.sex_pow_select = $('photo_rating_sex_filter');
		
		this.obj_points_container = $('photo_rating_points_container');
		
		// points nodes
		this.points = this.obj_points_container.getElementsByTagName('div');
		
		this.rate_photo(0);
	}
	
	
	this.rate_highlight = function( point )
	{
		var className = point ? 'rate_point_on' : 'rate_point_off';
		
		point--; // index begins at 0, points at 1
		
		for( var i = 0; i < this.points.length; i++ )
		{
			if( i == point )
			{
				this.points[i].className = 'rate_point_active';
				className = 'rate_point_off';
				continue;
			}
			
			this.points[i].className = className;
		}
		
		return;
	}
	
	
	this.rate_higlight_reset = function()
	{
		return this.rate_highlight(0);
	}
	
	
	this.rate_photo = function( score )
	{
		this.obj_container.className = 'photo_rating_inactive';
		
		xajax_ratePhoto( this.curr_photo_id, score, this.sex_pow_select.value );
	}
	
	
	this.loadPhoto = function( photo_id, profile_id, img_url, average_score, rates, profile_href, error_message )
	{
		this.curr_photo_id = photo_id;
		this.curr_profile_id = profile_id;
		
		this.new_img = new Image();
		
		this.new_img.src = img_url;
		
		if( error_message != '' )
			this.obj_average_score_container.innerHTML = error_message;
		else
		{
			this.obj_average_score.innerHTML = average_score;
			this.obj_average_score_rates.innerHTML = rates;
		}
		
		$('photo_rating_image_screen').innerHTML = '';
		
		$('photo_rating_profile_href').href = profile_href;
		
		var screen = $('photo_rating_image_screen');
		
		if( screen.getElementsByTagName('img').length )
			screen.removeChild( screen.getElementsByTagName('img')[0] );
		
		screen.appendChild(this.new_img);
		
		this.new_img.onload = function()
		{
			$('photo_rating_container').className = '';
		}
	}
	
	
	this.printError = function( text )
	{
		var screen = $('photo_rating_image_screen');
		
		if( screen.getElementsByTagName('img').length )
			screen.removeChild( screen.getElementsByTagName('img')[0] );
		
		screen.innerHTML = text;
		$('photo_rating_container').className = '';
		$('photo_rating_profile_href').href = '#';
	}
	
	
	this._construct();
}
