

var PosWinY;
var id_comm = 0;
var fieldd;
var itemd;


function Items(action, type, cat, item, mode)
{
	StartLoading();
	//ajaxpack.getAjaxRequest('index.php', 'actionblock=' + action + '&type=' + type + '&cat=' + cat, processGetPost, "html");
	if(type == 'L' || type == 'E' || type == 'Z' || type == 'p')
		new Ajax.Request('index.php', {method: 'post', parameters:'actionblock=' + action + '&type=' + type + '&cat=' + cat, onSuccess:ParseLogin, onFailure:ErrorView});
	else if(type == 'f')
		new Ajax.Request('index.php', {method: 'post', parameters:'actionblock=' + action + '&type=' + type + '&cat=' + cat + '&item=' + item, onSuccess:ParseComments, onFailure:ErrorView});
	else if(type == 'comm_ajax')
		new Ajax.Request('/tools/comment.php', {method: 'post', parameters:'action=' + action + '&type=' + type + '&cat=' + cat + '&item=' + item, onSuccess:ParseComments, onFailure:ErrorView});
	else if(type == 'comm')
		new Ajax.Request('/tools/comment.php', {method: 'post', parameters:'action=' + action + '&type=' + type + '&cat=' + cat + '&item=' + item, onSuccess:ParseComments, onFailure:ErrorView});
	else if(type == 'count_comm')
	{
		//alert(item);
		id_comm = item;
		new Ajax.Request('/tools/comment.php', {method: 'post', asynchronous: false, parameters:'action=' + action + '&type=' + type + '&cat=' + cat + '&item=' + item, onSuccess:CountComments, onFailure:ErrorView});
	}
	EndLoading();
}


function PosWin(e)
{
	PosWinY = Event.pointerY(e);
}


function DocResize(e)
{
	if(document.getElementById("ContLogin"))
	{
		var c = document.getElementById("ContLogin");
		c.style.left = document.body.clientWidth / 2 - 200 + "px";
		//c.style.top = document.body.clientHeight / 2 - 100 + "px";
		if(PosWinY)
			c.style.top = PosWinY + "px";
		else
			c.style.top = document.body.scrollTop + (document.body.clientHeight / 2) - 100 + "px";
	}

	if(document.getElementById("Loading"))
	{
		var c = document.getElementById("Loading");
		c.style.left = document.body.clientWidth / 2 - 130 + "px";
		//c.style.top = document.body.clientHeight / 2 - 40 + "px";
		if(PosWinY)
			c.style.top = PosWinY + "px";
		else
			c.style.top = document.body.scrollTop + (document.body.clientHeight / 2) - 100 + "px";
	}
}


function StartLoading()
{
	if($('Loading'))
		EndLoading();
	var d = document.createElement("div");
	d.setAttribute("id", "Loading");
	d.className = "Loading";
	d.style.left = document.body.scrollWidth / 2 - 130 + "px";
	if(PosWinY)
		d.style.top = PosWinY + "px";
	else
		d.style.top = document.body.scrollTop + (document.body.clientHeight / 2) - 40 + "px";

	var text = document.createTextNode("Загрузка...");
	d.appendChild(text);

	document.body.appendChild(d);
	document.body.style.cursor = "wait";
}


function EndLoading()
{
	var d = document.getElementById("Loading");
	document.body.removeChild(d);
	document.body.style.cursor = "default";
}


var ErrorView = function(t)
{
	alert('Ошибка ' + t.status + ' -- ' + t.statusText);
}


//	Login

function PostLogin(f)
{
	StartLoading();
	var param = Form.serialize($(f));
	new Ajax.Request('index.php', {method: 'post', parameters:param + '&actionblock=getblock&type=L&mode_ajax=1', onSuccess:CommentsAddForm, onFailure:ErrorView});
	EndLoading();
}


