﻿if (myTabbar.Tabs['photo']) {
	myTabbar.Tabs['photo'].onactivate = function () {
		var upload = getObj('upload');
		if (upload) upload.style.display = 'block';
		this.cont.parentNode.style.height = '400px';
	}
	myTabbar.Tabs['photo'].unactivate = function () {
		var upload = getObj('upload');
		if (upload) upload.style.display = 'none';
		this.cont.parentNode.style.height = '';
	}
}


refreshImages();




var ISDRAGPHOTO;
var SEPARATOR = false;
var DRAGDROP;
var DRAGITEM;
var SIDE;
var PHOTOITEM;
var PHOTOITEMS=[];

var ACTIVEPHOTO = null;




function refreshImages() {
	getByXajax(['users', 'myprofile_loading'],'page=xa_myprofile_refresh_images;nick='+nick + ';usertype=' + usertype);
}

function setImages(data)
{
	var cont = document.getElementById('mini_photos');
		cont.innerHTML = '';
		cont.parentNode.parentNode.style.overflow = 'hidden';

	for (var i=0; i<data.length; i++) {
		var item = data[i];
		var src = item.name.replace('.', '-1.');
		var src2 = item.name.replace('.', '-2.');
		var itemobj = document.createElement('LI');
		var itemimg = document.createElement('DIV');
			itemobj.title = item.title;
			itemobj.id = "photo" + item.id;
			itemobj.setAttribute('photoid', item.id);
			itemimg.style.background = "url('"+src2+"') center center no-repeat";

			itemimg = itemobj.appendChild(itemimg);
		
		PHOTOITEMS[item.id] = item;
		
		var libr = document.createElement('LI');
			libr.className = "libr";
			libr.innerHTML = "<div style='width:100%; height:100%'></div>";
			
		var itemobj = cont.appendChild(itemobj);
		var libr = cont.appendChild(libr);
		
		item.src = src;
		
			(function (itemobj, img, libr, item) {
				
				itemobj.onmouseover = function (e) {overPhoto (itemobj, true);}
				// itemobj.onmousemove = function () {overPhoto (itemobj, true)}
				itemobj.onmouseout = function () {overPhoto (itemobj, false)}
				
				addEvent(itemobj, 'mousedown', function (e) {dragPhoto (e, itemobj, img, item)});
				
				addEvent(libr, 'mouseover', function () {selectSep(libr, itemobj)});
				addEvent(libr, 'mousemove', function () {selectSep(libr, itemobj)});
				
			}) (itemobj, itemimg, libr, item)
		
		if (!i) showPhoto(item);
	}
	
	if (CANEDIT) {
		var dragli = document.createElement('LI');
		dragli = cont.appendChild(dragli);
		dragli.id = 'movableNode';
	
		setDragDrop();
		initPhotoTitle ();
	}
}

	
	function showPhoto (item)
	{
		if (!item) return;
		var prev = document.getElementById('showing');
			prev.style.backgroundImage = 'url(' + item.src + ')';
			prev.setAttribute('photoid', item.id);

		if (ACTIVEPHOTO) ACTIVEPHOTO.className = '';
		ACTIVEPHOTO = document.getElementById('photo'+item.id);
		if (ACTIVEPHOTO) ACTIVEPHOTO.className = 'active';
		
		insertPhotoTitle(item.title ? item.title : CANEDIT ? 'Щелчек - ввести имя' : '');
		
		if (CANEDIT) getObj('delete_photo').style.display = 'block';
		getObj('title_spans').style.display = 'block';
	}
	
	
	function overPhoto (obj, is)
	{
		if (ISDRAGPHOTO) return;
		if (is && obj.className=='hover') return;
		if (is) obj.className +=  ' hover'
			else obj.className = obj.className.replace(/hover/g, '');
	}
	
	
	function deletePhoto ()
	{
		var prev = document.getElementById('showing');
		var id = prev.getAttribute('photoid');
			prev.style.backgroundImage = '';
		
		if (!id) return;
		
		getByXajax(['users', 'myprofile_loading'],'page=xa_myprofile_delete_photo;nick='+nick + ';usertype=' + usertype + ';id=' + id);
		savePhotoPosition ();
		getObj('upload').src = getObj('upload').src;
		
		getObj('delete_photo').style.display = 'none';
		getObj('title_spans').style.display = 'none';
		getObj('title_inputs').style.display = 'none';
	}

	
	function savePhotoPosition ()
	{
		var cont = document.getElementById('mini_photos');
		var childs = cont.getElementsByTagName('LI');
		var ids = '';
		
		for (var i=0; i<childs.length; i++) {
			var id = childs[i].getAttribute('photoid');
			if (id) ids+= (id + '&');
		}
		
		ids += '';

		getByXajax(['users', 'myprofile_loading'],'page=xa_myprofile_savephotoposition;ids=' + ids);
	}
	
	
	function dragPhoto (e, itemobj, img, item)
	{
		if (!CANEDIT) return showPhoto (item);
		PHOTOITEM = item;
		DRAGITEM = itemobj;
		DRAGDROP.obj.style.display = 'block';
		var cont = getObj('mini_photos');

		var left = itemobj.offsetLeft;// - cont.scrollLeft;
		var top = itemobj.offsetTop;// - cont.scrollTop;
		
		DRAGDROP.obj.appendChild(img);
		DRAGDROP.obj = itemobj.parentNode.insertBefore(DRAGDROP.obj, itemobj);
		
		DRAGDROP.obj.style.top = top + 'px';
		DRAGDROP.obj.style.left = left + 'px';
		DRAGDROP.initMove(e);

		ISDRAGPHOTO = true;
	}
	
	
	function selectSep (li) {
		if (!ISDRAGPHOTO) return;
		if (SEPARATOR) SEPARATOR.className = 'libr';
		SEPARATOR = li;
		if (SEPARATOR) SEPARATOR.className = ' librhover libr';
	}
	
	
	
	
	
	function setDragDrop ()
	{
		DRAGDROP = new DragDrop ('movableNode', 'Y');
		DRAGDROP.init(getObj('mini_photos'));

		DRAGDROP.onMoveStop = function ()
		{
			this.obj.style.display = 'none';
			
			stopDragPhoto();
		}
	}
	
	function stopDragPhoto ()
	{
		if (SEPARATOR) {
			var libr = DRAGITEM.nextSibling;
			var UL = SEPARATOR.parentNode;
			var temp = document.createElement('LI');
				temp = UL.insertBefore(temp, DRAGITEM);
				temp.style.background = "#fff";
			
			UL.insertBefore(libr, SEPARATOR);
			UL.insertBefore(DRAGITEM, SEPARATOR);
			
			var height = DRAGITEM.offsetHeight;
			var tempDI = DRAGITEM;
			DRAGITEM.style.height = '0px';
			DRAGITEM.style.overflow = 'hidden';

			(function () {
				var hg = tempDI.offsetHeight + 10;
				tempDI.style.height = hg + 'px';

				temp.style.height = height - hg + 'px';

				if (hg < height) setTimeout(arguments.callee, 10)
					else {
						tempDI.style.height = '';
						tempDI.style.overflow = '';
						UL.removeChild(temp);
					}
			}) ()
			
			savePhotoPosition ();
		} else {
			showPhoto (PHOTOITEM)
		}
		
		if (DRAGITEM) DRAGITEM.appendChild(DRAGDROP.obj.getElementsByTagName('DIV')[0]);
		SEPARATOR.className = 'libr';
		SEPARATOR = false;
		ISDRAGPHOTO = false;
		DRAGITEM = false;
	}
	
	
	function initPhotoTitle ()
	{
		getObj('title').parentNode.onclick = setPhotoTitle;
		getObj('title_ok').onclick = savePhotoTitle;
		getObj('title_cancel').onclick = cancelPhotoTitle;
		getObj('title').title = "Щелчок - изменить";
	}
	
	
	
	function setPhotoTitle ()
	{
		var id = getObj('showing').getAttribute('photoid');

		getObj('title_write').value = PHOTOITEMS[id].title;
		getObj('title').style.display = 'none';
		getObj('title_inputs').style.display = 'block';
		getObj('title_write').focus();
	}
	
	function insertPhotoTitle(title) {
		getObj('title').innerHTML = title;
	}
	
	
	function savePhotoTitle ()
	{
		var title = getObj('title_write').value;
		getObj('title').innerHTML = title;
		cancelPhotoTitle ();
		
		var id = getObj('showing').getAttribute('photoid');
		PHOTOITEMS[id].title = title;

		if (!id) return;
		getByXajax(['users', 'myprofile_loading'],'page=xa_myprofile_savetitle;id=' + id + ';title=' + encodeURIComponent(title));
	}
	
	
	function cancelPhotoTitle ()
	{
		getObj('title').style.display = 'block';
		getObj('title_inputs').style.display = 'none';
	}
