using System.ComponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AlfredoLotar.Livro.Aspnet
{
    [DefaultProperty("Text"),
    ToolboxData("<{0}:AdRotatorFlash runat=\"server\"></{0}:AdRotatorFlash")]
    public class AdRotatorFlash : System.Web.UI.WebControls.AdRotator
    {
        private bool isSWF;
        private string ImgUrl;
        private string NavUrl;
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            if (isSWF)
            {
                StringBuilder sb = new StringBuilder("<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"");
                sb.Append("codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0\" width={0} height={1}>");
                sb.Append("  <param name=\"movie\" value={2}>");
                sb.Append("  <param name=\"quality\" value=\"high\">");
                sb.Append("  <embed src={3} quality=\"high\" ");
                sb.Append("  pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\"></embed></object>");

                StringBuilder sb1 = new StringBuilder();
                sb1.AppendFormat(sb.ToString(), 1000, 1000, ImgUrl, NavUrl);
                writer.Write(sb1.ToString());
            }
            else
            {
                base.Render(writer);
            }
        }

        protected override void OnAdCreated(AdCreatedEventArgs e)
        {
            base.OnAdCreated(e);
            if (e.ImageUrl.ToLower().IndexOf(".swf") != -1)
            {
                isSWF = true;
                ImgUrl = e.ImageUrl;
                NavUrl = e.NavigateUrl;
            }
            else
            {
                isSWF = false;
            }
        }
    }
}