var ParseLogin = function(req)
{
	if($('ContLogin'))
		NoLogin();
	var text = req.responseText;
	//alert(window.event);
	//alert(document.body.position + ' ' + document.body.scrollWidth + ' ' + document.body.offsetHeight + ' ' + document.body.scrollTop);
	var d = document.createElement("div");
	d.setAttribute("id", "MaskBody");
	d.setAttribute("align", "center");
	d.className = "MaskBody";
	d.style.height = document.body.scrollHeight + "px";
	d.style.width = document.body.scrollWidth + "px";

	document.body.appendChild(d);

	var c = document.createElement("div");
	c.setAttribute("id", "ContLogin");
	c.className = "LoginBlock";
	//c.style.display = "none";
	c.style.width = "400px";
	//c.style.height = "200px";
	c.style.left = document.body.clientWidth / 2 - 200 + "px";
	//c.style.top = document.body.clientHeight / 2 - 100 + "px";
	if(PosWinY)
		c.style.top = PosWinY + "px";
	else
		c.style.top = document.body.scrollTop + 100 + "px";
		//c.style.top = document.body.scrollTop + (document.body.clientHeight / 2) - 100 + "px";
	document.body.appendChild(c);

	//Effect.Grow(c);
	//Effect.Appear(c);
	//Effect.BlindDown(c);

	var con = document.getElementById("ContLogin");
	con.innerHTML = text;

	new Draggable('ContLogin', {} );
}


function NoLogin()
{
	if(document.getElementById("ContLogin"))
	{
		var c = document.getElementById("ContLogin");
		document.body.removeChild(c);
		//Effect.Fold(c);
		//Effect.Squish(c);
		//Effect.Shrink(c);
	}
	if(document.getElementById("MaskBody"))
	{
		var d = document.getElementById("MaskBody");
		document.body.removeChild(d);
	}
}


function ShowRegBl()
{
	$('L_tbl').style.display = "none";
	$('R_tbl').style.display = "";
}


//	Comments

var ParseComments = function(req)
{
	if($('ContLogin'))
		NoLogin();
	var text = req.responseText;
	var c = document.createElement("div");
	c.setAttribute("id", "ContLogin");
	c.className = "LoginBlock";
	c.style.zIndex = "20";
	c.style.display = "block";
	c.style.width = "500px";
	c.style.backgroundColor = "";
	//c.style.height = "200px";
	c.style.left = document.body.clientWidth / 2 - 200 + "px";
	if(PosWinY)
		c.style.top = PosWinY + "px";
	else
		c.style.top = (document.body.scrollTop + (document.body.clientHeight / 2) - 100) + "px";

	document.body.appendChild(c);

	var con = document.getElementById("ContLogin");
	con.innerHTML = text;

	//con.style.top = document.body.clientWidth / 2 - con.offsetHeight + "px";

	new Draggable('ContLogin', {} );
	HeightCont();
}


function HeightCont()
{
	if($('cont_view'))
	{
		var d = $('cont_view');
		var y = d.offsetHeight;
		if(y > 300)
		{
			d.style.height = "300px";
			d.overflow = "auto";
		}

	}
}


var CommentsAddForm = function(req)
{
	var text_f = req.responseText;

	var c = document.getElementById("cont_tab");
	var con = document.getElementById("ContLogin");
	c.innerHTML = null;
	c.innerHTML = text_f;
	//c.style.display = "none";
	con.style.width = c.offsetWidth;
	HeightCont();
}


var CommentsAddFormnoAjax = function(req)
{
	var text_f = req.responseText;

	var c = document.getElementById("add_comm");
	c.innerHTML = text_f;
}


function ViewComm(cat, item, offset)
{
	StartLoading();
	new Ajax.Request('/tools/comment.php', {method: 'post', parameters:'action=view&type=comm&cat=' + cat + '&item=' + item + '&offset_c=' + offset, onSuccess:CommentsAddForm, onFailure:ErrorView});
	var d = document.getElementById("comm_div");
	d.style.display = "";
	Effect.SlideDown(d);
	//Effect.Appear(d);
	var l = document.getElementById("comm_link");
	l.style.display = "none";
	setTimeout('DisplayLink()', 1000);
	EndLoading();
}


