var map = null;
var basepin = null;

var basepin_big = null;
var basepin_med = null;
var basepin_small = null;
var hightlight = null;

function initmap( obj , controlMap )
{
	if (!(isdefined(controlMap))) controlMap = 1;
	var compatible = false;
	try
	{
		if (GBrowserIsCompatible()) compatible = true;
	}
	catch(e) 
	{ 
		reloadpage();
		return;
	}

	if (GBrowserIsCompatible()) {
	        map = new GMap2(obj);
		if (controlMap == 1) {
			//debug ('1');
			map.addControl(new GLargeMapControl());
			map.addControl(new GMapTypeControl());
		} else {
			//debug ('1');
		}
		mapwidth = obj.clientWidth;
		mapheight = obj.clientHeight;
		maptop = getTop( obj );
		mapleft = getLeft( obj );

		basepin = new GIcon();
		basepin.shadow = "tool/c_main/pin1shadow.png";
		basepin.image = "tool/c_main/pin1.png";
		basepin.iconSize = new GSize(41,37);
		basepin.shadowSize = new GSize(41, 37);
		basepin.iconAnchor = new GPoint(7, 36);  // from the pin base
		basepin.infoWindowAnchor = new GPoint(10, 5); // from the pin head
		basepin.infoShadowAnchor = new GPoint(10, 5); // from the pin head

		hightlight = new GIcon();
		hightlight.image = "tool/c_main/hightlight.png";
		hightlight.iconSize = new GSize(25,25);
		hightlight.iconAnchor = new GPoint(23, 12);  // from the pin base
		hightlight.infoWindowAnchor = new GPoint(23, 12); // from the pin head

		basepin_big = new GIcon();
		basepin_big.shadow = "tool/c_main/pin_shadow_med.png";
		basepin_big.image = "tool/c_main/pin_yellow_med.png";
		basepin_big.iconSize = new GSize(46,42);
		basepin_big.shadowSize = new GSize(46, 42);
		basepin_big.iconAnchor = new GPoint(8, 41);  // from the pin base
		basepin_big.infoWindowAnchor = new GPoint(12, 6); // from the pin head
		basepin_big.infoShadowAnchor = new GPoint(12, 6); // from the pin head

		basepin_med = new GIcon();
		basepin_med.shadow = "tool/c_main/pin_shadow_med.png";
		basepin_med.image = "tool/c_main/pin_yellow_med.png";
		basepin_med.iconSize = new GSize(41,37);
		basepin_med.shadowSize = new GSize(41, 37);
		basepin_med.iconAnchor = new GPoint(7, 36);  // from the pin base
		basepin_med.infoWindowAnchor = new GPoint(10, 5); // from the pin head
		basepin_med.infoShadowAnchor = new GPoint(10, 5); // from the pin head

		basepin_small = new GIcon();
		basepin_small.shadow = "tool/c_main/pin_shadow_small.png";
		basepin_small.image = "tool/c_main/pin_yellow_small.png";
		basepin_small.iconSize = new GSize(36,33);
		basepin_small.shadowSize = new GSize(36, 33);
		basepin_small.iconAnchor = new GPoint(6, 31);  // from the pin base
		basepin_small.infoWindowAnchor = new GPoint(9, 4); // from the pin head
		basepin_small.infoShadowAnchor = new GPoint(9, 4); // from the pin head
	}
}

function setmap( latitude, longitude )
{
	map.setCenter(new GLatLng(latitude, longitude), 13);
}

var pins = [];
var mapwidth, mapheight, maptop, mapleft;


