/*
		UWAGA wszystkie obiekty, tablice i funkcje sÂš kopiowane przez referencje - ZAWSZE!!!
*/

downloaditems = [];
currentdownload = -1;

function download(name, path){
	var items = document.getElementsByName(name + '[]');
	for(var i = 0; i < items.length; i++){
		if(items[i].checked == true){
  			downloaditems[downloaditems.length] = path + '?id=' + items[i].value;
//			downloadItem(path + '?id=' + items[i].value);
//			window.location.href = path + '?id=' + items[i].value;
//			alert(path + '?id=' + items[i].value);
		}
	}
//	currentdownload = 0;
	downloadItem(0);
}

function downloadItem(nr){
//	if(currentdownload == -1) return;
	if(downloaditems[/*currentdownload*/nr] != undefined){
		window.location.href = downloaditems[/*currentdownload*/nr];
	}
	if(downloaditems[/*currentdownload*/nr + 1] != undefined){
		window.onfocus = function(){
			setTimeout(function(){downloadItem(nr + 1);}, 250);
//			downloadItem(nr + 1);//w firefoxie przeskakuje na ostatnie
		};
	} else{
//		alert('over');
//		window.onblur = null;
		window.onfocus = null;
//		currentdownload = -1;
		downloaditems = [];
	}
}



