	
Object.extend(String.prototype, {
	trim:function() {
	  var sInString = this.replace( /^\s+/g, "" );// strip leading
	  return sInString.replace( /\s+$/g, "" );// strip trailing	
	}
});


if (!window.JONGO) 
	var JONGO = {};

JONGO.PopupSuggest = Class.create();
JONGO.PopupSuggest.prototype = {
	initialize: function() {
		var args = $A(arguments);
		this.element = $(args[0]);
		this.handler = $(args[1]);
		this.options = args[2] || {	};
		this.handler.style.cursor = 'pointer';
		
		this.initPopup();
		Event.observe(this.element, 'mouseover',this.__focus.bind(this), false);
		Event.observe(this.element, 'mouseout',this.__blur.bind(this), false);
		Event.observe(this.handler, 'mousedown', this.__show.bindAsEventListener(this),false);
		
		this.element.__hideEventListener = this.__hide.bindAsEventListener(this);
		
  	},
    
    initPopup:function() {
    	if (Effect.Dropshadow) {
    		if (!this.element._shadow) {
				this.element = Effect.Dropshadow(this.element);
				Element.hide(this.element);
				Element.show(this.element._original);
			}else{
				this.element = this.element._shadow;
			}
			if (!this.options.offestX)		
				this.options.offestX = 5;
			if (!this.options.offsetY)		
				this.options.offestY = 5;
		}
	    if(!this.element.style.position || this.element.style.position!='absolute') {
	       this.element.style.position = 'absolute';
	    }
  	},	
  	
  	show:function() {
  		this.__show();
  	},

	hide:function() {
		this.__blur();
		this.__hide();
	},
  	
  	__focus:function() {
  		this.hasFocus = true;
  	},
  	
  	__blur:function() {
 		this.hasFocus = false;  	
  	},
  	  	
  	__show:function(event) {
		if (this.options.width) {
			var target = this.element._original || this.element;
			target.style.width = this.options.width+'px';
		}  	
  		var pos = Position.positionedOffset(this.handler);
		var eh = Element.getDimensions( this.handler ).height;
		var ew =  ( this.element._original || this.element ).style.width.replace(/px/,'') - Element.getDimensions( this.handler ).width;
  		this.element.style.left = (pos[0]-ew + this.options.offestX)+'px';
  		this.element.style.top = (pos[1]+eh + this.options.offestY)+'px';

		if (this.element.__hideEventListener) {
			this.__unregisterHideListener();
			Element.hide(this.element);
		}
      	Effect.Appear(this.element,{duration:0.15});
      	//Element.show(this.element);
      	if (!this.element.__timeout) {
	      	this.element.__timeout = setTimeout(this.__registerHideListener.bind(this),150);
	    }
	    if (this.options.onShow) {
	    	this.options.onShow.call(this);
	    }
	    JONGO.PopupSuggest.current = this;
  	},
  	
  	__hide:function(event) {
  		if (this.hasFocus) 
  			return;
  		Effect.Fade(this.element,{duration:0.15});	
  		//Element.hide(this.element);
  		this.__unregisterHideListener();
	    if (this.options.onHide) {
	    	this.options.onHide.call(this);
	    }  
	    JONGO.PopupSuggest.current = null;		
  	},
  	
  	
  	__registerHideListener:function() {
  		Event.observe(document, 'click', 
      				this.element.__hideEventListener,
      				false);
  	},
  	
  	__unregisterHideListener:function() {
  		Event.stopObserving(document, 'click', 
  						this.element.__hideEventListener,
  						false);
  		if (this.element.__timeout) {
	  		clearTimeout(this.element.__timeout);
	  		this.element.__timeout = null;
	  	}
  	}
}

JONGO.MONTHS = new Array("January", "February", "March", 
"April", "May", "June", "July", "August", "September", 
"October", "November", "December");


// Remote JST Wrapper,
// Usage:
// new JONGO.RemoteJST('template.jst',data, onComplete);
JONGO.RemoteJST = Class.create();
JONGO.RemoteJST.prototype = {
	initialize:function(file, data, callback) {
		this.file = file;
		this.data = data;
		this.callback = callback;
		if (!window.__jst) {
			window.__jst = [];
		}
		if (window.__jst[file]) {
			this.__parse(window.__jst[file]);
		}else{
			var instance = this;
			new Ajax.Request(this.file, {
				onSuccess:function(req) {
					var jst = req.responseText;
					instance.__parse(jst);
					window.__jst[file] = jst;
				}
			});
		}
	},
	
	__parse:function(jst) {
		this.data._MODIFIERS = JONGO.RemoteJST.MODIFIERS;
		var result = jst.process(this.data);
		this.callback(result, jst);
	}
}
JONGO.RemoteJST.MODIFIERS = {
	encodeURIComponent:function(s) {
		return encodeURIComponent(s);
	}
}

JONGO.getCookie = function(Name) {
	var search = Name + "=";
	if(document.cookie.length > 0) {
		offset = document.cookie.indexOf(search);
		if(offset != -1) {
			offset += search.length;
			end = document.cookie.indexOf(";", offset);
			if(end == -1) end = document.cookie.length;
			return unescape(document.cookie.substring(offset, end));
		} else {
			return('');
		}
	} else {
		return('');
	}
}



