function setGalleryPaging(page, keyword, point) {
	document.getElementById('gallerySearchResults').action = "gallery.asp?p=" + page + "&point=" + point;
	document.getElementById('searchfieldHidden').value = keyword;
	document.getElementById('gallerySearchResults').submit();
}
function CheckAll(obj, arrChk) {
  if (arrChk != null) 
    if (arrChk.length == undefined) arrChk.checked = obj.checked;
  else 
    for (i = 0; i < arrChk.length; i++) arrChk[i].checked = obj.checked;
}

function control_in(control, text){ if (text == control.value) control.value = ''; }
function control_out(control, text){ if ('' == control.value) control.value = text;}

String.prototype.capitalize = function(){
   return this.replace( /(^|\s)([a-z])/g , function(m,p1,p2){ return p1+p2.toUpperCase(); } );
  };

var online_stream_check_time = 2000;
var model_check_time = 5000;
var chat_update_time = 2500;
var elapsed_update_time = 1000;
var elapsed_text_update_time = 60000;

/* broadcast functions */
var is_video = false;
var iam = '';
var rings = false;
var status = 'offline';
var elapsed = 0;

function set_offline()
{
	user_check = function(data) {}
	$.ajax({
		type: 'POST',
		url: "set_offline.asp?id=" + logged_user_id +"&t=" + Math.random(),		
		success: function(data)
		{
			window.location = "/gallery.asp"; 
		}
	});
	return false;
}

function stop_video() 
{
	if (is_video)
	{
		is_video = false;
		if ('man' == iam) $('#time_message').html('THE LADY HAS NOT STARTED VIDEO CHAT');
		$('#mediaplayer1').hide();
		$('.video-player h3').show();
		checkstream_url = "";
	}
}

function start_video()
{
	if (!is_video) 
	{
		is_video = true;
		$('#video-player').hide();	
		swf();
		if ("lady" != iam) 
			init_time();	
		
		$('.video-player h3').hide();
		$('#mediaplayer1').show();		
	}
}
function online_stream_ready()
{
	if( "" != checkstream_url && 'online' == status ) 
	{
		$.ajax({
			type: 'POST',
			url: "checkstream.asp?id=" + checkstream_url +"&t=" + Math.random(),
			success: function(data)
			{
				try {				
					if (1 == data)
					{
						start_video();
					} 
					else
					{
						stop_video();
					}
					
				} catch(e) {}	
				
				setTimeout("online_stream_ready()", online_stream_check_time);
			}
		});
	} else 
	{
		setTimeout("online_stream_ready()", online_stream_check_time);
	}
}

function get_url()
{
	$.ajax({
		type: 'POST',
		url: "get_url.asp?id=" + logged_user_id +"&t=" + Math.random(),
		dataType: "json",			
		success: function(data)
		{
			try {	
				if (undefined != data['streamer'] && undefined != data['file'] && undefined != data['checkstream_url'])
				{
					flashvars['file'] = encodeURIComponent(data['file']);
					flashvars['streamer'] = encodeURIComponent(data['streamer']);
					checkstream_url = data['checkstream_url'];			
				}
			} catch(e) {}				
		}
	});

}


function user_check()
{
	$.ajax({
		type: 'POST',	
		url: "/status.asp?id=" + logged_user_id +"&t=" + Math.random(),
		dataType: "json",		
		success: function(data)
		{
			try {
				iam = data['iam'];
				if (undefined != data['oppo_status'] &&  null == data['oppo_user_name'])
					data['oppo_user_name'] = 'Noname';
				
				if (undefined != data['oppo_status'] && "1" == data['oppo_status']) 
					status = 'online';
				else 
					status = 'offline';	
				
				//if man lost his tokens
				if (is_video && 'man' == iam && undefined != data['tokens'] && 0 == data['tokens']) 
				{
					window.location = "/VideoChat.asp?id=" + data['oppo_user_id']; return;
				}

				if ('offline' == status ) 
				{
					$('#lady-title').html('No one in chat.'); 
					rings = false;
				}
				else
				{
					$('#lady-title').html(data['oppo_prefix'] + '&nbsp;' + data['oppo_user_name'] + '&nbsp;<span>(ID: ' + data['oppo_prefix_short'].substring(0,1) + data['oppo_user_id'] + ')</span>&nbsp;<span class="' + status + '">' + status.capitalize() + '</span>');
				 
					if ('lady' == iam  && !rings )  
					{ 
						$.dbj_sound.play( $('#man-online') );
						rings = true;
					} 

				}

				if( !is_video && "" == checkstream_url && 'online' == status ) 
					get_url();	
					
				if(0 != elapsed) show_elapsed_time();	
					
			} catch(e) {}						
		},
	   complete: function(){
			setTimeout("user_check()", model_check_time);
	   }
		
	});		
}

var chat_last_id = 0;

