/*****************************************************************************
 *
 * File     : main.js
 * 
 * Project  : Whistler
 * 
 * Contents : Main dump
 *
 * Author: 
 *
 * Copyright 2007 Whistler Info
 * http://www.whistlerinfo.ca
 * All rights reserved.
 * 
 ***|***************************|***********************|*******************|*/


/************************************************************************
*
*	function:	showContent
*
*	purpose:	
*
*************************************************************************/
function showContent()
{
	// prvo sracunaj validity
	
	var description = _contentView.getAttribute("photoDescription");
	if (description == null) description = "";

	if (isVisible(_photoView) && _photoIsValid == false)
	{
		var url = _photoView.getAttribute("photoUrl");
		if (url == null) url = "";
		
		_photoIsValid = true;
		_descriptionView.innerHTML = description;
		_photo.src = url;
	}
	else if (isVisible(_commentsView) && _commentIsValid == false)
	{
		_commentsView.innerHTML = "comments";
		_commentIsValid = true;
		_descriptionView.innerHTML = description;
	}
	else if (isVisible(_mapView) && _mapIsValid == false)
	{
		// if something is wrong, we'll not try again until the photo is selected again
		_mapIsValid = true;

		var location = _mapView.getAttribute("photoLocation");
		if (location == null || trim(location) == "") return;
		var direction = _mapView.getAttribute("photoDirection");
		if (direction == null || trim(direction) == "") return;

		var index = location.indexOf(",");
		if (index == -1) return;
		
		try
		{
			var lat = parseFloat(location.substring(0, index));
			var lon = parseFloat(location.substring(index + 1));
			loadMap(lat, lon, direction);
		}
		catch (exception)
		{
		}	
		_descriptionView.innerHTML = description;
	}
}

/************************************************************************
*
*	function:	loadMap
*
*	purpose:	loads the google map
*
*************************************************************************/
function loadMap(lat, lon, direction) 
{
	if (GBrowserIsCompatible() == false) return;
	var map = new GMap2(_mapView);
	map.enableDoubleClickZoom();
	map.addControl(new GSmallZoomControl());
	map.addControl(new GMapTypeControl());
	map.addControl(new GOverviewMapControl());
	map.setCenter(new GLatLng(lat, lon), 13);

	var selectedType = getSelectedMapType();
	var theTypes = map.getMapTypes();
	map.setMapType(theTypes[selectedType]);

	//-----------------------
	var icon = new GIcon();
	icon.image = "images/aggregator/whistler/focus-" + direction + ".gif";
	//icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
	icon.iconSize = new GSize(60, 60);
	//icon.shadowSize = new GSize(22, 20);
	icon.iconAnchor = new GPoint(30, 30);
	icon.infoWindowAnchor = new GPoint(5, 1);

	var point = new GLatLng(lat, lon);
	map.addOverlay(new GMarker(point, icon));
	
	//-----------------------
}

/************************************************************************
*
*	function:	getSelectedMapType
*
*	purpose:	returns the map type currently showing
*
*************************************************************************/
function getSelectedMapType() 
{
	var buttonGroup = document.getElementById("mapTypeSelector");
	var buttons = buttonGroup.getElementsByTagName("input");
	for (var index = 0; index < buttons.length; index++)
	{
		var aButton = buttons.item(index);
		if(aButton.checked == true) break;
	}
	return index;
}

/************************************************************************
*
*	function:	changeTab
*
*	purpose:	
*
*************************************************************************/
function changeTab(theButton)
{
	// select this button and deselect other buttons
	if (isButtonSelected(theButton)) return;
	selectButton(theButton);

	// switch panels
	switchPanels(theButton);
	
	// show content
	showContent();
}

/************************************************************************
*
*	function:	isButtonSelected
*
*	purpose:	
*
*************************************************************************/
function isButtonSelected(theButton)
{
	return (theButton.getAttribute("selected") != null);
}

/************************************************************************
*
*	function:	selectButton
*
*	purpose:	
*
*************************************************************************/
function selectButton(theButton)
{
	var toolbar = theButton.parentNode;
	var buttons = toolbar.childNodes;
	for (var index = 0; index < buttons.length; index++)
	{
		var aButton = buttons.item(index);
		if (aButton.tagName == null) continue;
		if (aButton.tagName.toLowerCase() != "a") continue;
		if (aButton.className != "tabButton") continue;
		if (aButton == theButton) continue;
		aButton.removeAttribute("selected");
		var image = aButton.getElementsByTagName("img").item(0);
		if (image.src.indexOf("photo") != -1) image.src = "images/aggregator/whistler/photoTabInactive.gif";
		else if (image.src.indexOf("map") != -1) image.src = "images/aggregator/whistler/mapTabInactive.gif";
		else if (image.src.indexOf("comment") != -1) image.src = "images/aggregator/whistler/commentTabInactive.gif";
	}
	theButton.setAttribute("selected", "true");
	var image = theButton.getElementsByTagName("img").item(0);
	if (image.src.indexOf("photo") != -1) image.src = "images/aggregator/whistler/photoTabActive.gif";
	else if (image.src.indexOf("map") != -1) image.src = "images/aggregator/whistler/mapTabActive.gif";
	else if (image.src.indexOf("comment") != -1) image.src = "images/aggregator/whistler/commentTabActive.gif";
}


/************************************************************************
*
*	function:	switchPanels
*
*	purpose:	
*
*************************************************************************/
function switchPanels(theButton)
{
	_photoView.style.display = "none";
	_mapView.style.display = "none";
	_commentsView.style.display = "none";

	if (theButton.id == "photoTabButton")	_photoView.style.display = "";
	else if (theButton.id == "mapTabButton") _mapView.style.display = "";
	else if (theButton.id == "commentsTabButton") _commentsView.style.display = "";
}


