if (window.XMLHttpRequest) var object = new XMLHttpRequest();
else if (window.ActiveXObject) var object = new ActiveXObject("Microsoft.XMLHTTP");

var ajaxData = new Array();

function getData(ajaxData) { 
	if (object) {
		var itemData = ajaxData.shift();
		itemData = itemData.split("|");
		var itemDiv = itemData[0];
		var itemUrl = itemData[1];
		document.getElementById('wait').style.display = '';
		var target = document.getElementById(itemDiv);
		object.open("GET", itemUrl);
		object.onreadystatechange = function() {
			if (object.readyState == 4) {
				target.innerHTML = object.responseText;
				if (ajaxData.length > 0) getData(ajaxData);
				else document.getElementById('wait').style.display = 'none'; } }
	object.send(null); } }

function postData(itemUrl, itemDiv, postParametrs, ajaxData) { 
	if (object) {
		document.getElementById('wait').style.display = '';
		var target = document.getElementById(itemDiv);
		object.open('POST', itemUrl, true);
		object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		object.setRequestHeader("Content-length", postParametrs.length);
		object.setRequestHeader("Connection", "close");
		object.onreadystatechange = function() {
			if (object.readyState == 4) {
				target.innerHTML = object.responseText;
				if (ajaxData.length > 0) getData(ajaxData);
				else document.getElementById('wait').style.display = 'none'; } }
		object.send(postParametrs); } }

function ajaxPlaylistShow(video) {
	ajaxData[0] = 'playlist_show|index.php?action=playlist_show&video=' + video + '&ajax=true';
	getData(ajaxData); }

function ajaxPlaylistItemAdd(video, page) {
	ajaxData[0] = 'header_info|index.php?action=playlist_item_add&video=' + video + '&ajax=true';
	if (page != "watch") ajaxData[1] = 'playlist_show|index.php?action=playlist_show&video=' + video + '&ajax=true';
	getData(ajaxData); }

function ajaxPlaylistItemUp(video, position) {
	ajaxData[0] = 'header_info|index.php?action=playlist_item_up&position=' + position + '&video=' + video + '&ajax=true';
	ajaxData[1] = 'playlist_show|index.php?action=playlist_show&video=' + video + '&ajax=true';
	getData(ajaxData); }

function ajaxPlaylistItemDown(video, position) {
	ajaxData[0] = 'header_info|index.php?action=playlist_item_down&position=' + position + '&video=' + video + '&ajax=true';
	ajaxData[1] = 'playlist_show|index.php?action=playlist_show&video=' + video + '&ajax=true';
	getData(ajaxData); }

function ajaxPlaylistItemDelete(video, position) {
	ajaxData[0] = 'header_info|index.php?action=playlist_item_delete&position=' + position + '&video=' + video + '&ajax=true';
	ajaxData[1] = 'playlist_show|index.php?action=playlist_show&video=' + video + '&ajax=true';
	getData(ajaxData); }

function ajaxPlaylistDelete(video) {
	ajaxData[0] = 'header_info|index.php?action=playlist_delete&ajax=true';
	ajaxData[1] = 'playlist_show|index.php?action=playlist_show&video=' + video + '&ajax=true';
	getData(ajaxData); }

function ajaxFavorite(video) {
	ajaxData[0] = 'header_info|index.php?action=favorite&video=' + video + '&ajax=true';
	getData(ajaxData); }

function ajaxVote(video, vote) {
	ajaxData[0] = 'header_info|index.php?action=vote&video=' + video + '&vote=' + vote + '&ajax=true';
	ajaxData[1] = 'vote_show|index.php?action=vote_show&video=' + video + '&ajax=true';
	getData(ajaxData); }

function ajaxReport(video, type) {
	if (type == 'W') {
		var postParametrs = 'action=report&video=' + video + '&report_message=' + encodeURI(document.getElementById("report_message").value) + '&report_token=' + encodeURI(document.getElementById("report_token").value);
		postData('index.php?ajax=true', 'header_info', postParametrs, ajaxData);
		document.getElementById("report_token").value = '';	
		document.getElementById("report_message").value = ''; }
	else {
		ajaxData[0] = 'header_info|index.php?action=report&video=' + video + '&type=' + type + '&ajax=true';
		getData(ajaxData); } }

function ajaxCommentShow(video, page) {
	ajaxData[0] = 'comment_show|index.php?action=comment_show&video=' + video + '&page=' + page + '&ajax=true';
	getData(ajaxData); }

function ajaxCommentRate(video, comment, page, type) {
	ajaxData[0] = 'header_info|index.php?action=comment_rate&video=' + video + '&comment=' + comment + '&type=' + type + '&ajax=true';
	ajaxData[1] = 'comment_show|index.php?action=comment_show&video=' + video + '&page=' + page + '&ajax=true';
	getData(ajaxData); }

function ajaxCommentSubmit(video) {
	var postParametrs = 'action=comment&video=' + video + '&message_text=' + encodeURI(document.getElementById("message_text").value);
	ajaxData[0] = 'comment_show|index.php?action=comment_show&video=' + video + '&ajax=true';
	postData('index.php?ajax=true', 'header_info', postParametrs, ajaxData);
	document.getElementById("message_text").value = ''; }

function ajaxLoad(video) {
	ajaxData[0] = 'comment_show|index.php?action=comment_show&video=' + video + '&ajax=true';
	ajaxData[1] = 'playlist_show|index.php?action=playlist_show&video=' + video + '&ajax=true';
	getData(ajaxData); }