/*****************************************************************************
 *
 * File     : selector.js
 * 
 * Project  : Whistler
 * 
 * Contents : Manager of panels (thumbs, albums, etc)
 *
 * Author: 
 *
 * Copyright 2007 Whistler Info
 * http://www.whistlerinfo.ca
 * All rights reserved.
 * 
 ***|***************************|***********************|*******************|*/

var _mapIsValid = false;
var _photoIsValid = false;
var _commentIsValid = false;

/************************************************************************
*
*	function:	changeContent
*
*	purpose:	Changes the photo or the map to a new one
*
*************************************************************************/
function changeContent(thumb)
{
	/// invalidateContent
	_mapIsValid = false;
	_photoIsValid = false;
	_commentIsValid = false;
	
	// unselect the current thumb
	deselectCurrentThumb();
	// select the new thumb
	selectThumb(thumb);

	var id = thumb.getAttribute("id");
	var description = thumb.getAttribute("photoDescription");
	var url = thumb.getAttribute("photoUrl");
	var location = thumb.getAttribute("photoLocation");
	var direction = thumb.getAttribute("photoDirection");

	_contentView.setAttribute("contentId", id);
	_contentView.setAttribute("photoDescription", description);
	
	_photoView.setAttribute("photoUrl", url);
	
	_mapView.setAttribute("photoLocation", location);
	_mapView.setAttribute("photoDirection", direction);
	
	showContent();
	
	var albumId = _contentView.getAttribute("albumId");
	//window.location.href = "#" + albumId + "-" + id;
}

/************************************************************************
*
*	function:	changeAlbum
*
*	purpose:	
*
*************************************************************************/
function changeAlbum(album)
{
	// unselect the current album
	deselectCurrentAlbum();
	/*
	var oldAlbum = _contentView.getAttribute("album");
	if (oldAlbum != null)
	{
		oldAlbum.removeAttribute("selected");
		unhighlightAlbum(oldAlbum);
	}
	*/
	// set the new current values
	_contentView.setAttribute("albumId", album.id);
	selectAlbum(album);
//	album.setAttribute("selected", "true");

	loadAlbum(album);
}

/************************************************************************
*
*	function:	loadAlbum
*
*	purpose:	
*
*************************************************************************/
function loadAlbum(album)
{
	try
	{
		_xmlhttp = createXMLHTTPObject();
		_xmlhttp.onreadystatechange = albumLoaded;
		// Use POST because no time for finding how to disable IE caching of GET
		//_xmlhttp.open("POST", "http://dev.whistlerinfo.ca:8050/wmst/http?request=getalbum&app=whistler&id=" + album.getAttribute("id"), true);
		_xmlhttp.open("POST", "?request=getalbum&app=whistler&id=" + album.getAttribute("id"), true);
		_xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		_xmlhttp.send(null);
	}
	catch (exception)
	{
		alert(exception.name + ": " + exception.message);
	}
}

/************************************************************************
*
*	function:	albumLoaded
*
*	purpose:	
*
*************************************************************************/
function albumLoaded()
{
	if (_xmlhttp.readyState != 4) return;
	_thumbView.innerHTML = _xmlhttp.responseText;

	var thumbs = getChildNodes(_thumbView, "div");
	var thumbIndex = Math.floor(Math.random() * thumbs.length)
	changeContent(thumbs[thumbIndex]);
}

/************************************************************************
*
*	function:	selectAlbum
*
*	purpose:	
*
*************************************************************************/
function selectAlbum(album)
{
	album.setAttribute("selected", "true");
	highlightAlbum(album);
}

/************************************************************************
*
*	function:	deselectCurrentAlbum
*
*	purpose:	
*
*************************************************************************/
function deselectCurrentAlbum()
{
	var id = _contentView.getAttribute("albumId");
	deselectAlbum(id);
}

/************************************************************************
*
*	function:	deselectAlbum
*
*	purpose:	
*
*************************************************************************/
function deselectAlbum(id)
{
	if (id == null || trim(id) == "") return;
	var album = document.getElementById(id);
	if (album == null) return;
	album.removeAttribute("selected");
	unhighlightAlbum(album);
}

/************************************************************************
*
*	function:	highlightAlbum
*
*	purpose:	when user hovers over thumbnail
*
*************************************************************************/
function highlightAlbum(album)
{
	var image = album.getElementsByTagName("img").item(0);
	image.className = "albumHighlighted";
}

/************************************************************************
*
*	function:	unhighlightAlbum
*
*	purpose:	when user hovers out of thumbnail
*
*************************************************************************/
function unhighlightAlbum(album)
{
	if (album.getAttribute("selected") != null) return;
	var image = album.getElementsByTagName("img").item(0);
	image.className = "album";
}

/************************************************************************
*
*	function:	selectThumb
*
*	purpose:	
*
*************************************************************************/
function selectThumb(thumb)
{
	thumb.setAttribute("selected", "true");
	highlightThumb(thumb);
}

/************************************************************************
*
*	function:	deselectCurrentThumb
*
*	purpose:	
*
*************************************************************************/
function deselectCurrentThumb()
{
	var id = _contentView.getAttribute("contentId");
	deselectThumb(id);
}

/************************************************************************
*
*	function:	deselectThumb
*
*	purpose:	
*
*************************************************************************/
function deselectThumb(id)
{
	if (id == null || trim(id) == "") return;
	var thumb = document.getElementById(id);
	if (thumb == null) return;
	thumb.removeAttribute("selected");
	unhighlightThumb(thumb);
}

/************************************************************************
*
*	function:	highlightThumb
*
*	purpose:	when user hovers over thumbnail
*
*************************************************************************/
function highlightThumb(thumb)
{
	var image = thumb.getElementsByTagName("img").item(0);
	image.className = "thumbHighlighted";
}

/************************************************************************
*
*	function:	unhighlightThumb
*
*	purpose:	when user hovers out of thumbnail
*
*************************************************************************/
function unhighlightThumb(thumb)
{
	if (thumb.getAttribute("selected") != null) return;
	var image = thumb.getElementsByTagName("img").item(0);
	image.className = "thumb";
}


