﻿// Contains script to support the content pages.
var WineRegion = 
{
	_regionid : null, 
	_category : null,
	_siteurl : null,
	_waitingAsyncServiceCall : false,
	_onsuccessCallback :  null,
	
    Init:function( regionid, category, siteurl, callback )
    {        
        this._regionid = regionid; 
        this._category = category; 
        this._siteurl = siteurl; 
		if( typeof( callback )  !== "undefined" )
			this._onsuccessCallback = callback ; 
    	this.UpdateBtns( category );
	},

	Load:function( )
	{
		ServiceClasses.WineServices.set_timeout( 10000 );
		ServiceClasses.WineServices.set_defaultFailedCallback( this.FailedCallback );
	},

	finished:function( )
	{
		return (this._waitingAsyncServiceCall == false);
	},

	startCallback:function( )
	{
		this._waitingAsyncServiceCall = true;
        this.LoadingSymbol( $get( "listingsContainer" ) );
	},

	endCallback:function(  )
	{
        $get( "progressImage" ).style.display = "none";	
       var contentblock = $get( "listingsContainer" ); 
		{ if( typeof( contentblock.$oldClass ) !== 'undefined' )
	            contentblock.className = contentblock.$oldClass; 
	    } 
		this._waitingAsyncServiceCall = false;
	},

	GetListings:function( e, category )
	{
        if( !this.finished( ) ) return; 
		ServiceClasses.WineServices.GetListings( this._regionid, category, true, this.GetInfoCallback );
		this.startCallback(  );
    	this.UpdateBtns( category );
        this.displayRegionMap( category );
	},

	LoadingSymbol:function( ctrl )
	{
	    var wait = $get( "progressImage" );
        var b = Sys.UI.DomElement.getBounds(ctrl);
        Sys.UI.DomElement.setLocation( wait, Math.floor(b.x+b.width/2-27), Math.floor(b.y+b.height/2-27) );
        wait.style.display = "block";
        ctrl.$oldClass = ctrl.className;
        ctrl.className = ctrl.className + " faint";	   
	},
	
	UpdateUI:function( htmlstr )
	{
        $get( "listingsContainer" ).innerHTML = htmlstr; 

        // Call additional page functions if necessary	    	
		if( this._onsuccessCallback != null )
		    this._onsuccessCallback( );
	},
	
	UpdateBtns:function( category )
	{
        if( $get( "listingsContainer" ) === null ) return;
	    $get( "wineryimg" ).setAttribute( "src", (category == "Winery") ? "Images/winery1.gif" : "Images/winery0.gif" );
	    $get( "accomimg" ).setAttribute( "src", (category == "Accommodation") ? "Images/accom1.gif" : "Images/accom0.gif" );
	    $get( "diningimg" ).setAttribute( "src", (category == "Dining") ? "Images/dining1.gif" : "Images/dining0.gif" );
	    $get( "tourismimg" ).setAttribute( "src", (category == "Tourism") ? "Images/tourism1.gif" : "Images/tourism0.gif" );
	    _category = category;
	},

	displayRegionMap:function( thecategory )
	{
        var rf = $get( "regionmapframe");
	    var url =  this._siteurl + "RegionMap.aspx?region="+ this._regionid + "&category=" + thecategory;
	    if( rf.src !== url ) rf.src = url;
	},

	// ### CALLBACKS (Not in 'this' context ### //
	GetInfoCallback:function( result )
	{
        WineRegion.endCallback(  );
		if( result != null ) 
		    WineRegion.UpdateUI( result );
	},

	// This is the failed callback function.
	FailedCallback:function( error )
	{
        WineRegion.endCallback( );
	    alert( error.get_message( ) );
	}
}
// Notify ScriptManager that this is the end of the script.
if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();

