/*
 * gei.digital site setup - http://www.edumeres.net
 *
 * jQuery functions and execution for general site setup
 *
 * Copyright (C) 2010 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) {
            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 
            $(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...
//
////////////////////////////////////////////////////////////////////////////////
$(document).ready(function () {
    
    
    // Setup of Son of Suckerfish Dropdown Menu
    //--------------------------------------------------------------------------
    $('.hlist').sfHover();
    
    // toggle default values in forms
    //--------------------------------------------------------------------------
    $('.has-default-value').toggleDefaultValue();
    
    // make PDF titles clickable
    //--------------------------------------------------------------------------
    $('.section a.pdf[href!=#]').each(function(){
        $(this).next('.pdf-title').wrap('<a href="' + $(this).attr('href') + '" class="bigger" />');
    });
    
    
    // simulierte #col3 in echte #col3 umwandeln
    //--------------------------------------------------------------------------
    $('#col3').appendTo('#main').addClass('realCol3');
    
    // add collection filter button
    //--------------------------------------------------------------------------
    /*var searchterm = $('#search input#formquery').val();
    var searchform = $('#search');
    var collection = $('.collection h2');
    if( searchterm && (collection.length > 0) ) {
        var dest = 'cms/start/buchsuche/?tx_goobit3_search[order]=BYTITLE&tx_goobit3_search[link]=0';
        var button = $('<a href="#" class="col-filter"><img title="In allen Sammlungen suchen" alt="[X]" src="fileadmin/digitale_bibliothek/templates/images/close.png" /></a>');
        button.click(function(){
            searchform.attr('action',dest).submit();
            return false;
        });
        $('.collection').append(button);
    }*/
    
    // enhance search pagination
    //--------------------------------------------------------------------------
    var searchpagination = $('.search-pagination');
    if( searchpagination.length > 0 ) {
        var pages = searchpagination.find('.goto-page');
        var urls = [];
        
        // we need to cut and paste url to prevent it from being altered by the following regex
        pages.find('a').each(function(i){
            urls[i] = $(this).attr('href');
            $(this).attr('href',false);
        });
        
        // do the magic
        pages.find('a').addClass('goto-page');
        pages.html(pages.html().replace(/([0-9]+)/ig, '<span>$1</span>'));
        
        
        // paste old url
        pages.find('a').each(function(i){
            $(this).attr('href',urls[i]);

        });
    }
});
