/* Переменные */

var v=0;
var x=0;
var max=0;
var type='inbox';
var country=0;
var country_id=0;
var city=0;
var city_id=0;
var empty=0;
var opened=0;
var xmlhttp = getXmlHttp();


/* XMLHttpRequest */

function getXmlHttp(){

	var xmlhttp;

	try {
    	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}

	catch (e) {

		try {
	  		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}

		catch (E) {
	      xmlhttp = false;
	    }
	}

	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest();
	}

	return xmlhttp;
}


/* Показ объекта */

function show(objectid) {

	var s=document.getElementById(objectid);

	if(s.style.display == 'block')
		s.style.display = 'none';

	else
		s.style.display = 'block';
}


/* ################ СООБЩЕНИЯ ################ */


/* Переключение между входящими и исходящими */

function show_type(newtype) {

	if(newtype=='inbox') document.getElementById('folder').innerHTML = "Входящие";
	else if(newtype=='sent') document.getElementById('folder').innerHTML = "Исходящие";
	else document.getElementById('folder').innerHTML = "История переписки";

	type=newtype;
	show_messages('now');
}


/* Вывод сообщений */

function show_messages(page) {

	opened = 0;

	document.getElementById('loading').innerHTML = "<img src='/images/progress3.gif'>";

	if(page=='now') x=0;
	else if(page=='forward') x--;
	else if(page=='next') x++;

	if(x<0) x=0;

	xmlhttp.open("POST", "/messages/", true);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send('message=view&type='+type+'&page='+x);
	xmlhttp.onreadystatechange=function() {

		if (xmlhttp.readyState != 4) return

		if (xmlhttp.status == 200) {

	  		document.getElementById('view_messages').innerHTML = xmlhttp.responseText;
	  		document.getElementById('new_messages').innerHTML = '';
	  		document.getElementById('loading').innerHTML = '';
		}
	}
}


/* Быстрый ответ */

function answer(message) {

	var form=document.getElementById(message);

	if(form.style.display == 'block') {

		form.style.display = 'none';
		if(opened>0) opened = opened-1;
	}

	else {

		opened = opened + 1;

		xmlhttp.open("POST", "/messages/", true);
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlhttp.send('message=answer&mid='+message);
		xmlhttp.onreadystatechange=function() {

			if (xmlhttp.readyState != 4) return

			if (xmlhttp.status == 200) {

		  		document.getElementById(message).innerHTML = xmlhttp.responseText;
		  		document.getElementById(message).style.display = 'block';

			}
		}
	}
}






/* Отправка сообщения */

function send_message(msgid) {

	var y=document.getElementById('t'+msgid).value;

	xmlhttp.open("POST", "/messages/", true);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send('message=send_message&mid='+msgid+'&content='+y+'&folder='+type);
	xmlhttp.onreadystatechange=function() {

		if (xmlhttp.readyState != 4) return

		if (xmlhttp.status == 200) {

			opened = 0;
			document.getElementById(msgid).innerHTML = xmlhttp.responseText;
			document.getElementById(msgid).style.display = 'none';
			document.getElementById('loading').innerHTML = "сообщение отправлено";
			setTimeout('clear_loading()', 5000);
		}
	}
}



/* Удаление сообщения */

function delete_message(msgid,spam) {

	xmlhttp.open("POST", "/messages/", true);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send('message=erase_message&mid='+msgid+'&folder='+type+'&spam='+spam);
	xmlhttp.onreadystatechange=function() {

		if (xmlhttp.readyState != 4) return

		if (xmlhttp.status == 200) {

			document.getElementById(msgid).style.display = 'none';
			show_messages('some');
			document.getElementById('loading').innerHTML = "сообщение удалено";
			setTimeout('clear_loading()', 5000);
		}
	}
}


/* Проверка новых сообщений */

