// firebug console output
//------------------------------------------------------------------------------
function debug(text) {
    if (window.console && window.console.log) {
        window.console.log(text);
    }
}

// switch contact field
//------------------------------------------------------------------------------
function contactSwitch() {
    if($("#f-callback option:selected").val() == 'email') {
        $('#f-email').parent().show();
        $('#f-tel').parent().hide();
        $('#f-fax').parent().hide();
    }
    else if($("#f-callback option:selected").val() == 'telephone') {
        $('#f-tel').parent().show();
        $('#f-email').parent().hide();
        $('#f-fax').parent().hide();
    }
    else if($("#f-callback option:selected").val() == 'fax') {
        $('#f-fax').parent().show();
        $('#f-tel').parent().hide();
        $('#f-email').parent().hide();
    }
    else {
        $('#f-tel').parent().hide();
        $('#f-email').parent().hide();
        $('#f-fax').parent().hide();
    }
}

// stage visuals switch
//------------------------------------------------------------------------------
function stageShow(el,arr,speed) {
    
    // cache array length
    var len = arr.length;
    var c = 0; // current index
    var n = 1; // new index (start at 1 because first image gets set seperately)
    
    // 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="visual-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();
        });
    });
}




////////////////////////////////////////////////////////////////////////////////
//
// -> domready...
//
////////////////////////////////////////////////////////////////////////////////
$(function() {
  
    // Son of Suckerfish jQuery Version
    //--------------------------------------------------------------------------
    $("#nav li").hover(
        function() {
            $(this).addClass('hover');
        },
        function() {
            $(this).removeClass('hover');
        }
    );
    
    // Stage Visuals
    //--------------------------------------------------------------------------  
    var el = $('#visual');
    var interval = 4000; //speed of slideshow loop
    var speed = 3000; //speed of fading transition
    
    //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 () {
                stageShow(el,visuals,speed);
            },
            interval);
    });
    
    // jQuery UI Datepicker
    //--------------------------------------------------------------------------
    $('#f-from').datepicker();
    $('#f-to').datepicker();
    
    // Kontakt-Switch
    //--------------------------------------------------------------------------
    $('#f-email').parent().hide();
    $('#f-tel').parent().hide();
    $('#f-fax').parent().hide();
    $("#f-callback").change(function() {
        contactSwitch();
    });
    
    // load selected contact field on pageload 
    contactSwitch();
    
    
    // antispam
    //--------------------------------------------------------------------------
    
    //cache mailto-links
    var mailto = $("a[href^='mailto:']");
    
    //replace antispam-pattern
    mailto.each( function() {
        
        //get href and text value
        var address =  $(this).attr('href');
        var value =  $(this).text();
        
        //replace {at} and {dot}
        address = address.replace(/{at}/g, "@");
        address = address.replace(/{dot}/g, ".");
        value = value.replace(/{at}/g, "@");
        value = value.replace(/{dot}/g, ".");
        
        //update mailto-links
        $(this).attr('href',address);
        $(this).text(value);
    });
    
    // image gallery
    //--------------------------------------------------------------------------
    
    $('a.thumb img').hover(
        function() {
            $(this).fadeTo('fast',0.6);
        },
        function() {
            $(this).fadeTo('fast',1);
        }
    );
    $("a[rel^='prettyPhoto']").prettyPhoto({
        animationSpeed: 'fast',
        padding: 40,
        opacity: 0.35,
        showTitle: false,
        allowresize: true,
        counter_separator_label: ' von '
    });
    
    
});