function pinover( p )
{
	nobubble();
	clearTimeout( pintim );
	var pos = map.fromLatLngToContainerPixel(p.latlng);
	var str = '<table cellpadding=0 cellspacing=0 border=0 class=mapuser>';
	var tmp = [];
	for( var i = 0 ; i < pins.length ; i++ )
		if( pins[i].coord == p.coord )
			tmp.push( pins[i] );

	var addr = p.addr;
	var maxusr = 5;
	addr = '<a href="#" onmouseup="pinvenue(' + p.vid + ', ' + p.vsr + ')">' + addr;
	addr = addr.replace( /<\/b>/, '</b></a>' );
	more = '<a href="#" onmouseup="pinvenue(' + p.vid + ', ' + p.vsr + ')">(more)</a>';
	if( tmp.length > 1 )
	{
		var max = tmp.length > maxusr ? maxusr : tmp.length;
		for( i = 0 ; i < max ; i++ )
		{
			if( tmp[i].uid == 0 ) continue;
			str += '<tr><td><div class=square25 style="cursor: pointer" onclick="pinusr(' + tmp[i].uid + ', '+appsource+')"><img src="' + tmp[i].thumb + '" onload="imgsquare( this, 25 )"></td><td class=mapusertxt ><a href="javascript:pinusr(' + tmp[i].uid + ', '+appsource+')"><b>' + tmp[i].name + '</b></a><br>' + tmp[i].time + '</td></tr>';
		}
		if( tmp.length > maxusr ) str += '<tr style=""><td></td><td class=mapusertxt style="height: auto">' + more + '</td></tr>';
		str += '<tr><td></td><td class=mapusertxt><div class=sep></div>' + addr + '</td></tr>';
	}
	else
	{
		if( p.uid == 0 )
			str += '<tr><td></td><td class=mapusertxt>' + addr + '</td></tr>';
		else
			str += '<tr><td><div class=square25 style="cursor: pointer" onclick="pinusr(' + p.uid + ', '+appsource+')"><img src="' + p.thumb + '" onload="imgsquare( this, 25 )"></td><td class=mapusertxt><a href="#" onclick="pinusr(' + p.uid + ', '+appsource+')"><b>' + p.name + '</b></a><br>' + addr + '<br>' + p.time + '</td></tr>';
	}
	str += '</table>';
	bcont = sbubble( str, maptop + pos.y - 30, mapleft + pos.x + 3, 350, 0.9 );
	bcont.onmouseover = function() { clearTimeout( pintim ); };
	bcont.onmouseout = function() { pinout( p ) };
}

function pinvenue( vid , vsr, buzz)
{
	if( !isdefined( buzz ) ) buzz = '';
	if (rvbrowseNoAct == 1) {
		//topclick(apppath + 'p=comment&id=' + vid + '&sr=' + vsr + '&ext=0&atoken=' + auth_token + '&mydomain='+appdomain);
		topclick(apppath);
		return;
	}
	var url = nocache( 'comment.php?id=' + vid + '&sr=' + vsr + '&buzz=' + buzz + '&ext=0&atoken=' + auth_token + '&mydomain='+appdomain);
	document.location = url;
}


function pinusr( uid , usr, vis )
{
	if (rvbrowseNoAct == 1) {
		//topclick(apppath + 'p=comment&id=' + vid + '&sr=' + vsr + '&ext=0&atoken=' + auth_token + '&mydomain='+appdomain);
		topclick(apppath);
		return;
	}
	if (isdefined(vis) && vis == 0) { alert('This user is not your friend or public');  return; }
	if (!(isdefined(uid))) return;
	if (!(isdefined(usr))) usr = appsource;
	document.location = nocache( 'home.php?uid=' + uid + '&usr='+usr+'&ext=0&atoken=' + auth_token+'&mydomain='+appdomain);
}


var pintim = null;
function pinout( p )
{
	clearTimeout( pintim );
	pintim = setTimeout( function() { nobubble(); }, 100 );
//	debug( 'out', p.uid );
}

var pinicon;
var pinmarker;
function pinpoint( loc )
{
	var pos = loc.split(',');
	var latitude = pos[0];
	var longitude = pos[1];
	var point = new GLatLng(latitude, longitude);
	pinicon = new GIcon(hightlight);
	pinicon.image = "tool/c_main/hightlight_sel.png";

	var markerOptions = { icon:pinicon, zIndexProcess:function(marker,b) { return 0; } };
//	var markerOptions = { icon:pinicon };
	var marker = new GMarker( point, markerOptions );
	map.addOverlay(marker);
	pinmarker = marker;
}

function pinpointout()
{
	map.removeOverlay(pinmarker);
}

var pinplaces = Array();

function pinplaceout()
{
	while( pinplaces.length )
	{
		var p = pinplaces.pop()
		map.removeOverlay(p.marker);
		delete p;
	}
}

function pinplaceover( p )
{
	nobubble();
	clearTimeout( pintim );
	var pos = map.fromLatLngToContainerPixel(p.latlng);
	var str = '<table cellpadding=0 cellspacing=0 border=0 class=mapuser>';
	var addr = p.addr;
	addr = '<a href="comment.php?vid=' +  p.vid + '&vrid=' + p.vrid + '&sr=' + p.sr + '&atoken=' + auth_token + '&mydomain=' + appdomain + '&ext='+p.ext+'">' + addr;
	addr = addr.replace( /<\/b>/, '</b></a>' );
	str += '<tr><td></td><td class=mapusertxt>' + addr + '</td></tr>';
	str += '</table>';
	bcont = sbubble( str, maptop + pos.y, mapleft + pos.x, 350, 0.9 );
	bcont.onmouseover = function() { clearTimeout( pintim ); };
	bcont.onmouseout = function() { pinout( p ) };
}