function check_messages() {

	xmlhttp.open("POST", "/messages/", true);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send('message=check_messages');
	xmlhttp.onreadystatechange=function() {

		if (xmlhttp.readyState != 4) return

		if (xmlhttp.status == 200) {

			if(xmlhttp.responseText>0) {

				document.getElementById('new_messages').innerHTML = '('+xmlhttp.responseText+')';
				play_sound().play();
			}

			else document.getElementById('new_messages').innerHTML = '';
		}
	}
}



/* Добавление в друзья */

function add_friend(msgid) {

	xmlhttp.open("POST", "/messages/", true);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send('message=add_friend&mid='+msgid);
	xmlhttp.onreadystatechange=function() {

		if (xmlhttp.readyState != 4) return

		if (xmlhttp.status == 200) {

			document.getElementById('loading').innerHTML = "пользователь добавлен";
			setTimeout('clear_loading()', 4000);
			//document.getElementById(msgid).style.display = 'none';
			//show_messages(x);
		}
	}
}

function add_friend_header(friend) {

	xmlhttp.open("POST", "/friends/", true);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send('action=add_friend&fid='+friend);
	xmlhttp.onreadystatechange=function() {

		if (xmlhttp.readyState != 4) return

		if (xmlhttp.status == 200) {

			document.getElementById('add_friend').innerHTML = "В друзьях";
			//setTimeout('clear_loading()', 4000);
			//document.getElementById(msgid).style.display = 'none';
			//show_messages(x);
		}
	}
}

function clear_loading() {

	document.getElementById('loading').innerHTML = '';
}


/* ################ СТЕНА ################ */


/* Вывод стены */

function show_wall(id) {

	xmlhttp.open("POST", "/"+id, true);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send('action=show_wall');
	xmlhttp.onreadystatechange=function() {

		if (xmlhttp.readyState != 4) return

		if (xmlhttp.status == 200) {

			document.getElementById('view_wall').innerHTML = xmlhttp.responseText;
		}
	}
}


/* Добавление нового сообщения на стену*/

function add_new_wall_message(id) {

	msg=document.getElementById('new_wall_message');

	xmlhttp.open("POST", "/"+id, true);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send('action=new_wall_message&message='+msg.value);
	xmlhttp.onreadystatechange=function() {

		if (xmlhttp.readyState != 4) return

		if (xmlhttp.status == 200) {

			msg.value = '';
			document.getElementById('add_wall').style.display = 'none';
			document.getElementById('view_wall').innerHTML = xmlhttp.responseText;

		}
	}
}


/* Удаление сообщения со стены */

function erase_message(mid) {

	xmlhttp.open("POST", "/"+user, true);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send('action=erase_wall_message&mid='+mid);
	xmlhttp.onreadystatechange=function() {

		if (xmlhttp.readyState != 4) return

		if (xmlhttp.status == 200) {

			document.getElementById('view_wall').innerHTML = xmlhttp.responseText;
		}
	}
}



/* Удаление сообщения со стены и отправка его в спам */

function spam_message(mid) {

	xmlhttp.open("POST", "/"+user, true);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send('action=spam_wall_message&mid='+mid);
	xmlhttp.onreadystatechange=function() {

		if (xmlhttp.readyState != 4) return

		if (xmlhttp.status == 200) {

			document.getElementById('view_wall').innerHTML = xmlhttp.responseText;
		}
	}
}


/* Фотогаллерея */

function next_photo(imgid) {

	var img=document.getElementById('view_img');

	//img.innerHTML = "<img src='/images/progress1.gif'>";

	xmlhttp.open("POST", "/photo/", true);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send('action=next_photo&pid='+imgid);
	xmlhttp.onreadystatechange=function() {

		if (xmlhttp.readyState != 4) return

		if (xmlhttp.status == 200) {

			img.innerHTML = xmlhttp.responseText;
		}
	}
}

