# Copyright (C) 2003-2005 Kouichirou Eto, All rights reserved.

module Qwik
  class Action
    def plg_trackfeed(id)
      raise unless /\A[0-9a-f]+\z/ =~ id
      [:script, {:src=>"http://trackfeed.com/usr/#{id}.js"}, ""]
    end

    def plg_sub_bloglines(url=nil, icon="modern4")
      raise unless /\A[a-z0-9]+\z/ =~ icon
      url = @site.url if url.nil?
      [:a, {:href=>"http://www.bloglines.com/sub/#{url}"},
	[:img,
	  {:src=>"http://www.bloglines.com/images/sub_#{icon}.gif",
	    :border=>"0",
	    :alt=>"subscribe Bloglines"
	  }]]
    end

    def plg_google_ad_button
      str = <<"EOT"

google_ad_client = "pub-5746941487859743";
google_alternate_color = "eeeeee";
google_ad_width   = 125;
google_ad_height  = 125;
google_ad_format  = "125x125_as";
google_ad_channel ="2257701812";
google_ad_type = "text_image";
google_color_border = "ffffff";
google_color_bg   = "ffffff";
google_color_link = "00cccc";
google_color_url  = "00cc66";
google_color_text = "000000";
EOT
      google_ad(str)
    end

    def plg_google_ad
      str = <<"EOT"

google_ad_client = "pub-5746941487859743";
google_alternate_color = "eeeeee";
google_ad_width   = 120;
google_ad_height  = 240;
google_ad_format  = "120x240_as";
google_ad_channel ="2257701812";
google_ad_type = "text_image";
google_color_border = "ffffff";
google_color_bg   = "ffffff";
google_color_link = "00cccc";
google_color_url  = "00cc66";
google_color_text = "000000";
EOT
      google_ad(str)
    end

    def google_ad(str)
      str << "//"

      [:div,
	{:style=>"text-align: center;"},
	[:script, {:type=>"text/javascript"},
	  [:"!--", str]],
	[:script, {:type=>"text/javascript", :src=>"http://pagead2.googlesyndication.com/pagead/show_ads.js"}, ""]
      ]
    end

    def plg_e_words
      [:script, {:type=>"text/javascript",
	  :src=>"http://e-words.jp/embed.x"}, ""]
    end
  end
end

if $0 == __FILE__
  $LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")
  require "t/test"
  $debug = true
end

if $debug
  class TC_ActExternalService < Test::Unit::TestCase
    include TestSession

    def test_all
      # test_trackfeed
      assert_wiki([:script, {:src=>"http://trackfeed.com/usr/1.js"}, ""],
		  "{{trackfeed(1)}}")
#      assert_raise(RuntimeError){
#	assert_wiki("", "{{trackfeed(z)}}")
#      }
#      assert_raise(ArgumentError){
#	assert_wiki("", "{{trackfeed}}")
#      }

      # test_sub_bloglines
      assert_wiki([:a, {:class=>"external", :href=>"http://www.bloglines.com/sub/http://example.com/test/"}, [:img, {:src=>"http://www.bloglines.com/images/sub_modern4.gif", :border=>"0", :alt=>"subscribe Bloglines"}]], "{{sub_bloglines}}")
      assert_wiki([:a, {:class=>"external", :href=>"http://www.bloglines.com/sub/http://e.com/"}, [:img, {:src=>"http://www.bloglines.com/images/sub_modern4.gif", :border=>"0", :alt=>"subscribe Bloglines"}]], "{{sub_bloglines(http://e.com/)}}")
      assert_wiki([:a, {:class=>"external", :href=>"http://www.bloglines.com/sub/http://e.com/"}, [:img, {:src=>"http://www.bloglines.com/images/sub_modern3.gif", :border=>"0", :alt=>"subscribe Bloglines"}]], "{{sub_bloglines(http://e.com/,modern3)}}")
#      assert_raise(RuntimeError){
#	assert_wiki("", "{{sub_bloglines(http://e.com/,-)}}")
#      }

      # test_e_words
      assert_wiki([:script, {:src=>"http://e-words.jp/embed.x",
		      :type=>"text/javascript"}, ""], "{{e_words}}")

      # test_google_ad
      @site.member.add("user@e.com")
      page = @site.create_new
      page.store("{{google_ad}}")
      session("/test/#{page.key}.html")
      #px("//div[@class='section']", 0)
      assert_wiki(/pagead2.googlesyndication.com/, "{{google_ad}}")
    end
  end
end
