// show or hide a DHTML layer
function showHide(id) {
  if (element = document.getElementById(id)) {
    element.style.display == 'none' ? element.style.display='block' : element.style.display='none';
  }
}

function showLayer(id) {
  if (element = document.getElementById(id)) {
    element.style.display='block';
  }
}

function showLayerInline(id) {
  if (element = document.getElementById(id)) {
    element.style.display='inline';
  }
}

function hideLayer(id) {
  if (element = document.getElementById(id)) {
    element.style.display='none';
  }
}

// check the page mode of this page and if not editing/previewing the currently
// viewed page reload the entire window
function checkMode(page,template,cache,baseurl) {
  
  // rebuild the Action object in case it changed
  action = new Action(baseurl);
  
  if ( page != action.Page() || template != action.Template() ) {
    if ( window.parent != window ) { window.parent.location.replace(cache); }
      else { window.location.replace(cache); }
  }
}

// function to display the login page in a popup window:
function popupLogin(loginurl) {
  var name='popup';

  // Open the pop-up window first
  y=screen.height/2-50;
  x=screen.width/2-100;
  popup=window.open(loginurl,name,"height=100,width=200,screenX="+x+",left="+x+",screenY="+y+",top="+y+",scrollbars=yes");
  popup.focus();
  popup.document.close();

}

// shows the 'reloading' message so users know the current page is reloading (on an element selection)
function cc_Reloading(msg) {
  if ( msg == null ) { msg = "Updating..."; }
  
  // was the window where the message should appear specified?
  var thewindow=window;
  if (( arguments.length == 2 ) && (arguments[1] != "")) { thewindow=eval(arguments[1]); }
  
  var Element = thewindow.document.getElementById("cc_reloading");
  if ( Element != null ) {
    objw = parseFloat(Element.style.width)/2;
    Element.style.top=Math.floor(Math.round(document.body.scrollTop+50))+'px';
    Element.style.left = Math.floor(Math.round((document.documentElement.offsetWidth/2)+document.body.scrollLeft)-objw)+'px';
    Element.innerHTML=msg;
    showLayer('cc_reloading');
  }
}

// *** Functions used to manipulate Action object:
// send the CC_ACTION cookie to the browser
function sendAction(domain,path,reload) {
  if ( arguments.length==3 ) { action.Send(domain,path,reload); }
  else { action.Send(domain,path,reload,arguments[3]); }
}

// set the date of the version of page to view ('[none]' for current date)
function setVersionDate(value) {
  action.VersionDate(value);
}

// set the element to edit
function setElement(value) {
  // we need to change the tabs on the page via DHTML
  if ( action.Element() != "[none]" ) {
    // set the old element's tab indicator to deselected
    var oldelement=action.Element().replace(/\s/g,"_");
    hideLayer(oldelement+"_indicator");
  }

  var newelement=value.replace(/\s/g,"_");
  showLayerInline(newelement+"_indicator");
  action.Element(value);
}

// set the authoring mode
function setMode(value) {
  action.Mode(value);
}

// set the page we are editing
function setPage(value) {
  action.Page(value);
}

// set the scroll value for this page
function setScroll(value) {
  action.Scroll(value);
}

// set the site we are editing
function setSite(value) {
  action.Site(value);
}

// set the tab to display in the control panel
function setTab(value) {
  action.Tab(value);
}

// set the template we are editing
function setTemplate(value) {
  action.Template(value);
}

// set the version of the element being edited
function setVersion(value) {
  var params=value.split(':');
  action.Version(params[0]);
}

// set the width of the control panel frame
function setWidth(value) {
  action.Width(value);
}

