var _isMap = true;
var _isExternalSidebar = false;
var _firstTPKLoad = false;
var _categoryList = new Array();
_categoryList[0]="Lodging";
_categoryList[1]="Activities";
_categoryList[2]="Attractions";
_categoryList[3]="Dining";


function resetPaging(){
    _currentPage = 1;
}
function sidebarSectionClick(event){
        
         resetPaging();
         cancelRequests();
       
         var thissidebarsection = $(this).parents(".sidebarsection");
	     openSection(thissidebarsection);
        
    }
   
 function openSection(thissidebarsection){
        var index = $(".sidebarsection").index(thissidebarsection);
        var insertSection = thissidebarsection.find(".content");
         thissidebarsection.find(".exp-filter-current").html("All");
        //check the sidebar checkbox
        
        thissidebarsection.find(".sectioncheck").attr("checked", "checked");
        //get the data for the sidebar
         getSidebarRecommendations(_north,_south,_east,_west,_zoom,_categoryList[index],insertSection,_currentPage,PAGE_SIZE,true);
         //set the sidebar index
         updateHash("sideindex",index.toString());
         //set the sidebar category
         updateHash("sidecat",_categoryList[index]);
          var map = document.getElementById('myflash');
          activePane = index;
          _currentCategory="all";
          try{
           map.ShowCategory(getCategoryNameFromActivePane());
            }catch(ev){}
 
 }    
     
