/** Used for pop up form **/

function set_opacity(obj, opacity) {
  try {
    obj.style.opacity=(opacity==1?'':opacity);
    obj.style.filter=(opacity==1?'':'alpha(opacity='+opacity*100+')');
  }
  catch (e) {}
  obj.setAttribute('opacity', opacity); // for future reference
}

function get_opacity(obj) {
  return obj.opacity ? obj.opacity : 1;
}

function ge()
{
  var ea;
  for( var i = 0; i < arguments.length; i++ ) {
    var e = arguments[i];
    if( typeof e == 'string' )
      e = document.getElementById(e);
    if( arguments.length == 1 )
      return e;
    if( !ea )
      ea = new Array();
    ea[ea.length] = e;
  }
  return ea;
}

//
// generic bubble class, does very little on its own
function parent_bubble(className) 
{
  this.className=className;
  this.content=null;
  this.obj=null;
  this.popup=null;
  this.iframe=null;
  this.hidden_objects=new Array();
}
parent_bubble.prototype.should_hide_objects=navigator.userAgent.indexOf('Mac OS X')!=-1;

// sets the content with raw html
parent_bubble.prototype.set_content=function(html) 
{
  this.content.innerHTML=html;
}

// shows a bubble with raw html
parent_bubble.prototype.show_bubble=function(html, lang) 
{
  if (!this.obj) {
    this.build_bubble(lang);
  }
  this.set_content(html);
  this.show();
}

// shows a pop bubble with an ajax request and uses that innerHTML
parent_bubble.prototype.show_ajax_bubble=function(type, id, lang) 
{
  this.show_bubble('<div class="bubble_loading"><img src="images/common/loader.gif"></div>', lang);
  xajax_MailForm(type, id, lang);
  return false;
}
// shows a pop bubble with an ajax request and uses that innerHTML
parent_bubble.prototype.show_media_bubble=function(id, type, t_id, lang) 
{
  this.show_bubble('<div class="bubble_loading"><img src="images/common/loader.gif"></div>', lang);
  xajax_MediaBubble(id, type, t_id, lang);
  return false;
}
// shows a pop bubble with an ajax request and uses that innerHTML
parent_bubble.prototype.show_intro=function(lang) 
{
  this.show_bubble('<table width="100%" height="600"><tr valign="middle" height="100%"><td align="center" height="100%"><input type="image" src="images/'+lang+'/M50.gif" onclick="parent_bubble.get_bubble(this).close(1000); return false;" /></td></tr></table>', lang);
  return false;
}

// closes the bubble
parent_bubble.prototype.close=function(interval) 
{
  if (this.obj) 
  {
	  temp = parent_bubble.get_bubble(this.obj);
	  temp.fade_out(interval);
	  temp = null;
  }
}

// hides the bubble
parent_bubble.prototype.hide=function() 
{
  if (this.obj) 
  {
    this.obj.style.display='none';
  }

  // unhide hidden objects
  if (this.hidden_objects.length) 
  {
    for (var i in this.hidden_objects) 
    {
      this.hidden_objects[i].style.visibility='';
    }
    this.hidden_objects=new Array();
  }
}

// fades the bubble out over X seconds
parent_bubble.prototype.anim_res=5;
parent_bubble.prototype.fade_out=function(interval, first_call)
{
  if (!interval)
    interval=350;
  if (!first_call)
    first_call=(new Date).getTime()-this.anim_res;
  var fade=1.0-(((new Date).getTime()-first_call)/interval)*1.0;

  if (fade>0) 
  {
    set_opacity(this.obj, fade);
    var myself=this;
    setTimeout(function(){myself.fade_out(interval, first_call)}, this.anim_res);
  }
  else
  {
    this.hide();
    set_opacity(this.obj, 1);
  }
}

// shows the bubble (if it's built already)
parent_bubble.prototype.show=function() 
{
  if (this.obj && this.obj.style.display) 
  {
    this.obj.style.visibility='hidden';
    this.obj.style.display='';
    this.reset_bubble();
    this.obj.style.visibility='';
    this.obj.bubble=this; // for onclick events, etc
    this.hide_objects();
  }
  else
  {
    this.reset_bubble();
    this.hide_objects();
  }
}

parent_bubble.prototype.hide_objects=function()
{
  if (!this.should_hide_objects) { return; }

  var rect={x:elementX(this.content), y:elementY(this.content), w:this.content.offsetWidth, h:this.content.offsetHeight};
  var objects=new Array();
  var iframes=document.getElementsByTagName('iframe');
  for (var i=0; i<iframes.length; i++)
  {
    if (iframes[i].className.indexOf('share_hide_on_bubble')!=-1)
    {
      objects.push(iframes[i]);
    }
  }
  var swfs=document.getElementsByTagName('embed');
  for (var i=0; i<swfs.length; i++)
  {
    objects.push(swfs[i]);
  }
  for (var i=0; i<objects.length; i++)
  {
    var pn=false;
    var node = objects[i].offsetHeight ? objects[i] : objects[i].parentNode;
    swf_rect={x:elementX(node), y:elementY(node), w:node.offsetWidth, h:node.offsetHeight};
    if (rect.y + rect.h > swf_rect.y &&
        swf_rect.y + swf_rect.h > rect.y &&
        rect.x + rect.w > swf_rect.x &&
        swf_rect.x + swf_rect.w > rect.w &&
        array_indexOf(this.hidden_objects, node) == -1) {
          this.hidden_objects.push(node);
          node.style.visibility='hidden';
          node.style.visibility='hidden';
    }
  }
}

