//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using CommunityServer.Components;
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace CommunityServer.Controls 
{
    public enum Zones 
    {
        StandardTop,
        StandardBottom,
        Inline,
        GoogleAdsense,
        Skyscraper,
        Square
    }

	// *********************************************************************
	//  Ads
	//
	/// <summary>
	/// This server control is a placeholder where the user can add advertising.
	/// </summary>
	// ***********************************************************************/
    public class Ads : TemplatedWebControl
    {
        #region Child Controls & Member vars

        Control GoogleAdsense;
        Control StandardTop;
        Control StandardBottom;
        Control Square;
        Control Inline;
        Control Skyscraper;

        CSContext csContext = CSContext.Current;
        Zones zone = Zones.StandardTop;
        int googleRotationPercent = 0;
        
        #endregion

        protected override void OnInit(EventArgs e) 
        {
            if (SkinName == null)                
                ExternalSkinFileName = "Skin-Ads.ascx";
            else 
                ExternalSkinFileName = SkinName;
			
            base.OnInit(e);
        }
		
        protected override void OnLoad(EventArgs e) 
        {
            if(!(this.Parent is AdPart))
            {
                if(!AdsConfiguration.DisplayAd(csContext.User))
                {
                    this.Visible = false;
                    return;
                }
            }

//            // Are they enabled?
//            //
//            if (csContext.SiteSettings.EnableAds == false)
//            {
//                this.Visible = false;
//                return;
//            }
//
//            // If the user is already authenticated we have no work to do
//            //
//            if (Page.Request.IsAuthenticated) 
//            {
//                if (csContext.User.IsAdministrator || csContext.User.IsModerator)
//                {
//                    this.Visible = false;
//                    return;
//                }
//            }

            if (!Page.IsPostBack)
                DataBind();

            base.OnLoad(e);
        }

        #region DataBind

        public override void DataBind() 
        {
            base.DataBind();
            
            switch (this.Zone) 
            {
                case Zones.StandardTop:
                    if (DisplayGoogleAd) 
                    {
                        if (GoogleAdsense != null)
                            GoogleAdsense.Visible = true;
                    } 
                    else 
                    {
                        if (StandardTop != null)
                            StandardTop.Visible = true;
                    }
                    break;

                case Zones.StandardBottom:
                    if (DisplayGoogleAd) 
                    {
                        if (GoogleAdsense != null)
                            GoogleAdsense.Visible = true;
                    } 
                    else 
                    {
                        if (StandardBottom != null)
                            StandardBottom.Visible = true;
                    }
                    break;

                case Zones.Square:
                    if (Square != null)
                        Square.Visible = true;
                    break;

                case Zones.Skyscraper:
                    if (Skyscraper != null)
                        Skyscraper.Visible = true;
                    break;

                case Zones.Inline:
                    if (Inline != null)
                        Inline.Visible = true;
                    break;

                case Zones.GoogleAdsense:
                    if (GoogleAdsense != null)
                        GoogleAdsense.Visible = true;
                    break;
            }
        }
		
        #endregion		
		
        #region Skin
                
        protected override void AttachChildControls() 
        {
            GoogleAdsense = FindControl("GoogleAdsense");
            StandardTop = FindControl("StandardTop");
            StandardBottom = FindControl("StandardBottom");
            Square = FindControl("Square");
            Inline = FindControl("Inline");
            Skyscraper = FindControl("Skyscraper");
        }
		        
        #endregion

        #region Properties

        public bool DisplayGoogleAd 
        {
            get 
            {
                Random r = new Random();

                if (r.Next(100) < googleRotationPercent)
                    return true;

                return false;
            }
        }

        public int GoogleRotationPercent 
        {
            get { return googleRotationPercent; }
            set { googleRotationPercent = value; }
        }

        public Zones Zone 
        {
            get { return zone; }
            set { zone = value; }
        }

        #endregion
    }
}