// JavaScript Document
var LocationsListUpdater = Class.create();

LocationsListUpdater.prototype = {
	initialize: function() {
		this.theList = null;
   	},
	ajaxUpdate: function(ajaxResponse) {
		
		this.setList(ajaxResponse);
	},
	setList: function(list) {
		this.theList = list;
		var counter = document.getElementById("locations_id").options.length;
		for (var i = 0; i < counter; i++) {
			document.getElementById("locations_id").options[0] = null;
		}
		for ( var i = 0 ; i < this.theList.childNodes.length ; i++ ) {
			if (this.theList.childNodes[i].nodeType != 3) {
				var counter = document.getElementById("locations_id").options.length;
			 	document.getElementById("locations_id").options[counter] = new Option ( this.theList.childNodes[i].getAttribute("label"), this.theList.childNodes[i].getAttribute("val"), this.theList.childNodes[i].getAttribute("selected"));
			}
		}
		document.getElementById('indicator').style.display = "none";
		getElement('submit').style.visibility = "visible";
	}
};

var EventsListUpdater = Class.create();

EventsListUpdater.prototype = {
	initialize: function() {
		this.theList = null;
   	},
	ajaxUpdate: function(ajaxResponse) {
		
		this.setList(ajaxResponse);
	},
	setList: function(list) {
		this.theList = list;
		getElement('indicator_photo').style.display = "none";
		getElement('submit_photo').style.visibility = "visible";
		var counter = document.getElementById("events_id").options.length;
		for (var i = 0; i < counter; i++) {
			
			document.getElementById("events_id").options[0] = null;
		}
		for ( var i = 0 ; i < this.theList.childNodes.length ; i++ ) {
			if (this.theList.childNodes[i].nodeType != 3) {
				var counter = document.getElementById("events_id").options.length;
			 	document.getElementById("events_id").options[counter] = new Option ( this.theList.childNodes[i].getAttribute("label"), this.theList.childNodes[i].getAttribute("val"), this.theList.childNodes[i].getAttribute("selected"));
			}
		}
		
		getElement('navbar').style.visibility = "visible";
		getElement('indicator').style.display = "none";
		getElement('submit').style.visibility = "visible";
		getElement('indicator_photo').style.display = "none";
		getElement('submit_photo').style.visibility = "visible";
	}
};

var PhotosListUpdater = Class.create();

