﻿// JScript File
// Contains script to support the listings (information) pages.
var Listing = 
{
	m_listingpanel : null,
	m_detailpanel : null,
	m_pageType : 0,
	m_sortID : 0,
	m_currentObj : null, 
	m_nextObj : null, 
	m_prevObj : null,
	m_listingcontent : null,
	m_topPageIndex : null,
	m_botPageIndex : null,
	m_defaultPage : 1,
	m_waitingAsyncServiceCall : false,
	m_onsuccessCallback :  null,
	
    Init:function( theType, theSortID, thepageNo, thelistingpanel, thedetailpanel )
    {        
		this.m_pageType = theType;
		this.m_listingpanel = $get( thelistingpanel );
		this.m_detailpanel = $get( thedetailpanel );
		if( typeof theSortID !== "undefined" )
			this.m_sortID = theSortID; 
		if( typeof thepageNo !== "undefined" )
			this.m_defaultPage = thepageNo;
	},

	Load:function( loadListings )
	{
		this.m_currentObj = null;
		this.m_nextObj = null;
		this.m_prevObj = null;

		this.m_listingcontent = $get("listingcontent");
		this.m_topPageIndex = $get( "toppagination" );
		this.m_botPageIndex = $get( "bottompagination" );

		ServiceClasses.ListingsService.set_timeout(30000);
		ServiceClasses.ListingsService.set_defaultFailedCallback( this.failedCallback );
	    
		Pagination.Init( this, this.m_topPageIndex, 
			 function( eventElement ) { return Listing.prevClick(  eventElement ) },
			 function( eventElement ) { return Listing.nextClick(  eventElement ) },
			 function( eventElement ) { return Listing.pageClick(  eventElement ) } );
		Pagination.Init( this, this.m_botPageIndex, 
			 function( eventElement ) { return Listing.prevClick(  eventElement ) },
			 function( eventElement ) { return Listing.nextClick(  eventElement ) },
			 function( eventElement ) { return Listing.pageClick(  eventElement ) } );

		$clearHandlers( this.m_listingcontent );
		$addHandler( this.m_listingcontent, "click", this.ListingClick );
		$clearHandlers( $get("pagenavleftinfo") );
		$addHandler( $get("pagenavleftinfo"), "click", this.SortClick );
			
		if( (Map.m_zoneID > 0) && (loadListings == true) )  
			this.callService( this.m_defaultPage, 0, false );
	},

	switchtolisting:function( )
	{
		window.scrollTo( Master.scrollX( ), 0 );
		this.m_detailpanel.style.display = "none";
		this.m_listingpanel.style.display = "block";
		$get( "headerright" ).innerHTML = "";
	},

	switchtodetail:function( )
	{
		window.scrollTo( Master.scrollX( ), 0 );
		this.m_listingpanel.style.display = "none";
		this.m_detailpanel.style.display = "block";
	},

	removedetails:function( )
	{
		this.switchtolisting( );
		$get( "headerleft" ).innerHTML = Category.m_breadcrumb;
	},
	
	selectionClick:function( chgtype )
	{
		Pagination.updatePaginationCtrl( this.m_topPageIndex, false );
		Pagination.updatePaginationCtrl( this.m_botPageIndex, false );
		
		if( typeof chgtype === "undefined" )
			this.removedetails( );
		else
			this.switchtolisting( );

		this.callService( 1, 0, false );
	},

	nextClick:function ( eventElement ) 
	{
		if( Pagination.isEnabled( eventElement.target ) == false )
	   { 
			if( (this.m_currentObj !== null ) && (this.m_currentObj.PageNo < this.m_currentObj.PageCount) )     
				this.nextClickInternal( ); 
		} 
		return Master.stopDefault( eventElement );
	},

	nextClickInternal:function ( ) 
	{
		this.m_prevObj = this.m_currentObj;
		
		if( this.m_nextObj != null )     
		{
			this.m_currentObj = this.m_nextObj;
			this.m_nextObj = null;
			this.updateUI( );
		}
		else
		{
			this.m_nextObj = null;
			if( this.m_currentObj.PageNo < this.m_currentObj.PageCount )     
			{
				this.callService( this.m_currentObj.PageNo+1, this.m_currentObj.TotalResults, true );
				return false;
			}
		}
		return true;
	},

	prevClick:function( eventElement ) 
	{
		if( Pagination.isEnabled( eventElement.target ) == false )
	   { 
			if( (this.m_currentObj !== null) && (this.m_currentObj.PageNo > 1) )
				this.prevClickInternal( );
		} 
		return Master.stopDefault( eventElement );
	},

	prevClickInternal:function( ) 
	{
		this.m_nextObj = this.m_currentObj;

		if( this.m_prevObj != null ) 
		{
			this.m_currentObj = this.m_prevObj;
			this.m_prevObj = null;
			this.updateUI( );
		}
		else
		{
			this.m_prevObj = null;
			if( this.m_currentObj.PageNo > 1 )
			{
				this.callService( this.m_currentObj.PageNo-1, this.m_currentObj.TotalResults, true );
				return false;
			}
		}
		return true;
	},

	pageClick:function( eventElement )
	{
		if( (this.m_currentObj == null ) || (eventElement.target.nodeName != "A") )
			return true;
		
		var pageNum = eventElement.target.innerHTML;
		if( (pageNum > 0) && (pageNum <= this.m_currentObj.PageCount) && (pageNum != this.m_currentObj.PageNo) )
		{
			if( (pageNum == this.m_currentObj.PageNo + 1)  )
				this.nextClickInternal( );
			else if( (pageNum == this.m_currentObj.PageNo - 1)  )
				this.prevClickInternal( ) 
			else
			{
				this.callService( pageNum, this.m_currentObj.TotalResults, false );
			}
		}        
		return Master.stopDefault( eventElement );
	},

	ListingClick:function( eventElement )
	{
		var ctrl = eventElement.target;
		if( ctrl.nodeName == "IMG" ) ctrl = ctrl.parentNode;
		if( ctrl.nodeName != "A" ) return true;
		
		var url = ctrl.getAttribute( "href" );
		var parentctrl = ctrl.parentNode;
		while( parentctrl != null && parentctrl.nodeName != "TD" )
			parentctrl = parentctrl.parentNode;
		
		if( parentctrl.className == "listingcategories" )
			Category.cat2Click( eventElement );
		else
		{
			var id = 0; var tab = 1;
			var pos1 = url.lastIndexOf( "_l-" ); 
			var pos2 = url.lastIndexOf( ".aspx" ); 
			if( pos1 >= 0 && pos2 >= 0 )
				id = url.substring( pos1+3, pos2 );

			pos1 = url.lastIndexOf( "_tab" ); 
			if( pos1 >= 0 )
				tab = url.substring( pos1+4, pos1+5 );

			if( id > 0 )
			{
				Listing.switchtodetail( );
				Master.showoverlay( 3 );
				ret = ServiceClasses.ListingsService.GetSingleListing( id, tab, ListDetail.successCallback   );
			}
			else
				return true;
		}
			
		return Master.stopDefault( eventElement );
	},

	SortClick:function( eventElement )
	{
		var ctrl = eventElement.target;
		if(  ctrl.nodeName == "A") 
		{
			var links = $get( "pagenavleftinfo" ).getElementsByTagName( "A" );
			if( links.length == 2 )
			{
				switch(  ctrl.innerHTML )
				{
					case "Generic" : Listing.m_sortID = 0; links[0].innerHTML = "Alphabetical"; links[1].innerHTML = "Most Popular"; break;
					case "Alphabetical" : Listing.m_sortID = 1; links[0].innerHTML = "Generic"; links[1].innerHTML = "Most Popular"; break;
					case "Most Popular" : Listing.m_sortID = 2; links[0].innerHTML = "Alphabetical"; links[1].innerHTML = "Generic"; break;
				}
				Pagination.updatePaginationCtrl( Listing.m_topPageIndex, false );
				Pagination.updatePaginationCtrl( Listing.m_botPageIndex, false );
				
				Listing.callService( 1, Listing.m_currentObj.TotalResults, false );
			}
		}
		return Master.stopDefault( eventElement );
	},

	callService:function( pageno, results, pagination )
	{
		Master.showoverlay( 3 );
		ret = ServiceClasses.ListingsService.GetListings( this.m_pageType, Category.selectedCategory( ), Map.m_zoneID, 
			Search.m_searchtype, Search.m_searchtext, this.m_sortID, pageno, results, this.successCallback   );
		if( pagination == false )
		{
			this.m_nextObj = null;
			this.m_prevObj = null;
		}
		this.m_waitingAsyncServiceCall = true;
	},

	successCallback:function( result ) 
	{
		if( result != null ) 
		{
			Listing.m_currentObj = Sys.Serialization.JavaScriptSerializer.deserialize( result ); 
			Listing.updateUI( );
			if( Listing.m_onsuccessCallback != null )
			{ Listing.m_onsuccessCallback( ); Listing.m_onsuccessCallback = null; }
		}
		Master.hideoverlay( );
		Listing.closemodalmap( );
	},

	updateUI:function( )
	{
		window.scrollTo( Master.scrollX( ), 0 );
		this.m_listingcontent.innerHTML = this.m_currentObj.ListHtml;
		Pagination.updatePaginationCtrl( this.m_topPageIndex, true );
		Pagination.updatePaginationCtrl( this.m_botPageIndex, true );
		
		if( this.m_currentObj.CategoryContent != null )
		{
			Search.clear( false );
			Category.updateUI( this.m_currentObj.CategoryContent, false );
		}
		this.UpdateSearchResults(  );
	},

	UpdateSearchResults:function(  )
	{
		var sb = new Sys.StringBuilder( );

		// Content right at the top
		if( Search.m_searchtext != "" )
		{
			sb.append( "Your search for <b>" );
			sb.append( Search.m_searchtext );
			sb.append( "</b> found <b>" );
			sb.append( this.m_currentObj.TotalResults );
			sb.append( "</b> matches for <b>" );
			sb.append( $get( Map.m_zoneNameTag ).innerHTML );
			sb.append( "</b>" );
			$get( 'pagenavresult' ).style.display = "block";
			
			var ctrlcontainer = $get("category2container");
			if( ctrlcontainer != null ) ctrlcontainer.innerHTML = "Click on the NZ map Zones on the right to reduce or increase the search area.<br/><br/>";
		}
		else if( Category.m_catName != null && Category.m_categSelectID != 0 )
		{
			sb.append( "You have <b>" );
			sb.append( this.m_currentObj.TotalResults );
			sb.append( "</b> results within  <b>" );
			sb.append( Category.m_catName );
			sb.append( "</b> for <b>" );
			sb.append( $get( Map.m_zoneNameTag ).innerHTML );
			sb.append( "</b>" );
			$get( 'pagenavresult' ).style.display = "none";
		}
		else
		{
			$get( 'resultstop' ).style.display = "none";
			$get( 'pagenavresult' ).style.display = "none";
		}

		if( sb.isEmpty( ) == false )
		{
			$get( 'resultstop' ).style.display = "block";
			$get( 'resultstop' ).innerHTML = sb.toString( ); 
			sb.clear( );
		}

		// Content above listings on the right.
		sb.append( "( " );
		sb.append( this.m_currentObj.StartItem );
		sb.append( " - " );
		sb.append( (this.m_currentObj.StartItem-1) + this.m_currentObj.PageResults );
		sb.append( " of " );
		sb.append( this.m_currentObj.TotalResults );
		if( Category.m_catName != null )
		{
			sb.append( " <b>" );
			sb.append( Category.m_catName );
			sb.append( "</b>" );
		}
		sb.append( " listings )" );
		$get( 'pagenavrightinfo' ).innerHTML = sb.toString( ); 
	},

	finished:function( )
	{
		return (Listing.m_waitingAsyncServiceCall == false);
	},
	
	closemodalmap:function( )
	{
		Listing.m_waitingAsyncServiceCall = false;

		var themodalmap = $get( "modalmap" );
		if( themodalmap != null && themodalmap.style.display != "none" )
		{
			if( ( typeof(Info) === 'undefined'  || Info.finished( ) ) &&
				 ( typeof(Category) === 'undefined'  || Category.finished( ) ) )
				Map.hideModalMap( null ); 
		}
	},

	failedCallback:function( error )
	{
		Master.showoverlay( 2 );
		Listing.closemodalmap( );
	}
}

// Notify ScriptManager that this is the end of the script.
if ( typeof(Sys) !== 'undefined' ) Sys.Application.notifyScriptLoaded( );

