// <![CDATA[

var Containers = new Object();

/*
	zainicjalizowanie kontenerow
*/
function createContainers() {
	Containers 			= new Object();
	Containers.new_id 	= 0;
	Containers.lock 	= 0;
	Containers.action   = '';
	Containers.parent   = 0;

	Position.includeScrollOffsets = true;
	startContainer('main');
	startContainer('move');
	startContainer('copy');

}

/*
	sklonowanie elementu listy uzywane przy kopiowaniu elementow
*/
function cloneContainers() {
	// trzeba sklonowac element i podmienic im identyfikatory, tak zeby ten w kontnerze glownym mial swoj stary id i stare miejsce
	var tmp_id 		= Containers.id_moved_element;
	var content 	= $('cont_'+tmp_id).innerHTML;
	var new_content = (content.indexOf('</a>') > -1) ? '<span class="browse">&raquo;</span>'+content.substring(content.indexOf('</a>')+4) : content;

	Containers.new_id++;
	var new_id = 'cont_'+Containers.id_moved_element
	for (var i = 0; i < Containers.new_id; i++) new_id += 'x';
	Element.update('cont_'+tmp_id, new_content);
	$('cont_'+tmp_id).id = new_id;

	if (Containers.last_position != 0) {
		new Insertion.After('cont_'+Containers.element_before, "<li id='cont_"+tmp_id+"'>"+content+"</li>");
	}
	else {
		new Insertion.Top('page-maincontainer', "<li id='cont_"+tmp_id+"'>"+content+"</li>");
	}
	$(new_id).style.backgroundColor = '#CCFF99';
}

/*
	uruchomienie pojedynczego kontenera
*/
function startContainer(elem) {
	var c = (Containers.action == '') ? ["page-maincontainer","page-movecontainer","page-copycontainer"] :
		(Containers.action == 'move') ? ["page-maincontainer", "page-movecontainer"] : ["page-maincontainer", "page-copycontainer"];

	Sortable.create("page-"+elem+"container", {
		starteffect: myStartEffect,
		dropOnEmpty: true,
		handle: 'handle',
		containment: c,
		constraint: false,
		onUpdate: function() {
			if (Containers.lock == 0) {
				Containers.lock = 1;
				setTimeout("refreshContainers('"+elem+"');", 500);
			}
		}
	});
}