PhotosListUpdater.prototype = {
	initialize: function() {
		
		this.theList = null;
   	},
	ajaxUpdate: function(ajaxResponse) {
		
		this.setList(ajaxResponse);
	},
	setList: function(list) {
		
		this.theList = list;
		
		
		
		// TOLGO LE VECCHIE
		while (document.getElementById("pic-gallery").firstChild) {
			document.getElementById("pic-gallery").removeChild(document.getElementById("pic-gallery").firstChild);
		}
		
		// AGGIORNO LA NAV BAR
		document.getElementById('tot').innerHTML = this.theList.getAttribute("tot");
		document.getElementById('pag').innerHTML = eval(this.theList.getAttribute("pag"))+1;
		document.getElementById('pagtot').innerHTML = this.theList.getAttribute("pagtot")
		
		if (eval(this.theList.getAttribute("pag")) == 0) {
			document.getElementById('back-page').innerHTML = "";
		} else {
			prev=eval(this.theList.getAttribute("pag"))-1;
			document.getElementById('back-page').innerHTML = '<a href="javascript:getList('+prev+')">&lt;&lt;</a>';
		}
		
		if (eval(this.theList.getAttribute("pag")) < eval(this.theList.getAttribute("pagtot"))-1) {
			next=eval(this.theList.getAttribute("pag"))+1;
			document.getElementById('next-page').innerHTML = '<a href="javascript:getList('+next+')">&gt;&gt;</a>';
		} else {
			document.getElementById('next-page').innerHTML = "";
		}
		var currPage = this.theList.getAttribute("pag");
		
		// METTO LE NUOVE
		getElement('indicator_photo').style.display = "none";
		getElement('submit_photo').style.visibility = "visible";
		var pos = 0;
		for ( var i = 0 ; i < this.theList.childNodes.length ; i++ ) {
				if (this.theList.childNodes[i].nodeType != 3) {
					var s = new Date();
					my_id = this.theList.childNodes[i].getAttribute("id");
					my_pic = this.theList.childNodes[i].getAttribute("pic");
					my_locations_id = this.theList.childNodes[i].getAttribute("locations_id");
					my_locations_x = this.theList.childNodes[i].getAttribute("locations_x");
					my_locations_y = this.theList.childNodes[i].getAttribute("locations_y");
					my_date = this.theList.childNodes[i].getAttribute("date");
					my_pic += "?rand="+s.getTime();
					my_event = this.theList.childNodes[i].getAttribute("event");
					mydiv = document.createElement("div");
					mydiv.setAttribute("id", "pic");
					var clickString;
					clickString = '<a href="javascript:showPicDetails('+my_id+','+currPage+','+pos+');';
					pos++;
					
					if ((my_locations_id != "1") && (my_locations_id != "")){
						clickString += 'spostaMappa('+my_locations_id+','+my_locations_x+','+my_locations_y+');';
					}
					clickString += '"';
					if ((my_locations_id != "1") && (my_locations_id != "")){
						clickString += ' onmouseover="javascript:spostaMappa('+my_locations_id+','+my_locations_x+','+my_locations_y+');"';
					}
					
					clickString +='><img src="'+my_pic+'" border="0"/></a><div id="date">'+my_date+'</div><div id="name">'+my_event+'</div>';
					mydiv.innerHTML = clickString;
					
					document.getElementById('pic-gallery').appendChild(mydiv);
				}
		}
		getElement('navbar').style.visibility = "visible";
		getElement('indicator').style.display = "none";
		getElement('submit').style.visibility = "visible";
		getElement('indicator_photo').style.display = "none";
		getElement('submit_photo').style.visibility = "visible";
		
		
	}
};


var PhotosBlogListUpdater = Class.create();

PhotosBlogListUpdater.prototype = {
	initialize: function() {
		
		this.theList = null;
   	},
	ajaxUpdate: function(ajaxResponse) {
		
		this.setList(ajaxResponse);
	},
	setList: function(list) {
		
		this.theList = list;
		
		// TOLGO LE VECCHIE
		while (document.getElementById("pic-gallery").firstChild) {
			document.getElementById("pic-gallery").removeChild(document.getElementById("pic-gallery").firstChild);
		}
		
		// AGGIORNO LA NAV BAR
		document.getElementById('tot').innerHTML = this.theList.getAttribute("tot");
		document.getElementById('pag').innerHTML = eval(this.theList.getAttribute("pag"))+1;
		document.getElementById('pagtot').innerHTML = this.theList.getAttribute("pagtot")
		
		if (eval(this.theList.getAttribute("pag")) == 0) {
			document.getElementById('back-page').innerHTML = "";
		} else {
			prev=eval(this.theList.getAttribute("pag"))-1;
			document.getElementById('back-page').innerHTML = '<a href="javascript:getList('+prev+')">&lt;&lt;</a>';
		}
		
		if (eval(this.theList.getAttribute("pag")) < eval(this.theList.getAttribute("pagtot"))-1) {
			next=eval(this.theList.getAttribute("pag"))+1;
			document.getElementById('next-page').innerHTML = '<a href="javascript:getList('+next+')">&gt;&gt;</a>';
		} else {
			document.getElementById('next-page').innerHTML = "";
		}
		var currPage = this.theList.getAttribute("pag");
		
		// METTO LE NUOVE
		var pos = 0;
		for ( var i = 0 ; i < this.theList.childNodes.length ; i++ ) {
				if (this.theList.childNodes[i].nodeType != 3) {
					var s = new Date();
					my_id = this.theList.childNodes[i].getAttribute("id");
					my_pic = this.theList.childNodes[i].getAttribute("pic");
					my_locations_id = this.theList.childNodes[i].getAttribute("locations_id");
					my_locations_x = this.theList.childNodes[i].getAttribute("locations_x");
					my_locations_y = this.theList.childNodes[i].getAttribute("locations_y");
					my_pic += "?rand="+s.getTime();
					my_event = this.theList.childNodes[i].getAttribute("event");
					mydiv = document.createElement("div");
					mydiv.setAttribute("id", "pic_"+my_id);
					mydiv.setAttribute("class", "pic-blog");
					mydiv.setAttribute("className", "pic-blog");
					
					mydiv.setAttribute("lang", my_id);
					
					
					//new Draggable("pic_"+my_id,{revert:true});
					var clickString;
					pos++;
					
					clickString = '<img src="'+my_pic+'" border="0"/></a><div id="date"></div><div id="name">'+my_event+'</div>';
					mydiv.innerHTML = clickString;
					document.getElementById('pic-gallery').appendChild(mydiv);
					new Draggable("pic_"+my_id,{revert:true});
					//new Draggable("pic_"+my_id,{revert:true});
					
				}
		}
		getElement('indicator_photo').style.display = "none";
		getElement('submit_photo').style.visibility = "visible";
		getElement('navbar').style.visibility = "visible";
	}
};



