/* *************************** various JS functions *************************** */

document.onmousemove = mouse_move;
var mouse_pos;
var float_on = false;
var bubble_on = false;
var lng_on = false;
var recipient = 0;

/* **************************************************************************** */
//mouse event handler
function mouse_move(ev) {
	ev        = ev || window.event;
	mouse_pos = mouse_coords(ev);
}

/* **************************************************************************** */
//get mouse coords relative to doc top
function mouse_coords(ev) {
	if(ev.pageX || ev.pageY) {
		return {x:ev.pageX, y:ev.pageY};
	}
	
	return {
		x:ev.clientX + document.documentElement.scrollLeft - document.documentElement.clientLeft,
		y:ev.clientY + document.documentElement.scrollTop  - document.documentElement.clientTop
	};
}

/* **************************************************************************** */
function window_size() {
	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;
	}
	
	return {width:myWidth,height:myHeight};
}

/* **************************************************************************** */
//check messages periodically
function check_messages()
{
	new Ajax.PeriodicalUpdater('new-message', '/files/msgchecker.php', {asynchronous:true, frequency:20});
	return true;
}

/* **************************************************************************** */
//show/hide box
var shvisible = false;
function shbox(box, tbox, smsg, hmsg)
{
	var obj = document.getElementById(tbox);
	Effect.toggle(box,'BLIND');

	if (shvisible == false) {
		obj.innerHTML = hmsg;
		shvisible = true;
	} else {
		obj.innerHTML = smsg;
		shvisible = false;
	}
}

/* **************************************************************************** */
//show login or highlite
function show_login(id)
{
	var element = document.getElementById(id).style;
	if (element.display == 'none') {
		Effect.toggle(id,'BLIND');
	} else {
		text = document.getElementById(id + '-text').style;
		element.borderColor = '#f00';
		text.color = '#f00';
	}
}

/* **************************************************************************** */
//video edit window
function edit_window(action)
{
	var info = document.getElementById('video-info');
	var edit = document.getElementById('video-edit');

	if (action == 'hide') {
		edit.style.display = 'none';
		info.style.display = 'block';
	} else {
		info.style.display = 'none';
		edit.style.display = 'block';
	}
}

/* **************************************************************************** */
//confirm video deletion, type==1 - video, type==2 - group
function confirm_del(text, msg, ID, type)
{
	if (confirm(text)) {
		if (type == 1) location.href='/videa/zmaz/' + ID;
		if (type == 2) close_group(ID, msg);
	}
}

/* **************************************************************************** */
//show PM box
function show_pm(friendID) {
	if (!float_on) {
		var element = document.getElementById('pm');
		element.style.top = (mouse_pos.y - 60) + 'px';
		element.style.left = (mouse_pos.x - 280) + 'px';
		element.style.display = 'block';
		float_on = true;
		recipient = friendID;
	}
}

/* **************************************************************************** */
//hide PM box
function hide_pm() {
	if (float_on) {
		var element = document.getElementById('pm');
		element.style.display = 'none';
		float_on = false;
		recipient = 0;
		document.getElementById('pm-message').value = '';
	}
}

/* **************************************************************************** */
//show invite box
function show_invite() {
	if (!float_on) {
		var element = document.getElementById('invite');
		element.style.top = (mouse_pos.y - 60) + 'px';
		element.style.left = (mouse_pos.x - 280) + 'px';
		element.style.display = 'block';
		float_on = true;
	}
}

/* **************************************************************************** */
//hide invite box
function hide_invite() {
	if (float_on) {
		var element = document.getElementById('invite');
		element.style.display = 'none';
		float_on = false;
		recipient = 0;
		document.getElementById('invite-users').value = '';
	}
}

/* **************************************************************************** */
//group icon edit
function show_gie() {
	if (!float_on) {
		var element = document.getElementById('gie');
		element.style.top = (mouse_pos.y - 40) + 'px';
		element.style.left = (mouse_pos.x - 40) + 'px';
		element.style.display = 'block';
		float_on = true;
	}
}

/* **************************************************************************** */
//hide GIE box
function hide_gie() {
	if (float_on) {
		var element = document.getElementById('gie');
		element.style.display = 'none';
		float_on = false;
		document.getElementById('gie-file').value = '';
	}
}

/* **************************************************************************** */
//show babble box
function show_bubble() {
	if (!bubble_on) {
		var element = document.getElementById('bubble');
		element.style.display = 'block';
		bubble_on = true;
		setTimeout("hide_bubble()", 30*1000);
	}
}

/* **************************************************************************** */
//hide bubble box
function hide_bubble() {
	if (bubble_on) {
		var element = document.getElementById('bubble');
		element.style.display = 'none';
		bubble_on = false;
	}
}

/* **************************************************************************** */
//show lng selector
function show_lng() {
	if (!lng_on) {
		var element = document.getElementById('select-lng');
		element.style.display = 'block';
		lng_on = true;
		setTimeout("hide_lng()", 15*1000);
	} else {
		hide_lng();
	}
}

/* **************************************************************************** */
//hide lng selector
function hide_lng() {
	if (lng_on) {
		var element = document.getElementById('select-lng');
		element.style.display = 'none';
		lng_on = false;
	}
}


/* **************************************************************************** */
//hide lng selector
var _previewt;
var _preview_obj;
var _preview_userid;
var _preview_filename;

function preview_video(i, obj, user_id, filename)
{
  _preview_obj      = obj;
  _preview_userid   = user_id;
  _preview_filename = filename;

  // Set slideShowSpeed (milliseconds)
  var slideShowSpeed = 500
  if(i==9) i = 0;

  document.getElementById(obj).src = 'http://static.kukaj.sk/videos/'+user_id+'/frames/' + filename + '-' + i + '.jpg';

  _previewt = setTimeout("preview_video("+(i+1)+",'"+obj+"','"+user_id+"','"+filename+"')", slideShowSpeed);
}

function preview_stop(filename)
{
  if(_previewt) { clearTimeout(_previewt); _previewt = null; }

  filename.src      = 'http://static.kukaj.sk/videos/'+_preview_userid+'/thumbs/' + _preview_filename + '.jpg';
  _preview_obj      = null;
  _preview_userid   = null;
  _preview_filename = null;
}



