
function getFileExtension(filename) {
    var parts = filename.split('.');
    return parts[parts.length-1];
}

// init menu onmouseover swap
function initMenu() {
    $$('.menu-button').each(function(elem) { initMenuMouseOver(elem); });   
}

function initMenuMouseOver(elem) {
    
    img_1 = elem.down();
    
    if(img_1.src.indexOf('_on') == -1) {
        
        var extension = getFileExtension(img_1.src);
        
        var img_2 = new Element('img');
        img_2.addClassName('png');
        img_2.setStyle({display: 'none', position: 'absolute', top: '0px' }); 
        img_2.src = img_1.src.replace('.' + extension, '_on.'+extension);
        
        img_1.up().insert(img_2);
        
        elem.effect_in  = false;
        elem.effect_out = false;
        
        Event.observe(elem, 'mouseover', swapMenuImgs);
        Event.observe(elem, 'mouseout', restoreMenuImgs);
    }
}

function swapMenuImgs(event) {
    
    var father = event.target.up();
    var img_1  = father.childElements()[0];
    var img_2  = father.childElements()[1];
    
    if(Prototype.Browser.Gecko) {
        if(father.effect_out) {
            father.effect_out.cancel();
            father.effect_out = false;
        }
        
        if(!father.effect_in)
            father.effect_in = new Effect.Parallel([
                                new Effect.Fade(img_1, { sync: true, from: img_1.getOpacity() }),
                                new Effect.Appear(img_2, { sync: true, from: img_2.getOpacity() })
                                ], { duration: 0.5, afterFinish: function() { father.effect_in = false; } });
    }
    else {
        
        if(img_1.visible()) {
            img_1.hide();
            img_2.show();
        }
        
    }
}

function restoreMenuImgs(event) {
    
    var father = event.target.up();
    var img_1  = father.childElements()[0];
    var img_2  = father.childElements()[1];
    
    if(Prototype.Browser.Gecko) {
        if(father.effect_in) {
            father.effect_in.cancel();
            father.effect_in = false;
        }
        
        if(!father.effect_out)
            father.effect_out = new Effect.Parallel([
                new Effect.Appear(img_1, { sync: true, from: img_1.getOpacity() }),
                                 new Effect.Fade(img_2, { sync: true, from: img_2.getOpacity() })
                                 ], { duration: 0.5, afterFinish: function() { father.effect_out = false; } });
    }
    else {
        
        if(!img_1.visible()) {
            img_1.show();
            img_2.hide();
        }   
    }    
}

var ajax_history   = new Array();
var current_anchor = -1;
var watch_hash_int = 0;

/* Ajax content load */
function initAjaxLoad() {
    
    $$('.ajaxload').each(function(elem) {
            elem.observe('click', function(event) { loadPage(event.target); event.stop(); });
    });
    
    watch_hash_int = setInterval(watchHash, 50);
}

function watchHash() {
    
    if(current_anchor > -1) {
        last_hash = ajax_history[current_anchor].href.split('/');
        last_hash = '#/'+last_hash[last_hash.length-2];
        
        if(window.location.hash != last_hash) {
           
            if(window.location.hash.length > 0)
                $$('.ajaxload').each(function(elem) {
                                        hash = elem.href.split('/');
                                        hash = '#/'+hash[hash.length-2];
                                        
                                        console.log(hash);
                                        
                                        if(hash == window.location.hash)
                                            loadPage(elem);
                });
            else {
                window.location = window.location;
                clearInterval(watch_hash_int);
            }
            
        }
    }
}

function loadPage(elem) {
    is_loading = 1;
    
    elem.addClassName('bold');
    
    $$('.ajaxload').each(function(e) {
                            if(e.rel == elem.rel && e != elem)
                                e.removeClassName('bold');
    });

    var regex = new RegExp("ajaxload\\[(.*)\\]");
    
    matches       = elem.rel.match(regex);
    var container = matches[1];
    
    $(container).hide();
    
    ajax_request = new Ajax.Request(elem.href+'?ajax', {
                        method: 'get',
                        encoding: 'iso8859-1',
                        onSuccess: function(transport) {
                                      $(container).update(transport.responseText);
                                      $(container).show();
                                      initToggableList();
                                      lightwin = new lightwindow();
                                      /* lightwindowInit(); */
                            }});
    
    hash = elem.href.split('/');
    window.location.hash = '/'+hash[hash.length-2];
    
    ajax_history.push(elem);
    current_anchor = ajax_history.length-1; 
}