function ViewCommPostAdd(cat, item, offset)
{
	StartLoading();
	new Ajax.Request('/tools/comment.php', {method: 'post', parameters:'action=view&type=comm&cat=' + cat + '&item=' + item + '&offset_c=' + offset, onSuccess:CommentsAddForm, onFailure:ErrorView});
	var d = document.getElementById("comm_div");
	d.style.display = "";
	var l = document.getElementById("comm_link");
	l.style.display = "none";
	setTimeout('DisplayNoLink()', 1000);
	EndLoading();
}


function DisplayLink()
{
	if(document.getElementById("viewadd_comm"))
	{
		if(document.getElementById("add_comm").style.display == '')
			document.getElementById("viewadd_comm").style.display = "none";
	}
}


function DisplayNoLink()
{
	var li = document.getElementById("viewadd_comm");
	li.style.display = "none";
}


function ViewAddComm(cat, item)
{
	StartLoading();
	new Ajax.Request('/tools/comment.php', {method: 'post', parameters:'action=add_form&type=comm&cat=' + cat + '&item=' + item, onSuccess:CommentsAddFormnoAjax, onFailure:ErrorView});
	var l = document.getElementById("viewadd_comm");
	l.style.display = "none";
	var d = document.getElementById("add_comm");
	d.style.display = "";
	//Effect.SlideDown(d);
	EndLoading();
}


function PostComment()
{
	StartLoading();
	var f = Form.serialize($('comment'));
	new Ajax.Request('/tools/comment.php', {method: 'post', asynchronous: false, parameters:f, onSuccess:CommentsAddForm, onFailure:ErrorView});
	EndLoading();
	reloadCountComments();
}


function PostCommentnoAjax(f, cat, item)
{
	StartLoading();
	var param = Form.serialize($(f));
	new Ajax.Request('/tools/comment.php', {method: 'post', parameters:param, onSuccess:CommentsAddFormnoAjax, onFailure:ErrorView});
	EndLoading();
	reloadCountComments();
	ViewComm(cat,item,0);
}


function CountComments(req)
{
	var text = req.responseText;
	if(id_comm)
		var d = document.getElementById("count_comm" + id_comm);
	else
		var d = document.getElementById("count_comm" + id_comm);
	d.innerHTML = text;
}


function reloadCountComments()
{
	var itm = document.comment.item.value;
	var cat = document.comment.cat.value;
	Items('count_comm', 'count_comm', cat, itm);
}


function ChangeTabComm(idtab, cat, item, offset)
{
	ChangeTab(idtab)

	if(idtab == 1)
		new Ajax.Request('/tools/comment.php', {method: 'post', parameters:'action=view&type=comm_ajax&cat=' + cat + '&item=' + item + '&offset_c=' + offset, onSuccess:CommentsAddForm, onFailure:ErrorView});
	else if(idtab == 3)
		new Ajax.Request('/tools/comment.php', {method: 'post', asynchronous: false, evalScripts: true, parameters:'action=add_form&type=comm_ajax&cat=' + cat + '&item=' + item, onSuccess:CommentsAddForm, onFailure:ErrorView});
}


// Photo