// builds a bubble base
parent_bubble.prototype.build_bubble=function()
{
  // build a holder
  if (!this.obj && !(this.obj=ge('parent_bubble'))) {
    this.obj=document.createElement('div');
    this.obj.id='parent_bubble';
  }
  this.obj.className='parent_bubble' + (this.className ? ' ' + this.className : '');
  this.obj.style.display='none';
  document.body.appendChild(this.obj);

  // build an iframe to block out select boxes
  if (!this.iframe && !(this.iframe=ge('parent_bubble_iframe')))
  {
    this.iframe=document.createElement('iframe');
    this.iframe.id='parent_bubble_iframe';
  }
  this.iframe.frameBorder='0';
  this.obj.appendChild(this.iframe);
 
  // build a div to hold the content
  if (!this.popup && !(this.popup=ge('parent_bubble_popup')))
  {
    this.popup=document.createElement('div');
    this.popup.id='parent_bubble_popup';
  }
  this.obj.appendChild(this.popup);
}

// repositions the elements to be where they should be
parent_bubble.prototype.reset_bubble=function()
{
  if (!this.popup || !this.iframe)
    return;
  this.reset_bubble_obj();
  this.iframe.style.width=this.popup.offsetWidth+'px';
  this.iframe.style.height=this.popup.offsetHeight+'px';
}

// does nothing
parent_bubble.prototype.reset_bubble_obj=function() {;}

// sets the width of the bubble. if w is false it will use the default
parent_bubble.prototype.set_width=function(w) 
{
  this.obj.style.width=w ? w+'px' : '';
}

// returns the bubble object in which obj is contained
/*static*/ parent_bubble.get_bubble=function(obj) {
  while (!obj.bubble && obj.parentNode) {
    obj=obj.parentNode;
  }
  return obj.bubble?obj.bubble:false;
}

// shows a bubble with the given title and body content
parent_bubble.prototype.show_prompt=function(title, content) {
  this.show_bubble('<h2><span>' + title + '</span></h2><div class="bubble_content">' + content + '</div>');
}

// shows a form that will cause a post
parent_bubble.prototype.show_form=function(title, content, button, target) 
{
  content='<form action="../includes/%27%20+%20target%20+%20%27" method="post">' + this.content_to_markup(content);
  var post_form_id=ge('post_form_id');
  if (post_form_id) 
  {
    content+='<input type="hidden" name="post_form_id" value="' + post_form_id.value + '" />';
  }
  content+='<div class="bubble_buttons"><input class="inputsubmit" name="confirm" type="submit" value="' + button + '" />';
  content+='<input type="hidden" name="next" value="'+htmlspecialchars(document.location)+'"/>';
  content+='<input class="inputsubmit" type="button" value="Cancel" onclick="parent_bubble.get_bubble(this).fade_out(100)" /></form>';
  this.show_prompt(title, content);
  return false;
}

parent_bubble.prototype.content_to_markup=function(content) 
{
  return (typeof content == 'string') ? 
         '<div class="bubble_body">' + content + '</div>' : 
         '<div class="bubble_summary">'+ content.summary +'</div><div class="bubble_body">'+ content.body +'</div>';
}




//
// class for centered bubble with flat transparent borders
function pop_bubble(className) {
	this.constructor(className);
}

pop_bubble.prototype = new parent_bubble();
//pop_bubble.extend(parent_bubble);

//http://www.ajaxpath.com/javascript-inheritance/
//http://home.cogeco.ca/~ve3ll/jstutorx.htm
//http://msdn.microsoft.com/library/default.asp?url=/workshop/author/filter/reference/filters/alphaimageloader.asp

// builds a pop bubble -- uses tables, but compatible in all browsers
pop_bubble.prototype.build_bubble=function(lang) 
{
  parent_bubble.prototype.build_bubble.call(this);

  this.obj.className+=' pop_bubble';
  this.popup.innerHTML='<table class="pop_bubble_table">'+
                       '<tr><td class="pop_topleft"><img src="images/'+lang+'/pop_bubble_top_left.png"></td><td class="pop_border"></td><td class="pop_topright"><img src="images/'+lang+'/pop_bubble_top_right.png"></td></tr>'+
                       '<tr><td class="pop_border"></td><td class="pop_content" id="pop_content"></td><td class="pop_border"></td></tr>'+
                       '<tr><td class="pop_bottomleft"><img src="images/'+lang+'/pop_bubble_bottom_left.png"></td><td class="pop_border"></td><td class="pop_bottomright"><img src="images/'+lang+'/pop_bubble_bottom_right.png"></td></tr>'+
                       '</table>';
  this.content=document.getElementById('pop_content');
}

// centers the bubble where it should be
pop_bubble.prototype.reset_bubble_obj=function()
{
	//this.obj.style.top='70px';
	//this.obj.style.top=((screen.height/2)-(this.popup.offsetHeight/2))+'px';
	//this.obj.style.top=((document.body.offsetHeight/2)-(this.popup.offsetHeight/2))+'px';
	//this.obj.style.marginTop=-(this.popup.offsetHeight/2)+'px';
	//this.obj.style.marginLeft=-(this.popup.offsetWidth/2)+'px';

	//this.obj.style.left=((screen.width/2)-(this.popup.offsetWidth/2))+'px';
	//this.obj.style.left=((document.body.offsetWidth/2)-(this.popup.offsetWidth/2))+'px';
}
