function debug(aMsg)
    {
	   alert("um");
      setTimeout(function() { throw new Error("[debug] " + aMsg); }, 0);
    }
// BEGIN BREAK OUT OF FRAMES

function bustFrames(){
   if (window != top){
      top.location.href=location.href
   }
}

// END BREAK OUT OF FRAMES


// BEGIN FIELD-CLEARING FUNCTION

function clearField(field){
   if (field.value == field.defaultValue) {
      field.value = "";
   }
}

// END FIELD-CLEARING FUNCTION

// DHTML FUNCTIONS
function initSmartNav(){
   var pathparts = location.pathname.split("/");
   // Deal with special case of 'friendly urls' (using rewrite) that begin with /character/)
   if (pathparts[1] == 'character'){
     pathparts[1] = 'characters';
   }
   for(i=0;i<pathparts.length;i++){
      if (pathparts[i].length > 0){
         if (pathparts[i].indexOf('.') != -1){
            nav = pathparts[i].substring(0, pathparts[i].lastIndexOf('.'));
         }
         else{
            nav = pathparts[i];
         }
//           alert ("test: "+nav);
         if (document.getElementById(nav+'_button')){
            navbutton = document.getElementById(nav+'_button');
            navbutton.className='highlighted';
            src = navbutton.src;
            ftype = src.substring(src.lastIndexOf('.'), src.length);
            if (src.indexOf('_on') == -1){
               newsrc = src.replace(ftype, '_on'+ftype);
               navbutton.src=newsrc;
            }
         }
      }
   }
}

function initRollovers(){
   var imgs,i;
   // Loop through all images, check if they contain the class roll
   imgs=document.getElementsByTagName('img');
   for(i=0;i<imgs.length;i++){
      img_obj = imgs[i];
      if(/roll/.test(imgs[i].className)){
         // add the function roll to the parent Element (i.e a href) of the image
         img_obj.parentNode.onmouseover=function(){rollOver(this, 'over');};
         img_obj.parentNode.onmouseout=function(){rollOver(this, '');};
         img_obj.parentNode.onfocus=function(){rollOver(this, 'over');};
         img_obj.parentNode.onblur=function(){rollOver(this, '');};
         // Preload images (on and over variations)
         img_width = img_obj.width;
         img_height = img_obj.height;
         img_name = img_obj.id;
         img_ext = img_obj.src.substring(img_obj.src.lastIndexOf('.'), img_obj.src.length);
         img_name_over = img_name.replace("_button", "_over");
         eval (img_name_over + " = new Image(" + img_width + "," + img_height + ")" );
         eval (img_name_over + ".src = \"" + img_obj.src.replace(img_ext, "_over" + img_ext) + "\"");
/*          img_name_on = img_name.replace("_button", "_on"); */
/*          eval (img_name_on + " = new Image(" + img_width + "," + img_height + ")" ); */
/*          eval (img_name_on + ".src = \"" + img_obj.src.replace(img_ext, "_on" + img_ext) + "\""); */
      }
   }
}

function rollOver(obj, rollstate){
   var i,isnode,src,ftype,newsrc,nownode;
   // loop through all childNodes (i.e. img within a)
   for (i=0;i<obj.childNodes.length;i++){
      nownode=obj.childNodes[i];
      // if the node is an element and an IMG set the variable and exit the loop
      if(nownode.nodeType==1 && /img/i.test(nownode.nodeName)){
         isnode=i;
         break;
      }
   }
   // check src and do the rollover
   src = obj.childNodes[isnode].src;
   ftype = src.substring(src.lastIndexOf('.'), src.length);
   if(src.lastIndexOf('_') > src.lastIndexOf('/')){
      currstate = src.substring(src.lastIndexOf('_'), src.lastIndexOf('.'));
   }
   else{
      currstate = '';
   }
   newsrc = '';
   if (rollstate == 'over'){
      if (currstate != '_over'){
         newsrc = src.replace(ftype, '_over'+ftype);
      }
   }
   if (rollstate.length == 0){
      if (currstate == '_over'){
         newsrc = src.replace(currstate, '');
      }
   }
   if (newsrc.length != 0){
      obj.childNodes[isnode].src=newsrc;
   }
}

// WINDOW FUNCTIONS
function loadPopup(url, name, width, height, chrome, scrollbars, resizable, centered) {
   // For chrome, scrollbars, resizable, centered use 1 or 0
   if (centered == 1){
      var winleft = Math.round((screen.width-width)/2);
      var wintop = Math.round((screen.height-height)/2);
      if (chrome == 1){
         wintop = wintop - 80;
      }
      else{
         wintop = wintop - 30;
      }
      if (wintop < 0) wintop = 0;
   }
   var features = '';
   features = features + 'width=' + width;
   features = features + ',height=' + height
   if (chrome == 1){
      features = features + ',directories=1,location=1,menubar=1,scrollbars=1,status=1,toolbar=1';
   }
   else{
      features = features + ',directories=0,location=0,menubar=0,scrollbars=0,status=0,toolbar=0';
   }
   features = features + ',resizable=' + (resizable - 0);
   if (winleft > 0 && wintop > 0){
      features = features + ',left=' + (winleft - 0);
      features = features + ',top=' + (wintop - 0);
   }
   //alert("Features: " + screen.width + "/" + screen.height + " -- " +features);
   popup = window.open (url, name, features);
   popup.focus();
   return void(0);
}

// To close a window use... window.close();
// To print use... window.print();
// To redirect use... window.location = "http://www.google.com/";


// Begin Show and Hide Layers

function showLayer (layer_id) {
   document.getElementById(layer_id).style.display='block';
   }
function hideLayer (layer_id) {
   document.getElementById(layer_id).style.display='none';
}

// End Show and Hide layers

// Characters AJAX Function

function showCharacter(sCharacterName)
{
    new Ajax.Request("/ajax/character_div.php?character="+sCharacterName,{
        method: "get",
        onSuccess: function(t)
        {
            // t is the object assembled when it receives data from the server,
            // and to get the text, do the following
            $("more_characters_description").innerHTML = t.responseText;
            // assumes responseText is formatted HTML as string
        }
    });
}