function AddElement(modetab)
{
	if(modetab == 1)
	{
		var t = document.getElementById("upltab");
		for(var c=0; c<t.childNodes.length; c++)
		{

			if(t.childNodes[c].tagName != undefined)
			if(t.childNodes[c].tagName.match(/tbody/gi))
			{
				var tb = t.childNodes[c];
			}
		}
		var s = $("subm");

		var f = document.upload;
		var Cnt = 0;
		for(var c=0; c<f.elements.length; c++)
		{
			if(f.elements[c].type == 'file')
				Cnt++;
		}

		if(Cnt < 15)
		{
			var row0 = document.createElement("tr");
			var col0 = document.createElement("td");
			col0.setAttribute("colspan", "2");
			col0.style.width = "100%";
			var img0 = document.createElement("img");
			img0.setAttribute("src", "/img/1.gif");
			img0.setAttribute("border", "0");
			img0.setAttribute("width", "1");
			img0.setAttribute("height", "10");
			img0.setAttribute("alt", "");

			col0.appendChild(img0);
			row0.appendChild(col0);
			tb.insertBefore(row0, s);

			var row01 = document.createElement("tr");
			var col01 = document.createElement("td");
			col01.setAttribute("colspan", "2");
			col01.className = "zagl_add";
			var sp01 =  document.createElement("span");
			sp01.className = "zagl";
			sp01.style.paddingLeft = "20px";
			sp01.appendChild(document.createTextNode("Добавить еще фото в прикол"));

			col01.appendChild(sp01);
			row01.appendChild(col01);
			tb.insertBefore(row01, s);

			var row02 = document.createElement("tr");
			var col02 = document.createElement("td");
			col02.setAttribute("colspan", "2");
			col02.style.backgroundColor = "#ffffff";
			col02.style.border = "solid 1px #c9ecff";
			col02.style.width = "100%";

			var t1 = document.createElement("table");
			var tb1 = document.createElement("tbody");
			t1.setAttribute("border", "0");
			t1.style.width = "100%";
			t1.setAttribute("cellspacing", "3");
			t1.setAttribute("cellpadding", "10");

			var row = document.createElement("tr");
			var col = document.createElement("td");
			col.setAttribute("align", "left");
			col.className = "form_txt";
			col.style.width = "130";
			col.style.paddingLeft = "35px";
			col.appendChild(document.createTextNode("Выберите файл:"));
			row.appendChild(col);

			var col2 = document.createElement("td");

			var inp = document.createElement("input");
			inp.setAttribute("type", "file");
			inp.setAttribute("name", "file" + Cnt);
			inp.setAttribute("size", "58");
			inp.className = "form";

			col2.appendChild(inp);
			row.appendChild(col2);
			tb1.appendChild(row);

			var row2 = document.createElement("tr");
			var col3 = document.createElement("td");
			col3.setAttribute("align", "left");
			col3.className = "form_txt";
			col3.style.width = "130";
			col3.style.paddingLeft = "35px";
			col3.appendChild(document.createTextNode("Название фото:"));
			row2.appendChild(col3);

			var col4 = document.createElement("td");

			var inp2 = document.createElement("input");
			inp2.setAttribute("type", "text");
			inp2.setAttribute("name", "ptitle" + Cnt);
			inp2.setAttribute("size", "50");
			inp2.className = "form";

			col4.appendChild(inp2);
			row2.appendChild(col4);
			tb1.appendChild(row2);
			t1.appendChild(tb1);
			col02.appendChild(t1);
			row02.appendChild(col02);
			tb.insertBefore(row02, s);
		}
	}
}


function ChangePhoto(el)
{
	var fc = $('photo_cont');
	var fi = $(el);
	var d = $('photo_comm');

	var fc_img = fc.getAttribute("src");
	var fc_imgid = fc.getAttribute("itid");
	var fc_imgs = fc.getAttribute("slink");
	var fc_imgb = fc.getAttribute("blink");
	var fc_comm = fc.getAttribute("alt");

	var fi_img = fi.getAttribute("src");
	var fi_imgid = fi.getAttribute("id");
	var fi_imgs = fi.getAttribute("slink");
	var fi_imgb = fi.getAttribute("blink");
	var fi_comm = fi.getAttribute("alt");

	fc.setAttribute("src", fi_imgb);
	fc.setAttribute("blink", fi_imgb);
	fc.setAttribute("slink", fi_imgs);
	fc.setAttribute("itid", fi_imgid);
	fc.setAttribute("alt", fi_comm);

	d.innerHTML = fi_comm;

	fi.setAttribute("src", fc_imgs);
	fi.setAttribute("blink", fc_imgb);
	fi.setAttribute("slink", fc_imgs);
	fi.setAttribute("id", fc_imgid);
	fi.setAttribute("alt", fc_comm);
}