/*
	sprawdzenie jakie sa zmiany w kontenerach pomiedzy poczatkiem akcji przenoszenia elementu do jego zakonczenia
*/
function setChanges() {
	if (Containers.id_moved_element != 'undefined') {

		// trzeba odczytac id elementu poprzedniego jesli ulegla zmianie lista main
		Containers.last_position = Containers.main_.indexOf(Containers.id_moved_element);

		if (Containers.action != 'move') {
			//  main -> copy
			if ((Containers.main_.length > Containers.main_new.length) && (Containers.copy_.length < Containers.copy_new.length)) {
				// ustawiamy akcje na copy i przyslaniamy elementy copy
				if (Containers.action != 'copy') {
					Containers.action = 'copy';
					$('div-movecontainer').hide();
				}

				if (Containers.last_position != 0) {
					Containers.element_before = Containers.main_[Containers.last_position - 1];
				}
				sru(navid+'/hierarchy-maintocopy/', '&idmoved='+Containers.id_moved_element+'&main='+Containers.main_+'&parent='+Containers.parent);
			}
			//  copy -> main
			if ((Containers.main_.length < Containers.main_new.length) && (Containers.copy_.length > Containers.copy_new.length)) {
				sru(navid+'/hierarchy-copytomain/', '&idmoved='+Containers.id_moved_element+'&main='+Containers.main_new+'&parent='+Containers.parent);
			}
		}

		if (Containers.action != 'copy') {
			//  main -> move
			if ((Containers.main_.length > Containers.main_new.length) && (Containers.move_.length < Containers.move_new.length)) {
				// ustawiamy akcje na move i przyslaniamy elementy copy
				if (Containers.action != 'move') {
					Containers.action = 'move';
					$('div-copycontainer').hide();
				}
				var content 	= $('cont_'+Containers.id_moved_element).innerHTML;
				var new_content = (content.indexOf('</a>') > -1) ? '<span class="browse">&raquo;</span>'+content.substring(content.indexOf('</a>')+4) : content;
				Element.update('cont_'+Containers.id_moved_element, new_content);

				sru(navid+'/hierarchy-maintomove/', '&idmoved='+Containers.id_moved_element+'&main='+Containers.main_new+'&parent='+Containers.parent);
			}
			//  move -> main
			if ((Containers.main_.length < Containers.main_new.length) && (Containers.move_.length > Containers.move_new.length)) {
				sru(navid+'/hierarchy-movetomain/', '&idmoved='+Containers.id_moved_element+'&main='+Containers.main_new+'&parent='+Containers.parent);
			}
			if ($('cont_'+Containers.id_moved_element)) $('cont_'+Containers.id_moved_element).style.backgroundColor = '#FFFF99';
		}

		//  main -> main
		if ((Containers.main_.length == Containers.main_new.length) && (Containers.main_.join() != Containers.main_new.join()) && Containers.action != 'copy') {
			Containers.action = 'move';
			$('div-copycontainer').hide();
			sru(navid+'/hierarchy-maintomain/', '&idmoved='+Containers.id_moved_element+'&main='+Containers.main_new+'&parent='+Containers.parent);
			if ($('cont_'+Containers.id_moved_element)) $('cont_'+Containers.id_moved_element).style.backgroundColor = '#FFFF99';
		}
	}
}

/*
	odswiezenie stanow kontenerow - konieczne przy wpisywaniu do nich nowych tresci
*/
function refreshContainers(elem) {
	setNewContainers();
	setChanges();

	if (Containers.action != 'move') {
		// trzeba obczaic czy jest kopiowany czy tylko przenoszony z kontenera copy do innego (na podstawie dlugosci kontenerow)
		if (elem && (Containers.copy_.length < Containers.copy_new.length)) {
			cloneContainers();
		}
	}

	Sortable.destroy("page-maincontainer");
	startContainer('main');
	if (Containers.action != 'copy') {
		Sortable.destroy("page-movecontainer");
		startContainer('move');
	}
	if (Containers.action != 'move') {
		Sortable.destroy("page-copycontainer");
		startContainer('copy');
	}
	Containers.lock = 0;
}

/*
	odczytanie wartosci danego kontenera
*/
function unserializeSortable(cont) {
	var serialized = new String(Sortable.serialize("page-"+cont+'container'));
	var tab = new Array();
	if (serialized == '') return tab;
	serialized.split('&').each(function(v,i){
		tab.push(v.split('=')[1]);
	});
	return tab;
}

/*
	zapisanie do lokalnych zmiennych poczatkowych wartosci wszystkich kontenerow
*/
function setContainers(event_id) {
	// zapisanie pozycji kontenerow, oraz id elementu powodujacego zdarzenie
	Containers.id_moved_element = event_id.split('_')[1];
	Containers.main_ = unserializeSortable('main');
	Containers.move_ = unserializeSortable('move');
	Containers.copy_ = unserializeSortable('copy');
}

/*
	jak wyzej, z tym ze wykonywane po zmianie elementow
*/
function setNewContainers() {
	Containers.main_new = unserializeSortable('main');
	if (Containers.action != 'copy') Containers.move_new = unserializeSortable('move');
	if (Containers.action != 'move') Containers.copy_new = unserializeSortable('copy');
}

/*
	efekt powodujacy zapalenie sie elementu listy w momencie zaznaczenia
*/
var myStartEffect = function(element) {
	element._opacity = Element.getOpacity(element);
	new Effect.Opacity(element, { duration: 0.2, from: element._opacity, to: 0.7 });
	new Effect.Highlight(element, {});
}

// ]]>
