



function DiCYT (init)
{
  this.http = null;

  if (typeof init != 'undefined')
  {
    for (var property in init)
      this [property] = init [property];
  }  // if

  return;
}  // function [DiCYT]














DiCYT.search = function (text, handler, hints)
{ 
  try
  {
    // Firefox, Opera 8.0+, Safari
    this.http = new XMLHttpRequest ();
  }  // try
  catch (exception)
  {
    // Internet Explorer
    try
    {
      this.http = new ActiveXObject ("Msxml2.XMLHTTP");
    }
    catch (exception)
    {
      try
      {
        this.http = new ActiveXObject ("Microsoft.XMLHTTP");
      }
      catch (e)
      {
        alert ("Your browser does not support AJAX!");
        return false;
      }  // catch
    }  // catch
  }  // catch
  
  httpRequest = this.http;
  this.http.onreadystatechange = function ()
  { 
    if (httpRequest.readyState == 4)
    {
      result = DiCYT.searched (httpRequest.responseXML);
      if (result != null)
      {
      	result.info.code   = httpRequest.status;
      	result.info.status = httpRequest.statusText;
      }  // if

      DiCYT.found (result, handler);
    }  // if
  }  // function [onreadystatechange]
  
  var url = '/redaccion/searchNews.php?searchText=' + text;
  if (typeof hints == 'object')
    {
    for (hint in hints)
      url += '&' + hint + '=' + hints [hint];
    }  // if

  this.http.open ('POST', url, true);
  this.http.send (null);

  return true;
}  // function [search]
  


                    
                    
                    
                    
                    
                    

DiCYT.searched = function (xml)
{               
  if (typeof xml != 'object')
  {       
    alert ('Error al realizar la consulta');
    return {result: null, info: {status: 'error', total: 0, rows: 0}};
  }  // if
  
  try
  {  
    var results  = new Array ();
    var className;
    var root     = xml.documentElement;
    var nodes    = root.getElementsByTagName ('result');
    var current;
  
    for (var i = 0; i < nodes.length; i++)
    { 
    	current = nodes [i];
    	
    	className = current.getAttribute ('class');
    	/*
    	constructor = top [className].prototype.constructor;
    	if (typeof constructor == 'undefined')
    	  continue;
    	
    	result = constructor.call ();
    	*/  
    	result = new News ();
    	
    	result.fromXML (current);
      results.push (result);
    }  // for
    
    
    info   = {   status: 'ok', 
              searching: root.getAttribute ('searching'), 
                   page: Number (root.getAttribute ('page')), 
                  pages: Number (root.getAttribute ('pages')), 
                  total: Number (root.getAttribute ('total')), 
                   rows: Number (root.getAttribute ('rows')) };
    result = {  results: results, 
                   info: info };
  }  // try
  catch (exception)
  {          
    alert (exception);
  }  // catch
  
  return result;
}  // function [searched]








DiCYT.found = function (result, handler)
{                              
	if (typeof handler != 'undefined')
	{
	  ok = handler (result.results, result.info);
	}  // if

  return ok;
}  // function [found]







DiCYT.fronts = function (element)
{
	//alert ('fronts!!!! [' + element.innerHTML + '][');
  var fronts = new Array;
  if ((element == null) || (typeof element != 'object'))
    return fronts;

  var elements = getChildrenByClass (element, 'front');
  
  //alert (' found ' + elements.length);
  if (elements == null)
    return fronts;
    
  for (element in elements)
  {
    front = new Front ();
    front.droppable (elements [element]);
  
    fronts.push (front);
  }  // fronts
    
  
  
  return fronts;    
}  // function [fronts]






function News (init)
{               
  this.newsId        = -1;
  this.languageId    = '';
  this.summary       = null;
  this.body          = null;
  this.contents      = null;
  this.items         = null;

  if (typeof init != 'undefined')
  {
    for (var property in init)
      this [property] = init [property];
  }  // if
  
  return this;
}  // function [News]














News.prototype.toFront = function (front)
{     
  var issued;

  if ((issued = getChildByClass (front, 'issued')) != null)
  {
    if (this.issued)
      issued.innerHTML = this.issued;
    else
      issued.innerHTML = issued.innerHTML + '!!!';
  }  // if
  
  return;
}  // function [toFront]











News.prototype.addContent = function (content)
{
	if (content.type == 'SM')
	  this.summary = content;
	else if (content.type == 'BD')
	  this.body = content;
	  
	this.contents [content.contentId] = content;
	this.contents.length++;

	content.news = this;
	
	return;
}  // function [addContent]






News.prototype.addItem = function (item)
{
	this.items [item.itemId] = item;
	this.items.length++;

	item.news = this;
	
	return;
}  // function [addItem]








