document.observe('dom:loaded', function() {
    treeTraverseIterative(document.getElementsByTagName("body").item(0));


/*
  $$('.wc').each(function(element) {

    new Tip(element, {
        title : 'WordCode Editor'
        , ajax: {
            url: site_path+site_gallery_path+'/iFrame?wc='+element.innerHTML+'&contest=1&settings_path='+'/content/'
            , options: {
                method: 'get' 
                , onComplete: function(transport) {
                    $$('form').each(function(item) {
                        item.action = site_path+'/admin/international/modify?ret_url='+location.href;
                    });
                    // you could do something here after the ajax call is finished
                }
            }
        }
        , hideOn: { element: 'closeButton', event: 'click' }
        , showOn: 'click'
        , width: 'auto' // We don't want the default 250px.
                       // Images inside the tooltip will need to have dimensions set since Prototip needs to fixate width for proper rendering.
        , hook: { target: 'bottomMiddle', tip: 'topMiddle' }
        , stem: 'topMiddle'
        , offset: { x: 7, y: 18 }
    });  
    element.style.color = '#FF0000';
    element.style.border = '1px doth #FFFFFF';
    element.style.cursor = 'pointer';
    element.innerHTML = "Edit: "+element.innerHTML;
  
  
  

    
  

  
  }); */
});





function treeTraverseIterative(elem) {
    var queue = new Array();
    var i, currentElem, childs;
    var count = 0;

    queue.push(elem);
    while (queue.length > 0) {
        count++;
        currentElem = queue.pop();
        visit(currentElem, count);
        childs = currentElem.childNodes.length;
        for (i = 0; i < childs; i++) {
            queue.push(currentElem.childNodes[i]);
        }
    }
}

function visit(elem, count) {
    if(((elem.nodeName == "#text" && elem.length > 0) || elem.nodeName == "INPUT") && elem.parentNode.nodeName != "SCRIPT") {
        var fatherElem = "";
        var elemData = "";
        var elemDataPos = -1;
        var icoType = "";
        var newEl = "";
        var newData = "";
        var code = "";
        
        if(elem.nodeName == "#text") {
            elemData = elem.data;
        } else {
            elemData = elem.value;
        }

        if(elemData.search('#wc#') >= 0) {
            code = elemData.substr(elemData.search('#wc#') + 4, elemData.indexOf('#wc#', elemData.search('#wc#') + 4) - elemData.search('#wc#') - 4);
            elemDataPos = elemData.search('#wc#');
            if(code == "") {
                icoType = 'wci';
                newData = elemData.replace('#wc##wc#', '');
                code = newData;
            } else {
                icoType = 'wcu';
                newData = elemData.replace('#wc#'+code+'#wc#', '');
            }
        }
        
        if(elemDataPos >= 0)  { 
            switch(elem.parentNode.nodeName) {    
                case "OPTION":
                    fatherElem = elem.parentNode.parentNode.parentNode;
                    break;
                case "A":
                    fatherElem = elem.parentNode.parentNode;
                    break;
                default:
                    fatherElem = elem.parentNode;
            }
            newEl = document.createElement('img')
            newEl.setAttribute('src', site_path+'/themes/'+theme_inset+'/images/'+icoType+'.png');
            newEl.setAttribute('alt','Edit WordCode');
            fatherElem.appendChild(newEl);
           
            newEl.style.cursor = 'pointer';

            if(elem.nodeName == "#text") {
                elem.data = newData;
            } else {
                elem.value = newData;
            }
            /*if(elem.nodeName == "#text") {
                newData = elem.data.replace('#wci# ','');
                elem.data = newData;
            } else {
                newData = elem.value.replace('#wci# ','');
                elem.value = newData;
            } */

            new Tip(newEl, {
                title : 'WordCode Editor'
                , ajax: {
                    url: site_path+'/admin/international/modify?ret_url='+location.href+'&wc='+code+'&full_size=1&unic='+count
                    , options: {
                        method: 'get' 
                        , onComplete: function(transport) {
                            $$('form').each(function(item) {
                                if(item.name == "frmInternational"+count) {
                                    item.action = site_path+'/admin/international/modify?ret_url='+location.href+'&wc='+code+'&unic='+count;
                                }
                            });
                            // you could do something here after the ajax call is finished
                        }
                    }
                }
                , hideOn: { element: 'closeButton', event: 'click' }
                , showOn: 'click'
                , width: 'auto' // We don't want the default 250px.
                               // Images inside the tooltip will need to have dimensions set since Prototip needs to fixate width for proper rendering.
                , hook: { target: 'bottomMiddle', tip: 'topMiddle' }
                , stem: 'topMiddle'
                , offset: { x: 7, y: 18 }
            });
            
        }
        
        
        /*
        if( elemDataPos >= 0 
            && elem.parentNode.nodeName != "SCRIPT" 
            && elem.parentNode.nodeName != "OPTION" 
            && elem.parentNode.nodeName != "TEXTAREA" 
            && elem.parentNode.nodeName != "INPUT"
        ) {
            element = elem.parentNode;

            if(element.nodeName == "A") {
                new Tip(element, {
                    title : 'WordCode Editor'
                    , ajax: {
                        url: site_path+'/admin/international/modify?ret_url='+location.href+'&wc='+elem.data.replace('#wc# ','')+'&full_size=1'
                        , options: {
                            method: 'get' 
                            , onComplete: function(transport) {
                                $$('form').each(function(item) {
                                    item.action = site_path+'/admin/international/modify?ret_url='+location.href;
                                });
                                // you could do something here after the ajax call is finished
                            }
                        }
                    }
                    , delay: 1
                    , hideOn: { element: 'closeButton', event: 'click' }
                    //, showOn: 'click'
                    , width: 'auto' // We don't want the default 250px.
                                   // Images inside the tooltip will need to have dimensions set since Prototip needs to fixate width for proper rendering.
                    , hook: { target: 'bottomMiddle', tip: 'topMiddle' }
                    , stem: 'topMiddle'
                    , offset: { x: 7, y: 18 }
                });
            } else {
                new Tip(element, {
                    title : 'WordCode Editor'
                    , ajax: {
                        url: site_path+'/admin/international/modify?ret_url='+location.href+'&wc='+elem.data.replace('#wc# ','')+'&full_size=1'
                        , options: {
                            method: 'get' 
                            , onComplete: function(transport) {
                                $$('form').each(function(item) {
                                    item.action = site_path+'/admin/international/modify?ret_url='+location.href;
                                });
                                // you could do something here after the ajax call is finished
                            }
                        }
                    }
                    , hideOn: { element: 'closeButton', event: 'click' }
                    , showOn: 'click'
                    , width: 'auto' // We don't want the default 250px.
                                   // Images inside the tooltip will need to have dimensions set since Prototip needs to fixate width for proper rendering.
                    , hook: { target: 'bottomMiddle', tip: 'topMiddle' }
                    , stem: 'topMiddle'
                    , offset: { x: 7, y: 18 }
                });
            }
            element.style.color = '#FF0000';
            element.style.cursor = 'pointer';
            //alert(element.nodeName+' '+element.innerHTML+' '+elem.data.replace('#wc#','ciao'));
            elem.data = elem.data.replace('#wc#','Edit: ');
        } */
    }    
    // visits the element
}