/*
	Browser
*/
function Browser(direction, size, scroll, url, count, start){
	var self = this;

	this.direction = (direction == 'v') ? 'v' : 'h';
	this.size = size;
	this.scroll = scroll;
	this.gap = 4;
	this.url = url;
	this.step = 10;
	this.delay = 20;
	var count = count;
	
	this.id = '';
	
	var wrapper = getObject('wrapper');
	var button_prev = getObject('browser_prev');
	var button_next = getObject('browser_next');
	var items = [];
	for(var i = 0/*start*/; i < /*start + */size[0] * size[1]; i++) items[i] = getObject('thumb[' + i + ']');
	var current = [];
	var timeout = [null, null];
	if(start != undefined){
		this.first = start;
//		this.getImages(this.start, true);
	} else this.first = 0;
	this.last = this.first + size[0] * size[1] - 1;
	var cache = [];
	var j = 0;
	for(i = this.first; i <= this.last; i++) cache[i] = items[j++];

	var acquired = 0;
	var inprogress = false;
	
	/*if(this.first == 0) button_prev.disabled = 'disabled';
	if(count < size[0] * size[1] || this.last == count - 1) button_next.disabled = 'disabled';*/
	if(this.first == 0) button_prev.style.visibility = 'hidden';
	if(count < size[0] * size[1] || this.last == count - 1) button_next.style.visibility = 'hidden';

	
	/*this.init = function(){
		var temp = this.scroll;
//		this.first 
		this.getImages(this.first - 1, true);
		this.scroll	= temp;
	}*/
	
	this.getImages = function(start/*, init*/){
//		alert(start);
		if(inprogress) return;
		if(timeout[0]/*[nr]*/ != null || timeout[1]/*[nr]*/ != null) return;
		//if(timeout1.length || timeout2.length) return;
		if((this.last >= count - 1 && start > this.first) || (this.first <= 0 && start < this.first)) return;
		//alert(cache[nr]);
		var acquire = false;
// 1 - poprawione juÂ¿ (start i end pokazuja dokladny numer elementow do pobrania: <start;end>)
		var end;
		if(start < this.first){
			if(start < 0) start = 0;
			//to tylko w 2 wymiarowych jakby od jakiegos konkretnego zaczynac
			/*if(this.direction == 'v'){
				if(start % this.size[0] != 0) start -= $options['size'][0] - (start % this.size[0]);
			} else{
				if(start % this.size[1] != 0) start -= $options['size'][1] - (start % this.size[1]);
			}*/
//			alert(start);
			end = start + this.scroll;
			if(end > this.first - 1) end = this.first - 1;
			//acquired = this.first - /*Math.abs(*/start/*)*/;
		} else{
			end = ((count - 1 < this.last + this.scroll) ? count - 1 : this.last + this.scroll);
//			alert(end);
			//to tylko w 2 wymiarowych (raczej)
			if(this.direction == 'v'){
//				if((end + 1) % this.size[0] != 0) end += $options['size'][0] - ((end + 1) % this.size[0]);
				if((end + 1) % this.size[0] != 0) end += this.size[0] - ((end + 1) % this.size[0]);
			} else{
//				if((end + 1) % this.size[1] != 0) end += $options['size'][1] - ((end + 1) % this.size[1]);
				if((end + 1) % this.size[1] != 0) end += this.size[1] - ((end + 1) % this.size[1]);
			}
//			alert(end);
			start = end - this.scroll;
			if(start < this.last + 1) start = this.last + 1;
			//acquired = end - ((this.scroll == this.size[0]) ? start : this.last);
		}
//		alert(start + ' ' + end);
		acquired = end - start + 1;
//		alert(acquired);

		/*if(start == 0) button_prev.disabled = 'disabled';
		else if(button_prev.disabled) button_prev.disabled = '';
		
		if(count < this.size[0] * this.size[1] || end == count - 1) button_next.disabled = 'disabled';
		else if(button_next.disabled) button_next.disabled = '';*/
		if(start == 0) button_prev.style.visibility = 'hidden';
		else if(button_prev.style.visibility == 'hidden') button_prev.style.visibility = '';
		
		if(count < this.size[0] * this.size[1] || end == count - 1) button_next.style.visibility = 'hidden';
		else if(button_next.style.visibility == 'hidden') button_next.style.visibility = '';

		var j = 0;
		for(var i = start; i <= end; i++){
//			alert(i);
			if(cache[i] != undefined){
				current[j] = cache[i];
//				alert(current[i]);
				j++;
			} else if(i >= count) current[i] = createEmpty();
			else{
				acquire = true;
				break;
			}
			//alert(current[i]);
		}
//		return;
		if(!acquire){
//			alert('from cache');
			/*if(init != undefined && init) this.showInitial();
			else */if(start < this.first) this.showPrev();
			else this.showNext();
		} else{
			//alert(acquire);
			//alert(nr);
			//alert((this.size[0] - this.scroll + (nr * this.scroll)));
			inprogress = true;
			ajax.get({
				url: url + '&epp=' + /*acquired*/((start < self.first) ? ((i - 24 < 0) ? 25 + i - 24 : 25) : 25) + '&nr=' + /*i*/((start < self.first) ? ((i - 24 < 0) ? 0 : i - 24) : i),
				//async: false,
				onSuccess: function(obj){
//					alert(obj.responseText);
					if(obj.responseText == 'ERROR'){
						alert('An error occurred.');
						return;
					}
					/*result = */eval('result = ' + obj.responseText + ';');
//					alert(result);
//					alert(result.length);
					/*var qwe = window.open();
					qwe.document.write(obj.responseText);
					qwe.document.close();*/
					
					if(start < self.first){
						result = result.reverse();
						//alert('asd');
					}
					
					for(var i = 0; i < 25/*acquired*/; i++){
						if(result[i] != undefined){
							var temp = document.createElement('a');				
							temp.setAttribute('href', result[i].href + (self.id != '' ? '#' + self.id : ''));
							temp.setAttribute('title', result[i].title);
							temp.style.position = 'absolute';
							/*var temp2 = result.test2;
							temp.innerHTML = temp2;*/
							var temp2 = document.createElement('img');
							temp2.setAttribute('src', result[i].src + '_4.jpg');
							temp2.setAttribute('alt', result[i].alt);
							temp2.setAttribute('width', '75');
							temp2.setAttribute('height', '75');
							temp2.setAttribute('class', 'img');
							temp.appendChild(temp2);
							temp2 = null;
						} else{// a powinno byäŸ´ak ze nie przesuwa sièŸ¤alej jak juÂ¿ nic nie ma
							var temp = createEmpty();
						}
						//alert(((start < self.first) ? start + i : self.last + 1 + i));
//to tez do sprawdzenia
//						alert(start + i);
						if(start < self.first){
							cache[((start < self.first) ? start : self.last + 1) - i] = temp;
						} else{
							cache[((start < self.first) ? start : self.last + 1) + i] = temp;
						}
						current[i] = temp;
						//alert(current[i]);
						temp = null;
					}
					inprogress = false;
					/*if(init != undefined && init) self.showInitial();
					else */if(start < self.first) self.showPrev();
					else self.showNext();
				}/*,
				onError: function(objr){
					alert(objr.responseText);
				}*/
			});
		}
		
		function createEmpty(){
			var temp = document.createElement('div');
			temp.style.border = '1px black solid';
			temp.style.position = 'absolute';
			var temp2 = document.createElement('div');
			temp2.style.width = '73px';
			temp2.style.height = '73px';
			temp2.style.fontSize = '7pt';
			temp2.style.textAlign = 'center';
			temp2.innerHTML = '<br /><br />' + lang['noimage'] + '.';
			temp.appendChild(temp2);
			temp2 = null;
		
			return temp;
		}
	}
	
	/*this.showInitial = function(){
		var temp = 0;
		for(var i = 0; i < this.size[1]; i++){
			for(var j = 0; j < this.size[0]; i++){
				if(this.direction == 'v') temp = i * this.size[0] + j;
				else temp = i + j * this.size[1];
				alert(temp);
				current[temp].style.top = (i * 79) + 'px';
				current[temp].style.left = (j * 79) + 'px';
				wrapper.appendChild(current[temp]);
			}		
		}
	}*/

	this.showNext = function(){
		if(inprogress) return;
//		if(this.last >= count - 1) return;

		var step = Math.ceil((75 + this.gap) / 8);
		
		if(this.direction == 'h'){
		
			var first = parseInt(items[0].style.left);
			var start = 0;
			if(first == -(75 + this.gap)){
				var i = 0;
				while(i < items.length - 1 && parseInt(items[i].style.left) == -(75 + this.gap)) i++;
				first = parseInt(items[i].style.left);
				start = i;
				//alert(start);//nie moÂ¿e byäŸ²owne scroll
			}
			var last = parseInt(items[this.size[0] - 1].style.left);
			//alert(start);
			var temp;
			if(last % (75 + this.gap) == 0 && last >= (this.size[0] - acquired) * (75 + this.gap)){
				//alert(start);
				//alert(this.size[0] - 1 - last / (75 + this.gap));//moÂ¿na na start zamienic
				current[acquired - 1 - first / (75 + this.gap)].style.top = '0px';
				current[/*start*/this.size[0] - 1 - last / (75 + this.gap)].style.left = ((75 + this.gap) * this.size[0]) - 1 + 'px';
				current[/*start*/this.size[0] - 1 - last / (75 + this.gap)].style.clip = 'rect(0px, 0px, 75px, 0px)';
				wrapper.appendChild(current[/*start*/this.size[0] - 1 - last / (75 + this.gap)]);
			}
	//		alert((this.size[0] - acquired) * (75 + this.gap) - 75);
			if(last > (this.size[0] - acquired - this.size[1]) * (75 + this.gap)/* - 75*//* + (this.size[0] - acquired) * this.gap*/){
	// 2 - do sprawdzenia
	//			alert('test');
				if(first > /*((this.size[0] - this.scroll - 1) * (75 + this.gap))*/ -(75 + this.gap)){
					temp = (first - step);
					if(temp < -(75 + this.gap)) temp = -(75 + this.gap);
					items[start].style.left = temp + 'px';
					temp = Math.abs(temp);
					if(temp > 75) temp = 75;
					items[start].style.clip = 'rect(0px, 75px, 75px, ' + temp + 'px)';
				}
				
				if(start + 1 < items.length){
					temp = (parseInt(items[start + 1].style.left) - step);
					if(temp < 0) temp = 0;
					var j = 0;
					for(var i = start + 1; i < this.size[0]; i++){
						items[i].style.left = (temp + (j * (75 + this.gap))) + 'px';
						j++;
					}
				}
				
				for(i = 0; i < start + 1; i++){
					temp = (parseInt(current[i].style.left) - step);
					if(temp < (this.size[0] - 1 - (start - i)) * (75 + this.gap)) temp = (this.size[0] - 1 - (start - i)) * (75 + this.gap);
					current[i].style.left = temp + 'px';
					temp = Math.abs(this.size[0] * 75 + (this.size[0] - 1) * this.gap - temp);
					if(temp > 75) temp = 75;
					current[i].style.clip = 'rect(0px, ' + temp + 'px, 75px, 0px)';
				}
				
				temp = null;
				
				timeout[0]/*[nr]*/ = setTimeout(function(){self.showNext();}, this.delay);
			} else{
				clearTimeout(timeout[0]/*[nr]*/);
				//delete timeout1[nr];
				timeout[0]/*[nr]*/ = null;
				for(i = 0; i < acquired; i++) wrapper.removeChild(items[i]);
				for(i = 0; i < this.size[0] - acquired; i++) items[i] = items[i + acquired];
				var j = 0;
				for(i = this.size[0] - acquired; i < this.size[0]; i++){
					items[i] = current[j];
					current[j++] = null;
				}
	//			for(i = 0; i < this.size[0]; i++) alert(items[i]);
				this.first += acquired;
				this.last += acquired;
	//			alert(this.first + ' ' + this.last);
			}
			
		} else{
		
			var first = parseInt(items[0].style.top);
			var start = 0;
			if(first == -(75 + this.gap)/* && items[this.size[0]].style.top < 0*/){
				var i = 0;
				while(i < items.length - 1 && parseInt(items[i].style.top) == -(75 + this.gap)) i++;
				first = parseInt(items[i].style.top);			
				start = i;
				//alert(start);//nie moÂ¿e byäŸ²owne scroll
			}
			var last = parseInt(items[this.size[1] - 1].style.top);
//			alert(start);
			var temp;
			if(last % (75 + this.gap) == 0 && last >= (this.size[1] - acquired) * (75 + this.gap)){
				//alert(start);
				//alert(this.size[0] - 1 - last / (75 + this.gap));//moÂ¿na na start zamienic
				current[acquired - 1 - first / (75 + this.gap)].style.left = '0px';
				current[/*start*/this.size[1] - 1 - last / (75 + this.gap)].style.top = ((75 + this.gap) * this.size[1]) - 1 + 'px';
				current[/*start*/this.size[1] - 1 - last / (75 + this.gap)].style.clip = 'rect(0px, 75px, 0px, 0px)';
				wrapper.appendChild(current[/*start*/this.size[1] - 1 - last / (75 + this.gap)]);
			}
	//		alert((this.size[0] - acquired) * (75 + this.gap) - 75);
			if(last > (this.size[1] - acquired - this.size[0]) * (75 + this.gap)/* - 81*//*(67 + this.gap)*//* + (this.size[0] - acquired) * this.gap*/){
//			if(parseInt(items[start + 1].style.top) > 0){
	// 2 - do sprawdzenia
				//alert('test');
				if(first > /*((this.size[0] - this.scroll - 1) * (75 + this.gap))*/ -(75 + this.gap)){
					temp = (first - step);
					if(temp < -(75 + this.gap)) temp = -(75 + this.gap);
					items[start].style.top = temp + 'px';
					temp = Math.abs(temp);
					if(temp > 75) temp = 75;
					items[start].style.clip = 'rect(' + temp + 'px, 75px, 75px, 0px)';
				}
				
				if(start + 1 < items.length){
					temp = (parseInt(items[start + 1].style.top) - step);
					if(temp < 0) temp = 0;
					var j = 0;
					for(var i = start + 1; i < this.size[1]; i++){
						items[i].style.top = (temp + (j * (75 + this.gap))) + 'px';
						j++;
					}
				}
				
				for(i = 0; i < start + 1; i++){
					temp = (parseInt(current[i].style.top) - step);
					if(temp < (this.size[1] - 1 - (start - i)) * (75 + this.gap)) temp = (this.size[1] - 1 - (start - i)) * (75 + this.gap);
					current[i].style.top = temp + 'px';
					temp = Math.abs(this.size[1] * 75 + (this.size[1] - 1) * this.gap - temp);
					if(temp > 75) temp = 75;
					current[i].style.clip = 'rect(0px, 75px, ' + temp + 'px, 0px)';
				}
				
				temp = null;
				
				timeout[0]/*[nr]*/ = setTimeout(function(){self.showNext();}, this.delay);
			} else{
				clearTimeout(timeout[0]/*[nr]*/);
				//delete timeout1[nr];
				timeout[0]/*[nr]*/ = null;
				for(i = 0; i < acquired; i++) wrapper.removeChild(items[i]);
				for(i = 0; i < this.size[1] - acquired; i++) items[i] = items[i + acquired];
				var j = 0;
				for(i = this.size[1] - acquired; i < this.size[1]; i++){
					items[i] = current[j];
					current[j++] = null;
				}
	//			for(i = 0; i < this.size[0]; i++) alert(items[i]);
				this.first += acquired;
				this.last += acquired;
	//			alert(this.first + ' ' + this.last);
			}
		
		}
	}

	this.showPrev = function(){
		if(inprogress) return;
//		if(this.first <= 0) return;
		
		var step = Math.ceil((75 + this.gap) / 8);
		
		if(this.direction == 'h'){
		
			var first = parseInt(items[0].style.left);
			var start = acquired - 1;
			var last = parseInt(items[this.size[0] - 1].style.left);
			if(last == this.size[0] * (75 + this.gap) - this.gap){
				var i = this.size[0] - 1;
				while(i > 0 && parseInt(items[i].style.left) == this.size[0] * (75 + this.gap) - this.gap) i--;
				last = parseInt(items[i].style.left);
				//alert(i);
				start = acquired - this.size[0] + i;
				//alert('start' + start);//nie moÂ¿e byäŸ²owne scroll
			}
			//alert(start);
			var temp;
			if(first % (75 + this.gap) == 0 && first <= (acquired - 1) * (75 + this.gap)){
				//alert(start);
				//alert(acquired - 1 - first / (75 + this.gap));//moÂ¿na na start zamienic
				current[acquired - 1 - first / (75 + this.gap)].style.top = '0px';
				current[acquired - 1 - first / (75 + this.gap)].style.left = '-' + (75 + this.gap) + 'px';
				current[acquired - 1 - first / (75 + this.gap)].style.clip = 'rect(0px, 75px, 75px, 75px)';
				wrapper.appendChild(current[acquired - 1 - first / (75 + this.gap)]);
			}
	//		alert(acquired * (75 + this.gap) - ((acquired == this.size[0]) ? this.gap : 0));
			if(first < acquired * (75 + this.gap) - ((acquired == this.size[0]) ? this.gap : 0)){
	// 3 - do sprawdzenia
	//			alert('test');
				if(last < this.size[0] * (75 + this.gap) - this.gap - 1){//do przemyslenia
					//alert(temp1);
					var temp = (last + step);
					//alert(temp);
					if(temp > this.size[0] * (75 + this.gap) - this.gap) temp = this.size[0] * (75 + this.gap) - this.gap;
					//alert(temp);
					items[this.size[0] - acquired + start].style.left = temp + 'px';
					temp = Math.abs(this.size[0] * (75 + this.gap) - this.gap - temp);
	//				if(temp < 0) temp = 0;
					items[this.size[0] - acquired + start].style.clip = 'rect(0px, ' + temp + 'px, 75px, 0px)';
				}
				
				if(start >= 0 && this.size[0] - acquired + start - 1 >= 0){
					temp = (parseInt(items[this.size[0] - acquired + start - 1].style.left) + step);
					if(temp > (this.size[0] - 1) * (75 + this.gap)) temp = (this.size[0] - 1) * (75 + this.gap);
					var j = 0;
					for(var i = this.size[0] - acquired + start - 1; i >= 0; i--){
						items[i].style.left = (temp - (j * (75 + this.gap))) + 'px';
						j++;
					}
				}
				
				for(i = acquired - 1; i >= start; i--){
					temp = (parseInt(current[i].style.left) + step);
					if(temp > (i - start) * (75 + this.gap)) temp = (i - start) * (75 + this.gap);
					current[i].style.left = temp + 'px';
					temp = Math.abs(temp);
					if(temp < 0 || (i - start > 0 && temp > 0)) temp = 0;
					current[i].style.clip = 'rect(0px, 75px, 75px, ' + temp + 'px)';
				}

				temp = null;

				timeout[1]/*[nr]*/ = setTimeout(function(){self.showPrev();}, this.delay);
			} else{
				clearTimeout(timeout[1]/*[nr]*/);
				//delete timeout2[nr];
				timeout[1]/*[nr]*/ = null;
	// 3 - ponizej jeszcze zle chyba
				for(i = this.size[0] - 1; i > this.size[0] - acquired; i--) wrapper.removeChild(items[i]);
				for(i = this.size[0] - 1; i > acquired - 1; i--) items[i] = items[i - acquired];
				//var j = acquired - 1;
				for(i = acquired - 1; i >= 0; i--){
					items[i] = current[i];
					current[i] = null;
				}
	//			for(i = 0; i < this.size[0]; i++) alert(items[i]);
				this.first -= acquired;
				this.last -= acquired;
	//			alert(this.first + ' ' + this.last);
			}
			
		} else{
		
			var first = parseInt(items[0].style.top);
			var start = acquired - 1;
			var last = parseInt(items[this.size[1] - 1].style.top);
			if(last == this.size[1] * (75 + this.gap)/* - this.gap*/){
				var i = this.size[1] - 1;
				while(i > 0 && parseInt(items[i].style.top) == this.size[1] * (75 + this.gap)/* - this.gap*/) i--;
				last = parseInt(items[i].style.top);
				//alert(i);
				start = acquired - this.size[1] + i;
				//alert('start' + start);//nie moÂ¿e byäŸ²owne scroll
			}
			//alert(start);
			var temp;
			if(first % (75 + this.gap) == 0 && first <= (acquired - 1) * (75 + this.gap)){
				//alert(start);
				//alert(acquired - 1 - first / (75 + this.gap));//moÂ¿na na start zamienic
				current[acquired - 1 - first / (75 + this.gap)].style.left = '0px';
				current[acquired - 1 - first / (75 + this.gap)].style.top = '-' + (75 + this.gap) + 'px';
				current[acquired - 1 - first / (75 + this.gap)].style.clip = 'rect(75px, 75px, 75px, 0px)';			
				wrapper.appendChild(current[acquired - 1 - first / (75 + this.gap)]);
			}
	//		alert(acquired * (75 + this.gap) - ((acquired == this.size[0]) ? this.gap : 0));
			if(first < acquired * (75 + this.gap)/* - ((acquired == this.size[1]) ? this.gap : 0)*/){
	// 3 - do sprawdzenia
	//			alert('test');
				if(last < this.size[1] * (75 + this.gap)/* - this.gap*/ - 1){//do przemyslenia
					//alert(temp1);
					var temp = (last + step);
					//alert(temp);
					if(temp > this.size[1] * (75 + this.gap)/* - this.gap*/) temp = this.size[1] * (75 + this.gap)/* - this.gap*/;
					//alert(temp);
					items[this.size[1] - acquired + start].style.top = temp + 'px';
					temp = Math.abs(this.size[1] * (75 + this.gap)/* - this.gap*/ - temp);
	//				if(temp < 0) temp = 0;
					items[this.size[1] - acquired + start].style.clip = 'rect(0px, 75px, ' + temp + 'px, 0px)';
				}
				
				if(start >= 0 && this.size[1] - acquired + start - 1 >= 0){
					temp = (parseInt(items[this.size[1] - acquired + start - 1].style.top) + step);
					if(temp > (this.size[1] - 1) * (75 + this.gap)) temp = (this.size[1] - 1) * (75 + this.gap);
					//alert(temp);
					var j = 0;
					for(var i = this.size[1] - acquired + start - 1; i >= 0; i--){
						items[i].style.top = (temp - (j * (75 + this.gap))) + 'px';
						j++;
					}
				}
				
				for(i = acquired - 1; i >= start; i--){
					temp = (parseInt(current[i].style.top) + step);
					if(temp > (i - start) * (75 + this.gap)) temp = (i - start) * (75 + this.gap);
					current[i].style.top = temp + 'px';
					temp = Math.abs(temp);
					if(temp < 0 || (i - start > 0 && temp > 0)) temp = 0;
					current[i].style.clip = 'rect(' + temp + 'px, 75px, 75px, 0px)';
				}

				temp = null;

				timeout[1]/*[nr]*/ = setTimeout(function(){self.showPrev();}, this.delay);
			} else{
				clearTimeout(timeout[1]/*[nr]*/);
				//delete timeout2[nr];
				timeout[1]/*[nr]*/ = null;
	// 3 - ponizej jeszcze zle chyba
				for(i = this.size[1] - 1; i > this.size[1] - acquired; i--) wrapper.removeChild(items[i]);
				for(i = this.size[1] - 1; i > acquired - 1; i--) items[i] = items[i - acquired];
				//var j = acquired - 1;
				for(i = acquired - 1; i >= 0; i--){
					items[i] = current[i];
					current[i] = null;
				}
	//			for(i = 0; i < this.size[0]; i++) alert(items[i]);
				this.first -= acquired;
				this.last -= acquired;
	//			alert(this.first + ' ' + this.last);
			}
		
		}		
	}
}