News.prototype.fromXML = function (element)
{     
	var node, item; 
	
	if (element.tagName != 'news')
	  element = element.firstChild;
	  
	
	for (var i = 0; i < element.childNodes.length; i++)
	{ 
		node = element.childNodes [i];
		
		if (node.tagName == 'contents')
		{  
			this.contents = {length: 0};

			for (j = 0; j < node.childNodes.length; j++)
			{ 
				content = new Content ();                   
		    content.fromXML (node.childNodes [j]);
				
				this.addContent (content);
			}  // for
    }  // if
    else if (node.tagName == 'items')
		{  
			this.items = {length: 0};

			for (j = 0; j < node.childNodes.length; j++)
			{ 
				item = new Item ();                   
		    item.fromXML (node.childNodes [j]);
				
				this.addItem (item);
			}  // for
    }  // if
		else
  	  this [node.tagName] = node.text; 
	}  // for
	
  return;
}  // function [fromXML]













function Content (init)
{               
  this.newsId        = -1;
  this.contentId     = -1;
  this.languageId    = '';
  this.headline      = '';
  this.superheadline = '';
  this.subheadline   = '';

  if (typeof init != 'undefined')
  {
    for (var property in init)
      this [property] = init [property];
  }  // if
  
  return this;
}  // function [Content]









Content.prototype.fromXML = function (element)
{     
	var node, item; 
	
	if (element.tagName != 'content')
	  element = element.firstChild;
	  
	for (var i = 0; i < element.childNodes.length; i++)
	{ 
		node = element.childNodes [i];
 	  this [node.tagName] = node.text; 
	}  // for
	
  return;
}  // function [fromXML]








Content.prototype.toFront = function (front)
{     
  var headline;
  var superheadline;
  var subheadline;
  var content;
  
  if (typeof front == 'string')
  {
    front = document.getElementById (front);
    if (front == null)
      return;
  }  // if
  
  
  if ((headline = getChildByClass (front, 'headline')) != null)
    {
    if (this.headline)
      headline.innerHTML = '<a href="' + this.news.url + '">' + this.headline + '</a>';
    else
      headline.innerHTML = headline.innerHTML + '!!!';
    }  // if
    	

  if ((superheadline = getChildByClass (front, 'superheadline')) != null)
  {
    if (this.superheadline)
      superheadline.innerHTML = this.superheadline;
    else
      superheadline.innerHTML = superheadline.innerHTML + '!!!';
  }  // if

  if ((subheadline = getChildByClass (front, 'subheadline')) != null)
  {
    if (this.subheadline)
      subheadline.innerHTML = this.subheadline;
    else
      subheadline.innerHTML = subheadline.innerHTML + '!!!';
  }  // if

  if ((content = getChildByClass (front, 'content')) != null)
  {
    if (this.body)
      content.innerHTML = this.body;
  }  // if
    
  if (this.news != null)
    this.news.toFront (front);

  return;
}  // function [toFront]









          

Content.prototype.drag = function (event)
{
  top.dropobject = this;

  return false;
}  // function [drag]









Content.prototype.dragged = function (event)
{                       
  return;
}  // function [dragged]









Content.prototype.draggable = function (element)
{                       
  var source = this;
  
  element.ondragstart = function ()
	{ 
		source.drag (event);
	}  // function [ondragstart]
	  
	element.onselectstart = function ()
	{
		return false;
	}  // function [onselectstart]
	
	  
  return;
}  // function [draggable]




Content.droppable = function (element)
{                                                  
  if (element == null)
    return;
    
  element.ondrop = function ()
  { 
    this.ownerDocument.parentWindow.event.cancelBubble = true;
    FCKeditorAPI.GetInstance ('body').EditorWindow.parent.FCKUndo.SaveUndoStep ();
    top.dropobject.toFront (this);    
    top.dropobject = null;
    return;
  }   // function [ondrop]


  element.ondragenter = function ()
  { 
    this.ownerDocument.parentWindow.event.returnValue = false;
    return;
  }   // function [ondragenter]
  
  element.ondragleave = function ()
  { 
    return;
  }   // function [ondragenter]

  element.ondragover = function ()
  { 
    this.ownerDocument.parentWindow.event.returnValue  = false;
    return;
  }   // function [ondragover]

  return;
}  // function [droppable]













function Item (init)
{               
  this.itemId        = -1;
  this.contentId     = -1;

  if (typeof init != 'undefined')
  {
    for (var property in init)
      this [property] = init [property];
  }  // if
  
  return this;
}  // function [Item]




Item.prototype.fromXML = function (element)
{     
	var node; 
	
	if (element.tagName != 'item')
	  element = element.firstChild;
	  
	for (var i = 0; i < element.childNodes.length; i++)
	{ 
		node = element.childNodes [i];
		this [node.tagName] = node.text; 
	}  // for
	
  return;
}  // function [fromXML]








Item.prototype.drag = function (event, mode)
{
  top.dropobject = this;
  this.mode = mode;

  return false;
}  // function [drag]