JONGO.setCookie = function(name,value) {
	var today = new Date();
	var expires = new Date();
	expires.setTime(today.getTime() + 1000*60*60*24*365);
	document.cookie = name + "=" + escape(value) + ";path=/"+"; expires=" + expires.toGMTString();
}


JONGO.Map = new function() {
	this.small = function(pid, city,width, height,scale) {
		var map = Builder.node('iframe', {
						src:'http://211.157.24.133:8080/api/smap.jsp?pid='+pid+'&cityCode='+city+'&width='+(width)+'&height='+(height)+'&scale='+scale,
						frameborder:'0', marginheight:'0', marginwidth:'0', scrolling:'no',
							style:'width:'+width+'px;height:'+height+'px;'});	
		return map;														
	}, 

	this.large = function(pid, city,width, height,scale) {
		var map = Builder.node('iframe', {
						src:'http://211.157.24.133:8080/api/showmap.jsp?id='+pid+'&cityCode='+city+'&width='+(width-24)+'&height='+(height-24)+'&scale='+scale,
						frameborder:'0', marginheight:'0', marginwidth:'0', scrolling:'no', width:width, height:height});	
		return map;														
	}, 

	this.load = function(options, onLoading, onComplete) {
		var map = this[options.type||'small'].call(this,options.pid, options.city, options.width, options.height, options.scale || 7);
		map.style.width = '0';
		map.style.height = '0';
		map.style.visibility = 'hidden';
		onLoading.call(this,map);
		Event.observe(map,'load', function() {
			map.style.visibility = 'visible';
			map.style.width = options.width+'px';
			map.style.height = options.height+'px';			
			onComplete.call(this,map);
			if (options.onComplete) {
				options.onComplete.call(this);
			}
		}, false);		
	},
	
	// replace content of the container with a map
	this.replace = function(container, options) {
		container = $(container);
		options.width = options.width || Element.getDimensions(container).width;
		options.height = options.height ||  Element.getDimensions(container).height;	
		var progress_id = 'load-map-progress_'+(new Date()).getTime();
		container.innerHTML = '<span id="'+progress_id+'" style="display:block;margin-left:'+(options.width / 2 - 50)+'px;margin-top:'+(options.height / 2 - 10)+'px"><img src="'+JONGO.Config.ContextPath+'/images/spinner_long.gif"/><br />Please wait...</span>';
		this.load(options,function(map) {
			container.appendChild(map);		
		}, function(map) {
			Element.hide(progress_id);
		});
	},
	
	
	// pop up a map just beside the handler
	this.popup = function(handler, options) {
		handler = $(handler);
		var wrapper;
		if (options.type == 'large') {
			options.width = options.width || 380;
			options.height = options.height || 300;
			wrapper = Builder.node('div', {style:'background-color:#fff;display:none;position:absolute;z-index:999;width:'+(options.width)+'px;height:'+(options.height)+'px;border:1px solid #415c8e;padding:1px'});
		}else{
			options.width = options.width || 284;
			options.height = options.height || 180;
			wrapper = Builder.node('div', {style:'background-color:#fff;display:none;position:absolute;z-index:999;width:'+(options.width+6)+'px;height:'+(options.height+6)+'px;border:1px solid #415c8e;padding:1px'});
		}
		
	
  		var pos = Position.positionedOffset(handler);
		var eh = options.offsetY || Element.getDimensions( handler ).height;
		var ew = options.offsetX || Element.getDimensions( handler ).width;
		document.body.appendChild(wrapper);
		var progress_id = 'load-map-progress_'+(new Date()).getTime();
		wrapper.innerHTML = '<span id="'+progress_id+'" style="display:block;margin-left:'+(options.width / 2 - 50)+'px;margin-top:'+(options.height / 2 - 10)+'px"><img src="'+JONGO.Config.ContextPath+'/images/spinner_long.gif"/><br />Please wait...</span>';
		this.load(options,function(map) {
			if (options.toolbar) {
					var toolbar = Builder.node('div');
					toolbar.innerHTML = options.toolbar;
					wrapper.appendChild(toolbar);	
					var dim = Element.getDimensions(toolbar);
					wrapper.height += dim.height;
					toolbar.hide();
					wrapper._toolbar = toolbar;
			}			
			wrapper.appendChild(map);		
		}, function(map) {
			Element.hide(progress_id);
			wrapper._toolbar.show();
			if (options.type == 'large') {
				wrapper.style.borderWidth = 0;
			}
		});
		
		
		var hide = function() {
			Event.stopObserving(document, 'click', 
  						this.__hideEventListener,
  						false);
  			var o = this;
			Effect.Fade(this, {duration:0.15, afterFinish:function() {
				document.body.removeChild(o);
			}});
		}
		wrapper.__hideEventListener = hide.bindAsEventListener(wrapper);
		
		setTimeout(function() {
			Event.observe(document,'click',wrapper.__hideEventListener,false);
		},150);
		
		
	    if(!wrapper.style.position || wrapper.style.position=='absolute') {
	       wrapper.style.position = 'absolute';
	    }		
	    wrapper.style.left = (pos[0]+ew)+'px';
  		wrapper.style.top = (pos[1]+eh)+'px';
      	Effect.Appear(wrapper,{duration:0.15});
		
	}
}

JONGO.Config = {};
JONGO.Config.ContextPath = '';