function ChangeTab(idtab)
{
	StartLoading();

	RefrashTab();

	var tab = document.getElementById("tab" + idtab);
	var content = document.getElementById("content");
	var img1 = document.getElementById("img1tab" + idtab);
	var img2 = document.getElementById("img2tab" + idtab);
	var img3 = document.getElementById("img3tab" + idtab);

	tab.style.backgroundColor = "#f2fafd";
	img1.setAttribute("src", ImgPath + "img/tab_l_a.gif");
	img1.setAttribute("height", "31");
	img2.style.backgroundImage = "url(" + ImgPath + "img/tab_bg_a.gif)";
	img2.style.backgroundRepeat = "repeat-x";
	img2.setAttribute("height", "31");
	if(idtab == 3)
		img3.setAttribute("src", ImgPath + "img/tab_r_last_a.gif");
	else
		img3.setAttribute("src", ImgPath + "img/tab_r_a.gif");
	img3.setAttribute("height", "31");

	if(document.upload)
	{
		var f = document.upload;
		f.type_mode.value = idtab;
		f.tab_mode.value = idtab;
	}
	var cont_f = document.getElementById("content_form" + idtab);
	if(cont_f)
	{
		var text_form = cont_f.innerHTML;
		content.innerHTML = text_form;
	}

	EndLoading();
}


function RefrashTab()
{
	for(var i=1; i<=3; i++)
	{
		var tab = document.getElementById("tab" + i);
		var img1 = document.getElementById("img1tab" + i);
		var img2 = document.getElementById("img2tab" + i);
		var img3 = document.getElementById("img3tab" + i);

		if(tab)
			tab.style.backgroundColor = "#73d0fc";
		if(img1)
		{
			img1.setAttribute("src", ImgPath + "img/tab_l.gif");
			img1.setAttribute("height", "32");
		}
		if(img2)
		{
			img2.style.backgroundImage = "url(" + ImgPath + "img/tab_bg.gif)";
			img2.style.backgroundRepeat = "repeat-x";
			img2.setAttribute("height", "32");
		}
		if(img3)
		{
			if(i == 3)
				img3.setAttribute("src", ImgPath + "img/tab_r_last.gif");
			else
				img3.setAttribute("src", ImgPath + "img/tab_r.gif");
			img3.setAttribute("height", "32");
		}
	}
}


function SendMailFriend(f, type)
{
	StartLoading();
	var param = Form.serialize($(f));
	new Ajax.Request('index.php', {method: 'post', parameters:param, onSuccess:ParseComments, onFailure:ErrorView});
	EndLoading();
}


function ItemValid(action, field, val)
{
	document.getElementById(field + "_valid").innerHTML = "Проверка!";
	document.getElementById(field + "_valid").style.color = "red";

	fieldd = field;

	if(val == '')
		parseResultValid(2);
	else
	{
		if(field == 'passwd2')
		{
			var pass1 = document.register_form.passwd.value;
			if(pass1 != val)
				parseResultValid(6);
			else
				parseResultValid(1);
		}
		else
			new Ajax.Request('/tools/validator.php', {method: 'post', parameters:'action=' + action + '&field=' + field + '&val=' + val, onSuccess:ResultValidator, onFailure:ErrorView});
	}
}


function ResultValidator(req)
{
	var response  = req.responseXML.documentElement;
	var result = response.getElementsByTagName('result')[0].firstChild.data;
	parseResultValid(result);
}


