
var place_id = "";
var photos = new Array();
var current_rating = -1;

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}

function Photo(id, title, filename, roomtype) {
	this.id 	= id;
	this.title	= title;
	this.filename	= filename;
	this.roomtype	= roomtype;
}

function add_photo(id, title, filename, roomtype) {
	this.photos[this.photos.length] = new Photo(id, title, filename, roomtype);
}

function set_photo_handler(evt) {
	set_photo(Event.element(evt).getAttribute("id"));
}

function set_photo(id) {

	var photo_handle 	= $("big_photo");
	var photo_info_handle 	= $("big_photo_info");
	var photo_link_handle	= $("big_photo_link");

	if (photo_handle == null) {
		photo_handle = document.createElement("img");
		photo_handle.setAttribute("id", "big_photo");
		photo_handle.style.border = "1px solid #a0a0a0";
		photo_handle.style.padding = "4px";
		photo_handle.style.background = "#ffffff";

		$("big_photo_container").appendChild(photo_handle);

		photo_info_handle = document.createElement("div");
		photo_info_handle.setAttribute("id", "big_photo_info");
		$("big_photo_container").appendChild(photo_info_handle);

		photo_link_handle = document.createElement("div");
		photo_link_handle.setAttribute("id", "big_photo_link");
		photo_link_handle.appendChild(document.createTextNode("Direct image link: "));

		var link = document.createElement("input");
		link.type = "text";
		link.style.width = "500px";
		link.setAttribute("readonly", "");
		link.setAttribute("id", "big_photo_link_input");
		photo_link_handle.appendChild(link);
		$("big_photo_container").appendChild(photo_link_handle);
		Event.observe(link, "click", select_photo_link);

	}

	var photo_obj = null;
	for (var i=0; i<photos.length; i++) {
		if (photos[i].id == id) {
			photo_obj = photos[i];
			break;
		}
	}

	if (photo_obj != null) {

		setTimeout("$(\"big_photo\").src = '"+photo_obj.filename+"';", 10); //Stupid ie...
		$("big_photo_link_input").value = "http://www.xhibi.com/?id="+place_id+"&i="+photo_obj.id;

		while (photo_info_handle.hasChildNodes())
			photo_info_handle.removeChild(photo_info_handle.firstChild);


		if ( (photo_obj.roomtype != "") || (photo_obj.title != "") ) {
			photo_info_handle.style.display = "block";

			if (photo_obj.roomtype != "") {
				var span = document.createElement("span");
				span.style.fontWeight = "bold";
				span.appendChild(document.createTextNode(photo_obj.roomtype + " "));
				photo_info_handle.appendChild(span);
			}

			if (photo_obj.title != "")
				photo_info_handle.appendChild(document.createTextNode(photo_obj.title));

		} else {
			photo_info_handle.style.display = "none";
		}

	}

}

function select_photo_link(evt) {
	$("big_photo_link_input").select();
}


function hide_tag_input() {
	hide('tag_input');
	$('tags').value = "";
}

function show_tag_input() {
	show('tag_input');
}

function submit_tags(place_id) {
	var tag_string = $('tags').value;

	new Ajax.Request("add_tags.php?place_id="+place_id+"&tags="+escape(tag_string),
	{
		method: 'post',
		onSuccess: function(transport) {

			var new_tags = transport.responseText.split(",");

			if (new_tags.length > 0) {
				var container = $("tag_container");
				while (container.hasChildNodes())
					container.removeChild(container.firstChild);

				for (var i=0; i<new_tags.length; i++) {
					if (i>0)
						container.appendChild(document.createTextNode(", "));

					var a = document.createElement("a");
					a.href = "./?page=search&tag="+escape(new_tags[i]);
					a.appendChild(document.createTextNode(new_tags[i]));
					container.appendChild(a);
				}

			} else {
				//Just leave the tags_container as is
			}


			$('tag_input').style.display = "none";
			$('tags').value = "";

			$("tag_submit_feedback").className = "message";
			$("tag_submit_feedback").innerHTML = "Tag(s) submitted successfully, thank you!";
			show("tag_submit_feedback");
			setTimeout('hide("tag_submit_feedback")', 5000);
		},

		onFailure: function(transport) {

			$('tag_input').style.display = "none";
			$('tags').value = "";

			$("tag_submit_feedback").className = "error";
			$("tag_submit_feedback").innerHTML = "An error occurred, please try again later";
			show("tag_submit_feedback");
			setTimeout('hide("tag_submit_feedback")', 5000);
		}
	});
}




function flag(id) {

	new Ajax.Request("add_flag.php?id="+id,
	{
		method: 'post',
		onSuccess: function(transport) {
			show("flag_submit_feedback");
			setTimeout('hide("flag_submit_feedback")', 5000);
		},

		onFailure: function(transport) {
		}
	});
}

function mark_rating(which) {

	current_rating = which;

	var elm 	= $("rating_number"+which);
	var desc_elm 	= $("rating_description");

	elm.style.color = "#ffffff";
	elm.style.fontSize = "26pt";

	var desc_color 	= "#404040";
	var desc_txt  	= "";
	var desc_weight = "normal";
	switch (which) {
		case 1:
			desc_weight 	= "bold";
			desc_color 	= "#D73535";
			desc_txt 	= "A first-class dump!";
			break;
		case 2:
			desc_txt 	= "Yikes...";
			break;
		case 3:
			desc_txt 	= "Rock-bottom getting closer";
			break;
		case 4:
			desc_txt 	= "I've seen worse";
			break;
		case 5:
			desc_txt 	= "Decent, at most";
			break;
		case 6:
			desc_txt 	= "It's working, yeah";
			break;
		case 7:
			desc_txt 	= "Now we're talking!";
			break;
		case 8:
			desc_txt 	= "Just lovely";
			break;
		case 9:
			desc_txt 	= "Wow!";
			break;
		case 10:
			desc_weight 	= "bold";
			desc_color 	= "#4CD735";
			desc_txt 	= "Gimme the key, now!!";
			break;
		/*
		case 11:
			desc_txt 	= "Next place";
			break;
		*/
	}

	desc_elm.style.fontWeight 	= desc_weight;
	desc_elm.style.color 		= desc_color;
	desc_elm.innerHTML 		= desc_txt;

	var rate_pos = findPos(elm);
	var rate_w = elm.offsetWidth;
	var rate_h = elm.offsetHeight;

	var desc_w = desc_elm.offsetWidth;
	var desc_h = desc_elm.offsetHeight;

	desc_elm.style.visibility = "visible";
	desc_elm.style.top  = rate_pos[1] - desc_h + "px";
	desc_elm.style.left = Math.round(rate_pos[0] + rate_w/2 - desc_w/2) + "px";
}

function unmark_rating(which) {
	$("rating_number"+which).style.fontSize = "18pt";
	$("rating_number"+which).style.color = "#404040";
	$("rating_description").style.visibility = "hidden";
	//current_rating = -1;
}

function submit_rating() {
	$("my_rating").value = current_rating;
	setTimeout('$("rating_form").submit();', 10); //Stupid ie
}

onload = function() {
	var is_first = true;
	for (var i=0; i<photos.length; i++) {
		if (photos[i].roomtype != "") {

			if (!is_first) {
				$("roomtypes_container").appendChild(document.createTextNode(", "));
			} else {
				is_first = false;
			}

			var link = document.createElement("a");
			link.href = "javascript:;";
			link.setAttribute("id", photos[i].id);
			link.appendChild(document.createTextNode(photos[i].roomtype));
			$("roomtypes_container").appendChild(link);
			Event.observe(link, "click", set_photo_handler);
		}
	}
}