/*
  Common.js
*/


/*********************************************************************
 * Add every panel loaded to a global array of panels.
 *********************************************************************/
function addPanel(panel){if(panels.length){panels[panels.length]=panel;}else{panels[0] = panel;}}
function getPanel(id){if(panels.length){for(i=0;i<panels.length;i++ ){if(panels[i].id==id){return panels[i];}}}}

/*********************************************************************
 * Add every panelContentObject loaded to a global array of panelContentObjects.
 * These should contain a referance to the panel it belongs to.
 *********************************************************************/
function addPanelContentObject( panelContent ){
  if( panelContentObjs.length ) {
    panelContentObjs[panelContentObjs.length] = panelContent;	
  } else {
	panelContentObjs[0] = panelContent;
  }
}

function removePanelContentObject( path ){
  if( panelContentObjs.length ) {
    for( i=0; i<panelContentObjs.length; i++ ){
	  if( panelContentObjs[i].contentPath == path ){
		  panelContentObjs.splice(i, 1);
	  }
	}
  } 	
}

function getPanelContentObject( path ){
  if( panelContentObjs.length ) {
    for( i=0; i<panelContentObjs.length; i++ ){
	  if( panelContentObjs[i].contentPath == path ){
		return panelContentObjs[i];  
	  }
	}
  }
  return false;
}

/*********************************************************************
 * Initialize the content for the panel
 *********************************************************************/

function InitContent( panel ) {
  
	
	
  try {
  	// consume the XmlHttpObject to get the xml for the current session
	  panel.consume(new XmlHttpObject(NEB_serverName+"/node"+NEB_elementPath));
	  var contentDom = panel.GetResponseDom().getElementsByTagName("ISElement")[0];
    // find the type and go from there.  
    type = panel.display;
    switch( type ){
	  case "forum":
    // consume a forumManager.
	  addPanel( panel );
		panel.consume( new ForumManager(contentDom));
	  break;
	  case "campreview":
        addPanel(panel);
		loadCamPreview( panel.id );
      break;
    }
  } catch(e){
	  //alert( 'error initting the content ' + e.description )  
  }
}

/*********************************************************************
 * called onclick of the forum node.  expand the panel.
 *********************************************************************/

function expandMe( who ){
  panel = getPanel( who )
  panelParent = getPanel( panel.parentid )
  switch( panelParent.display ){
	  case "forum":
	    panelParent.expandNode( who );
	  break;
  }
}

/*********************************************************************
 * called when the reply link is clicked in the forum
 *********************************************************************/
function showreply( who ){
  document.getElementById(who).style.display='block';	
  document.getElementById( who+'_link' ).innerHTML = '<a href="javascript:savereply(\''+who+'\')">save and close</a>';
} 

/*********************************************************************
 * posts to the server and saves the reply to the forum in the xml
 *********************************************************************/
function savereply( who ){
	  // this needs fixed.
	parent = document.getElementById( who ).parentNode.parentNode.id;
	var title = document.getElementById( who ).getElementsByTagName("input").item(0).value;
	var ownerid = NEB_accessorInfo['uid'];
	var content = document.getElementById( who ).getElementsByTagName("textarea").item(0).innerHTML;
	var postxml = buildForumPostXml( title, ownerid, content );
	
	var postobj = new XmlHttpObject( NEB_serverName+"/postdoc.php"+NEB_elementPath+"/"+ parent + "/"+getTimeStamp() , postxml);		
	
}

function buildForumPostXml(title, ownerid, content){
   	var postxml = '<ContentElement implClassName="isdocument">'+
		          '<property name="content.type" type="string"><![CDATA[text/html]]></property>'+
                  '<property name="author.nickname" type="string"></property>'+
                  '<property name="content.title" type="string"><![CDATA['+title+']]></property>'+
                  '<BLiveSecurity>'+
                  '<ownerId>'+ ownerid +'</ownerId>'+
                  '<rule>update role super_users@piston.blive.net</rule>'+
                  '<rule>allow all</rule>'+
                  '</BLiveSecurity>'+
                  '<content><![CDATA['+content+']]></content>';
                  '</ContentElement>';
	return postxml;
}

function getTimeStamp(){
  var date = new Date();
  var timestamp = date.getFullYear().toString() + date.getMonth().toString() + date.getDay().toString() + date.getHours().toString() + date.getMinutes().toString() + date.getSeconds().toString();
  return timestamp;
}
	