var PhotosDetailUpdater = Class.create();
PhotosDetailUpdater.prototype = {
	initialize: function() {
		this.theList = null;
   	},
	ajaxUpdate: function(ajaxResponse) {
		this.setList(ajaxResponse);
	},
	setList: function(list) {
		getElement('indicator_photo').style.display = "none";
		getElement('submit_photo').style.visibility = "visible";
		getElement('foto-dettaglio').style.visibility = "visible";
		getElement('navbar').style.visibility = "visible";
		
		this.theList = list;
		for ( var i = 0 ; i < this.theList.childNodes.length ; i++ ) {
			if (this.theList.childNodes[i].nodeType != 3) {
				
				
				
				my_id = this.theList.childNodes[i].getAttribute("id");
				my_pic = this.theList.childNodes[i].getAttribute("pic");
				my_pic_big = this.theList.childNodes[i].getAttribute("pic_big");
				my_event = this.theList.childNodes[i].getAttribute("event");
				my_date =  this.theList.childNodes[i].getAttribute("date");
				my_name = this.theList.childNodes[i].getAttribute("name");
				my_w = this.theList.childNodes[i].getAttribute("width");
				my_h = this.theList.childNodes[i].getAttribute("height");
				my_voti = this.theList.childNodes[i].getAttribute("voti");
				my_comments = this.theList.childNodes[i].getAttribute("comments");
				my_comment = this.theList.childNodes[i].getElementsByTagName('comment')[0].firstChild.nodeValue;
				my_comment += '<br/><br/><a href="'+my_pic_big+'" target="_blank">Vedi grande</a><br/><br/>';
				my_comment += '<br/><br/><a href="photo_detail.php?action=modify&id='+my_id+'">Modifica dettagli</a><br/><br/>';
				my_comment += '<br/><br/><a href="javascript:delPic('+my_id+')">Elimina questa foto</a><br/><br/>';
				
				
				var s = new Date();
				document.getElementById("foto-img").src = my_pic;
				document.getElementById("foto-data").innerHTML = my_date;
				document.getElementById("foto-autore").innerHTML = my_name;
				document.getElementById("foto-evento").innerHTML = my_event;
				document.getElementById("foto-commento").innerHTML = my_comment;
				document.getElementById("foto-voti").innerHTML = my_voti;
				document.getElementById("foto-commenti").innerHTML = my_comments;
				
			}
		}
		
		
	}
};

var PhotosRemover = Class.create();
PhotosRemover.prototype = {
	initialize: function() {
		this.theList = null;
   	},
	ajaxUpdate: function(ajaxResponse) {
		
		this.setList(ajaxResponse);
	},
	setList: function(list) {
		this.theList = list;
		getList(currentPage);
		getElement('indicator').style.display = "none";
		getElement('submit').style.visibility = "visible";
		getElement('navbar').style.visibility = "visible";
	}
};