var EVA =
{
  server: 'marketing',
  App: {},
  Site: 
  {
    News: function()
    {
      this.showing_news_post_num = null;
    }
  }
};

EVA.Site.News.prototype =
{
  make_show_post_function: function (num)
  {
    var app = this;

    return function ()
    {
      if(app.showing_news_post_num != null)
      {
        document.getElementById('post-' + app.showing_news_post_num).style.display = 'none';
      }
  
      document.getElementById('news-posts').style.display = 'block';
      document.getElementById('post-' + num).style.display = 'block';
      app.showing_news_post_num = num;
	  resize_descendant_imgs_function();
      return false;
    }
  },

  make_hide_post_function: function (num)
  {
    return function ()
    {
      document.getElementById('news-posts').style.display = 'none';
      document.getElementById('post-' + num).style.display = 'none';
      return false;
    }
  },
  
  run: function()
  {
    var app = this;

    document.observe("dom:loaded", function ()
    {
      if($('locale-field'))
      {
        Event.observe($('locale-field'), 'change', function (event)
        {
          var locale =  $('locale-field').getValue();
          
          if(locale.length > 0) {
            try { window.location = '/locale/' + locale; }
            catch(e) { } // who cares what IE thinks?
          }
        });
      }
    
      if($('news-posts'))
      {
        var posts = document.getElementsByClassName('post', $('news-posts'));
      
        for(var i = 0; i < posts.length; i++)
        {
          document.getElementById('close-post-' + i).onclick = 
            app.make_hide_post_function(i);
        }
      
        var posts = document.getElementsByClassName('summary', $('news-summaries'));
        
        for(var i = 0; i < posts.length; i++)
        {
          document.getElementById('open-post-' + i).onclick = 
            app.make_show_post_function(i);
        }
      }
    });
  }
};

var site_news = new EVA.Site.News;
site_news.run();

function insert_flash(locale, header_image)
{
  document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" height="192" width="794">');
  document.write('<param name="movie" value="/images/' + locale + '/' + header_image + '.swf" />');
  document.write('<param name="quality" value="best" />');
  document.write('<param name="wmode" value="transparent" />');
  document.write('<embed src="/images/' + locale + '/' + header_image + '.swf" type="application/x-shockwave-flash" height="192" width="794" wmode="transparent" />');
  document.write('</object>');
}

function resize_descendant_imgs_function()
{
	var imgs = document.images;
	var setwidth = 400;

	for (img in imgs) {
		var x = document.images[img];
		if (x.width > setwidth) {
			x.height = x.height * (setwidth / x.width);
			x.width = setwidth;
		}
	}
}