Event.observe(window, 'load', init, false);

function init() {
	getTabs(setPage);
}

var handlerFunc = function(t) {
    alert(t.responseText);
}

var errFunc = function(t) {
    alert('Error ' + t.status + ' -- ' + t.statusText);
}

var map = null;
var geocoder = null;

function loadMap(address, mapID) {

  if (GBrowserIsCompatible()) {
	map = new GMap2( $(mapID) );
	map.setCenter(new GLatLng(37.4419, -122.1419), 13);
	map.addControl(new GSmallMapControl());
	geocoder = new GClientGeocoder();
	showAddress(address, map, mapID);
  }

}

function showAddress(address, map, mapID) {
  if (geocoder) {
	geocoder.getLatLng(
	  address,
	  function(point) {
		if (!point) {
		  $(mapID).innerHTML = '<img src="../site_assets/css/imgs/300x200_undefined.jpg">';
		} else if (point) {
		  map.setCenter(point, 13);
		  var marker = new GMarker(point);
		  map.addOverlay(marker);
		}
	  }
	);
  }
}

/* connectLink - flow script */

function getTabs(id) {
	if ( $('loader') ) {
		$('loader').innerHTML = '<img src="site_assets/css/imgs/loading.gif">';
	}
	if ( timerID != null ) {
		clearTimeout(timerID);
		timerID = null;
	}

	var myAjax = new Ajax.Request(
			connectLink, 
			{
				parameters: 'access=public&action=tabs&id='+id+'&siteLink='+siteLink, 
				onComplete: function(response) {
					objTabs = eval ('(' + response.responseText + ')');
					drawPage ( objTabs, [ $('tab_menu'), $('container_center') ] );
				}
			});
			
}

function drawPage (pageObj, containers) {
	
	/* tabs  tab_menu */
	str = '<ul>';
		for( k = 0; k < pageObj['tabs'].length; k++ ) {
			if ( pageObj['tabs'][k]['a'] == 1 ) {
				str += '<li id="current" ><a href="#">' + pageObj['tabs'][k]['n'] + '</a></li>';
			} else {
				str += "<li><a href=\"JavaScript:getTabs(" + pageObj['tabs'][k]['id'] + ")\">" + pageObj['tabs'][k]['n'] + '</a></li>';
			}
		}
	str += '</ul><div id="loader"></div>';
	containers[0].innerHTML = str;
	containers[1].innerHTML = pageObj['page'];

	if ( $('loader') ) {
		$('loader').innerHTML = '';
	}
	curMap = 0;
	loadAllMaps();
	
}


var curMap = 0;
var timerID = null;
function loadAllMaps() {
	
	if ( $('form_Map' + curMap) ) {
		var add = $('addr' + curMap).value;
		loadMap ( add, 'map'+curMap );
		curMap++;
		
		if ( $('form_Map' + curMap) ) {
			timerID = setTimeout('loadAllMaps()', 1000);
		} else {
			clearTimeout(timerID);
		}
	
	} else {
		clearTimeout(timerID);
	}

}
