﻿// Contains script to support the content pages.
var WineContent = 
{
    _maincontent : null, 
    _auxcontent : null, 
    _indexcontent : null, 
    _regionlabel : null,
    _regionhidden : null, 
    _contentlabel : null,
    _contenthidden : null, 
	_waitingAsyncServiceCall : false,
	_onsuccessCallback :  null,
	_contentObj : null, 
	_updatectrl : null,
	_reviewurl : null,
	_category : null,
	_filter : false,
	
    Init:function( category, filter, regionlabel, regionhidden, contentlabel, contenthidden, indexcontent, reviewurl, callback )
    {        
        this._category = category; 
        this._filter = filter; 
        this._regionlabel = regionlabel; 
	    this._regionhidden = regionhidden; 
        this._contentlabel = contentlabel; 
	    this._contenthidden = contenthidden; 
        this._maincontent = $get( "mainContent" ); 
        this._auxcontent = $get( "auxContent" );
        this._indexcontent = indexcontent; 
        this._reviewurl = reviewurl; 
		if( typeof( callback )  !== "undefined" )
			this._onsuccessCallback = callback ; 
	},

	Load:function( ) 	{
        var indexpanel =  $get( this._indexcontent );
        var oRows = indexpanel.getElementsByTagName( "A" );
        if ( oRows !== null && typeof( oRows) !== 'undefined' )  {
            for ( var i=0; i < oRows.length; i++ )
                $addHandler( oRows[i], "click", WineContent.GetListing );
        } 

		ServiceClasses.WineServices.set_timeout( 10000 );
		ServiceClasses.WineServices.set_defaultFailedCallback( this.FailedCallback );
	},

	finished:function( )	{
		return (this._waitingAsyncServiceCall == false);
	},

	startCallback:function( ctrl )	{
	    this._updatectrl = ctrl;
		this._waitingAsyncServiceCall = true;
        // Update display
	    var d = $get( "DescriptionView" );
	    if( d.style.display !== "none" )
	        this.LoadingSymbol( d );
	    else
	        this.LoadingSymbol( ctrl );
	},

	endCallback:function(  )	{
        $get( "progressImage" ).style.display = "none";	
		if( this._updatectrl !== null ) 
		{ if( typeof( this._updatectrl.$oldClass ) !== 'undefined' )
	            this._updatectrl.className = this._updatectrl.$oldClass; 
    	    this._updatectrl = null; 
	    } 
		this._waitingAsyncServiceCall = false;
	},

	GetNext:function( )	{
        if( !this.finished( ) ) return; 
		ServiceClasses.WineServices.GetNext( $get( this._contenthidden ).value, this._category, this._filter, this.GetNextPrevCallback );
		this.startCallback( this._maincontent );
	},

	GetPrev:function( )	{
        if( !this.finished( ) ) return; 
		ServiceClasses.WineServices.GetPrev( $get( this._contenthidden ).value, this._category, this._filter, this.GetNextPrevCallback );
		this.startCallback( this._maincontent );
	},

	GetContactInfo:function( e, source )	{
	    if( source.className.indexOf( "faint" ) > -1) return;
        if( !this.finished( ) ) return; 
		ServiceClasses.WineServices.GetContactInfo( $get( this._contenthidden ).value, this._category, this._filter, this.GetAuxInfoCallback );
		this.startCallback(this._auxcontent);
        this.UpdateAllBtns( "Cimg" );
	},

	GetDistributorInfo:function( e, source )	{
	    if( source.className.indexOf( "faint" ) > -1) return;
        if( !this.finished( ) ) return; 
		ServiceClasses.WineServices.GetDistributorInfo( $get( this._contenthidden ).value, this._filter, this.GetAuxInfoCallback );
		this.startCallback(this._auxcontent);
        this.UpdateAllBtns( "Bimg" );
	},

	GetTastingSalesInfo:function( e, source ) 	{
	    if( source.className.indexOf( "faint" ) > -1) return;
        if( !this.finished( ) ) return; 
		ServiceClasses.WineServices.GetTastingSalesInfo( $get( this._contenthidden ).value, this._filter, this.GetAuxInfoCallback );
		this.startCallback(this._auxcontent);
        this.UpdateAllBtns( "Timg" );
	},

	GetFunctionInfo:function( e, source ) {
	    if( source.className.indexOf( "faint" ) > -1) return;
        if( !this.finished( ) ) return; 
		ServiceClasses.WineServices.GetFunctionInfo( $get( this._contenthidden ).value, this._category, this._filter, this.GetAuxInfoCallback );
		this.startCallback(this._auxcontent);
        this.UpdateAllBtns( "Bimg" );
	},

	GetFeedbackInfo:function( e, source )	{
	    if( source.className.indexOf( "faint" ) > -1) return;
        if( !this.finished( ) ) return; 
		ServiceClasses.WineServices.GetFeedbackInfo( $get( this._contenthidden ).value, this._category, this._filter, this.GetAuxInfoCallback );
		this.startCallback(this._auxcontent);
        this.UpdateAllBtns( "Eimg" );
	},

	GetWineryAccomInfo:function( e, source )	{
	    if( source.className.indexOf( "faint" ) > -1) return;
        if( !this.finished( ) ) return; 
		ServiceClasses.WineServices.GetWineryAccomInfo( $get( this._contenthidden ).value, this._filter, this.GetAuxInfoCallback );
		this.startCallback(this._auxcontent);
        this.UpdateAllBtns( "Limg" );
	},

	GetWineryAwardInfo:function( e, source )	{
	    if( source.className.indexOf( "faint" ) > -1) return;
        if( !this.finished( ) ) return; 
		ServiceClasses.WineServices.GetWineryAwardInfo( $get( this._contenthidden ).value, this._filter, this.GetAuxInfoCallback );
		this.startCallback(this._auxcontent);
        this.UpdateAllBtns( "Aimg" );
	},

	GetWineryDiningInfo:function( e, source )	{
	    if( source.className.indexOf( "faint" ) > -1) return;
        if( !this.finished( ) ) return; 
		ServiceClasses.WineServices.GetWineryDiningInfo( $get( this._contenthidden ).value, this._filter, this.GetAuxInfoCallback );
		this.startCallback(this._auxcontent);
        this.UpdateAllBtns( "Dimg" );
	},

	GetMapInfo:function( e, source )	{
	    if( source.className.indexOf( "faint" ) > -1) return;
        if( !this.finished( ) ) return; 
		ServiceClasses.WineServices.GetMapInfo( $get( this._contenthidden ).value, this._category, this._filter, this.GetAuxInfoCallback );
		this.startCallback(this._auxcontent);
        this.UpdateAllBtns( "Mimg" );
	},

	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( )	{
        // Display the contents
        $get( "DescriptionView" ).style.display = "none";
        $get( "ContentView" ).style.display = "block"; 
        $get( "listingName" ).style.display = "block"; 
		$get( this._regionlabel ).innerHTML = this._contentObj.RegionName;
		$get( this._regionhidden ).value = this._contentObj.RegionID;
		$get( this._contentlabel ).innerHTML = this._contentObj.Name;
		$get( this._contenthidden ).value = this._contentObj.Name;
		 this._maincontent.innerHTML = this._contentObj.HtmlContent;
   	     this._auxcontent.innerHTML =  this._contentObj.HtmlContact;
        
        // Image map...
        if( typeof( imagemaplink ) !== "undefined" ) imagemaplink( this._contentObj.ImageMapPos );
        // Email
        if(  this._contentObj.Email !== null && this._contentObj.Email !== "" )
       {  var el = $get(  'emaillink' ); el.href = this._contentObj.Email; el.style.display = "block"; }
        else $get( 'emaillink' ).style.display = "none";     
        // Web
        if(  this._contentObj.WebUrl !== null && this._contentObj.WebUrl !== "" )
        {  var wl = $get( 'weblink' ); wl.href = this._contentObj.WebUrl; wl.style.display = "block";  }
        else $get( 'weblink' ).style.display = "none";     
        // Gallery link
        if(  this._contentObj.GalleryUrl !== null && this._contentObj.GalleryUrl !== "" )
        {  var gl = $get( 'gallerylink' ); gl.href = this._contentObj.GalleryUrl; gl.style.display = "block";  }
        else $get( 'gallerylink' ).style.display = "none";     
        // Reviews 
        var rl = $get( 'reviewlink' );
        if( rl !== null )
        {  rl.href = this._reviewurl + this._contentObj.ReviewName; 
            rl.style.display = (this._contentObj.ContentIndex.indexOf( "R" ) > -1 ) ? "block" : "none";
        }

        this.UpdateBtns( $get( 'Aimg' ), "A" );
        this.UpdateBtns( $get( 'Cimg' ), "C" );
        this.UpdateBtns( $get( 'Bimg' ), "B" );
        this.UpdateBtns( $get( 'Dimg' ), "D" );
        this.UpdateBtns( $get( 'Timg' ), "T" );
        this.UpdateBtns( $get( 'Fimg' ), "F" );
        this.UpdateBtns( $get( 'Eimg' ), "E" );
        this.UpdateBtns( $get( 'Limg' ), "L" );
        this.UpdateBtns( $get( 'Mimg' ), "M" );

        // Call additional page functions if necessary	    	
		if( this._onsuccessCallback != null )
		    this._onsuccessCallback( );
	},

    UpdateBtns:function( ctrl, param )    {
        if( typeof(ctrl) !== 'undefined' && ctrl !== null ) 
        { 
            ctrl.className = (this._contentObj.ContentIndex.indexOf( param ) > -1 ) ? "btmbtns pointerCursor" : "btmbtns faint";
            // The contact us button is always the default.
            if( param == "C")
	            ctrl.setAttribute( "src", ctrl.getAttribute( "src" ).replace( "C.jpg", "C_.jpg") );
            else
	            ctrl.setAttribute( "src", ctrl.getAttribute( "src" ).replace( "_.jpg", ".jpg") );
        }
    },
    	
    UpdateAllBtns:function( btnid )    {
        var btns = $get( "btmbtns" ).getElementsByTagName( "IMG" );
        for ( var i=0; i < btns.length; i++ )
        {
            if( btns[ i ].id == btnid )
            { if( btns[ i ].getAttribute( "src" ).indexOf( "_.jpg" ) < 0 )
                    btns[ i ].setAttribute( "src", btns[ i ].getAttribute( "src" ).replace( ".jpg", "_.jpg") ); }
            else
                btns[ i ].setAttribute( "src", btns[ i ].getAttribute( "src" ).replace( "_.jpg", ".jpg") );
        } 
    },
    	
    UpdateIndex:function(  )    {
        var indexpanel =  $get( this._indexcontent );

        // Scan through the rows in the table and set the colour.
        var oRows = indexpanel.getElementsByTagName( "A" );
        if ( oRows === null || typeof( oRows) === 'undefined' )  return;

        // Find the clicked row
        for ( var i=0; i < oRows.length; i++ )
        {
            oRows[i].className = "";
            if( oRows[i].innerHTML == this._contentObj.Name )
            {
                oRows[i].className = "selected";

               // Maybe scroll up
               if( (oRows[i].offsetTop - oRows[0].offsetTop) > (indexpanel.scrollTop + indexpanel.clientHeight - oRows[0].offsetHeight ))
                    indexpanel.scrollTop = (oRows[i].offsetTop - oRows[0].offsetTop) - indexpanel.clientHeight + oRows[0].offsetHeight; 
               // Maybe scroll down
               else if( (oRows[i].offsetTop - oRows[0].offsetTop) < indexpanel.scrollTop )
                    indexpanel.scrollTop = (oRows[i].offsetTop - oRows[0].offsetTop); 
            } 
        }
    },

	// ### CALLBACKS (Not in 'this' context ### //
	GetListing:function( eventElement )   	{
		if( WineContent.finished( ) && eventElement.target.tagName == "A" )
		{ 
		    ServiceClasses.WineServices.GetListing( eventElement.target.innerHTML, WineContent._category, WineContent._filter, WineContent.GetInfoCallback );
		    WineContent.startCallback( WineContent._maincontent );
		}
		return stopDefault( eventElement );
	},

	GetInfoCallback:function( result )	{
        WineContent.endCallback(  );
		if( result != null ) 
		{ 
		    WineContent._contentObj = Sys.Serialization.JavaScriptSerializer.deserialize( result ); 
		    WineContent.UpdateUI( );
		    WineContent.UpdateIndex( );
        }
	},

	GetNextPrevCallback:function( result )	{
        WineContent.endCallback(  );
		if( result != null ) 
		{ 
		    WineContent._contentObj = Sys.Serialization.JavaScriptSerializer.deserialize( result ); 
		    WineContent.UpdateUI( );
		    WineContent.UpdateIndex( );
        }
	},

	GetAuxInfoCallback:function( result )	{
        WineContent.endCallback( );
		if( result != null ) 
		    WineContent._auxcontent.innerHTML = result;
	},

	// This is the failed callback function.
	FailedCallback:function( error )	{
        WineContent.endCallback( );
	    alert( error.get_message( ) );
	}
}
// Notify ScriptManager that this is the end of the script.
if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();

