/* flash_im.js -- flash instant message support */

function set_recipient(id, name)
{
	$('recipient_id').value = id;
	$('recipient_name').innerHTML = name;
    $('flash_reply').style.display = 'block';
	$('message').focus();
}

/* Close flash window - hide it until next time */
function close_flash()
{
	$('flash_window').style.display = 'none';
}

/* Acknowledge sending of Flash by adding it to the display and clearing the message so a new one can be written */
function clear_flash()
{
	$('message').value = '';
	fd = $('flash_display');
	add_to_flash($('flash_ack').innerHTML + "<br />");
	$('commit').value = 'flash';
    $('commit').disabled = false;
}

function clear_display()
{
    flash_text = "";
	$('flash_display').innerHTML = '';
}

/* Flashbox routines */

function flashbox_update_msg()
{
	$('flash_im_message').value = "";
	return(false);
}


// Do the ajax submission of a flash request.  Use by flash_ims/list
// no longer used as of March 2007?
function submit_flash(this_form, id)
{
	reply_id = 'reply'
	new Ajax.Updater(reply_id, '/flash_ims/create', 
	  {asynchronous:true, evalScripts:true, parameters:Form.serialize(this_form),
		onComplete:function(request){flash_submission('reply_' + id);}}); 
	return false;
}

/* This deletes and hides bulletins */

function delete_bulletin(id) {
	// Hide the bulletin from view
	$('bulletin_' + id).style.display = 'none';
	
    // Not sure why this doesn't really work to remove the bulletin too, 
    // but it's not that important since it does delete the bulletin successfully.
	new Ajax.Updater('bulletin_' + id, '/bulletins/delete/' + id,
	  { asynchronous:true });
	
	return(false);
}

/* The following routine
is_ie = false;
is_iewin = false;
is_browser_known = false; */

/* Find out browser type */
var is_ie = false;

function know_browser()
{
    var agt = navigator.userAgent.toLowerCase();

    is_ie = (agt.indexOf("msie") != -1) &&  (agt.indexOf("opera") == -1);
    is_iewin = (is_ie &&  (agt.indexOf("win") != -1));
    is_iemac = (is_ie &&  (agt.indexOf("mac") != -1));
}

/* A tip of the hat to: http://www.bobbyvandersluis.com/articles/dynamicCSS.php */
function create_style_rule(selector, declaration) {
    if (!document.getElementsByTagName ||
      !(document.createElement || document.createElementNS)) return;
	if (!is_browser_known) {
		know_browser();
	}

    if (is_iemac) return; // script doesn't work properly in IE/Mac
    var head = document.getElementsByTagName("head")[0]; 
    var style = (typeof document.createElementNS != "undefined") ?
      document.createElementNS("http://www.w3.org/1999/xhtml", "style") :
      document.createElement("style");
    if (!is_iewin) {
        var styleRule = document.createTextNode(selector + " {" + declaration + "}");
	    style.appendChild(styleRule); // bugs in IE/Win
    }
	style.setAttribute("type", "text/css");
    style.setAttribute("media", "screen"); 
    head.appendChild(style);
    if (is_iewin &&  document.styleSheets &&  document.styleSheets.length > 0) {
        var lastStyle = document.styleSheets[document.styleSheets.length - 1];
        if (typeof lastStyle.addRule == "object") {
            lastStyle.addRule(selector, declaration);
        }
    }
}

/* Every other browser but IE does this correctly out of the box.
   The inner div transparency hack requires that the entire inner div fill the content of the outer div.
   Normally, height/width of 100% in the CSS works, but for IE we have to size it explicitly. */
var is_browser_known = false;

function compare_screen_width(screen_width, redirect_location)
{
	if (redirect_location.length == 0) {
		return(0);
	}

	width = find_screen_width();
	
	if (width != screen_width) {
		location = redirect_location + width;
	}
}

function find_screen_width()
{
	if (document.documentElement && document.documentElement.clientWidth) {
		width = document.documentElement.clientWidth;
    }
	else if (document.body) {
		width = document.body.clientWidth;
    }
    else {
   	   width = 1024;
    }

	return(width);
}

//compare_screen_width(screen_width, screen_width_uri);

/* Find out browser type */
function know_browser()
{
    var agt = navigator.userAgent.toLowerCase();

    is_ie = (agt.indexOf("msie") != -1) &&  (agt.indexOf("opera") == -1);
    is_iewin = (is_ie &&  (agt.indexOf("win") != -1));
    is_iemac = (is_ie &&  (agt.indexOf("mac") != -1));

}


function adjust_size_in_ie(the_div)
{
  if (!is_browser_known) {
  	know_browser();
  }

  /* We only need to do this in Internet Explorer */
  if (!is_ie) {
  	return(0);
  }

  parent_div = $(the_div);
 
  if (!parent_div) {
	return(false);
  }

  bg_div = $(the_div + "_bg");

  if (bg_div) {
    bg_div.style.width = parent_div.offsetWidth;
    bg_div.style.height = parent_div.offsetHeight - 10;  // don't include whitespace between blocks
  }
}

/* For editing blocks */
var old_height = 0;

function start_block_edit(block, is_blog)
{
	know_browser();
	/* Temporarily enlarge height so editing text is visible */
	if (is_blog && !is_ie) {
		fg = $('foreground_' + block)
		old_height = fg.style.height;
		fg.style.height = "250px";
	}
	e = $('edit_mode_' + block);
	e.innerHTML = 'Loading ...';
	e.style.backgroundColor = 'green';
}

function end_of_start_block_edit(block)
{
	e = $('edit_mode_' + block);
	if (is_ie) {
		// we are covering the parent element entirely so we leave this out.
	}
	else {
		e.innerHTML = 'Edit';
	}

	be = $('block_' + block + "_edit");
	if (is_ie) {
		be.className = "block_edit_ie";
		foreground = $('foreground_' + block);
		//alert("foreground: " + foreground.offsetHeight + " client " + foreground.clientHeight);
        mt = -(foreground.offsetHeight + 10);  //-(foreground.height + foreground.style.marginBottom);
		be.style.marginTop = mt;
		be.style.marginLeft = be.style.marginLeft - 5;
	}
	Effect.Appear(be);
}

function end_block_edit(block)
{
	/* Shrink height back to old size. */
	if (old_height) {
		fg = $('foreground_' + block);
		fg.style.height = old_height + "px";
		old_height = 0;
	}

	be = $('block_' + block + '_edit');
	new Effect.Fade(be);  // be.style.display = 'none';
	e = $('edit_mode_' + block);
	e.innerHTML = 'Edit';
	e.style.backgroundColor = 'red';
}

/* done editing blocks */

/* Ratings */

function activate(id)
{
	new Ajax.Updater("activate_" + id, "/creations/activate/" + id, 
	    { asynchronous:true,evalScripts:true })

}

function rate(id, rating, class_name, template)
{
	new Ajax.Updater("rate_" + id, 
	  "/creations/rate/" + id + "?rating=" + rating + "&class=" + class_name + "&template=" + template,
	    { asynchronous:true,evalScripts:true });
}

/* Interests */

function show_interest(item)
{
	$('interest').value = item;
	
}

/* Menu */

function menu_hover(o)
{
	o.className = 'h';
}

function menu_hover_out(o)
{
	if (o.past_class != 'n' && o.past_class != 's') {
		o.className = 'n';
	}
	else {
		o.className = o.past_class;
	}
}