function parseResultValid(result)
{
	var vtext;
	var vimg;
	var vcolor;
	var vsave;
	if(result)
	{
		if(result == 2)
		{
			vtext = " Ошибка! Не верный формат ввода данных!";
			vimg = "<img src='/img/required_invalid.gif' border='0' alt='' width='16' hright='16' title='" + vtext + "'>";
			vcolor = 'red';
			vsave = 0;
		}
		else if(result == 3)
		{
			vtext = " Ошибка! Логин, который Вы выбрали уже занят, попробуйте выбрать другой!";
			vimg = "<img src='/img/required_invalid.gif' border='0' alt='' width='16' hright='16' title='" + vtext + "'>";
			vcolor = 'red';
			vsave = 0;
		}
		else if(result == 4)
		{
			vtext = " Ошибка передачи данных!";
			vimg = "<img src='/img/required_invalid.gif' border='0' alt='' width='16' hright='16' title='" + vtext + "'>";
			vcolor = 'red';
			vsave = 0;
		}
		else if(result == 5)
		{
			vtext = " Ошибка! Пароль должен быть длиной от 5 до 15 символов!";
			vimg = "<img src='/img/required_invalid.gif' border='0' alt='' width='16' hright='16' title='" + vtext + "'>";
			vcolor = 'red';
			vsave = 0;
		}
		else if(result == 6)
		{
			vtext = " Ошибка! Вы ошиблись при повторном вводе пароля!";
			vimg = "<img src='/img/required_invalid.gif' border='0' alt='' width='16' hright='16' title='" + vtext + "'>";
			vcolor = 'red';
			vsave = 0;
		}
		else if(result == 7)
		{
			vtext = " Ошибка! Длина логина - должна быть до 50 символов.";
			vimg = "<img src='/img/required_invalid.gif' border='0' alt='' width='16' hright='16' title='" + vtext + "'>";
			vcolor = 'red';
			vsave = 0;
		}
		else if(result == 8)
		{
			vtext = " Ошибка! Ваше имя (ник) пользователя содержит недопустимые символы или первый символ не буква!";
			vimg = "<img src='/img/required_invalid.gif' border='0' alt='' width='16' hright='16' title='" + vtext + "'>";
			vcolor = 'red';
			vsave = 0;
		}
		else if(result == 9)
		{
			vtext = " Ошибка! Неправильный E-Mail адрес. Правильный адрес должен выглядеть как <I>username</I><B>@</B><I>provider.com</I>";
			vimg = "<img src='/img/required_invalid.gif' border='0' alt='' width='16' hright='16' title='" + vtext + "'>";
			vcolor = 'red';
			vsave = 0;
		}
		else if(result == 10)
		{
			if(document.register_form.email)
				var uemail = document.register_form.email.value
			vtext = " Пользователь с таким Email: " + uemail + " уже есть, введите другой Email!";
			vimg = "<img src='/img/required_invalid.gif' border='0' alt='' width='16' hright='16' title='" + vtext + "'>";
			vcolor = 'red';
			vsave = 0;
		}
		else if(result == 11)
		{
			if(document.amneziy.email)
				var uemail = document.amneziy.email.value;
			vtext = " Данный Email: " + uemail + " не содержится в базе данных сайта!";
			vimg = "<img src='/img/required_invalid.gif' border='0' alt='' width='16' hright='16' title='" + vtext + "'>";
			vcolor = 'red';
			vsave = 0;
		}
		else if(result == 1)
		{
			vtext = " OK";
			vimg = "<img src='/img/validated.gif' border='0' alt='' width='16' hright='16' title='" + vtext + "'>";
			vcolor = 'green';
			vsave = 1;
		}
	}

	var d = $(fieldd + "_valid");
	//d.style.left = (f.scrollWidth + 30) + "px";
	//d.style.top = (f.offsetTop + 10) + "px";
	d.innerHTML = vimg + vtext;
	d.style.color = vcolor;
	setTimeout('ClearValidText("' + vimg + '")', 2000);
}


function ClearValidText(img)
{
	var d = $(fieldd + "_valid");
	d.innerHTML = img;
}


// Voting

function VotingAdd(action, item)
{
	itemd = item;
	new Ajax.Request('/tools/voting.php', {method: 'post', asynchronous: true, parameters:'action=' + action + '&item=' + item, onSuccess:ResultVoting, onFailure:ErrorView});
}


var ResultVoting = function(req)
{
	var response  = req.responseXML.documentElement;

	for(var c=0; c<response.childNodes.length; c++)
	{
		if(response.childNodes[c].tagName == 'errors')
		{
			var error = response.getElementsByTagName('errors')[0].firstChild.data
			if(error == 1)
				alert("Вы не можете голосовать!");
			else if(error == 2)
				alert("Вы уже голосовали за этот прикол!");
		}
		else if(response.childNodes[c].tagName == 'result')
		{
			var result = response.getElementsByTagName('result')[0].firstChild.data;
			if(result)
				$('rating_val' + itemd).innerHTML = result;

		}
		else if(response.childNodes[c].tagName == 'plus')
			$('rating_img' + itemd).setAttribute('src', ImgPath + "img/ret_up.gif");
		else if(response.childNodes[c].tagName == 'minus')
			$('rating_img' + itemd).setAttribute('src', ImgPath + "img/ret_dw.gif");
	}
}


window.onresize = DocResize;
document.onmouseclick = PosWin;