function sidebarPhotosClick(event){
    
	     resetPaging();
	     cancelRequests();
	    
	     $(".sidebarphotos .sectioncheck").attr("checked", "checked");
	    var insertSection = $(".sidebarphotos .content");
	    $(this).find(".sectioncheck").attr("checked", "checked");
	    getSideBarPhotos(_north,_south,_east,_west,_zoom,insertSection,_currentPage,PAGE_SIZE);
	    updateHash("sideindex","p");
	    activePane = -1;
	    _currentCategory="all";
	    var map = document.getElementById('myflash');
	    try{
	          map.ShowCategory(getCategoryNameFromActivePane());
	    }catch(dfg){}
	    
	


}
var  _showCategories=null;
$(document).ready(function(){
	//hide the sections that our customers do not want to see
	
	if(_showCategories!=null){
	    for(var x = 0;x<_showCategories.length;x++){
	        if(!_showCategories[x]){
	            switch(x){
	                case 0:
	                    $(".sidebarphotos").css("display","none");
	                    break;
    	              
	                case 1:
	                $(".sidebarsection").eq(x-1).css("display","none");
	                break;
    	            
	                case 2:
	                 $(".sidebarsection").eq(x-1).css("display","none");
	                break;
    	            
	                case 3:
	                $(".sidebarsection").eq(x-1).css("display","none");
	                break;
    	            
	                case 4:
	                $(".sidebarsection").eq(x-1).css("display","none");
	                break;
	            }
	        }
	    }
	    
	
	}
	//subsection click function
    $(".subsection a").click(function(event){
    
         //rest paging
       cancelRequests();
       resetPaging();
       event.stopPropagation();
	    //get the subcat text 
	    var map = document.getElementById('myflash');
	    var subcatName = $(this).html();
	    //update section name
	    var thissidebarsection = $(this).parents(".sidebarsection");
	    thissidebarsection.find(".exp-filter-current").html(subcatName);
	    //get the data for the section
	    //handle the all category
	    var secIndex = $(".sidebarsection").index(thissidebarsection);
	    
	   
	    
	    
	    if(_currentCategory!="all"){ //we're on a sub category
           
            if(subcatName.toLowerCase()=="all"){ //switching to the main level
                
                subcatName = _categoryList[secIndex];
                try{
                map.HideCategory(_currentCategory);
                map.ShowCategory(subcatName);
                }catch(ev){}
                _currentCategory = "all";
            }else{ //switching to another subcategory
               
                try{
                map.HideCategory(_currentCategory);
                map.ShowCategory(subcatName);
               }catch(ev){}
                _currentCategory = subcatName;
            }
           
           
        
    }else{  //we're on a main category
        
                
		        if(subcatName.toLowerCase()!="all"){
		            try{
		            map.HideCategory(getCategoryNameFromActivePane()); //hide the top section
		            map.ShowCategory(subcatName);//show the filtered section
		             }catch(ev){}
		            _currentCategory = subcatName;
		        }else{ //all was clicked dont do anything
		            
		            subcatName = _categoryList[secIndex];
		        }
    }
	    
	    
	     var insertSection = $(this).parents(".sidebarsection").find(".content");
	    getSidebarRecommendations(_north,_south,_east,_west,_zoom,subcatName,insertSection,_currentPage,PAGE_SIZE,true);
	    
	    updateHash("sideindex",secIndex.toString());
         //set the sidebar category
         updateHash("sidecat",subcatName);
         //close the section
         var par = $(this).parents(".all-innards");
         
         par.find(".exp-filter-show").css("display","block");
         par.find(".exp-filter-hide").css("display","none");
         par.find(".exp-filter-content").css("display","none");
         
        

       
         return false;
    });
    
    //click handler for the section title clicking
    $(".sidebarsection .title").click(sidebarSectionClick);
    $(".sidebarsection .single-special-view-all-link").click(sidebarSectionClick);
    
    
     /**
     * Click Handler for the section checkbox click
     */
     $(".sidebarsection .sectioncheck").click(function(event){
        //stop events from bubbling
        
         resetPaging();
         cancelRequests();
        event.stopPropagation();

        var sectionIndex = $(".sidebarsection").index($(this).parents(".sidebarsection"));
		    
		   
		    
		    var sectionid = _categoryList[sectionIndex];
		     
		    
		    
		    var map = document.getElementById('myflash');
		   
            if(this.checked){
                
                //turn on the layer on the map
                try{
                 map.ShowCategory(sectionid);
                  }catch(ev){}
                 //open the section
                 bar_clicked($(this).parents(".color-log"));
                 //load the data
                 activePane = sectionIndex;
                 updateSideBar();
            }else{
                //turn off the layer
               try{
               map.HideCategory(sectionid);
                }catch(ev){}
               if(_currentCategory!="all"){
                try{
                 map.HideCategory(_currentCategory);
                  }catch(ev){}
               }
              
                  //close the section if the currently active one.
                  if(sectionid==_categoryList[activePane]){
                  icontent_hide_all(); 
				    }
         
            }
     });
     /**
     * Click Handler for the Photos checkbox click
     */
     $(".sidebarphotos .sectioncheck").click(function(event){
        //stop events from bubbling
        cancelRequests();
         resetPaging();
        event.stopPropagation();
            
           

		    var map = document.getElementById('myflash');
		   
            if(this.checked){
                activePane = -1;
		        var sectionid =  getCategoryNameFromActivePane();
                //turn on the layer on the map
                try{
                 map.ShowCategory(sectionid);
                  }catch(ev){}
                 //open the section
                 bar_clicked($(this).parents(".color-log"));
                 //load the data
                  
                 updateSideBar();
            }else{
                //turn off the layer
               
		        var sectionid =  "Media";
               try{
               map.HideCategory(sectionid);
               
               if(_currentCategory!="all"){
                 map.HideCategory(_currentCategory);
               }
               }catch(ev){}
                 
                //close the section if the currently active one.
                  if(sectionid==getCategoryNameFromActivePane()){
                    icontent_hide_all(); 
				    } 
				    
         
            }
     
     
     });
	/**
	 * Photos section click handler
	 */
	$(".sidebarphotos .title").click(sidebarPhotosClick);
	$(".sidebarphotos .single-special-view-all-link").click(sidebarPhotosClick);
	/**
	 * Travelpacks section click handler
	 */
	$(".sidebartravelpacks .title").click(function(){
	    cancelRequests();
	    resetPaging(); 
	    var insertSection = $(".sidebartravelpacks .content");
	    $(this).find(".sectioncheck").attr("checked", "checked");
	    getTravelPackMedia(_north,_south,_east,_west,_zoom,insertSection,_currentPage,PAGE_SIZE);
	    getTravelPackItems(_north,_south,_east,_west,_zoom,insertSection,_currentPage,PAGE_SIZE);
	     updateHash("sideindex","t");
	     activePane = -2;
	
	});
	//load the state info
	 
	var sidebarindex = getHashValue("sideindex");
	var sidebarcat = getHashValue("sidecat");
	if(sidebarindex!=null ){
	    if(sidebarindex!="t" && sidebarindex!="p" && sidebarcat!=null ){
	        var insertS = $(".sidebarsection .content").eq(sidebarindex);
	        //open the correct section
        	bar_clicked(insertS.parents(".sidebarsection"));
	        //load data into it
	        getSidebarRecommendations(_north,_south,_east,_west,_zoom,sidebarcat,insertS,_currentPage,PAGE_SIZE,true);
	    }else if(sidebarindex=="p"){
    	    //open photo section
    	    bar_clicked($(".sidebarphotos"));
    	    var insertSection = $(".sidebarphotos .content");
    	    //load data into it
	        getSideBarPhotos(_north,_south,_east,_west,_zoom,insertSection,_currentPage,PAGE_SIZE);
	    }else if(sidebarindex=="t"){
	        //open travelpack section
	        
	        bar_clicked($(".sidebartravelpacks"));
	        // load data into it
	        var insertSection = $(".sidebartravelpacks .content");
	        getTravelPackMedia(_north,_south,_east,_west,_zoom,insertSection,_currentPage,PAGE_SIZE);
	        getTravelPackItems(_north,_south,_east,_west,_zoom,insertSection,_currentPage,PAGE_SIZE);
	    }
	}
});


