/*
 * Main VHD-Site-Setup
 *
 * author:  Christoph Schuessler (cs@hirnstrom.de)
 *
 */


////////////////////////////////////////////////////////////////////////////////
//
// firebug console output
// @param text String the debug message
// @param type String the message type [error | info | warn] (optional)
//
////////////////////////////////////////////////////////////////////////////////
function debug(text,type) {
    if (window.console && window.console.log) {
        if(type === 'error' && window.console.error) {
            window.console.error(text);
        }
        else if(type === 'info' && window.console.info) {
            window.console.info(text);
        }
        else if(type === 'warn' && window.console.warn) {
            window.console.warn(text);
        }
        else {
            window.console.log(text);
        }
    }
}


////////////////////////////////////////////////////////////////////////////////
//
// teaser stage animation
//
// @param arr Array the image links
// @param speed Int the transistion speed
// @param interval Int the interval between transitions 
//
////////////////////////////////////////////////////////////////////////////////
jQuery.fn.slideshow = function(arr,speed,interval) { 
        
    $(this).each(function(){
        
        // vars
        var el = $(this);
        var len = arr.length;
        var c = 0; // current index
        var n = 1; // new index (start at 1 because first image gets set seperately)
        
        //set first image to stage
        el.attr('src', visuals[0]); 
        
        // once all elements have loaded
        $(window).bind('load', function() {
        
            // periodically call stage visuals function
            window.setInterval(
                function () {
                
        
                    // get current array index from storage
                    if( $.data(el, 'counter') !== undefined) {
                        c = $.data(el, 'counter');
                        
                        // limit to array length
                        if($.data(el, 'counter') < (len - 1)) {
                            n = $.data(el, 'counter') + 1;
                        }
                        else {
                            
                            n = 0;
                        }
                    }
                    
                    // update counter storage
                    $.data(el, 'counter', n); 
                       
                    // display new visual with fade-to-new-image transition
                    var temp = $('<img id="slideshow-overlay" alt="" />');
                    el.after(temp);
                    temp.attr('src', visuals[(c)]);
                    el.attr('src', visuals[n]);

                    //once image has loaded
                    el.unbind(); // fix memory leak
                    el.bind('load', function() {
                        
                        //fade overlay
                        temp.fadeTo(speed, 0, function(){
                            temp.remove();
                        });
                    });
                },
            interval);
        });
    });
};


////////////////////////////////////////////////////////////////////////////////
//
// son of suckerfish dropdown (original code see:
// http://htmldog.com/articles/suckerfish/dropdowns/)
//
////////////////////////////////////////////////////////////////////////////////
jQuery.fn.sfHover = function() {
    
    // the list to enhance
    var list = $(this);
    
    list.each(function(i){
        
        //add classes for ie on hover
        $(this).children('li').hover(
            function() {
                $(this).addClass('hover');
            },
            function() {
                list.find('li').removeClass('hover');
            }
        );

    });
};


////////////////////////////////////////////////////////////////////////////////
//
// 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'));
                }
            });
        }
    });
};

////////////////////////////////////////////////////////////////////////////////
//
// domready
//
////////////////////////////////////////////////////////////////////////////////
$(function() {


    // Setup of Son of Suckerfish Dropdown Menu
    //--------------------------------------------------------------------------
    $('#nav').children('ul').sfHover();
    
    // Setup of font resizer
    //--------------------------------------------------------------------------
    $('.fontResizer a').click(function(e){ e.preventDefault(); });
    fontResizer('15px','16px','17px');
    

    // Slideshow
    //--------------------------------------------------------------------------  
    var stage = $('#slideshow');
    if(stage.length > 0) {
        stage.slideshow(visuals,speed,interval);
    }
    
    // capitals
    //--------------------------------------------------------------------------
    var text = $('#content p:first:not(.noinitial)').html();
    if (text) {
        $('#fp #content p:first:not(.noinitial)').html(text.replace(/^([A-Za-z0-9])/g,'<span class="caps"><img src="fileadmin/_vhd/templates/images/abc-w/$1.png" alt="$1" /></span>'));
        $('#cp #content p:first:not(.noinitial)').html(text.replace(/^([A-Za-z0-9])/g,'<span class="caps"><img src="fileadmin/_vhd/templates/images/abc-g/$1.png" alt="$1" /></span>'));
    }
    
    // toggle default values in forms
    //--------------------------------------------------------------------------
    $('.has-default-value').toggleDefaultValue();
    
    // table zebra striping
    //--------------------------------------------------------------------------
    $('table.zebra tr:odd').addClass('odd');

    
});

