/* Merged Plone Javascript file
 * This file is dynamically assembled from separate parts.
 * Some of these parts have 3rd party licenses or copyright information attached
 * Such information is valid for that section,
 * not for the entire composite file
 * originating files are separated by ----- filename.js -----
 */

/* ----- formsubmithelpers.js ----- */
function inputSubmitOnClick(event) {
    if (!event) var event = window.event; // IE compatibility

    if (hasClassName(this, 'submitting')) {
        return confirm(window.form_resubmit_message);
    } else {
        addClassName(this, 'submitting');
    }
    return true;
}

function registerSubmitHandler() {
    var nodes = cssQuery('input[type=submit]');
    for (var i=0; i<nodes.length; i++) {
        var node = nodes[i];
        if (!node.onclick) {
            node.onclick = inputSubmitOnClick;
        }
    }
}
registerPloneFunction(registerSubmitHandler);


/* ----- emoticons.js ----- */
function quote(blockquote, uid, title) {
    var blockquote;
    var blog_form = document.forms['edit_form'];
    var reply_to = blog_form['reply_to'];
    var current_content = blog_form['text'];
    var title_ob = blog_form['title'];
    var new_content = current_content.value + blockquote;

    current_content.value = new_content;
    current_content.focus();
    reply_to.value = uid;
    title_ob.value = title
    return false;
}

function emoticon(ecode, form_name) {
  // Inserts emoticon codes
  frm = document.forms[form_name];
  frm.text.value = frm.text.value + ecode;
  frm.text.focus();
  return false;
}

function wizard(wiz_type, form_name) {
  var content = document.forms[form_name].text
  var new_content;
  
  if (wiz_type == "email") {
    var tmp_image = prompt('Please enter Email:', '');
    if (tmp_image) {
      var pseudo_html = '[email]' + tmp_image + '[/email]';
      new_content = content.value + pseudo_html;
      content.value = new_content;
    }
    content.focus();
    return false;
  }
  if (wiz_type == "image") {
    var tmp_image = prompt('Please enter the URL where your image is located:', 'http://');
    if (tmp_image) {
      var pseudo_html = '[img]' + tmp_image + '[/img]';
      new_content = content.value + pseudo_html;
      content.value = new_content;
    }
    content.focus();
    return false;
  }
  if (wiz_type == 'link') {
    var tmp_href = prompt('Please enter the URL for your page:', 'http://');
    var tmp_title = prompt('Please enter the title for the link You just provided:', tmp_href)
    if (tmp_href && tmp_title) {
      pseudo_html = '[url href="' + tmp_href + '"]' + tmp_title + '[/url]';
      new_content = content.value + pseudo_html;
      content.value = new_content;
    }
    content.focus();
    return false;
  }
  if (wiz_type == "italic") {
    var tmp_word = prompt('Please enter a word or sentence:', '');
    if (tmp_word) {
      var pseudo_html = '[em]' + tmp_word + '[/em]';
      new_content = content.value + pseudo_html;
      content.value = new_content;
    }
    content.focus();
    return false;
  }
  if (wiz_type == "bold") {
    var tmp_word = prompt('Please enter a word or sentence:', '');
    if (tmp_word) {
      var pseudo_html = '[strong]' + tmp_word + '[/strong]';
      new_content = content.value + pseudo_html;
      content.value = new_content;
    }
    content.focus();
    return false;
  }
  if (wiz_type == "size") {
    var tmp_word = prompt('Please enter a word or sentence:', '');
    if (tmp_word) {
      var pseudo_html = '[size]' + tmp_word + '[/size]';
      new_content = content.value + pseudo_html;
      content.value = new_content;
    }
    content.focus();
    return false;
  }
  if (wiz_type == "big") {
    var tmp_word = prompt('Please enter a word or sentence:', '');
    if (tmp_word) {
      var pseudo_html = '[size]' + tmp_word + '[/size]';
      new_content = content.value + pseudo_html;
      content.value = new_content;
    }
    content.focus();
    return false;
  }
  if (wiz_type == "code") {
    var tmp_word = prompt('Please enter a word or sentence:', '');
    if (tmp_word) {
      var pseudo_html = '[code]' + tmp_word + '[/code]';
      new_content = content.value + pseudo_html;
      content.value = new_content;
    }
    content.focus();
    return false;
  }
}

function showWizards() {
    try{
	document.getElementById('wizards').style.display = 'block';
    } catch(e) {}
    try{
	document.getElementById('iconsWizard').style.display = 'block';
    } catch(e) {}
    document.getElementById('showWL').style.display = 'none';
    document.getElementById('hideWL').style.display = 'inline';
}

function hideWizards() {
    try{
	document.getElementById('wizards').style.display = 'none';
    } catch(e) {}
    try{
	document.getElementById('iconsWizard').style.display = 'none';
    } catch(e) {}
    document.getElementById('showWL').style.display = 'inline';
    document.getElementById('hideWL').style.display = 'none';
}


/* ----- cmfboard_menu.js ----- */
// Code to determine the browser and version.

