
function blinkNotify(container, notify_str, keep_around, duration)
{
  $(container).setStyle('display', 'block').set('html', notify_str);
  $(container).setStyle('opacity', 1);

  if (!duration)
    duration = "short";

  var fx = new Fx.Tween($(container), { 'link': 'chain', 'duration': duration });
  fx.start('background-color', '#fff', '#fff6ca');
  fx.start('background-color', '#fff6ca', '#fff');
  
  if (!keep_around)
  {
    fx.start('opacity', 1, 0);
    fx.start('display', 'block', 'none');
  }
}

function showAlert(container, type)
{
  var fx = new Fx.Morph($(container), { 'duration': 'long' });

  if (type == "error")
      fx.start({ 'background-color': ['#fff', '#eba78c'], 'color': ['#4f474b', '#fff'], 'border-color': ['#ccc', '#d76256'] });
  else if (type == "success")
    fx.start({ 'background-color': ['#fff', '#8cadeb'], 'color': ['#4f474b', '#fff'], 'border-color': ['#ccc', '#569bd7'] });

}


function twitterUpdates(data)
{
  var html = '<ul>';

  found = 0;

  for (var x = 0; x < data.length && found < 10; ++x)
  {
    if (data[x]['text'].match(/^(RT )?@/))
      continue;

    found++;

    var mc = data[x]['created_at'].match(/^[A-Za-z]+ ([A-Za-z]+ [0-9]+)/);
    if (mc)
      var dd = mc[1];
    else
      var dd = "";

    html += '<li>' + data[x]['text'] + ' [' + dd + ']</li>';
  }

  html += '</ul>';

  $('twitterUpdates').set('html', html);
}

function setTwitterUpdates()
{
  var headID = document.getElementsByTagName("head")[0];         
  var newScript = document.createElement('script');
  newScript.type = 'text/javascript';
  newScript.src = 'http://twitter.com/statuses/user_timeline/onsitebrakes.json?callback=twitterUpdates&count=20';
  headID.appendChild(newScript);

}

//
//  copy to clipboard
//

var CopyClipboardButton = {};

CopyClipboardButton.getCopyText = function(dom_id) {
    var el = document.getElementById(dom_id);
	try	{
		return (el.value || el.innerText || el.textContent);
	} catch(e) {
		return '';
	}
}
CopyClipboardButton.appendButton = function(appendCopyButtonContainerId, copyTextContainerId, options) {
	var a = document.getElementById(appendCopyButtonContainerId);
	var button = CopyClipboardButton.create(copyTextContainerId, options);
	a.appendChild(button);
}

CopyClipboardButton.listen = function (elem, evnt, func) {
  if (elem.addEventListener) // W3C DOM
    elem.addEventListener(evnt,func,false);
  else if (elem.attachEvent) { // IE DOM
    var r = elem.attachEvent("on"+evnt, func);
    return r;
  }
}

CopyClipboardButton.targ = function(e) {
	var targ;
	if (!e) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;
	return targ;
}