function pinplace( loc, vid, vrid, sr, addr, flag, ext )
{
	var pos = loc.split(',');
	var latitude = pos[0];
	var longitude = pos[1];
	var point = new GLatLng(latitude, longitude);
	var pin = new GIcon(hightlight);
	pin.image = flag ? "tool/c_main/hightlight_sel.png" : "tool/c_main/hightlight.png";

//	var markerOptions = { icon:pin, zIndexProcess:function(marker,b) { return 900; } };
	var markerOptions = { icon:pin };
	var marker = new GMarker( point, markerOptions );
	map.addOverlay(marker);
	var p = new Object;
	if (ext != null) p.ext = ext;
	else p.ext = 0;
	p.marker = marker;
	p.vid = vid;
	p.vrid = vrid;
	p.sr = sr;
	p.addr = addr;
	p.latlng = point;

	GEvent.addListener(marker, "mouseover", function() { pinplaceover( p ); } );
	GEvent.addListener(marker, "mouseout", function() { pinout( p ); } );

	pinplaces.push( p );
}



function setpin( uid, latitude, longitude, me, name, thumb, vid, addr, time, vsr, active, isfriend )
{
	//debug( 'setpin', uid, latitude, longitude, me, name, thumb, vid, addr, time, pins.length );
	var p = null;
	var point = new GLatLng(latitude, longitude);
	for( var i = 0 ; i < pins.length ; i++ )
		if( pins[i].uid == uid )
		{
			p = pins[i];
			break;
		}
	if( p == null )
	{
		p = pins[pins.length] = new Object;
		p.marker = null;
	}
	var coord = latitude + ',' + longitude;
	coord = coord.replace( /[ \t\r\n]/, '' );

	me = myuid == uid;

	p.latitude = latitude;
	p.longitude = longitude;
	p.coord = coord;
	p.latlng = point;
	p.uid = uid;
	p.usr = appsource;
	p.vid = vid;
	p.vsr = vsr;
	p.name = name;
	p.thumb = thumb;
	p.addr = addr;
	p.time = time;
	p.active = active > 0;
	p.isfriend = isfriend;
	p.me = me;
	
	var samelocation = false;
	var haveme = false;
	var havefriend = false;
	var haveactive = false;
	var count = 0;

	for( var i = 0 ; i < pins.length ; i++ ) {
		tmpcoord = pins[i].coord;
		if( tmpcoord == coord )
		{
			if( pins[i].marker != null ) p.marker = pins[i].marker;
			samelocation = true
			count++;
			if( pins[i].me ) haveme = true;
			if( pins[i].isfriend ) havefriend = true;
			if( pins[i].active ) haveactive = true;
		}
	}
	if( 1 )
	{
		var color = 'blue';
		if( havefriend ) color = 'red';
		if( havefriend && haveactive ) color = 'green';
		if( haveme ) color = 'yellow';
		var size = 'small';
		var bpin = basepin_small;
		if( count >= 2 ) { size = 'med'; bpin = basepin_med; }
		if( count >= 3 ) { size = 'big'; bpin = basepin_big; }
		p.icon = new GIcon(bpin);
		p.icon.image = "tool/c_main/pin_" + color + "_" + size + ".png";
		if( p.marker != null ) map.removeOverlay(p.marker);
		var markerOptions = { icon:p.icon };
		var marker = new GMarker( point, markerOptions);

		GEvent.addListener(marker, "mouseover", function() { pinover( p ); } );
		GEvent.addListener(marker, "mouseout", function() { pinout( p ); } );
		map.addOverlay(marker);
		p.marker = marker;

	}

	//debug( name, p.coord, p.icon.image, 'haveme', haveme, 'havefriend', havefriend, 'haveactive', haveactive, 'count', count );
}

function setstatus( active, uid, usr, latitude, longitude, name, thumb, vid, addr, time, vsr )
{
//	debug( active, uid, latitude, longitude, name, thumb, vid, addr, time, vsr  );
	addr = addr.replace( /<\/b>/i, '</b></a>' );
	if( vid > 0 )
		$('mylocation').innerHTML = '<a href="javascript:pinvenue(' + vid + ', ' + vsr + ')">' + addr + '</a>';
	else
		$('mylocation').innerHTML = addr;
	setmap( latitude, longitude );
	setpin( uid, latitude, longitude, true, name, thumb, vid, addr, time, vsr, 1, true );
}

function setmystatus( active, uid, usr, latitude, longitude, name, thumb, vid, addr, time, vsr )
{
//	debug( active, uid, latitude, longitude, name, thumb, vid, addr, time, vsr  );
	/*
	addr = addr.replace( /<\/b>/i, '</b></a>' );
	if( vid > 0 )
		$('mylocation').innerHTML = '<a href="javascript:pinvenue(' + vid + ', ' + vsr + ')">' + addr + '</a>';
	else
		$('mylocation').innerHTML = addr;
	*/
	setmap( latitude, longitude );
	setpin( uid, latitude, longitude, true, name, thumb, vid, addr, time, vsr, 1, true );
}
