// TPhoto() GMaps API extension copyright 2005-2006 Tom Mangan
// http://gmaps.tommangan.us/tphoto.html
// free for non-commercial use

//-----------------------------------
// TPhoto Object Definition 
//-----------------------------------
function TPhoto(){}
TPhoto.prototype.initialize=function(a){
	this.parentMap=a;
	var b=document.createElement('img');
	b.style.display='none';
	b.setAttribute('id',this.id);
	b.onload = this.onload ;
	b.setAttribute('src',this.src);
	b.setAttribute('TITLE',this.title);
	b.style.position='absolute';
	b.style.zIndex=1;
	this.mapTray=a.getPane(G_MAP_MAP_PANE);
	this.mapTray.appendChild(b);
	this.setPosition(a);
	b.style.display='block';
	if(this.percentOpacity){this.setOpacity(this.percentOpacity);}
	GEvent.bind(a,"zoomend",this,function(){this.setPosition(a)});
	GEvent.bind(a,"moveend",this,function(){this.setPosition(a)});
}
TPhoto.prototype.setPosition=function(a){
	var d=this.parentMap.fromLatLngToDivPixel(this.anchorTopLeft);
	var e=this.parentMap.fromLatLngToDivPixel(this.anchorBottomRight);
	var x=document.getElementById(this.id);
	x.style.top=d.y+'px';
	x.style.left=d.x+'px';
	x.style.width=e.x-d.x+'px';
	x.style.height=e.y-d.y+'px';
}
TPhoto.prototype.setOpacity=function(b){
	if(b<0){b=0;}  if(b>=100){b=100;}
	var c=b/100;
	var d=document.getElementById(this.id);
	if(typeof(d.style.filter)=='string'){d.style.filter='alpha(opacity:'+b+')';}
	if(typeof(d.style.KHTMLOpacity)=='string'){d.style.KHTMLOpacity=c;}
	if(typeof(d.style.MozOpacity)=='string'){d.style.MozOpacity=c;}
	if(typeof(d.style.opacity)=='string'){d.style.opacity=c;}
}
TPhoto.prototype.hide=function(c){
	document.getElementById(this.id).style.display='none';
}
//-----------------------------------
// Functions added in GMAP
//-----------------------------------
GMap2.prototype.addTPhoto=function(a){
	a.initialize(this);
}
GMap2.prototype.removeTPhoto=function(a){
	var b=document.getElementById(a.id);
	this.getPane(G_MAP_MAP_PANE).removeChild(b);
	delete(b);
}

//-----------------------------------
// TPhotoManager object definition.
//-----------------------------------
function TPhotoManager(map){
	this.map = map;
	this.TPhotoList = new Array(); 	// [TphotoObject,TphotoObject...]
	this.swapList = new Array(); 	// {"countryName":{"srcT":[src,src,..], ix:0},"countryName":{"srcT":[src,src,..], ix:0}}
	this.imgsLoaded = 0 ;        	// Numbres of loaded images.
	this.imgsTobeLoad = 0 ;			// Numbres of images to be load. 
}

// 
TPhotoManager.prototype.imagesOnLoad = function (tpManager) {
	tpManager.imgsLoaded++ ; 
	
	if( tpManager.imgsLoaded == tpManager.imgsTobeLoad){
		this.imagesLoaded() ; 
	}
} 

// Add a TPhot in the Manager and in the GMAP. 
TPhotoManager.prototype.addTPhoto=function(id,goUrl,goN, goS, goE, goW, goZoom, goName, goUsername){
	var photo = new TPhoto();
	photo.id = id;
	photo.src = goUrl ;
	photo.percentOpacity = 100;
	photo.anchorTopLeft = new GLatLng(goN,goW);
	photo.anchorBottomRight = new GLatLng(goS,goE);
	photo.title = 'Made by '+goName + ' ' + goUsername ;

	this.TPhotoList.push(photo);
	this.map.addTPhoto(photo);
	photo.onload = this.imagesOnLoad(this) ; 
}

// Remove a TPhoto or a TPhoto List from the GMAP.
TPhotoManager.prototype.remove=function(tphoto){

	if( tphoto ){
		this.map.removeTPhoto(tphoto);	
		this.TPhotoList[this.TPhotoList.indexOf(tphoto) ] = null ; 	
	}else { 
		for( var i = 0 ; i < this.TPhotoList.length ; i++ ) 
		{
			this.map.removeTPhoto( this.TPhotoList[i] ) ; 
			this.TPhotoList[i] = null ; 
		}
	}
	this.TPhotoList = this.TPhotoList.compact() ; 	
}

TPhotoManager.prototype.showHide=function(id){

	if(id){
		if(document.getElementById(id).style.display == "none")
			document.getElementById(id).style.display = "block";
		else
			document.getElementById(id).style.display = "none";
	}else {
		for( var i = 0 ; i < this.TPhotoList.length ; i++ ) 
		{
			if(document.getElementById(this.TPhotoList[i].id).style.display == "none")
				document.getElementById(this.TPhotoList[i].id).style.display = "block";		
			else
				document.getElementById(this.TPhotoList[i].id).style.display = "none";
		}	
	}
}
TPhotoManager.prototype.hide=function(id){

	if(id){
		document.getElementById(id).style.display = "none";
	}else {
		for( var i = 0 ; i < this.TPhotoList.length ; i++ ) 
		{
			this.TPhotoList[i].hide() ; 
		}	
	}
}
// Add a TPhoto in the swap List.
TPhotoManager.prototype.addSwapList=function(id, goUrl) {
	if( !this.swapList[id] || !this.swapList[id].srcT ){
		this.swapList[id] = new Object() ; 
		this.swapList[id].srcT = new Array();
		this.swapList[id].index = 1;  
	}
	this.swapList[id].srcT.push(goUrl);  
}

TPhotoManager.prototype.swapImg=function(id,marker){
	//var src = marker.getIcon().image ; 
	//marker.setImage(cpath+"modules/"+moduleName+"/scripts/upload/loader2.gif");  
	document.getElementById(id).src = this.swapList[id].srcT[this.swapList[id].index];	
	//document.getElementById(id).onload = marker.setImage(src);  

	this.swapList[id].index++;
	if( this.swapList[id].index >= this.swapList[id].srcT.length)
		this.swapList[id].index = 0 ; 
}