// reload control and/or content frames
function reloadFrames(content,control) {
  var contentmsg = "Updating...";
  var controlmsg = "Updating...";

  if ( arguments.length == 4 ) {
    if ( arguments[2] != "" ) { contentmsg = arguments[2]; }
    if ( arguments[3] != "" ) { controlmsg = arguments[3]; }
  }

  if ( window.name == 'content' ) {
    if (control) {
      window.parent.control.cc_Reloading(controlmsg);
      window.parent.control.location.reload();
    }
    if (content) {
      cc_Reloading(contentmsg);
      window.location.reload();
    }
  }

  else {
    if (content) {
      window.parent.content.cc_Reloading(contentmsg);
      window.parent.content.location.reload();
    }

    if (control) {
      cc_Reloading(controlmsg);
      window.location.reload();
    }
  }
}


// *** Action objects are used to determine the user's authoring state and what 
// they might be doing
function Action(path) {
  if ( path == "" ) { path="/"; }

  // These are the action attributes and their defaults:
  this.date='[none]';
  this.element='[none]';
  this.mode='read';
  this.page='0';
  this.scroll='0';
  this.site='0';
  this.tab='page';
  this.template='0';
  this.version='[none]';
  this.width='250:*';

  // These are the action methods:
  this.VersionDate=VersionDate;
  this.Element=Element;
  this.Mode=Mode;
  this.Send=Send;
  this.Page=Page;
  this.Scroll=Scroll;
  this.Site=Site;
  this.Tab=Tab;
  this.Template=Template;
  this.Version=Version;
  this.Width=Width;

  // Get the current Action cookie (CC_ACTION) and set our Action object's 
  // attributes according to what the Action cookie has set
  if ( document.cookie != '' ) {
    var cookies=document.cookie.split('; ');
    for (var i=0; i<cookies.length; i++) {
      if ( cookies[i].substring(0,11+path.length) == 'CC_ACTION_'+path.toUpperCase()+'=' ) {
        var elements=cookies[i].substring(11+path.length).split('&');
        if ( elements.length > 1 ) {
          for (var j=0;j<elements.length;j=j+2) {
            eval("this."+elements[j]+"('"+elements[j+1]+"')");
          }
        }
      }
    }
  }
}

// Date of the page to view
function VersionDate(value) {
  if (value!=null) this.date=value;
  else return this.date;
}

// Element to edit 
function Element(value) {
  if (value!=null) this.element=value;
  else return this.element;
}

// Authoring state: read, edit, or preview
function Mode(modeval) {
  if (modeval!=null) this.mode=modeval;
  else return this.mode;
}

// Amount to scroll the screen down to display the selected element (IE only)
function Scroll(value) {
  if (value!=null) this.scroll=value;
  else return this.scroll;
}

// Set the CC_ACTION cookie in the browser:
function Send(domain,path,reload) {
  // the path can't be blank
  if ( path == '' ) { path='/'; }
  var cookiebase=path;

  var value='Mode&'+this.Mode()+'&Element&'+this.Element()+'&Tab&'+this.Tab()+'&Site&'+this.Site()+'&Page&'+this.Page()+'&Template&'+this.Template()+'&Scroll&'+this.Scroll()+'&Width&'+this.Width()+'&Version&'+this.Version()+'&VersionDate&'+this.VersionDate();
  var cookie='CC_ACTION_'+cookiebase.toUpperCase()+'='+value+'; path='+path+';';
  if ( domain != '' ) {
    cookie=cookie+' domain='+domain;
  }
  document.cookie=cookie;
  if ( reload ) {
    if ( arguments.length>3 ) {
      if ( window.parent != window ) { 
        if ( window.parent.location.pathname == arguments[3]) {
          window.parent.location.reload();
        }
        else {
          window.parent.location.replace(arguments[3]);
        }
      }
      else {
        if ( window.location.pathname == arguments[3]) {
          window.location.reload();
        }
        else {
          window.location.replace(arguments[3]);
        }
      }
    }
    else {
      if ( window.parent != window ) { window.parent.location.reload(); }
        else { window.location.reload(); }
    }
  }
}

// Set the page we are editing:
function Page(value) {
  if (value!=null) this.page=value;
  else return this.page;
}

// Set the site we are editing:
function Site(value) {
  if (value!=null) this.site=value;
  else return this.site;
}

// Set the tab selected in the control panel:
function Tab(value) {
  if (value!=null) this.tab=value;
  else return this.tab;
}