function CBrowser() 
{
    var ua, s, i;
    
    this.isIE = false; // Internet Explorer
    this.isNS = false; // Netscape
    this.isOP = false; // Opera
    this.version = null;
    
    ua = navigator.userAgent;
    
    s = "Opera";
    if ((i = ua.indexOf(s)) >= 0) {
        this.isOP = true;
        this.version = 7;
        return;
    }
    
    s = "MSIE";
    if ((i = ua.indexOf(s)) >= 0) {
        this.isIE = true;
        this.version = parseFloat(ua.substr(i + s.length));
        return;
    }  
    s = "Netscape6/";
    if ((i = ua.indexOf(s)) >= 0) {
        this.isNS = true;
        this.version = parseFloat(ua.substr(i + s.length));
        return;
    }
    
    // Treat any other "Gecko" browser as NS 6.1.
    
    s = "Gecko";
    if ((i = ua.indexOf(s)) >= 0) {
        this.isNS = true;
        this.version = 6.1;
        return;
    }
}

CBrowser = new CBrowser();

function CEvent()
{

    this.init = init;
    this.initEvent = initEvent;
    this.removeListener = removeListener;
    this.eventTypes = new Array("abort", "blur", "change", "click", "dblclick", 
                                "error", "focus", "keydown", "keypress", "keyup",
                                "load", "mousedown", "mousemove", "mouseout",
                                "mouseover", "mouseup", "reset", "submit", "unload")
                               
    function init(obj)
    {
        // Add member vars
        obj.handledEvents = new Array(); 
                
        // Add methods
        obj.addListener = addListener;
        obj.removeListenerFromId = removeListenerFromId;
        obj.removeListenersFromType = removeListenersFromType;
        obj.removeAllListeners = removeAllListeners;
        
        // Get defined events
        for (var i in this.eventTypes)
        {
            var event_type = this.eventTypes[i];
            var full_event = "on" + event_type;
            var event_func = obj[full_event];
            
            if (event_func)
            {
                obj.addListener(event_type, event_type, event_func);
            }
        }
    }
    
    function initEvent(event)
    {
        var mEvent = event;
        
        if (CBrowser.isIE)
        {
            mEvent = window.event;
            mEvent.layerX = mEvent.offsetX;
            mEvent.layerY = mEvent.offsetY;
            mEvent.currentTarget = mEvent.srcElement;
        }
        
        return mEvent;
    }
    
    function removeListener(obj, event_type)
    {
        var full_event = "on" + event_type;
        
        if (CBrowser.isIE)
        {
            obj[full_event] = null;
        }
        else
        {
            delete(obj[full_event]);
        }
    }
    
    
    function evalEvent(event)
    {   
        event = CEvent.initEvent(event);
        var event_type = event.type;
        
        for (var event_id in this.handledEvents)
        {
            if (this.handledEvents[event_id]["type"] == event_type)
            {
              this.handledEvents[event_id]["func"](event);
            }
        }
    }
    
    // CEvent methods
    function addListener(event_id, event_type, event_func)
    {       
            var event = new Array();
            var full_event = "on" + event_type;
            
            this[full_event] = evalEvent;
            
            event["type"] = event_type;
            event["func"] = event_func;
            this.handledEvents[event_id] = event;
    }
    
    function removeListenerFromId(event_id)
    {
        var event_type = this.handledEvents[event_id]["type"];
        var bExists = 0;
        
        // Delete event
        delete(this.handledEvents[event_id]);
        
        // Search if exists an event with the same type
        for (var event_id in this.handledEvents)
        {
            if (this.handledEvents[event_id]["type"] == event_type)
            {
                bExists = 1;
            }
        }
        
        // If not delete listener
        if (!bExists)
        {
            CEvent.removeListener(this, event_type);
        }
    }
    
    function removeListenersFromType(event_type)
    {
        var full_event = "on" + event_type;
        
        // Delete events from specified type
        for (var event_id in this.handledEvents)
        {
            if (this.handledEvents[event_id]["type"] == event_type)
            {
                delete(this.handledEvents[event_id]);
            }
        }
        
        // Delete listener
        if (this[full_event])
        {
            CEvent.removeListener(this, event_type);
        }
    }
    
    function removeAllListeners()
    {
        // Delete all events
        for (var event_id in this.handledEvents)
        {
            delete(this.handledEvents[event_id]);
        }
        
        // Delete all listeners
        for (var i in CEvent.eventTypes)
        {
            var event_type = CEvent.eventTypes[i];
            
            CEvent.removeListener(this, event_type);
        }
    }
}

CEvent = new CEvent();

// CMFBoard code
var cmfboard_browser = CBrowser;

// Code for handling the menu bar and active button.
var cmfboard_activeButton = null;
var cmfboard_eventInit = 0;

    
function cmfboard_initEvent()
{   
    if (!cmfboard_eventInit)
    {
        CEvent.init(document);    
        document.addListener("cmfboard_mousedown", "mousedown", cmfboard_pageMousedown);
        cmfboard_eventInit = 1;
    }
}