function stopEvent(obj, evt) {
    Event.observe(obj, evt, function(e) { e.stop(); });
}

function initToggableList() {
    
    $$('.modal-list-item').each(function(elem) {
                                    elem.observe('click', function() { elem.addClassName('lightblue'); });
                                }); 
    
    $$('.close-modal').each(function(elem) {
                            elem.observe('click', function() { $(elem.rel).modal.close();});     
    });
}

/* Formacao */
function toggleCourseText(event) {
    var btn = this;
    
    var fade_out, fade_in = null;
    
    if(btn.rel.substr(0, 8) != 'lightbox')
        $$('.course-text').each(function(elem) {
                                        if(elem.id != btn.rel && elem.visible())
                                            fade_out = elem;
                                        else if(elem.id == btn.rel)
                                            fade_in = elem;
        });
    
    $$('.course-btn').each(function(elem) { 
                            if(elem == btn) 
                                elem.addClassName('active');
                            else
                                elem.removeClassName('active');
    });
    
    if(fade_out != null && fade_in != null)
        fade_out.fade({duration: 0.5, afterFinish: function() { fade_in.appear({ duration: 0.5 }); }});
    
    event.stop();
}

function toggleBlindDown(event) {
   elem = event.target;
   
   if($$(elem.rel)[0].visible())
       new Effect.BlindUp($$(elem.rel)[0]);
   else {
    
       $$('.blinddown').each(function(e) {
                                
                                if(e != elem)
                                    new Effect.BlindUp($$(e.rel)[0]);
       });
       
       new Effect.BlindDown($$(elem.rel)[0]);
       
   }
   
   event.stop();
}

function initBlindDown() {

    $$('.blinddown').each(function(elem) {
                            elem.observe('click', toggleBlindDown);
                            
                            if(elem.select('.more').length == 1)
                                elem.select('.more')[0].observe('click', function(event) {
                                                                e = new Object();
                                                                e.stop = function() { };
                                                                
                                                                e.target = event.target.up();
                                                                toggleBlindDown(e);
                                                                
                                                                event.stop();
                                                                });
                                
                          });
}

function toggleCourseText() {
    var btn = this;
    
    var fade_out, fade_in = null;
    
    if(btn.rel.substr(0, 8) != 'lightbox')
        $$('.course-text').each(function(elem) {
                                        if(elem.id != btn.rel && elem.visible())
                                            fade_out = elem;
                                        else if(elem.id == btn.rel)
                                            fade_in = elem;
        });
    
    $$('.course-btn').each(function(elem) { 
                            if(elem == btn) 
                                elem.addClassName('active');
                            else
                                elem.removeClassName('active');
    });
    
    if(fade_out != null && fade_in != null)
        fade_out.fade({duration: 0.5, afterFinish: function() { fade_in.appear({ duration: 0.5 }); }});
}

function initCourseToggle() {
    
    $$('.course-btn').each(function(elem) {
                            Event.observe(elem, 'click', toggleCourseText);
    });
}

// check for article uri
function checkArticleURI() {
    
    // check for right location
    _uri_parts = window.location.href.split('/');
    
    uri_parts = new Array();
    
    // clean empty parts
    for(i = 0; i < _uri_parts.length; i++)
        if(_uri_parts[i].length > 0)
            uri_parts.push(_uri_parts[i]);
    
    if(uri_parts.length > 3 && uri_parts[3] == 'artigos') {
        
        if(window.location.hash != null && window.location.hash.length > 0) {
            art_id = window.location.hash.split('/')[0];
            lightwin.activate({}, $$(art_id+'_a')[0]);
        }
    }
}

var lightwin = null;

function init() {
    
    lightwin = new lightwindow();
    
    if(_ajax_load)
        initAjaxLoad();
    
    initToggableList();
    initBlindDown();
    initCourseToggle();
    
    checkArticleURI();
}

Event.observe(window, 'load', init);