function Template(value) {
  if (value!=null) this.template=value;
  else return this.template;
}

// Set the version of the element to be viewed:
function Version (value) {
  if (value!=null) this.version=value;
  else return this.version;
}

// Set the width of the control panel frame:
function Width(value) {
  if (value!=null) this.width=value;
  else return this.width;
}

function pageMode(actionurl,mode,site,page,template,domain,reload,file,path) {
  var cookiebase=path;
  
  // is the window to refresh specified?
  var where='';
  if ( arguments.length > 9 ) { where=arguments[9]; }
  
  // close this window after changing mode?
  var close='no';
  if ( arguments.length > 10 ) { close=arguments[10]; }

  // the path can not be blank:
  if ( path == '' ) { path='/'; }

  // display the "changing mode" div
  if ( where == "" ) { cc_Reloading("Changing to "+mode+"..."); }

  // need to set the site the user is trying to access:
  if ( site != '' ) {
    setSite(site);
    sendAction(domain,path,false);
  }

  // open the pop-up window
  y=screen.height/2-50;
  x=screen.width/2-100;
  pagemode=window.open("",'pageMode',"height=100,width=200,screenX="+x+",left="+x+",screenY="+y+",top="+y+",scrollbars=yes");

  // set up the form to change page mode:
  pagemode.document.writeln("<html><body onload='document.pagemode.submit()'>");
  pagemode.document.writeln("<div style='text-align: center; font-family: \"Verdana\", \"Arial\", \"Helvetica\", sans-serif; font-size: 12px;'>Changing page mode...</div>");
  pagemode.document.writeln("<form method='POST' action='"+actionurl+"' name='pagemode'>");
  pagemode.document.writeln("<input type='hidden' name='mode' value='"+mode+"'>");
  pagemode.document.writeln("<input type='hidden' name='site' value='"+site+"'>");
  pagemode.document.writeln("<input type='hidden' name='page' value='"+page+"'>");
  pagemode.document.writeln("<input type='hidden' name='template' value='"+template+"'>");
  pagemode.document.writeln("<input type='hidden' name='domain' value='"+domain+"'>");
  pagemode.document.writeln("<input type='hidden' name='reload' value='"+reload+"'>");
  pagemode.document.writeln("<input type='hidden' name='filename' value='"+file+"'>");
  pagemode.document.writeln("<input type='hidden' name='path' value='"+path+"'>");
  pagemode.document.writeln("<input type='hidden' name='cookiebase' value='"+cookiebase+"'>");
  pagemode.document.writeln("<input type='hidden' name='where' value='"+where+"'>");
  pagemode.document.writeln("<input type='hidden' name='close' value='"+close+"'>");
  pagemode.document.writeln("</form>");
  pagemode.document.writeln("</body></html>");
  pagemode.document.close();
}

// cc_randomDiv: selects a random <div> to display given a prefix for the divs'
// id attributes (eg, 'Image1','Image2', etc) and a count of the number of div
// sections to choose from
function cc_randomDiv(idprefix,count) {
  // first hide all of the divs:
  for (i=1;i<=count;i++) {
    hideLayer(idprefix+i);   
  }

  // now choose one to show:
  rndDiv=Math.floor(Math.random()*count)+1;
  showLayer(idprefix+rndDiv);
}

// popupLink: opens a url in a new window with the specified width and height
//   if width or height is blank that parameter is set the the maximum
function popupLink(url,width,height) {
  if ( width == '' ) width = screen.width;
  if ( height == '' ) height = screen.height;

  var x = Math.floor(screen.width/2 - width/2);
  var y = Math.floor(screen.height/2 - height/2);

  popuplink=window.open(url,'',"height="+height+",width="+width+",screenX="+x+",left="+x+",screenY="+y+",top="+y+",scrollbars=yes");
  popuplink.focus();

  return false;
}

function changeStyle(id,styleclass) {
  ident=document.getElementById(id);
  ident.className=styleclass;
}