function cmfboard_pageMousedown(event) 
{    
    var el;
    
    // If there is no active button, exit.
    if (cmfboard_activeButton == null)
    {
        return;
    }
    
    // Find the element that was clicked on.
    if (CBrowser.isIE)
    {
        el = window.event.srcElement;
    }
    else
    {
        el = (event.target.tagName ? event.target : event.target.parentNode);
    }
    
    // If the active button was clicked on, exit.
    if (el == cmfboard_activeButton)
    {
        return;
    }
    
    // If the element is not part of a menu, reset and clear the active
    // button.
    if (cmfboard_getContainerWith(el, "UL", "menuAction") == null) 
    {
        cmfboard_resetButton(cmfboard_activeButton);
        cmfboard_activeButton = null;
    }
}

function cmfboard_buttonClick(event, menuId) 
{    
    var button;
    
    cmfboard_initEvent();
    
    event = CEvent.initEvent(event);
    
    // Get the target button element.
    button = event.currentTarget;
    
    // Blur focus from the link to remove that annoying outline.
    button.blur();
    
    // Associate the named menu to this button if not already done.
    // Additionally, initialize menu display.
    if (button) 
    {
        button.menu = document.getElementById(menuId);
        button.menu.style.display = "block";
        
        /*dropdown_menu = cmfboard_getContainerWith(button.menu, 'LI', 'dropdownMenu');
        dropdown_menu.style.padding = "0 0 30em 0";*/
    }
    
    // Reset the currently active button, if any.
    if (cmfboard_activeButton != null)
    {
        cmfboard_resetButton(cmfboard_activeButton);
    }
    
    // Activate this button, unless it was the currently active one.
    if (button != cmfboard_activeButton) 
    {
        cmfboard_depressButton(button);
        cmfboard_activeButton = button;
    }
    else
    {
        cmfboard_activeButton = null;
    }
    
    return false;
}

function cmfboard_buttonMouseover(event, menuId) 
{
    var button;
    
    event = CEvent.initEvent(event);
    
    // Get the target button element.
    button = event.currentTarget;

    // If any other button menu is active, make this one active instead.
    if (cmfboard_activeButton != null && cmfboard_activeButton != button)
    {
        cmfboard_buttonClick(event, menuId);
    }
}

function cmfboard_depressButton(button) 
{
    var x, y;
    
    // Update the button's style class to make it look like it's
    // depressed.
    button.className += " menuButtonActive";
    
    // Make the associated drop down menu visible
    display = button.menu.style.display;
    button.menu.style.display = (display == "none" || display == '') ? "none" : "block";
}

function cmfboard_resetButton(button) 
{
    // Restore the button's style class.   
    cmfboard_removeClassName(button, "menuButtonActive");
    
    // Hide the button's menu, first closing any sub menus.
    if (button.menu != null) 
    {
        cmfboard_closeSubMenu(button.menu);
        button.menu.style.display = "none";
    }
}

function cmfboard_closeSubMenu(menu) 
{
    if (menu == null || menu.activeItem == null)
        return;
    
    // Recursively close any sub menus.
    if (menu.activeItem.subMenu != null) 
    {
        cmfboard_closeSubMenu(menu.activeItem.subMenu);
        menu.activeItem.subMenu.style.display = "none";
        menu.activeItem.subMenu = null;
    }
    
    cmfboard_removeClassName(menu.activeItem, "menuItemHighlight");
    menu.activeItem = null;
}


// General utility functions.
function cmfboard_getContainerWith(node, tagName, className) 
{
    // Starting with the given node, find the nearest containing element
    // with the specified tag name and style class.    
    while (node != null) 
    {
        if (node.tagName != null && node.tagName == tagName && cmfboard_hasClassName(node, className))
        {
            return node;
        }
        
        node = node.parentNode;
    }
    
    return node;
}

  
function cmfboard_hasClassName(el, name) 
{    
    var i, list;
    
    // Return true if the given element currently has the given class
    // name.   
    list = el.className.split(" ");
    for (i = 0; i < list.length; i++)
        if (list[i] == name)
        {
            return true;
        }
        
    return false;
}

function cmfboard_removeClassName(el, name) 
{    
    var i, curList, newList;
    
    if (el.className == null)
    {
        return;
    }
    
    // Remove the given class name from the element's className property.
    newList = new Array();
    curList = el.className.split(" ");
    for (i = 0; i < curList.length; i++)
    {
        if (curList[i] != name)
        {
            newList.push(curList[i]);
            el.className = newList.join(" ");
        }
    }
}

function cmfboard_getPageOffsetLeft(el) 
{
    var x;
    
    // Return the x coordinate of an element relative to the page.    
    x = el.offsetLeft;
    if (el.offsetParent != null)
        x += cmfboard_getPageOffsetLeft(el.offsetParent);
    
    return x;
}

function cmfboard_getPageOffsetTop(el) 
{    
    var y;
    
    // Return the x coordinate of an element relative to the page.   
    y = el.offsetTop;
    if (el.offsetParent != null)
    {
        y += cmfboard_getPageOffsetTop(el.offsetParent);
    }
    
    return y;
}