function chat_add(result, id, text, login, user, from_user)
{	
	if ($('#p' + id).size() > 0)
		return result;
	
	if (id && id > chat_last_id)
		chat_last_id = id;
						
	if (user == logged_user_id)
	{
		result = result + '<p id="p' + id + '"><i>You: </i>: ' +  text + '</p>';
	}
	else
	{
		result = result + '<p id="p' + id + '"><b>' + login + ' </b>: ' +  text + '</p>';
	}
	
	return result;
}	

function chat_update()
{
	$.ajax({
		type: 'POST',
		url: '/chat.asp?id=' + logged_user_id + '&last_id=' + chat_last_id +'&t=' + Math.random(),
		dataType: "json", 
		success: function(messages)
		{
			try {
				result = '';
				
				for (var key in messages)
				{
					result = chat_add(result, key, messages[key].d, messages[key].l, messages[key].u, messages[key].f);
				}
				
				if (result)
				{
					$('#chat-content').append($(result));
					$("#chat-content").animate({ scrollTop: $("#chat-content").attr("scrollHeight") }, 3000);					
				}
			} catch(e) {}
		},
	   complete: function(){
			setTimeout("chat_update()", chat_update_time);	   
	   }
	});

}

function init_chat()
{
		
	// init chat form
	$('.chat-input a').click(function()
	{
		v = $('#chat-text').val();
		
		if(v)
		{		
			$.ajax({
				type: 'POST',
				url: '/chat_add.asp?id=' + logged_user_id + '&t=' + Math.random(),
				data: { data: v },
				success: function(data){
					if ("" == data)
					{
						$('#chat-content').append($(chat_add('', data, "message didn't recorded " + v, '', logged_user_id, ("lady" == iam) ? 0 : 1)));							
					}
					else
					{
						$('#chat-content').append($(chat_add('', data, v, 'You: ', logged_user_id, ("lady" == iam) ? 0 : 1)));		
					}
					$("#chat-content").animate({ scrollTop: $("#chat-content").attr("scrollHeight") }, 3000);
					start_man_time();
				}
			});						
		}
		
		$('#chat-text').val('');
	});
	
	$('.chat-input input').keypress(function(event) {
		if ('13' == event.keyCode) 
		{
			$('.chat-input a').click();
		}
	});

	// init chat update	
	chat_update();	
}

var start_time;


function show_elapsed_time() 
{
	hours   = Math.floor(elapsed / 60);
	minutes = elapsed - hours * 60;
	
	if (minutes < 10)
	{
		minutes = '0' + minutes;
	}
	
	$('#time2').html(hours + ':' + minutes);
	
	$('#time2').show();		
	$('#time_message2').show();
}

function set_time(total) 
{
	hours   = Math.floor(total / 60);
	minutes = total - hours * 60;
	
	if (minutes < 10)
	{
		minutes = '0' + minutes;
	}
	
	$('#time').html(hours + ':' + minutes);
	$('#time').show();	
}

function finish_time(total)
{
	start_time = null;
	set_time(0);			
	elapsed += total;
	$('#time').hide();
	if(0 != elapsed) show_elapsed_time();	
}
function update_time()
{
	total = Math.floor(((new Date()).getTime() - start_time) / 60000);
	
	set_time(total);
	
	if ("man" == iam && !is_video) 
	{
		finish_time(total);
	}	
	else 
		setTimeout("update_time()", 1000);
}

function init_time()
{		
	if ("man" == iam && !is_video) 
	{
		$('#time').hide();
		return;
	}
	
	if ("lady" == iam)
	{
		$('#time_message').html('Duration of current Video Chat:');
	} else if (is_video) {
		$('#time_message').html('Time Elapsed:');
	}	
	
	if (null != start_time) 
		elapsed += Math.floor(((new Date()).getTime() - start_time) / 60000);
				
	set_time(0);
	start_time = (new Date()).getTime(); 	
	setTimeout("update_time()", elapsed_update_time);
}

function start_man_time()
{
	if ("man" != iam) return;
	if (is_video) return;
	if (null != start_time) return;

	$('#time_message').html('Time Elapsed:');
	
	start_time = (new Date()).getTime(); 	
	set_time(0);
	
	setTimeout("man_update()", elapsed_text_update_time);

}

function man_update()
{
	if ("man" != iam) return;
	if (is_video)
	{
		$('#time').hide();
		return;
	}
	
	$.ajax({
		type: 'POST',	
		url: "/text_chat.asp?id=" + logged_user_id +"&t=" + Math.random(),
		success: function(data) {},
		complete: function(){}
	});	
	
	total = Math.floor(((new Date()).getTime() - start_time) / 60000)
	set_time(total);

	if ("online" != status) 
	{
		$('#time_message').html('THE LADY HAS NOT STARTED VIDEO CHAT');
		finish_time(total);
		return;	
	}
	setTimeout("man_update()", elapsed_text_update_time);

}