/*
  evenHandler
*/
var EventHandler = {//przy okazji sièŸºrobi
	funcs: [],//potem zrobic Â¿eby przypisywalo funkcje ktíª…í¿ po kolei uruchamia wszystkie eventy

	add: function(obj, name, func){
//		alert(obj.sourceIndex);
		if(obj.attachEvent){
			/*if(typeof(obj['e_on' + name + func]) != 'function'){
				obj['e_on' + name + func] = func;
				obj['e2_on' + name + func] = function(e){obj['e_on' + name + func](e);};
			}*/
			obj.attachEvent('on' + name, func/*obj['e2_on' + name + func]*/);
		} else if(obj.addEventListener){
			obj.addEventListener(name, func, false);
		} else{
			alert('An error occurred.')
		}
		/*delete obj;
		delete func;
		obj = null;
		name = null;
		func = null;*/
	},
	
	remove: function(obj, name, func){
		if(obj.detachEvent){
//			alert('ok');
			obj.detachEvent('on' + name, func/*obj['e2_on' + name + func]*/);
			/*obj['e_on' + name + func] = null;
			obj['e2_on' + name + func] = null;*/
		} else if(obj.removeEventListener){
			obj.removeEventListener(name, func, false);
		} else{
			alert('An error occurred.')
		}
		/*delete obj;
		delete func;
		obj = null;
		name = null;
		func = null;*/
	}
};



