if(!henworx.MapCms.Widgets){
	henworx.MapCms.Widgets={}
}
henworx.MapCms.Widgets.ListView=function(){
	this.maxPage = 0;
	this.search_text = '';
	this.cat = '';
	this.range = '';
	this.currentPage = 1;
	this.postsPerPage=10;
	this.listview_text
	this.mapview_text
	this.currentView = 'mapview';
	var _ItemsContainerId = 'henworx_mapcms_widget_listview_mapview_data_page_';
	_listviewUrl=gPluginUrl+"/admin/includes/widgets/listview_mapview/get_listview.php"
	_lang = henworx.MapCms.Lang;
	var _this=this;
	this.show = function(){
		if(!document.getElementById(_ItemsContainerId+this.currentPage)){
			
			this.getData(show=true,_this.search_text,_this.cat);
			return false;
		}
		this.hide();
		
		$jQ("#"+_ItemsContainerId+this.currentPage).show();
		
		if(this.maxPage>=this.currentPage){
			_text = _lang.getWord('Page')+' '+this.currentPage+_lang.getWord(' of ')+this.maxPage;	
			$jQ('.listview_mapview_pagination .aligncenter').html(_text);
			$jQ('.listview_mapview_pagination').show();
		}else{
			$jQ('.listview_mapview_pagination').hide();
		}
	}
	
	this.getData = function(_show,_search_text,_cat,_range){
		if(typeof(_cat)=="undefined"){
			_cat="";
		}
		if(typeof(_range)=="undefined"){
			_range="";
		}
		if(typeof(_search_text)=="undefined"){
			_search_text="";
		}
		_this.search_text = _search_text;
		_this.cat = _cat;
		_this.range= _range;
		$jQ.ajax({
			url: _listviewUrl,
			data:{paged:this.currentPage, posts_per_page:this.postsPerPage, cat:_this.cat, s:_this.search_text, range:this.range},
			cache: false,
			success: function(html){
				$jQ("#henworx_mapcms_widget_listview_container_items").append(html);
				if(typeof(_show)!='undefined'){
					if(_show)(_this.show())
				}
			}
			
		});
	}
	
	this.reset=function(){
		$jQ("#henworx_mapcms_widget_listview_container_items").html("");
		$jQ('.listview_mapview_pagination .aligncenter').html("");
		
	}
	this.empty=function(){
		$jQ("#henworx_mapcms_widget_listview_container_items").html("<div id='"+_ItemsContainerId+this.currentPage+"'></div>");
		$jQ('.listview_mapview_pagination .aligncenter').html("");
		$jQ('.listview_mapview_pagination').hide();
		this.currentPage=1;
		this.maxPage=0;
		
	}
	
	
	/*
	Display the next page
	*/
	this.next = function(){
		
		if(this.currentPage < this.maxPage){
			this.currentPage = this.currentPage + 1;
			this.show();
		}
		else if(this.currentPage == this.maxPage){
			//just show the last page
		}
		
	}
	
	/*
	Display the previous page
	*/
	this.previous = function(){
		
		if(this.currentPage > 1){
			this.currentPage = this.currentPage - 1;
			this.show();
		}		
	}
	
	/*
	
	/*
	Hide all the pages
	*/
	
	this.hide = function(){
		for(i=1; i<=this.maxPage; i++){
			$jQ('#henworx_mapcms_widget_listview_mapview_data_page_'+i).hide();
		}				   
	}
	
	this.showOnMap=function(id){
		this.switchView();
		 widgetClickAction(id)
	}
	
	this.openNewWindow=function(id){
		if(!gBigWindows[id]){
			_loc=gMapCms.findLocation(id)
			_win= new henworx.MapCms.BigWindow(_loc);
			gBigWindows[id] = _win
		}
		gBigWindows[id].show(hide_stem=true);
		if(typeof(henworx.MapCms.Widgets.ListView.afterOpenNewWidow)=='function'){
			henworx.MapCms.Widgets.ListView.afterOpenNewWidow();
		}
	}
	
	this.switchView =function(){
		henworx.MapCms.BigWindow.hide();
		
		if($jQ('#listview_mapview_switch_button').attr("title")==this.mapview_text){
			this.setToMapView();
		}else{
			this.setToListView();
		}
		 //$jQ('#mapCms').toggle();
		 //$jQ('#henworx_mapcms_widget_listview_container').toggle();
	}
	/*
	show Map View
	*/
	this.setToMapView = function(){
		if(this.currentView == 'listview'){
			$jQ('#listview_mapview_switch_button').html(this.listview_text)
			$jQ('#listview_mapview_switch_button').attr("title", this.listview_text);
			this.currentView = 'mapview';
			$jQ('#mapCms').show();
			$jQ('#henworx_mapcms_widget_listview_container').hide();
		}
	}
	
	/*
	show List View
	*/
	this.setToListView = function(){
		if(this.currentView == 'mapview'){
			this.currentView = 'listview';
			$jQ('#listview_mapview_switch_button').html(this.mapview_text)
			$jQ('#listview_mapview_switch_button').attr("title", this.mapview_text);
			$jQ('#mapCms').hide();
			$jQ('#henworx_mapcms_widget_listview_container').show();
		}
	}
}