/*
 * edumeres.net site setup - http://www.edumeres.net
 *
 * jQuery functions and execution for general site setup
 *
 * Copyright (C) 2008, 2009 Christoph Schuessler (schreib@herr-schuessler.de)
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 */


////////////////////////////////////////////////////////////////////////////////
//
// debug console output
//
// @param {String} text The debug message (uses %s %f %d/%i format)
// @param {String} type can be error / info / warn or empty
//
////////////////////////////////////////////////////////////////////////////////
function debug(text,type) {
    if (window.console && window.console.log) {
        
        // display error message
        if(type === 'error' && window.console.error) {
            window.console.error(text);
        }
        
        // display info message
        else if(type === 'info' && window.console.info) {
            window.console.info(text);
        }
        
        // display warning message
        else if(type === 'warn' && window.console.warn) {
            window.console.warn(text);
        }
        
        // display log message
        else {
            window.console.log(text);
        }
    }
}


////////////////////////////////////////////////////////////////////////////////
//
// son of suckerfish dropdown (original code see:
// http://htmldog.com/articles/suckerfish/dropdowns/)
// and special menu underline style
//
////////////////////////////////////////////////////////////////////////////////
jQuery.fn.sfHover = function() {
    
    // the list to enhance
    var list = $(this);
    
    list.each(function(i){
        
        //add classes for ie on hover
        $(this).find('li').hover(
            function() {
                list.find('li').removeClass('sfhover');
                $(this).addClass('sfhover');
            },
            function() {
                list.find('li').removeClass('sfhover');
            }
        );
    });
    
    // calculate which element to underline
    // and apply class accordingly
    var items = list.children('ul').children('li');
    for(var i=0;i<items.length;i++) {
        if(items.eq(i).hasClass('active')) {
            $('#nav').addClass('nav' + (i + 1));
        }
    }
};


////////////////////////////////////////////////////////////////////////////////
//
// toggle default field value
//
////////////////////////////////////////////////////////////////////////////////
jQuery.fn.toggleDefaultValue = function() {
    $(this).each(function(i){
        
        // the default value
        var val = $(this).val();
        if(val) {
            
            // data storage
            $(this).data('default',val);
            
            // prevent value caching in ff (not xhtml-compliant!) 
            $(this).attr('autocomplete','off');
            
            // add focus and blur behaviour
            $(this).focus(function(){
                if($(this).val() === $(this).data('default')) {
                    $(this).val('');
                }
            });
            $(this).blur(function(){
                if($(this).val() === '') {
                    $(this).val($(this).data('default'));
                }
            });
        }
    });
};


////////////////////////////////////////////////////////////////////////////////
//
// display help tooltip
//
// @param {int} offset the left offset of tooltip
//
////////////////////////////////////////////////////////////////////////////////
jQuery.fn.helpTooltip = function(offset) {
    
    var offset = offset ? offset : 0;
    
    $(this).each(function(i){
        
        // the default value
        var val = $(this).attr('title');

        if(val) {
        
            // suppress browser tooltip
            $(this).removeAttr('title');
            
            // data storage
            $(this).data('tip',val);
            
            var tip = $('<div class="help-tooltip">' + val + '</div>');
            
            // add hover behaviour
            $(this).hover(
                function(){
                    
                    // append tooltip to body
                    $('body').append(tip);
                    
                    // calculate positions of tooltip based on position
                    // of help icon
                    var top  = $(this).offset().top;
                    var left = $(this).offset().left - tip.width() - $(this).width() + offset;
                    tip.css({
                        'top' :         top,
                        'left' :        left,
                        'position' :    'absolute',
                        'z-index' :     500
                    });
                },
                function(){
                    tip.remove();
                }
            );
        }
    });
};



////////////////////////////////////////////////////////////////////////////////
//
// domready...
//
////////////////////////////////////////////////////////////////////////////////
$(document).ready(function () {
    
    
    // Setup of Son of Suckerfish Dropdown Menu
    //--------------------------------------------------------------------------
    $('.hlist').sfHover();
    
    
    // Setup of jQuery UI Tabs for Glossar navigation
    //--------------------------------------------------------------------------
    // the number of tabs equals the index of the "A-Z" tab if tabs have class .tabs_index
    // therefore the "A_Z" tab starts as standard-tab
    // if no .tabs_index is present, -1 is returned so as to start the tabs closed
    var tab_index = $("#tabs.tabs_index").find('.button_abc li').length -1;
    $("#tabs").tabs({selected: tab_index, collapsible: false});
    
    
    // toggle default values in forms
    //--------------------------------------------------------------------------
    $('.has-default-value').toggleDefaultValue();
    
    
    // Setup of help toolstips
    //--------------------------------------------------------------------------
    $('.help').helpTooltip(2);
    
    // Meta-Data dl animation
    //--------------------------------------------------------------------------
    $('dl.meta-data').hide();
    $('.meta-data-trigger').click(function(){
        $(this).next('dl.meta-data').slideToggle('fast');
        $(this).toggleClass('open');
    });


});