Item.prototype.dragged = function (event)
{                       
  return;
}  // function [dragged]









Item.prototype.draggable = function (element, mode)
{                       
  var source = this;
  
  element.ondragstart = function ()
	{ 
		source.drag (event, mode);
	}  // function [ondragstart]
	  
	element.onselectstart = function ()
	{
		return false;
	}  // function [onselectstart]
	  
  return;
}  // function [draggable]









Item.droppable = function (element)
{                                                  
  if (element == null)
    return;
       
  element.ondrop = function ()
  { 
    this.ownerDocument.parentWindow.event.cancelBubble = true;
    FCKeditorAPI.GetInstance ('body').EditorWindow.parent.FCKUndo.SaveUndoStep ();
    
    top.dropobject.toFront (this);    
    top.dropobject = null;

    return;
  }   // function [ondrop]


  element.ondragenter = function ()
  { 
    this.ownerDocument.parentWindow.event.returnValue = false;
    return;
  }   // function [ondragenter]
  
  element.ondragleave = function ()
  { 
    return;
  }   // function [ondragenter]

  element.ondragover = function ()
  { 
    this.ownerDocument.parentWindow.event.returnValue  = false;
    return;
  }   // function [ondragover]

  return;
}  // function [droppable]







Item.prototype.toFront = function (front)
{     
  if (front.tagName != 'IMG')
    return;

  front.removeAttribute ('width');
  front.removeAttribute ('height');
  
  if ((typeof this.mode != 'undefined') && (this.mode != null))
    mode = this.mode;  
  else
  	mode = 'preview';
  
  front.src = this [mode];
  front.setAttribute ('title',  this.caption);
  front.setAttribute ('alt',    this.caption);
  if (front.getAttribute ('_fcksavedurl') != null)
    front.setAttribute ('_fcksavedurl', this [mode]) ;
  
  var parent = front.parentNode, caption = null;
  if (parent != null)
  {
    caption = getChildByClass (parent, 'caption');
    if (caption != null)
      caption.innerHTML = this.caption;
  }  // if
  
  
  return;
}  // function [toFront]












function Front (init)
{             
  this.element       = null;
  this.news          = null;

  if (typeof init != 'undefined')
  {
    for (var property in init)
      this [property] = init [property];
  }  // if

  return this;
}  // function [Front]







Front.prototype.droppable = function (element)
{                                                  
  if ((element == null) || (typeof element != 'object'))
    return;
  
  var elements = getChildrenByClass (element, 'news');
  if (elements != null)
  {
    for (var i in elements)
    {        
      Content.droppable (elements [i]);
    }  // for
  }  // if
  
	var items = getChildrenByClass (element, 'item');

	if (items != null)
	{
    for (var i in items)
    {        
      Item.droppable (items [i]);
    }  // for
  }  // if

	  
  return;
}  // function [droppable]















function getChildByClass (element, className)
  {
  var temp = element;
  
  try
  {
    for (var temp = element.firstChild; temp != null; temp = temp.nextSibling)
    { 
    	if (!temp.getAttribute)
    	  continue;

      if (temp.getAttribute ('className') != null)
      { 
        cN = temp.getAttribute ('className');
        if (cN.indexOf (' ') == -1)
        {       
          if (cN == className)
            return temp;
        }  // if 
        else
        {
          classes = cN.split (' ');
          for (var i = 0; i < classes.length; i++)
          {
            if (classes [i] == className)
            {
              return temp;
            }  // if
          }  // for
        }  // else
      }  // if
      
      if (temp.hasChildNodes ())
      {       
        var result = getChildByClass (temp, className);

        if (result != null)
          return result;
      }  // if
      
    }  // for
  }  // try
  catch (exception)
  {          
    return null;
  }  // catch
  
  return null;
  }  // function [getChildByClass]











function getChildrenByClass (element, className)
  {
  var temp     = element;
  var children = new Array ();
  var element;
  
  try
  {
    if (!temp.getAttribute)
      return children;

    if (element.getAttribute ('className') != null)
    { 
      cN = element.getAttribute ('className');
      if (cN.indexOf (' ') == -1)
      {       
        if (cN == className)
          children.push (element);
      }  // if 
      else
      {
        classes = cN.split (' ');
        for (var i = 0; i < classes.length; i++)
        {
          if (classes [i] == className)
          {
            children.push (element);
          }  // if
        }  // for
      }  // else
    }  // if

    if (element.hasChildNodes ())
    {       
      for (var temp = element.firstChild; temp != null; temp = temp.nextSibling)
      {
        var result = getChildrenByClass (temp, className);

        if ((result != null) && (result.length > 0))
          children = children.concat (result);
      }  // for
    }  // if
  }  // try
  catch (exception)
  {          
    return null;
  }  // catch
  
  return children;
  }  // function [getChildrenByClass]









