function toggle_help()
{
	if (div = document.getElementById('help-form'))
	{
		div.className = (div.className == 'help')? 'help visable' : 'help';
	}
}

function set_main_image(idx)
{
	if (img = document.getElementById('mainImage'))
	{
		img.src = image_collection[idx] + '&of=7';
	}

	for (i = 0; i < image_collection.length; i++)
	{
		if (img = document.getElementById('thumbNail' + i))
		{
			
			img.className = (i == idx)? "selected" : null;
		}
	}
}

function set_ad_type(value)
{
	if (price = document.getElementById('price-title-text'))
	{
		price.innerHTML = ((value == 'sell') || (value == 'buy'))? price_text : price_text_per_night;
	}
	if (time_from = document.getElementById('time_from'))
	{
		time_from.disabled = ((value == 'sell') || (value == 'buy'));
	}
	if (time_to = document.getElementById('time_to'))
	{
		time_to.disabled = ((value == 'sell') || (value == 'buy'));
	}
	if (yard_space = document.getElementById('yard_space'))
	{
		yard_space.disabled = ((value == 'rent') || (value == 'rentout'));
	}
	if (beds = document.getElementById('beds'))
	{
		beds.disabled = ((value == 'sell') || (value == 'buy'));
	}
	if (wprice = document.getElementById('wprice'))
	{
		wprice.disabled = (value != 'rent');
	}
	if (build_year = document.getElementById('build_year'))
	{
		build_year.disabled = (value != 'sell');
	}
}

function remove_helptext(control, value)
{
	if (control.value == value)
	{
		control.value = '';
	}
}

function set_helptext(control, value)
{
	if (control.value == '')
	{
		control.value = value;
	}
}

function do_reset_form()
{
	if (input = document.getElementById('reset_form'))
	{
		input.value = "true";
	}
	if (form = document.getElementById('MainForm'))
	{
		form.submit();
	}
}


function advert_tabs_set_tab(tab, tabName, divClass)
{
	var i = 0;
	var tr = tab.parentNode;
	for (var j = 0; j < tr.childNodes.length; j++)
	{
		if (tr.childNodes[j].className && (tr.childNodes[j].className != "tab last"))
		{
			td = tr.childNodes[j];
			td.className = "tab" + ((td == tab)? " selected" : "");

			if (div = document.getElementById(tabName + '_' + i))
			{
				div.className = divClass + ((td == tab)? " selected" : "");
			}
			i++;
		}
	}
}

function advert_tabs_select(tabId, caption, body)
{
	if (td = document.getElementById('td_' + caption + '_' + tabId))
	{
		advert_tabs_set_tab(td, caption, "caption");
	}
	if (td = document.getElementById('td_' + body + '_' + tabId))
	{
		advert_tabs_set_tab(td, body, "body");
	}
}


/*
 Microsoft Virtual Earth
 */

var map = null;
var zoom = 1;
var m_point = null;
var c_point = null;
var map_style = null;

var marker = null;

var mapContainer = 'mapContainer';

function EventMouseWheel(e)
{
   //insert custom behavior here...
   //return true to disable the default behavior. return false to enable the default behavior.
	window.scrollBy(0,-2*e.mouseWheelChange);
  	return true;
}

function init_ve_map()
{
	// setup defaults
	setup_map_defaults();

	// create a new map
	map = new VEMap(mapContainer);

	// use defaults if set
	version_info = VEMap.GetVersion().split('.');
	default_map_style = (version_info[2] >= 20070400000000)? VEMapStyle.Shaded : VEMapStyle.Road;
	c_point = (c_point == null)? new VELatLong(40, 10) : c_point;
	map_style = (map_style == null)? default_map_style : map_style;

	// load map
	map.LoadMap(c_point, zoom, map_style, false, VEMapMode.Mode2D, false);

	// Disable mouse wheel
       map.AttachEvent("onmousewheel", EventMouseWheel);

	// adding pin if previously selected
	if (m_point != null)
	{
		add_pin(m_point);
	}

	map.AttachEvent("onmouseover", function(e) {if (e.elementID != null) {return true;}});
	map.AttachEvent("onmouseout", function(e) {if (e.elementID != null) {return true;}});

	// attaching onclick function
	map.AttachEvent("onclick", function(map_event)
	{
		if (map_event.rightMouseButton && (map_style != VEMapStyle.Oblique) && (map_style != VEMapStyle.Birdseye))
		{
			// get point
			var point = map.PixelToLatLong(new VEPixel(map_event.mapX, map_event.mapY));
	
			add_pin(point);

			// update input fields
			if (mpoint = document.getElementById('map-mpoint'))
			{
				// sometimes point is given without lat lng values and just a decoded value, fix			
				mpoint.value = point.Latitude + ';' + point.Longitude;
			}
		}

	});

	// attaching on change view function to be able to restore center point and zoom when reloading page
	map.AttachEvent("onchangeview", function (map_event)
	{
		c_point = map.GetCenter();
		zoom = map_event.zoomLevel;
		map_style = map_event.mapStyle;

		// update input fields for map style
		if (ms = document.getElementById('map-ms'))
		{
			ms.value = map_style;
		}

		// update input fields for center point and zoom
		if ((map_style != VEMapStyle.Oblique) && (map_style != VEMapStyle.Birdseye) && 
			(cpoint = document.getElementById('map-cpoint')) && 
			(zoom_field = document.getElementById('map-zoom')))
		{
			cpoint.value = c_point.Latitude + ';' + c_point.Longitude;
			zoom_field.value = zoom;			
		}

	});

	// hide elements of the map
	if (div = document.getElementById(mapContainer))
	{
		var nodes = div.getElementsByTagName("div");
		
		for (i = 0; i < div.childNodes.length; i++)
		{
			var child = div.childNodes[i];

			if ((child.className != null) && (
			 (child.className.indexOf("MSVE_Scale") == 0) || 
			 (child.className.indexOf("MSVE_Powered") == 0) ||
				(child.className.indexOf("MSVE_Copyright") == 0)))
			{
				child.style.display = "none";
			}
		}
	}

}

function add_pin(point)
{
	if (map != null)
	{
		if (marker == null)
		{
			// create new marker
			marker = new VEShape(VEShapeType.Pushpin, point);
			marker.SetCustomIcon('<img src="./images/map/house.gif" alt="" style="margin: 5px 0px 0px 7px;" />');
			
			// add marker
			map.AddShape(marker);
		}
		else
		{
			// move marker
			marker.SetPoints(point);
		}
	}
}


// adding init_ve_map to onload
window.onload = init_ve_map;