CopyClipboardButton.create = function(copyTextContainerId, options) {
	var opts = {
		"height": "16",
		"width": "50",
		"fontSize": "14",
		"fontColor": "#000000",
		"fontFace": "Helvetica",
		"pathToSwf": "CopyClipboardButton.swf?v=3.0",
		"imageUrl": '',
		"copyText": '',
		"wmode": "transparent"
	}
	if (typeof(options) == "undefined") { var options = {}; }
	for (var k in options) {
		opts[k] = options[k];
	}
	var e = document.createElement('embed');
    var o = document.createElement("object");
  
	o.height = opts['height'];
	e.height = o.height;
    o.width = opts['width'];
	e.width = o.width;			
	
  e.setAttribute('type', "application/x-shockwave-flash");

  var pmovie = document.createElement("param");
  pmovie.name = "movie";
  pmovie.value = opts['pathToSwf'];
  o.appendChild(pmovie);
  e.setAttribute('src', pmovie.value);

  var pFlashVars = document.createElement("param");
  pFlashVars.name = "FlashVars";
  pFlashVars.value = "copyTextContainerId="+ copyTextContainerId +"&fontSize=" + opts['fontSize'] +"&fontFace=" + opts['fontFace'] +"&fontColor=" + opts['fontColor'] + "&imageUrl=" + opts['imageUrl'] + "&copyText=" + opts['copyText'] ;
  o.appendChild(pFlashVars);
  e.setAttribute('flashVars', pFlashVars.value);

  var pquality = document.createElement("param");
  pquality.name = "quality";
  pquality.value = "high";
  o.appendChild(pquality);
  
  var pmenu = document.createElement("param");
  pmenu.name = "menu";
  pmenu.value = "false";
  o.appendChild(pmenu);
  
  var pwmode = document.createElement("param");
  pwmode.name = "wmode";
  pwmode.value = opts['wmode'];
  o.appendChild(pwmode);
  e.setAttribute('wmode', opts['wmode']);

  try {
      o.appendChild(e);
  } catch(z) {

      var a = document.createElement('a');
      a.appendChild(document.createTextNode('Copy'));
      a.href = "javascript:copyToClipboard('"+copyTextContainerId+"')";

      return a;
  }
    
  
  return o;
}

function copyToClipboard(container)
{
  var s = document.getElementById(container).value;

  if( window.clipboardData && clipboardData.setData )
  {
    clipboardData.setData("Text", s);
  }
  else
    {
      // You have to sign the code to enable this or allow the action in about:config by changing
      user_pref("signed.applets.codebase_principal_support", true);
      netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
      
      var clip = Components.classes['@mozilla.org/widget/clipboard;[[[[1]]]]'].createInstance(Components.interfaces.nsIClipboard);
      if (!clip) return;
      
      // create a transferable
      var trans = Components.classes['@mozilla.org/widget/transferable;[[[[1]]]]'].createInstance(Components.interfaces.nsITransferable);
      if (!trans) return;
      
      // specify the data we wish to handle. Plaintext in this case.
      trans.addDataFlavor('text/unicode');
      
      // To get the data from the transferable we need two new objects
      var str = new Object();
      var len = new Object();
      
      var str = Components.classes["@mozilla.org/supports-string;[[[[1]]]]"].createInstance(Components.interfaces.nsISupportsString);
      
      var copytext=meintext;
      
      str.data=copytext;
      
      trans.setTransferData("text/unicode",str,copytext.length*[[[[2]]]]);
      
      var clipid=Components.interfaces.nsIClipboard;
      
      if (!clip) return false;
      
      clip.setData(trans,null,clipid.kGlobalClipboard);   
    }
}

function showAlertLightbox()
{
  var im = new Element('div', { 'id': 'alertlboxbg' });
  var im2 = new Element('div', { 'id': 'alertlbox' });

  im2.set('html', '<table border="0" width="100%" height="100%"><tr><td align="center" valign="center" width="100%" height="100%"><div style="width:80%;height:90%;max-width:600px;max-height:700px;-moz-border-radius: 20px;-webkit-border-radius: 20px;background-color:#ffffff;padding: 5px;text-align:center; position:relative;"><div style="position: absolute; right: -20px; top: -20px"><a href="javascript:void(0)" onClick="hideAlertLightbox()"><img src="http://static.chartbeat.com/images/bigclose.png" border="0"></a></div><iframe width="98%" height="98%" src="/alertstab" frameborder="0" style="max-width:800px;max-height:680px;border: 5px solid #FFFFFF" marginwidth="0" marginheight="0" scrolling="auto"></iframe></div></td></tr></table>');

  im.set('opacity', 0.6);

  $(document.body).adopt(im);
  $(document.body).adopt(im2);

  $(document.body).setStyle('overflow', 'hidden');

}

function hideAlertLightbox()
{
  $('alertlboxbg').destroy();
  $('alertlbox').destroy();
  
  $(document.body).setStyle('overflow', 'auto');
}
