function _guide_script(){
	var th=this
	//this.ajax_url='index.php?option=com_guide1&ajax=1'
	this.items={}
	this.cats={}
	this.fullscreen=false
	
	this.load=function(){
		$j('div#guide_viewcat.guide-fullscreen #guide_viewcat_wrapper').height($j('body').height())
		$j('div#guide_viewcat.guide-fullscreen #guide_map').height($j('body').height())
		
		$j('div.guide-viewcat-cat').each(
			function(){
				var cat=$j(this)
				cat.attr('id').match(/guide_cat_(\d+)/)
				var cat_id=RegExp.$1
				$j('td.guide-viewcat-catname',cat).click(function(){cat.toggleClass('guide-viewcat-collapsed')})
				$j('td.guide-viewcat-checkbox input',cat).click(function(){if($j(this).attr('checked'))th.show_cat_markers(cat_id);else th.hide_cat_markers(cat_id)})
			}
		)
		this.load_map()
	}
	this.load_map=function(){
		if(typeof(GMap2)=='undefined'){
			$j('#guide_map_loading').html('API key not found. Google Maps API not loaded.')
			return
		}
		if(!GBrowserIsCompatible()){
			$j('#guide_map_loading').html('Your browser is not compatible with Google Maps.')
			return
		}
		$j('div.guide-viewcat-item').each(
			function(){
				var item=$j(this)
				item.attr('id').match(/guide_item_(\d+)/)
				var item_id=RegExp.$1
				$j('a.guide-viewcat-item-name',item).click(function(){return th.info_window(item_id)})
				$j('a.guide-viewcat-item-reviews',item).click(function(){return th.info_window(item_id,'reviews')})
				$j('a.guide-viewcat-item-photos',item).click(function(){return th.info_window(item_id,'photos')})
			}
		)
		this.map=new GMap2($j('#guide_map').get(0))
		this.map.addControl(new _combined())
		if(this.fullscreen){
			this.map.enableScrollWheelZoom()
			this.map.addControl(new GNavLabelControl())
		}
		// GEvent.addListener(this.map,'load',
			// function(){
				// setTimeout('$j("img[src$=\'poweredby.png\'], #copyright",guide_script.map.getContainer()).hide()',500)
			// }
		// )
		if(this.show_cat){
			this.show_cat_markers(this.show_cat)
			if(!this.lat){
				var i=this.items[this.cats[this.show_cat].items[0]]
				this.lat=i.lat
				this.lng=i.lng
			}
		}
		else{
			for(var k in this.cats){
				this.show_cat_markers(k)
				if(!this.lat){
					var i=this.items[this.cats[k].items[0]]
					this.lat=i.lat
					this.lng=i.lng
				}
			}
		}
		this.map.setCenter(new GLatLng(this.lat,this.lng),this.zoom)
		if(this.show_item){
			this.info_window(this.show_item.id,this.show_item.tab)
		}
	}
	this.add_item=function(id,name,address,lat,lng,color,cat){
		if(!this.cats[cat]){
			this.cats[cat]={color:color,items:[]}
		}
		this.cats[cat].items.push(id)
		this.items[id]={id:id,name:name,address:address,lat:lat,lng:lng,cat:cat}
	}
	this.show_marker=function(item){
		var i=this.items[item]
		var icon=new GIcon(G_DEFAULT_ICON)
		icon.image=GUIDE_WWW+'/includes/marker.php?color='+this.cats[i.cat].color+'&fixme.png'
		i.marker=new GMarker(new GLatLng(i.lat,i.lng),{'icon':icon,'title':i.name+' - '+i.address})
		this.map.addOverlay(i.marker)
		GEvent.addListener(i.marker,'click',
			function(){
				th.info_window(item)
				//$j("div.guide-viewcat-cat[id!='guide_cat_"+i.cat+"']").addClass('guide-viewcat-collapsed')
				$j('#guide_cat_'+i.cat).removeClass('guide-viewcat-collapsed')
				$j('#guide_viewcat_wrapper').scrollTo('#guide_item_'+item,800,{offset:-5})
			}
		)
		GEvent.addListener(i.marker,'infowindowopen',function(){$j('#guide_item_'+item).addClass('guide-viewcat-item-selected')})
		GEvent.addListener(i.marker,'infowindowclose',function(){$j('#guide_item_'+item).removeClass('guide-viewcat-item-selected')})
	}
	this.hide_marker=function(item){
		this.map.removeOverlay(this.items[item].marker)
		delete this.items[item].marker
	}
	this.info_window=function(item,tab,opts){
		if(!this.items[item].marker)this.show_marker(item)
		var update=!this.map.getInfoWindow().isHidden() && this.info_window.item==item
		$j.post(this.ajax_url,$j.extend({task:update?'get_full':'get_both',item:item,tab:tab},opts),
			function(data){
				th.update_views(item,data)
				if(update){
					if(tab){
						th.map.getInfoWindow().maximize()
					}
					else{
						th.map.getInfoWindow().restore()
						var p=th.map.fromLatLngToContainerPixel(th.items[item].marker.getLatLng())
						p.y-=th.map.getSize().height*0.4
						p.x+=th.map.getSize().width*0.1
						th.map.panTo(th.map.fromContainerPixelToLatLng(p))
					}
				}
				else{
					if(tab)var maximize=GEvent.addListener(th.map,'infowindowopen',function(){th.map.getInfoWindow().maximize();GEvent.removeListener(maximize)})
					th.items[item].marker.openInfoWindow(th.info_window.brief_view,{maxContent:th.info_window.full_view,maxTitle:th.info_window.full_view_title,maxWidth:th.map.getSize().width*0.9})
				}
			},
			'json'
		)
		this.info_window.item=item
		return false
	}
	this.update_views=function(item,data){
		if(data.brief_view){
			if(!this.info_window.brief_view)this.info_window.brief_view=document.createElement('div')
			this.info_window.brief_view.innerHTML=data.brief_view
			$j('a.guide-item-brief-reviews',this.info_window.brief_view).click(function(){return th.info_window(item,'reviews')})
			$j('a.guide-item-logo',this.info_window.brief_view).click(function(){return th.info_window(item,'overview')})
			$j('a.guide-item-brief-more',this.info_window.brief_view).click(function(){return th.info_window(item,'overview')})
			$j('a.guide-item-brief-photos',this.info_window.brief_view).click(function(){return th.info_window(item,'photos')})
		}
		if(data.full_view){
			if(!this.info_window.full_view)this.info_window.full_view=document.createElement('div')
			this.info_window.full_view.innerHTML=data.full_view
			$j('div.guide-tab',this.info_window.full_view).each(
				function(){
					var tab=$j(this)
					$j('a',tab).click(function(){return th.info_window(item,tab.attr('id'))})
				}
			)
			$j('div.guide-item-write-review',this.info_window.full_view).each(
				function(){
					var review=$j(this)
					$j('a.guide-item-write-review-handle',review).click(function(){review.toggleClass('guide-item-write-review-collapsed');return false})
					$j("form input[name='cancel']",review).click(function(){review.toggleClass('guide-item-write-review-collapsed');return false})
					$j("form input[name='subm']",review).click(
						function(){
							var form=$j('form.guide-item-write-review-form',review).get(0)
							if(form.name.value.length==0)alert('You should enter your name')
							else if(form.text.value.length==0)alert('You should write a review')
							else if(form.rating.options[form.rating.selectedIndex].value==0)alert('You should specify your rating')
							else{
								$j.post(th.ajax_url,
									{task:'add_review',item:item,
									name:form.name.value,
									text:form.text.value,
									rating:form.rating.options[form.rating.selectedIndex].value},
									function(data){
										th.update_views(item,data)
									},
									'json'
								)
							}
							return false
						}
					)
				}
			)
			$j('div.guide-item-thumb',this.info_window.full_view).click(function(){return th.info_window(item,'photos',{photo:$j(this).attr('id')})})
			$j('a.guide-item-photos-previous',this.info_window.full_view).click(function(){return th.info_window(item,'photos',{photo:$j(this).attr('id')})})
			$j('a.guide-item-photos-next',this.info_window.full_view).click(function(){return th.info_window(item,'photos',{photo:$j(this).attr('id')})})
			$j('a.guide-item-photos-back',this.info_window.full_view).click(function(){return th.info_window(item,'photos')})
		}
		if(data.full_view_title)this.info_window.full_view_title=data.full_view_title
	}
	this.show_cat_markers=function(cat){
		for(var i=0;i<this.cats[cat].items.length;i++)
			if(!this.items[this.cats[cat].items[i]].marker)
				this.show_marker(this.cats[cat].items[i])
	}
	this.hide_cat_markers=function(cat){
		for(var i=0;i<this.cats[cat].items.length;i++)
			if(this.items[this.cats[cat].items[i]].marker)
				this.hide_marker(this.cats[cat].items[i])
	}
	$j(document).ready(function(){th.load()})
}