function new_photo_comment(imgid) {

	var comment=document.getElementById('new_photo_comment').value;

	xmlhttp.open("POST", "/photo/", true);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send('action=new_photo_comment&pid='+imgid+'&comment='+comment);
	xmlhttp.onreadystatechange=function() {

		if (xmlhttp.readyState != 4) return

		if (xmlhttp.status == 200) {

			document.getElementById('view_img').innerHTML = xmlhttp.responseText;
		}
	}
}

function erase_photo_comment(imgid) {

	xmlhttp.open("POST", "/photo/", true);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send('action=erase_photo_comment&pid='+imgid);
	xmlhttp.onreadystatechange=function() {

		if (xmlhttp.readyState != 4) return

		if (xmlhttp.status == 200) {

			document.getElementById('view_img').innerHTML = xmlhttp.responseText;
		}
	}
}


/* Проигрывание сообщения */

var FlashHelper =
{
	movieIsLoaded : function (theMovie)
	{
		if (typeof(theMovie) != "undefined") return theMovie.PercentLoaded() == 100;
		else return
		false;
  },

	getMovie : function (movieName)
	{
  	if (navigator.appName.indexOf ("Microsoft") !=-1) return window[movieName];
	  else return document[movieName];
	}
};

function play_sound()
{
	this.obj = FlashHelper.getMovie('play_msg');

	if (!FlashHelper.movieIsLoaded(this.obj)) return;

	this.play = function () {
		this.obj.TCallLabel('/','play');
	};

	this.stop = function () {
		this.obj.TCallLabel('/','stop');
	};

	this.pause = function () {
		this.obj.TCallLabel('/','pause');
	};

	this.playToggle = function () {
		this.obj.TCallLabel('/','playToggle');
	};

	this.reset = function () {
		this.obj.TCallLabel('/','reset');
	};

	this.load = function (url) {
		this.obj.SetVariable('currentSong', url);
		this.obj.TCallLabel('/','load');
	};

	this.loadAndPlay = function (url) {
		this.load(url);
		this.play();
	};

	this.getState = function () {
		var ps = this.obj.GetVariable('playingState');
		var ls = this.obj.GetVariable('loadingState');

		// returns
		//   'empty' if no file is loaded
		//   'loading' if file is loading
		//   'playing' if user has pressed play AND file has loaded
		//   'stopped' if not empty and file is stopped
		//   'paused' if file is paused
		//   'finished' if file has finished playing
		//   'error' if an error occurred
		if (ps == 'playing')
			if (ls == 'loaded') return ps;
			else return ls;

		if (ps == 'stopped')
			if (ls == 'empty') return ls;
			if (ls == 'error') return ls;
			else return ps;

		return ps;

	};

	this.getPlayingState = function () {
		// returns 'playing', 'paused', 'stopped' or 'finished'
		return this.obj.GetVariable('playingState');
	};

	this.getLoadingState = function () {
		// returns 'empty', 'loading', 'loaded' or 'error'
		return this.obj.GetVariable('loadingState');
	};

	this.registerEvent = function (eventName, action) {
		// eventName is a string with one of the following values: onPlay, onStop, onPause, onError, onSongOver, onBufferingComplete, onBufferingStarted
		// action is a string with the javascript code to run.
		//
		// example: niftyplayer('niftyPlayer1').registerEvent('onPlay', 'alert("playing!")');

		this.obj.SetVariable(eventName, action);
	};

	return this;
}



/* В спам */

function spam(id) {

	xmlhttp.open("POST", "/spam/", true);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send('action=add_to_spam&id='+id);
	xmlhttp.onreadystatechange=function() {

		if (xmlhttp.readyState != 4) return

		if (xmlhttp.status == 200) {

			document.getElementById('view_img').innerHTML = xmlhttp.responseText;
		}
	}
}


/**
 *
 * @access public
 * @return void
 **/
function view_gift(gift,status) {

	if(status==1) document.getElementById(gift).style.display='block';
	else document.getElementById(gift).style.display='none';



}
