/*
	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;
var gBigOpenWindow=false;
henworx.MapCms.Ewindow= function(_loc,_marker,_map){
	
	//this._eStyleMapCmsDefault = null;
	this.eStyleMapCms = null;
	this.content=null;	
	this.color='default';
	this.stemPath=gPluginUrl+"/images/stem_mapcms_worxpro.png";
	this.stemSize=new GSize(51,34)
	this.cssClass="estyle_mapcms"
	this.locationFormat=_loc.shortLocationFormat
	this.merkzettelIcon = '';
	this.boxPositionX=51-220; //boxPositionX=sizeofStemX - sizeofboxX
	this.boxPositionY=32; //boxPositionY=sizeofStemY-2
	this.closeIcon='';
	this.maxIcon=''
	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()">'+_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="";		 
		_html = this.locationFormat;
		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);
		}
	}
