photoAction = (function(){
	var url = "ajax/photo.php";
	var photolistarray = new Array();
	var eventid, act_photo_id, lastindex, pastindex, targetElement, targetId;
	var pajax, act;
	function setData(event){
		eventid = event;
	}
	function updatePhotoList(direct){
		var obj = ge('viewing');
		if(direct == "next"){
			for(i=0; i<obj.childNodes.length; i++){
				var obj = obj.childNodes[i];
				if(obj.nodeType == 1 && obj.tagName == "TABLE"){
					obj = obj.firstChild.firstChild; // <TR>
					break;
				}
			}
			// Удаляем первый TD
			obj.removeChild(obj.firstChild);
			// Скрываем первый видемый TD и делаем видимым первый TD после последнего видемого
			k = 0;
			for(i=0; i<obj.childNodes.length; i++){
				var obj1 = obj.childNodes[i];
				if(obj1.nodeType == 1 && obj1.tagName == "TD"){
					
					var display = getStyle(obj1, "display");
					if(display == "table-cell" && k == 0){
						setStyle(obj1, "display", "none");
						k = 1;
					}
					if(k > 0){
						k++
					}
					if(k == 7){
						setStyle(obj1, "display", "table-cell");
						break;
					}
				}
			}
			// Добавляем новый TD в конец списка
			if(lastindex == photolistarray.length){
				lastindex = 0;
			}
			if(pastindex == photolistarray.length){
				pastindex = 0;
			}
			var pact = '';
			if(photolistarray[lastindex]['act'] == 1){
				pact = ' class="act"';
			}
			var newTD = document.createElement("TD");
			setStyle(newTD, "display", "none");
			newTD.innerHTML = '<img'+pact+' src="modules/events/photo/'+photolistarray[lastindex]['id']+'.jpg" onClick="photoAction.updatePhoto(\''+photolistarray[lastindex]['id']+'\', this)" width="100" border="0">';
			lastindex++;
			pastindex++;
			obj.appendChild(newTD);
		}
		if(direct == "prew"){
			for(i=0; i<obj.childNodes.length; i++){
				var obj = obj.childNodes[i];
				if(obj.nodeType == 1 && obj.tagName == "TABLE"){
					obj = obj.firstChild.firstChild; // <TR>
					break;
				}
			}
			// Удаляем первый TD
			obj.removeChild(obj.lastChild);
			// Скрываем первый видемый TD и делаем видимым первый TD после последнего видемого
			k = 0;
			for(i=0; i<obj.childNodes.length; i++){
				var obj1 = obj.childNodes[i];
				if(obj1.nodeType == 1 && obj1.tagName == "TD"){
					var display = getStyle(obj1, "display");
					if(display == "table-cell" && k == 0){
						setStyle(obj2, "display", "table-cell");
						k = 1;
					}
					if(k > 0){
						k++
					}
					if(k == 6){
						setStyle(obj1, "display", "none");
						break;
					}
					obj2 = obj1;
				}
			}
			// Добавляем новый TD в начало списка
			if(pastindex == -1){
				pastindex = photolistarray.length - 1;
			}
			if(lastindex == -1){
				lastindex = photolistarray.length - 1;
			}
			var pact = '';
			if(photolistarray[pastindex]['act'] == 1){
				pact = ' class="act"';
			}
			var newTD = document.createElement("TD");
			setStyle(newTD, "display", "none");
			newTD.innerHTML = '<img'+pact+' src="modules/events/photo/'+photolistarray[pastindex]['id']+'.jpg" onClick="photoAction.updatePhoto(\''+photolistarray[pastindex]['id']+'\', this)" width="100" border="0">';
			pastindex--;
			lastindex--;
			obj.insertBefore(newTD, obj.firstChild);
		}
	}
	function updatePhotoListAct(id){
		for(i=0; i<photolistarray.length; i++){
			photolistarray[i]['act'] == 0;
			if(photolistarray[i]['id'] == id){
				photolistarray[i]['act'] == 1;
			}
		}
		var obj = ge('viewing');
		obj = obj.firstChild.firstChild.firstChild; // TR
		for(i=0; i<obj.childNodes.length; i++){
			var obj1 = obj.childNodes[i];
			if(obj1.nodeType == 1 && obj1.tagName == "TD"){
				if(obj1.firstChild.className == "act"){
					removeClass(obj1.firstChild, 'act');
				}
			}
		}
		addClass(targetElement, 'act');
	}
	var onDone = function(xml, text){
		if(act == 'getPhoto'){
			var photo = xml.getElementsByTagName('photo')[0].firstChild.nodeValue;
			ge('photo').innerHTML = photo;
			var photolist = xml.getElementsByTagName('photolist');
			var k=0;
			for(i=0; i<photolist.length; i++){
				var id  = photolist[i].getElementsByTagName('id')[0].firstChild.nodeValue;
				var pact = photolist[i].getElementsByTagName('act')[0].firstChild.nodeValue;
				photolistarray[i] = new Array('id','act');
				photolistarray[i]['id'] = id;
				photolistarray[i]['act'] = pact;
			}
			for(i=0; i<photolistarray.length; i++){
				if(photolistarray[i]['act'] == 1){
					act_photo_id = i;
				}
			}
			var index = '';
			var photolisthtml='<table class="album"><tr>';
			for(i=0; i<15; i++){
				if((i<5) || (i>9)){
					display = ' style="display: none"';
				}else{
					display = ' style="display: table-cell"';
				}
				photolisthtml+="<td"+display+">";
				if(act_photo_id <= 6 && index == ''){
					index = (photolistarray.length-1) - (7 - (act_photo_id + 1));
					pastindex = index-1;
				}
				if(act_photo_id >= (photolistarray.length-7) && index == ''){
					index = act_photo_id - 7;
					pastindex = index-1;
				}
				if(index == ''){
					index = act_photo_id - 7;
					pastindex = index-1;
				}
				if(index == photolistarray.length){
					index = 0;
				}
				var pact = '';
				if(photolistarray[index]['act'] == 1){
					pact = ' class="act"';
				}
				photolisthtml+='<img'+pact+' src="modules/events/photo/'+photolistarray[index]['id']+'.jpg" onClick="photoAction.updatePhoto(\''+photolistarray[index]['id']+'\', this)" width="100" border="0">';
				photolisthtml+="</td>";
				index++;
			}
			lastindex = index;
			photolisthtml+="</tr></table>";
			ge("viewing").innerHTML = photolisthtml;
		}
		if(act == 'updatePhoto'){
			updatePhotoListAct(act_photo_id);
			var photo = xml.getElementsByTagName('photo')[0].firstChild.nodeValue;
			ge('photolink').innerHTML = photo;
			var comment = xml.getElementsByTagName('comments')[0].firstChild.nodeValue;
			ge('photocomments').innerHTML = comment;
			for(i=0; i<photolistarray.length; i++){
				if(photolistarray[i]['id'] == act_photo_id){
					currentnum = i+1;
					if(i == (photolistarray.length-1)){ i = -1; }
					nextId = photolistarray[i+1]['id'];
					break;
				}
			}
			ge('photolink').onclick = function(){ return photoAction.next(nextId); }
			ge('num').innerHTML = currentnum;
		}
		if(act == 'getAlbum'){
			var photo = xml.getElementsByTagName('photo')[0].firstChild.nodeValue;
			ge('photo').innerHTML = photo;
		}
		if(act == 'getAlbumComments'){
			var photo = xml.getElementsByTagName('photo')[0].firstChild.nodeValue;
			ge('photo').innerHTML = photo;
		}
	}
	var onFail = function(){
	}
	if(!pajax){
		pajax = new  Ajax(onDone, onFail);
	}
	return{
		getPhoto: function(id, event){
			act = 'getPhoto';
			setData(event);
			pajax.post(url, {'event':eventid,'id':id,'act':act});
		},
		getAlbum: function(){
			act = 'getAlbum';
			pajax.post(url, {'event':eventid, 'act':act});
		},
		next: function(id){
			act = 'getPhoto';
			pajax.post(url, {'event':eventid,'id':id,'act':act});
		},
		prew: function(id){
			act = 'getPhoto';
			pajax.post(url, {'event':eventid,'id':id,'act':act});
		},
		nextlist: function(){
			updatePhotoList('next');
		},
		prewlist: function(){
			updatePhotoList('prew');
		},
		updatePhoto: function(id, target){
			act = 'updatePhoto';
			targetElement = target;
			act_photo_id = id;
			pajax.post(url, {'event':eventid,'id':id,'act':act});
		},
		getAlbumComments: function(id){
			act = 'getAlbumComments';
			pajax.post(url, {'id':id,'act':act});
		}
	}
}())
