/**
 * @author Rober
 */

var JMWphotos = {
	
	photosList : [],
	xml : null,
	photoContainer : null,
	nextPhotosButton : null,
	previousPhotosButton : null,
	imgSelectedId : null,
	
	init : function (req) {
		this.xml = req.responseXML;
		this.photoContainer = document.getElementById('image-list');
		this.nextPhotosButton = document.getElementById('next-photos-button');
		this.previousPhotosButton = document.getElementById('previous-photos-button');
		this.removePhotos();
		this.setPageButtons();
		var xmlPhotos = this.xml.documentElement.getElementsByTagName("photo");
		
		for (var i=0; i<xmlPhotos.length; i++) {
			this.photosList.push(new JMWphoto(xmlPhotos.item(i)));
		}
	},
	
	removePhotos : function () {
		for (var i=0; i<this.photosList.length; i++) {
			this.photosList[i].remove();
		}
		if (this.photoContainer != null) {
			this.photoContainer.innerHTML = '';
		}
	},
	
	setPageButtons : function () {
		var node = this.xml.getElementsByTagName('nextpag')[0];
		var value = null;
		if (node != null && node.hasChildNodes()) {
			value = node.childNodes[0].nodeValue;
		}
		
		if (value != null) {
			this.nextPhotosButton.style.display = 'inline';
			this.nextPhotosButton.pag = value;
			this.nextPhotosButton.onclick = function () {
				JMWmap.getImagesFromCoord(this.pag);
			}
		} else {
			this.nextPhotosButton.style.display = 'none';
			this.nextPhotosButton.pag = null;
			this.nextPhotosButton.onclick = function() {}
		}
		
		var node = this.xml.getElementsByTagName('prevpag')[0];
		var value = null;
		if (node != null && node.hasChildNodes()) {
			value = node.childNodes[0].nodeValue;
		}
		
		if (value != null) {
			this.previousPhotosButton.style.display = 'inline';
			this.previousPhotosButton.pag = value;
			this.previousPhotosButton.onclick = function () {
				JMWmap.getImagesFromCoord(this.pag);
			}
		} else {
			this.previousPhotosButton.style.display = 'none';
			this.previousPhotosButton.pag = null;
			this.previousPhotosButton.onclick = function() {}
		}
		
	},
	
	createImgMarker : function (props) {
		var imgmap = new GIcon();
		imgmap.image = '/imagefile/minimap/' + props.photo_id;
		imgmap.shadow = '/imagefile/minimap/' + props.photo_id;
		imgmap.iconSize = new GSize(props.photo_width,props.photo_height);
		imgmap.iconAnchor = new GPoint(16, 12);
		imgmap.shadowSize = new GSize(0, 0);
		imgmap.infoWindowAnchor = new GPoint(props.photo_width,props.photo_height);
		imgmap.infoShadowAnchor = new GPoint(props.photo_width,props.photo_height);
		var point = new GLatLng(props.photo_coord_x,props.photo_coord_y);
		var markermap = new GMarker(point, imgmap);
		map.addOverlay(markermap);
		var pane = map.getPane(G_MAP_MARKER_PANE);
		markermap.imgref = pane.childNodes[pane.childNodes.length-1];
		
		return markermap;
	}
}

var JMWphoto = function (photoNode) {
	
	this.photoNode = null;
	this.img = null;
	this.anchorImage = null;
	this.imgmap = null;
	this.markermap = null;
	this.photo_id = null;
	this.photo_title = null;
	this.photo_coord_x = null;
	this.photo_coord_y = null;
	this.photo_owner_id = null;
	this.photo_owner_nick = null;
	this.photo_width = 32;
	this.photo_height = 24;
	this.photo_visits = null;
	this.photo_note = null;
	
	this.init = function (photoNode) {
		this.photoNode = photoNode;
		
		/* Propiedades */
		for (var i=0; i<this.photoNode.childNodes.length;i++) {
			var node = this.photoNode.childNodes[i];
			
			if (node.childNodes.length>0) {
				this[node.nodeName] = node.childNodes[0].nodeValue;
			}
		}
		
		var img = document.createElement('img');
		img.src = '/imagefile/mini/' + this.photo_id;
		img.border="0";
		var img_a = document.createElement('a');
		img_a.href="/photo/" + this.photo_id;
		img_a.appendChild(img);
		
		var user_a = document.createElement('a');
		user_a.href="/user/" + this.photo_owner_id;
		user_a.innerHTML = this.photo_owner_nick;
		
		var span = document.createElement('p');
		span.className = 'comment';
		span.appendChild(user_a);
		span.innerHTML += '<br/> Visitas: ' + this.photo_visits + ' <br/> Nota: ' + this.photo_note;
		
		var div = document.createElement('div');
		div.className = 'photo-container';
		div.appendChild(img_a);
		div.appendChild(span);
		
		this.img = img;
		this.anchorImage = img_a;
		
		JMWphotos.photoContainer.appendChild(div);
		
		/* Imagen dentro de mapa */
		this.markermap = JMWphotos.createImgMarker(this);
		this.imgmap = this.markermap.imgref;
		this.setSelected(false);
		
		/* Referencias */
		this.anchorImage.objRef = this;
		this.markermap.objRef = this;
		
		this.anchorImage.onmouseover = function () {
			this.objRef.setSelected(true);
		}
		this.anchorImage.onmouseout = function () {
			this.objRef.setSelected(false);
		}
		
		GEvent.addListener(this.markermap, "mouseover",function(point){
			this.objRef.setSelected(true);
		});
		GEvent.addListener(this.markermap, "mouseout",function(point){
			this.objRef.setSelected(false);
		});
		
		GEvent.addListener(this.markermap, "click",function(point){
			window.location.href=this.objRef.anchorImage.href;
		});
	}
	
	this.remove = function () {
		if (this.photo_id != JMWphotos.imgSelectedId) {
			map.removeOverlay(this.markermap);
		}
	}
	
	this.setSelected = function (bol) {
		if (bol) {
			this.imgmap.style.borderStyle = 'solid';
			this.imgmap.style.borderWidth = '2px';
			this.imgmap.style.borderColor = '#FF0000';
			this.imgmap.style.zIndex = '900';
			
			this.img.style.borderStyle = 'solid';
			this.img.style.borderWidth = '2px';
			this.img.style.borderColor = '#FF0000';
			
		} else {
			this.imgmap.style.borderStyle = 'solid';
			this.imgmap.style.borderWidth = '1px';
			this.imgmap.style.borderColor = '#FFFFFF';
			this.imgmap.style.zIndex = '';
			
			this.img.style.borderStyle = 'solid';
			this.img.style.borderWidth = '1px';
			this.img.style.borderColor = '#FFFFFF';
		}
	}
	
	this.init(photoNode);
	
}
