/*
	To change the looks of EWindow
	you can set new EWindow Style in global variable globalEStyleMapCms
	following parameteres is needed
	
	
	globalEStyleMapCms=new EStyle(strIconImage, new GSize(iWidthOfStem,iHeightOfStem),strCssClassName, new GPoint(iBoxoffsetX,iBoxoffsetY));
	
	hint boxoffset ic calulated as
	boxoffsetX=sizeoficonX - sizeofboxX
	boxoffsetY=sizeoficonY-2
	
	
*/
var gTimerHideWindow;
var gOpenEWindow;
henworx.MapCms.Ewindow= function(_loc,_marker,_map){
	
	//this._eStyleMapCmsDefault = null;
	this.eStyleMapCms = null;
	this.content=null;	
	this.color='default';
	_lang = henworx.MapCms.Lang;
	this.stemPath=gPluginUrl+"/includes/ewindow/stem_mapcms_default.png";
	this.stemSize=new GSize(51,34)
	this.cssClass="estyle_mapcms"
	this.locationFormat=_loc.shortLocationFormat
	this.merkzettelSaveIcon = _lang.getWord('Save to My List');
	this.merkzettelDeleteIcon = _lang.getWord('Delete from My List');
	this.boxPositionX=51-220; //boxPositionX=sizeofStemX - sizeofboxX
	this.boxPositionY=32; //boxPositionY=sizeofStemY-2
	this.closeIcon='<img src="'+gPluginUrl+'/includes/ewindow/close.gif" class="close_ewindow" />';
	this.maxIcon='<img src="'+gPluginUrl+'/includes/ewindow/max.gif" class="open_big_window" />'
	this.stemPointer=false	
	var _ewindow= null;
	
	this.init = function (){
		//initalize Ewindow 
		//having problem with box position? 
		//check and be sure that HTMl is in strict mode otherwise the border of ewindow needs adjustment
		_eStyleMapCmsDefault = new EStyle(this.stemPath, this.stemSize, this.cssClass , new GPoint(this.boxPositionX,this.boxPositionY));
		if(this.eStyleMapCms==null){
			if(globalEStyleMapCms){
				this.eStyleMapCms=globalEStyleMapCms;
			}else{
			 	this.eStyleMapCms=_eStyleMapCmsDefault;
			 }
		 
		}
		_eWindow=new EWindow(_map, this.eStyleMapCms);
		if(!this.stemPointer){
			this.stemPointer=new GPoint(this.eStyleMapCms.stemSize.width*(-1),0)
		}
		this.makeContent();
	}
	//if you don't call this system will use global; settings
	this.setStem=function(){
		this.eStyleMapCms = new EStyle(this.stemPath, this.stemSize,  this.cssClass, new GPoint(this.boxPositionX,this.boxPositionY));
		this.init();
	}
	
	this.setContent=function(_content){
		
		this.content= '<div class="mapcms_small_ewindow '+this.color+'" id="mapcms_small_ewindow" onmouseover="henworx.MapCms.Ewindow.cancelHide()" onmouseout="henworx.MapCms.Ewindow.hide()">'+
						'<a href="javascript:void(0)" onclick="henworx.MapCms.BigWindow.maximize(\''+_loc.id+'\'); return false;" title="'+_lang.getWord('Maximize')+'" class="max_icon">'+this.maxIcon+'</a>'+
						'<a href="javascript:void(0)" onclick="henworx.MapCms.Ewindow.hide(\'immediate\'); return false;" title="'+_lang.getWord('Close')+'" class="close_icon">'+this.closeIcon+'</a> '+
								_content
						+"</div>"
								
								
	}
	//user overrides to get new type of content
	/*example usage
	 myEWindow= new henworx.MapCms.Ewindow();
	 myEWindow.makeContent=function(_loc,_marker,_map){
							 myEWindow.setContent("<h1>"+_loc.title+"</h1>");	
							}
	 myEwindow.show();
	 myEwindow.pan(); //pans the map show that Ewindow is shown ;
	*/
	this.makeContent=function(){
		_html="";
		 
		_more = '<a href="javascript:void(0)" onclick="henworx.MapCms.BigWindow.maximize(\''+_loc.id+'\')" title="'+_lang.getWord('more')+'" class="more_big_window" >'+_lang.getWord('more')+'</a>';
		_html = this.locationFormat;
		_html = _html.replace('#title#',_loc.title); 
		_html = _html.replace('#image#',_loc.image);
		_html = _html.replace('#logo#',_loc.logo);
		_html = _html.replace('#headshot#',_loc.headshot);
		_html = _html.replace('#excerpt#',_loc.excerpt);		
		_html = _html.replace('#more#',_more);	
		_merkzettel_save_icon = this.merkzettelSaveIcon;
		_merkzettel_delete_icon = this.merkzettelDeleteIcon;
		
		_html = _html.replace('#icon#','<img src="'+_loc.icon.image+'" class="ewindow-icon" />');
		_merkzettel_link = '';
		if(gMyListEnabled){
			if(isMerkzettelSaved(_loc.id)){
				//delete link
				_delete_merkzettel_link = 'style="display:inline;"';
				_add_merkzettel_link = 'style="display:none;"';			
			}else{
				//save link	
				_delete_merkzettel_link = 'style="display:none;"';
				_add_merkzettel_link = 'style="display:inline;"';
			}
			_merkzettel_link += '<a href="javascript:void(0)" id="delete_merkzettel_ewindow_link_'+_loc.id+'" '+_delete_merkzettel_link+' class="merkzettel_link delete" title="'+_lang.getWord('Delete from My List')+'" onclick="deleteMerkzettel(\''+_loc.id +'\'); return false;">'+_merkzettel_delete_icon+'</a>';
			_merkzettel_link += '<a href="javascript:void(0)" id="add_merkzettel_ewindow_link_'+_loc.id+'" '+_add_merkzettel_link+' class="merkzettel_link" title="'+_lang.getWord('Save to My List')+'" onclick="storeMerkzettel(\''+_loc.id +'\',\'\',\''+gDefaultLang+'\'); return false;">'+_merkzettel_save_icon+'</a>';
			
		}
			
	
			_html = _html.replace('#merkzettel#', _merkzettel_link);
	
		for(i=0;i<henworx.customFields.length;i++){
				_html = _html.replace('#'+henworx.customFields[i]+'#',_loc.getCustomField(henworx.customFields[i]));

		}
		this.setContent(_html)
	}
	//make default coantents
	
	
	//show
	
	
	this.show=function (){
		
		_map.addOverlay(_eWindow);
		_html= this.content;
		_offset=this._calculateStemOffset()
		
	     //having problem with box position? 
		 //check and be sure that HTMl is in strict mode otherwise the border of ewindow needs adjustment
		_eWindow.openOnMap(_loc.latlng, _html,_offset);
		_marker.EWindow=_eWindow;
		gOpenEWindow=_eWindow;
		
		// Ewindow should not hidden when mouse is over it
		_eWindow.div2.onmouseover=function(){
			henworx.MapCms.Ewindow.cancelHide()
		}
		_eWindow.div2.onmouseout=function(){
			henworx.MapCms.Ewindow.hide()
		}
		
	}
	
	this.pan=function(){
		
		divMap=gMapCms.mapDiv;
		var myP=_map.fromLatLngToContainerPixel(_loc.latlng)
		 
		 divWidth=_eWindow.div1.clientWidth;
		 divHeight=_eWindow.div1.clientHeight+75;
		 divMapWidth=divMap.clientWidth
		 divMapHeight=divMap.clientHeight
		 
		panLeft=0;
		panBottom=0;
		if(divWidth >myP.x){
			panLeft=divWidth-myP.x;
		}
		if(divHeight >myP.y){
			panBottom=divHeight-myP.y;
		}
		 if(myP.x > divMapWidth){
			panLeft=divMapWidth-myP.x-50;
		}
		if(myP.y>divMapHeight){
			panBottom=divMapHeight-myP.y-50;
		}
		 
		 panSize= new GSize(panLeft,panBottom)
		
		 if(Math.abs(panLeft)>2000 || Math.abs(panBottom)>2000){
		  
		  _map.panTo(_loc.latlng);
		 }else{
		 	_map.panBy(panSize);
		 }

	}
	
	
	
	//private function to claculate offset of stem
	this._calculateStemOffset=function(){
		//position stem on latlng
		posX=this.stemPointer.x;
		posY=this.stemPointer.y;
		
		//position stem on topleft of icon
		if(_loc.icon.iconAnchor.x)
		{
			posX= posX-_loc.icon.iconAnchor.x
		}
		if(_loc.icon.iconAnchor.y)
		{
			posY= posY+_loc.icon.iconAnchor.y
		}
		
		//position stem on infoWindowAnchor
		if(_loc.icon.infoWindowAnchor.x)
		{
			posX= posX+_loc.icon.infoWindowAnchor.x
		}
		if(_loc.icon.infoWindowAnchor.y)
		{
			posY= posY-_loc.icon.infoWindowAnchor.y
		}
		
		return new GPoint(posX,posY);
	}
}

henworx.MapCms.Ewindow.hide=function(immediate){
	if(typeof(gOpenEWindow)=='undefined'){ return false;}
	
		if(immediate=="immediate"){
			gOpenEWindow.hide();
		}else{
			henworx.MapCms.Ewindow.cancelHide();
			gTimerHideWindow=window.setTimeout('gOpenEWindow.hide()',800);
		}
	}
	
henworx.MapCms.Ewindow.cancelHide=function(){
	if(typeof(gTimerHideWindow)=='undefined'){ return false;}
		
		if(gTimerHideWindow){
	   		window.clearTimeout(gTimerHideWindow);
		}
	}