// This function will add an onload handler to the body.  It's fine to
// add a bunch of handlers, this keeps track of all of them.
function add_body_onload(func) {
  var old_body_onload=window.onload;
  window.onload=function() {
    if (old_body_onload) { old_body_onload(); }
    func();
  }
}

function proper_sizer(portal_div, kid_type) {
   portal_div=$(portal_div);
   kid_type=kid_type.toUpperCase();
   var divs = portal_div.getElementsByTagName('div');
   var scroller_div=false;
   for (var i=0 ; i < divs.length ; i++) {
      if (divs[i].className.match(/\bscroller\b/)) {
         scroller_div = divs[i];
         i=9999999;
      }
   }
   var scroller_width=0;
   var first_kid=false;
   for (var child = scroller_div.firstChild ; child ; child = child.nextSibling) {
      if (child.tagName == kid_type) {
         scroller_width += child.offsetWidth;
         if (!first_kid) first_kid=child;
      }
   }
   if (first_kid) {
      scroller_div.style.width = (scroller_width+5) + 'px';
      portal_div.style.height = (first_kid.offsetHeight+20) + 'px';
      portal_div.style.height = 'auto';
   }
}


function fix_odd_even(tbl, grouping) {
  if (!grouping) grouping=1;
  tbl=$(tbl);
  if (tbl) {
    tr_num=0;
    for (var row=0 ; row < tbl.childNodes.length ; row++) {
      if (tbl.childNodes[row].nodeName=='TR') {
        tr=tbl.childNodes[row];
        if (Math.floor(tr_num/grouping)&1==1) {
          // must have "odd"
          if (!tr.className.match(/\bodd\b/)) {
            tr.className += ' odd';
          }
        } else {
          // must not have "odd"
          if (tr.className.match(/\bodd\b/)) {
            tr.className = tr.className.replace(/ ?\bodd\b ?/g, ' ');
          }
        }
        tr_num++;
      }
    }
  }
}

// Simple cookie stuff - get/set/unset a cookie
function get_cookie_value(name) {
  var cookie_str = document.cookie;
  if (!cookie_str || !cookie_str.length) return null;
  var raw_cookies = cookie_str.split(/\s*;\s*/);
  for (var i=0 ; i<raw_cookies.length ; i++) {
    var pieces = raw_cookies[i].split('=');
    if (decodeURIComponent(pieces[0]) == name) {
      return decodeURIComponent(pieces[1]);
    }
  }
  return null;
}

function set_cookie_value(name, value, seconds) {
  var max_age='';
  if (seconds!==undefined) { max_age = "; max-age="+seconds; }
  document.cookie = encodeURIComponent(name)+'='+encodeURIComponent(value)+"; path=/"+max_age;
}

function unset_cookie_value(name) {
  set_cookie_value(name, '', 0);
}

function show_login_popup(track_id, track_version_id, action) {
  $('popup-login').show();
  if (track_id && $('last_track_id')) $('last_track_id').value=track_id;
  if (track_version_id && $('last_track_version_id')) $('last_track_version_id').value=track_version_id;
  if (action && $('last_action')) $('last_action').value=action;
  new Draggable('popup-login', {handle:'drag-handle'});
}

function restyle_keywords(field, value) {
  if (field && value && value!='Your own keyword, lyric or search term') {
    field.style.color = '#000';
  }
}

// window stuff
function remember_position(item) {
 set_cookie_value(item.id+'_position', item.style.left+';'+item.style.top+';'+(item.style.display!='none' ? 'true' : 'false'));
 //window.status=(item.id+'_position', item.style.left+';'+item.style.top+';'+(item.style.display!='none' ? 'true' : 'false'));
}

// project stuff
var current_project_id=false;

function add_to_project(tvid) {
 if (current_project_id) {
   new Ajax.Request('/project/add_track_version_popup/'+current_project_id+'/'+tvid, {asynchronous:true, evalScripts:true})
 } else {
   // have to choose one
   new Ajax.Request('/project/choose/'+tvid, {asynchronous:true, evalScripts:true})
 }
}

function choose_another_project(id) {
 if (id) {
   new Ajax.Request('/project/show_popup/'+id, {asynchronous:true, evalScripts:true})
 }
}

function drop_project(draggable_item,event) {
 remember_position(draggable_item.element);
 return true;
}

function close_project_window(item_id) {
 Element.hide(item_id);
 remember_position($(item_id));
 current_project_id=false;
 new Ajax.Request('/project/forget_default', {asynchronous:true, evalScripts:true})
}

var isIE=(navigator.appName.indexOf('Microsoft')!=-1);
var ie_version=0;
if (isIE) {
	var p=navigator.userAgent.match(/MSIE (\d+\.\d+)/);
	if (p) {
		ie_version=parseFloat(p[1]);
	}
}