/*
	Drag & Drop
*/
//if(window.Event && document.captureEvents) document.captureEvents(Event.MOUSEMOVE);//netscape 4 (na chuj mi to)
document.onmousemove = getMousePos;
var pos = {
	x: 0,
	y: 0
};

function getMousePos(e){
	if(!e) var e = window.event || window.Event;
	if(typeof(e.pageX) != 'undefined'){
		pos.x = e.pageX;
		pos.y = e.pageY;
	} else{
		pos.x = e.clientX + document.body.scrollLeft - 2;
		pos.y = e.clientY + document.body.scrollTop - 2;
	}
//	alert(pos.x + ' ' + pos.y);
//	getObject('temp1').innerHTML = pos.y + ', ' + pos.x;
}

var DragHandler = {
//	var dd = {
	timeout: null,
	active: false,
	current: null,
	clone: false,
	relative: 0,
	fromtop: 0,
	fromleft: 0,
	parent: null,
	parenttop: 0,
	parentleft: 0,
	prevtop: 0,
	prevleft: 0,
	prevmargin: {
		top :0,
		right: 0,
		bottom: 0,
		left: 0
	},
	prevposition: '',
	prevcursor: '',
//	};

	start: function(obj, clone){
		//alert(obj.tagName);
	  /*if(selected[obj.id] != undefined){//moÂ¿e to powinno byäŸ§dzie indziej np. tam gdzie jest event dodany
			alert('You already added this image.');
			return;
		}*/
//		alert(getLeft(DropHandler.dropAreas['droparea1'][0]));
//		return;

		DragHandler.fromtop = pos.y - getTop(obj)/*obj.offsetTop*//* - obj.offsetParent.offsetTop + 1*/;
		DragHandler.fromleft = pos.x - getLeft(obj)/*obj.offsetLeft*//* - obj.offsetParent.offsetLeft*/;

		DragHandler.parenttop = getTop(obj.parentNode);
		DragHandler.parentleft = getLeft(obj.parentNode);
		DragHandler.parent = obj.parentNode;
		
		if(DragHandler.parent.style.position == 'relative') DragHandler.relative = 1;
		
		EventHandler.add(document, 'mouseup', DragHandler.MouseUp);

		/*dd.*/DragHandler.timeout = setTimeout(function(){
			//alert('dupa');
			if(clone == undefined || clone == true){
				DragHandler.clone = true;
				var temp = obj.cloneNode(true);
				temp.onmousedown = function(){return false;};
		//		temp.ondragstart = function(){return false;};
		//		temp.onselectstart = function(){return false;};

				//var temp = document.createElement('img');
				/*temp.setAttribute('id', obj.id);
				temp.setAttribute('src', obj.src);
				temp.setAttribute('alt', obj.alt);
				temp.setAttribute('width', '75');
				temp.setAttribute('height', '75');
				temp.setAttribute('class', 'img');
				
				//temp.style = obj.style;

				temp.style.border = '1px black solid';
				temp.style.zIndex = 1000;
				//temp.style.display = 'block';
				*/
	//			temp.style.border = '1px black solid';
				temp.style.margin = '0px';
				temp.style.position = 'absolute';
				temp.style.cursor = 'default';
				temp.style.zIndex = 20;//nie bardzo dziala
				
				temp.style.top = /*obj.offsetTop*/getTop(obj) - getTop(obj.parentNode) + 'px';
				temp.style.left = /*obj.offsetLeft*/getLeft(obj) - getLeft(obj.parentNode)/* - obj.style.marginLeft*/ + 'px';

				if(is.ie) temp.style.filter = 'alpha(opacity=85)';
				else temp.style.opacity = 0.85;
				
				obj.parentNode/*document.getElementsByTagName('body')[0]*/.appendChild(temp);
				
				DragHandler.current = temp;
				
				temp = null;//przez to (bez tego) sièŸ«urwa jebalo wszystko (pamiec w ie)
			} else{
				DragHandler.prevtop = getTop(obj)/*obj.offsetTop*//* - obj.offsetParent.offsetTop + 1*/;
				DragHandler.prevleft = getLeft(obj)/*obj.offsetLeft*//* - obj.offsetParent.offsetLeft*/;

				DragHandler.prevmargin.top = obj.style.marginTop;
				DragHandler.prevmargin.right = obj.style.marginRight;
				DragHandler.prevmargin.bottom = obj.style.marginBottom;
				DragHandler.prevmargin.left = obj.style.marginLeft;
				DragHandler.prevposition = obj.style.position;
				DragHandler.prevcursor = obj.style.cursor;

				//musi byäŸ·czesniej (przed position) bo inaczej zla pozycje odczyta albo wcale tego nie zrobi
				if(obj.style.top == 0){
//					alert('dupa');
					obj.style.top = getTop(obj) - getTop(obj.parentNode) + 'px';
					obj.style.left = getLeft(obj) - getLeft(obj.parentNode)/* - obj.style.marginLeft*/ + 'px';
				}
				
				obj.style.margin = '0px';
				obj.style.position = 'absolute';
				obj.style.cursor = 'default';
				obj.style.zIndex = 10;//nie bardzo dziala
				
				DragHandler.current = obj;			
			}
			/*dd.*/DragHandler.active = true;

			EventHandler.add(document, 'mousemove', DragHandler.MouseMove);

		}, 100);


	//	return false;
	},

	MouseMove: function(){
		if(/*dd.*/DragHandler.active){
			//if(DragHandler.checkVertical()) /*dd.*/DragHandler.current.style.top = pos.y - /*dd.*/DragHandler.fromtop + 'px';
			DragHandler.setTop();
			//if(DragHandler.checkHorizontal()) /*dd.*/DragHandler.current.style.left = pos.x - /*dd.*/DragHandler.fromleft + 'px';
			DragHandler.setLeft();			
		
			if(DropHandler.count > 0 && DragHandler.clone){
//				alert('ok');	
				for(var id in DropHandler.dropAreas){
					if(DragHandler.checkCovering(DropHandler.dropAreas[id][0])){
						DropHandler.dropAreas[id][1]();
					} else{
						DropHandler.dropAreas[id][2]();
					}
				}			
			}
		}
	},

	MouseUp: function(){
		if(DragHandler.timeout){
			clearTimeout(DragHandler.timeout);
			DragHandler.timeout = null;
		}

		if(DragHandler.active){
//			alert('dupa');
			EventHandler.remove(document, 'mousemove', DragHandler.MouseMove);
			EventHandler.remove(document, 'mouseup', DragHandler.MouseUp);

			//po to Â¿eby ie sièŸ®ie kaszanil (nie mogl obliczyc koordynatow DragHandler.current i przez to funkcja z DropHandler sièŸ·ogole nie wlaczala)
			if(is.ie) DragHandler.current.style.filter = 'alpha(opacity=0)';
			else DragHandler.current.style.opacity = 0;

			if(DropHandler.count > 0 && DragHandler.clone){//musi tutaj zostac bo inaczej sièŸ«azda przegladraka pieprzy
//				alert('ok');	
				for(var id in DropHandler.dropAreas){
					if(DragHandler.checkCovering(DropHandler.dropAreas[id][0])){
						DropHandler.dropAreas[id][3]();
					}
				}			
			}

			//przez jebane tooltipy sièŸ«urwa jebalo (chyba jednak nie) - po czasie podobny do ukazywanie sièŸ´ooltipow jak sièŸ°rzenioslo to sièŸªebalo
 			if(DragHandler.clone) DragHandler.parent/*document.getElementsByTagName('body')[0]*/.removeChild(/*dd.*/DragHandler.current);
			else{
				if(is.ie) DragHandler.current.style.filter = 'alpha(opacity=100)';
				else DragHandler.current.style.opacity = 1;

//wylaczone Â¿eby zostawal na miejscu			
/*				DragHandler.current.style.top = DragHandler.prevtop - DragHandler.parenttop + 'px';
				DragHandler.current.style.left = DragHandler.prevleft - DragHandler.parentleft + 'px';
				
				DragHandler.current.style.marginTop = DragHandler.prevmargin.top;
				DragHandler.current.style.marginRight = DragHandler.prevmargin.right;
				DragHandler.current.style.marginBottom = DragHandler.prevmargin.bottom;
				DragHandler.current.style.marginLeft = DragHandler.prevmargin.left;
				DragHandler.current.style.position = DragHandler.prevposition;
				DragHandler.current.style.cursor = DragHandler.prevcursor;*/
				
				DragHandler.prevtop = 0;
				DragHandler.prevleft = 0;
				DragHandler.prevmargin.top = 0;
				DragHandler.prevmargin.right = 0;
				DragHandler.prevmargin.bottom = 0;
				DragHandler.prevmargin.left = 0;
				DragHandler.prevposition = '';
				DragHandler.prevcursor = '';
			}
			
			DragHandler.fromtop = 0;
			DragHandler.fromleft = 0;
			DragHandler.parent = null;
			DragHandler.parenttop = 0;
			DragHandler.parentleft = 0;
			DragHandler.clone = false;
			DragHandler.current = null;
			DragHandler.active = false;
		}
	},
	
	checkCovering: function(obj){//tutaj jak jest batch do przeciaganie firefox pieprzy sièž¨1px w dol i w prawo)
		return (pos.y > getTop(obj) - DragHandler.current.offsetHeight + DragHandler.fromtop && pos.y < getTop(obj) + obj.offsetHeight + DragHandler.fromtop) && (pos.x > getLeft(obj) - DragHandler.current.offsetWidth + DragHandler.fromleft && pos.x < getLeft(obj) + obj.offsetWidth + DragHandler.fromleft);
	},
	
	setTop: function(){
		var top, height;
		if(window.innerHeight){
			top = window.pageYOffset;
			height = window.innerHeight;
		} else{
			top = document.body.scrollTop;
			height = document.body.clientHeight;
		}
//		getObject('temp1').innerHTML = top;
		
		if(pos.y < top + DragHandler.fromtop){
			DragHandler.current.style.top =  top - ((DragHandler.clone || DragHandler.relative) ? DragHandler.parenttop : 0) + 'px';
		} else if(pos.y > top + height - DragHandler.current.offsetHeight + DragHandler.fromtop){
			DragHandler.current.style.top = top + height - DragHandler.current.offsetHeight - ((DragHandler.clone || DragHandler.relative) ? DragHandler.parenttop : 0) + 'px';
		} else{
			DragHandler.current.style.top = pos.y - DragHandler.fromtop - ((DragHandler.clone || DragHandler.relative) ? DragHandler.parenttop : 0) + 'px';
		}
		//return pos.y >= top + DragHandler.fromtop && pos.y <= height - DragHandler.current.offsetHeight + DragHandler.fromtop;		
	},

	setLeft: function(){
		var left, width;
		if(window.innerWidth){
			left = window.pageXOffset;
			width = window.innerWidth;
		} else{
			left = document.body.scrollLeft;
			width = document.body.clientWidth;
		}
		
		if(pos.x < left + DragHandler.fromleft){
			DragHandler.current.style.left = left - ((DragHandler.clone || DragHandler.relative) ? DragHandler.parentleft : 0) + 'px';
		} else if(pos.x > left + width - DragHandler.current.offsetWidth + DragHandler.fromleft){
			DragHandler.current.style.left = left + width - DragHandler.current.offsetWidth - ((DragHandler.clone || DragHandler.relative) ? DragHandler.parentleft : 0) + 'px';
		} else{
			DragHandler.current.style.left = pos.x - DragHandler.fromleft - ((DragHandler.clone || DragHandler.relative) ? DragHandler.parentleft : 0) + 'px';
		}
		//return pos.x >= left + DragHandler.fromleft && pos.x <= width - DragHandler.current.offsetWidth + DragHandler.fromleft;
	}
};

var DropHandler = {
	dropAreas: [],
	count: 0,
	
	addArea: function(id, MouseOver, MouseOut, MouseUp){
		DropHandler.dropAreas[id] = [getObject(id), MouseOver, MouseOut, MouseUp];
		DropHandler.count++;
	},

	removeArea: function(id){
		//DropHandler.dropAreas[id] = null;
		delete DropHandler.dropAreas[id];
		DropHandler.count--;
	}	
};