function getSuicideMode(north,south,east,west,zoom){
        var insertSection = $(".sidebarphotos .single-special-innard .singlecontent");
    	    //load data into it
	    getSideBarPhotos(north,south,east,west,zoom,insertSection,-1,1);
	    
	    
    for(var x =0 ;x<_categoryList.length;x++){
            
            if(x<2 || !_isMap){
            var insertS = $(".sidebarsection .single-special-innard .singlecontent").eq(x);
            var sidebarcat = _categoryList[x];
	        //open the correct section
        	
	        //load data into it
	        getSidebarRecommendations(north,south,east,west,zoom,sidebarcat,insertS,-1,1,false);
	        //check if anything was put there
	            
            }
    }

}
function updateSideBar(){
    cancelRequests();
    try{
        if(document.getElementById("MapBreadCrump")){
            PlanetEye.WebSite.WebService.AjaxService.GetCityForMapPage(_clat,_clon,_zoom,SucceededGetCityForMapPageCallback,FailGetCityForMapPageCallback,"");
        }
    }
    catch(ezx){}
    if(activePane==null){
        activePane = -1; //photos
        var insertSection = $(".sidebarphotos .content");
    	    //load data into it
	    getSideBarPhotos(_north,_south,_east,_west,_zoom,insertSection,_currentPage,PAGE_SIZE);
	    try{
	    var map = document.getElementById("myflash");
	    map.ShowCategory("Media");
	    }catch(swdg){
	    
	    }
    }else{
    
        
           if(activePane == -1){
             var insertSection = $(".sidebarphotos .content");
             getSideBarPhotos(_north,_south,_east,_west,_zoom,insertSection,_currentPage,PAGE_SIZE);
        }else if(activePane==-2){
            var insertSection = $(".sidebartravelpacks .content");
            
	        getTravelPackMedia(_north,_south,_east,_west,_zoom,insertSection,_currentPage,PAGE_SIZE);
	        getTravelPackItems(_north,_south,_east,_west,_zoom,insertSection,_currentPage,PAGE_SIZE);
           
           // showCategory(activePane);
         }else if(activePane==-3){ //suicide mode regular page  / destination page
           getSuicideMode(_north,_south,_east,_west,_zoom);
           
         }else if(activePane == -4){ //travelpack non suicide mode center map
            if(sections.length>0){
                moveMap(sections[0].lat,sections[0].lon,13,0);
            }
            activePane = -2;
         
        }else if (activePane == -5){ //travelpack suicidemode center map on first item
            if(sections.length>0){
                moveMap(sections[0].lat,sections[0].lon,13,0);
            }
            getSuicideMode(_north,_south,_east,_west,_zoom);
            activePane = -3;
            
        }else{
        
            
            var insertS = $(".sidebarsection .content").eq(activePane);
             var sidebarcat ="" 
            if(_currentCategory!="all"){
             sidebarcat =  _currentCategory;
            }else{
              sidebarcat =  _categoryList[activePane];
            }
           
	        //open the correct section
        	
	        //load data into it
	        getSidebarRecommendations(_north,_south,_east,_west,_zoom,sidebarcat,insertS,_currentPage,PAGE_SIZE,true);
        }
    }

}


function filterCategory(categoryCode){
    cancelRequests();
    
    var map = document.getElementById('myflash');
    //show the category on the map, hide the current category
    
    if(_currentCategory!="all"){ //we're on a sub category
           
            if(categoryCode.toLowerCase()=="all"){ //switching to the main level
                
                try{
                map.HideCategory(_currentCategory);
                map.ShowCategory(getCategoryNameFromActivePane());
               }catch(ev){}
                _currentCategory = "all";
            }else{ //switching to another subcategory
               
                try{
                map.HideCategory(_currentCategory);
                map.ShowCategory(categoryCode);
               }catch(ev){}
                _currentCategory = categoryCode;
            }
           
           
        
    }else{  //we're on a main category
        
                
		        if(categoryCode!="all"){
		            try{
		            map.HideCategory(getCategoryNameFromActivePane()); //hide the top section
		            map.ShowCategory(categoryCode);//show the filtered section
		             }catch(ev){}
		            _currentCategory = categoryCode;
		        }else{ //all was clicked dont do anything
		            
		        
		        }
    }
    getSideBarPlaces(_north,_south,_east,_west,_zoom,activePane,catid);
}

function attach_paging_events(){
    $('.paging .pageprev').click(function(){
           
        _currentPage--;
        updateSideBar();
         return false;
    });
    
    $('.paging .pagenext').click(function(){
        
        _currentPage++;
        updateSideBar();
        return false;
    
    });
}


function notez_attach_events() {

  $('.tpk_notez_clicker').click(function() {
    notez_clicked($(this));
    return false;
  });

}

var NOTEZ_REG_HEIGHT = '17px';

function notez_clicked(x) {

  if (x.hasClass('tpk_notez_clicker_collapsed')) {
  // currently collapsed

    x.removeClass('tpk_notez_clicker_collapsed');
    x.addClass('tpk_notez_clicker_expanded');
    x.parent().children('.tpk_notez_text').css('height', 'auto');

  } else {
  // currently expanded

    x.removeClass('tpk_notez_clicker_expanded');
    x.addClass('tpk_notez_clicker_collapsed');
    x.parent().children('.tpk_notez_text').css('height', NOTEZ_REG_HEIGHT);

  }
}


