function customStopPropagation(event){
	if (event.stopPropagation)//this code is for Mozilla and Opera
		event.stopPropagation();
	else if (window.event)// this code is for IE
		window.event.cancelBubble = true;
}
function closeTroughBody(){
	jQuery('body').click();
}
function closedByBody(clickTarget){
	jQuery('body').bind('click', function(event){
		clickTarget.trigger('click');
		jQuery(this).unbind('click');
	});
};
function getPosforGMAP(el){
	var r = new String;
	if(el.parent().get(0).tagName.toLowerCase() == 'li')
		el = el.parent();
	else
		el = el.parent().parent();
	return {top:el.position().top,left:el.position().left};
}
function getGMAP(params){
	this.map_options = {};
	this.resourceImages = {image:{},shadow:{}};
	this.params = {};
	this.group_markres = {};
	this.clusters = {};
	this.checkParameter = function(params,param, alert_){
		var res = true;
		if((typeof(params) == 'undefined') || (typeof(params[param]) == 'undefined')){
			if(typeof(params) == 'undefined')
				return;
			if(alert_ != false)
				alert('missing param (params.'+param+')');
			res = false;
		}
		return res;
	};
	
	this.getIconMarker = function(type,size){
		var img = {};
		if(typeof(this.resourceImages.image[type]) == 'undefined'){
			img.image = new google.maps.MarkerImage('images/google/icons/markers/'+type+'.png',
					new google.maps.Size(size[0], size[1]),// This marker is 20 pixels wide by 32 pixels tall.
					new google.maps.Point(0,0),// The origin for this image is 0,0.
					new google.maps.Point(0,0)// The anchor for this image is the base of the flagpole at 0,32.
			);
			img.shadow = new google.maps.MarkerImage('images/google/icons/markers/shadow_'+type+'.png',
					new google.maps.Size(size[0], size[1]),
					new google.maps.Point(0,0),
					new google.maps.Point(-2,-2)
			);
			this.resourceImages.image[type] = img.image;
			this.resourceImages.shadow[type] = img.shadow;
		}else{
			img.image = this.resourceImages.image[type];
			img.shadow = this.resourceImages.shadow[type];
		}
		return img;
	};
	
	this.getCustomWindowOptions = function(text,o_){
		var boxText = document.createElement("div");
		boxText.style.cssText = "border: 1px solid black; margin-top: 8px; background: #fff; padding: 5px;";
		boxText.innerHTML = '<div class="cust-html-window-in">'+text+'</div>';
		var obj = {
			content: boxText,
			disableAutoPan: false,
			maxWidth: 0,
			pixelOffset: new google.maps.Size(30, -50),
			zIndex: null,
			boxStyle: { 
				background: "#fff",
				opacity: 1,
				width: "280px"
			},
			closeBoxMargin: "10px 2px 2px 2px",
			closeBoxURL: "http://www.google.com/intl/en_us/mapfiles/close.gif",
			infoBoxClearance: new google.maps.Size(1, 1),
			isHidden: false,
			pane: "floatPane",
			enableEventPropagation: false
		};
		if(this.checkParameter(o_,'content', false))
			obj.content = o_.content;
		if(this.checkParameter(o_,'disableAutoPan', false))
			obj.disableAutoPan = o_.disableAutoPan;
		if(this.checkParameter(o_,'maxWidth', false))
			obj.maxWidth = o_.maxWidth;
		if(this.checkParameter(o_,'zIndex', false))
			obj.zIndex = o_.zIndex;
		if(this.checkParameter(o_,'closeBoxMargin', false))
			obj.closeBoxMargin = o_.closeBoxMargin;
		if(this.checkParameter(o_,'closeBoxURL', false))
			obj.closeBoxURL = o_.closeBoxURL;
		if(this.checkParameter(o_,'cssText', false)){
			obj.content.style.cssText = o_.cssText;
		}
		
		if(this.checkParameter(o_,'boxStyle', false)){
			if(this.checkParameter(o_.boxStyle,'background', false))
				obj.boxStyle.background = o_.boxStyle.background;
			if(this.checkParameter(o_.boxStyle,'opacity', false))
				obj.boxStyle.opacity = o_.boxStyle.opacity;
			if(this.checkParameter(o_.boxStyle,'width', false))
				obj.boxStyle.width = o_.boxStyle.width;
		}
		return obj;
	};
	
	this.myCustmWindowCreate = function(params,latlng,map){
		var o_ = {};
		if(this.checkParameter(params,'custom_html_window', false))
			o_ = params.custom_html_window;
		else
			o_ = this.map_options.custom_html_window;
		
		if(!o_){
			infowindow = new google.maps.InfoWindow();
			infowindow.setContent(params.name);
			infowindow.setPosition(latlng);
			return infowindow;
		}else{
			return new InfoBox(this.getCustomWindowOptions(params.name,o_));
		}
	};
	
	this.getMapMarker = function(params,index, map){
		var latlng = new google.maps.LatLng(params.lat,params.lng);
		var obj_ = this;
		var icon = this.getIconMarker(params.type,((typeof(params.size) == 'undefined')?[19,25]:params.size));
		var marker = new google.maps.Marker({
			position: latlng, 
			'map': map,
			shadow:icon.shadow,
			icon:icon.image
		});
		//adding all paarkers into array
		if(typeof(this.group_markres[params.type]) == 'undefined'){
			this.group_markres[params.type] = [];
		}
		this.group_markres[params.type].push(marker);
		marker.htmlwin = this.myCustmWindowCreate(params,latlng,map);

		if((typeof(params.showhtmlwindow) == 'undefined') || (params.showhtmlwindow == 'click')){
			google.maps.event.addListener(marker, 'click', function(e){
				obj_.closeArllMarkersHtmlwin();
				this.htmlwin.open(map, marker);
			});
		}else if(params.showhtmlwindow == 'over'){
			google.maps.event.addListener(marker, 'mouseover', function(e){
				obj_.closeArllMarkersHtmlwin();
				this.htmlwin.open(map, marker);
			});
		}else if(params.showhtmlwindow == 'overout'){
			google.maps.event.addListener(marker, 'mouseover', function(e){
				this.htmlwin.open(map, marker);
			});
			google.maps.event.addListener(marker, 'mouseout', function(e){
				this.htmlwin.close(map);
			});
		}
	};
	
	this.getGmapContainer = function(){
		return jQuery('<div><div id="layer-front"></div><div id="layer-back"></div>').attr('id', this.params.container).css({zIndex:10}).click(function(event){event.stopPropagation();});
	};
	
	this.setMarkerVisibility = function(type, visibility){
		visibility = (typeof(visibility) == 'undefined')?10:visibility;
		if(type == 'undefined'){
			for(type in this.group_markres){
				var ml = this.group_markres[type].length;
				for ( var int = 0; int < ml; int++){
					if(visibility == 10){
						if((typeof(this.group_markres[type][int].visible) == 'undefined') || (this.group_markres[type][int].visible == true))
							this.group_markres[type][int].setVisible(false);
						else
							this.group_markres[type][int].setVisible(true);
					}else
						this.group_markres[type][int].setVisible(visibility);
				}
			}
		}else if(typeof(this.group_markres[type]) == 'undefined'){
			this;	
		}else{
			var ml = this.group_markres[type].length;
			for ( var int = 0; int < ml; int++){
				if(visibility == 10){
					if((typeof(this.group_markres[type][int].visible) == 'undefined') || (this.group_markres[type][int].visible == true)){
						this.group_markres[type][int].setVisible(false);
					}else{
						this.group_markres[type][int].setVisible(true);
					}
				}else
					this.group_markres[type][int].setVisible(visibility);
			}
		}
		return this;
	};
	
	this.hideMarkers = function(type){
		this.setMarkerVisibility(type, false);
	};
	
	this.showMarkers = function(type){
		this.setMarkerVisibility(type, true);
	};
	
	this.unsetGmapContainer = function(){
		var cont = jQuery('#'+this.params.container);
		if(typeof(cont.attr('id')) != 'undefined'){
			jQuery('#'+this.params.container).remove();
			return true;
		}
		return false;
	};
	
	this.closeArllMarkersHtmlwin = function(){
		for(i in this.group_markres){
    		for(ii in this.group_markres[i]){
    			if(typeof(this.group_markres[i]) == 'undefined')
    				continue;
    			if((typeof(this.group_markres[i][ii].htmlwin) == 'object') && (typeof(this.group_markres[i][ii].htmlwin.close) == 'function')){
    				this.group_markres[i][ii].htmlwin.close();
    			}
    		}
    	}
	};
	
	this.initMap = function(HTML_container,params){
		var obj = this;
		this.map_options.center = new google.maps.LatLng(params.map_ceter[0], params.map_ceter[1]);
		this.map_options.zoom = 12;
		this.map_options.custom_html_window = false;
		if(this.checkParameter(params,'zoom', false)){
			this.map_options.zoom = params.zoom;
		}
		if(this.checkParameter(params,'custom_html_window', false)){
			this.map_options.custom_html_window = params.custom_html_window;
		}
		this.map_options.mapTypeId = google.maps.MapTypeId.ROADMAP;
		var map = new google.maps.Map(HTML_container.get(0), this.map_options);
	    for(i in params.poi){
	    	obj.getMapMarker(params.poi[i],i, map);
	    }
	    google.maps.event.addListener(map, 'click', function(e){
	    	obj.closeArllMarkersHtmlwin();
		});
	    this.onMapLoad_();
	};
	
	this.setPosition = function(element){
		element.css({position:'absolute',top:0,left:0});
	};
	
	this.setLayerBackContent = function(layer, params){
		if(this.checkParameter(params,'layer_back', false)){
			layer.append(params.layer_back);
		}
	};
	
	this.onMapLoad_ = function(){
		if(typeof(params.onMapLoad) == 'function')
			params.onMapLoad(this);
	};
	
	this.init = function(){
		if(!this.params.totalResult)
			return false;
		var obj = this;
		if(true === this.unsetGmapContainer())
			return false;
		var gmapHolder = this.getGmapContainer();
		this.setPosition(gmapHolder);
		jQuery(this.params.attach).after(gmapHolder);
		if((typeof(google) == 'undefined') || (typeof(google.setOnLoadCallback) == 'undefined')){
			jQuery.getScript('http://www.google.com/jsapi?key='+google_api_key, function(){
				google.load("maps","3.x",{
					"other_params":"sensor=false",
					"callback":function(){
						jQuery.getScript('js/infobox.js',function(){
							obj.initMap(gmapHolder.find('#layer-front'),params);
							obj.setLayerBackContent(gmapHolder.find('#layer-back'),params);
						});
					}
				});
			});
		}else{
			obj.initMap(gmapHolder.find('#layer-front'),params);
			obj.setLayerBackContent(gmapHolder.find('#layer-back'),params);
		}
		return gmapHolder;
	};
	
	this.getParams = function(params){
		if(typeof(params) == 'undefined')
			return this.params.totalResult = false;
		if(!this.checkParameter(params,'zoom', false))
			params.zoom = 6;
		if(!this.checkParameter(params,'poi'))
			return this.params.totalResult = false;
		if(!this.checkParameter(params.poi[0],'lat') || !this.checkParameter(params.poi[0],'lng'))
			return this.params.totalResult = false;
		if((params.poi[0].lat == '') || (params.poi[0].lng == ''))
			return this.params.totalResult = false;
		if(!this.checkParameter(params,'container'))
			return this.params.totalResult = false;
		if(!this.checkParameter(params,'size'))
			return this.params.totalResult = false;
		if(!this.checkParameter(params,'map_ceter'))
			return this.params.totalResult = false;
		if(!this.checkParameter(params,'attach'))
			return this.params.totalResult = false;
		this.params = params;
		return this.params.totalResult = true;
	};
	this.getParams(params);
}
