/* $Id: dspace.js 138 2008-03-17 10:47:22Z christofh $ */


/* jQuery dropdown menus
 * 
 * HTML::
 *   
 *   <div class="dropdown-example">
 *     <h2>handle</h2>
 *     <ol>
 *       <li><a href="#1">1</a></li>
 *       <li><a href="#2">2</a></li>
 *     </ol>
 *   </div>
 *   
 * JS::
 * 
 *   $('.dropdown-example').dropdownMenu({menu: 'ol', left: '50px', top: 'auto'})
 *      
 * options
 * -------
 * menu = 'ul'
 *    CSS selector for the menu which should be shown
 * left = '10px'
 *    left offset for dropdown
 * top = '0'
 *    top offset for dropdown, default puts menu above the handle
 * opacity = '0.9'
 *    opacity for menu
 * hover = ''
 *    class which is added to container during hovering
 * 
 */
jQuery.fn.dropdownMenu = function(options) {
	var options = options || {}
	var menusel = options.menu || 'ul'
    var left = options.left || '10px'
    var top = options.top || '0'
    var bottom = options.bottom || 'auto'
    var opacity = options.opacity || 0.9
    var hover = options.hover || 'over'
    var do_opacity = !($.browser.msie && $.browser.version < 7 )

    return this.each(function(i) {	
        var holder = jQuery(this);
        var menu = jQuery(menusel, holder)

        holder.css('zIndex', 100-i)
		      .css('position', 'relative')
			  .css('cursor', 'pointer')
		menu.css('position', 'absolute')
            .css('top', top)
            .css('bottom', bottom)
            .css('left', left)
		
        holder.hover(function(){
		   if (do_opacity) menu.css('opacity', opacity)
           menu.show()
		   holder.addClass(hover)  
        }, function(){
           menu.fadeOut('fast')
           holder.removeClass(hover)  
        })
    })
}

var dspace = {
	// URL where autocompletion request is send to
	AUTOCOMPLETE_URL: '/suggest',
	
	// drop shadow configutation
    shadowconf: {left: 1, top: 1, blur: 1, color: '#C7E2ED'},
    tabshadowconf: {left: 0, top: 0, blur: 1, color: '#777'},
    homeshadowconf: {left: 1, top: 1, blur: 1, color: '#aaa'},
	
    // called onload
	init: function() {

        // init topmenu
        $('.topnav>ul>li').each(function() {
            var li = $(this)
            var e = $('a, b', li)
            var menu = $('ul', li)
			var do_opacity = !($.browser.msie && $.browser.version < 7 )
            li.hover(
                function() {
					e.addClass('high')
                    if (do_opacity) menu.css('opacity', 0.9)
                    menu.fadeIn('fast')
                },
                function() {
                    menu.fadeOut('fast')
                    e.removeClass('high')
                }
            )
        })
		
        // init dropdownMenus
        $('div.browse').dropdownMenu({left: '0', top: '2em', opacity: 0.95, hover: 'over'})
		if ($.browser.msie && $.browser.version < 7) {
            $('.splash div.browse').dropdownMenu({
                left: '-10px',
                top: '-114px',
                opacity: 1,
                hover: 'over'
            })          
		}
		else {
            $('.splash div.browse').dropdownMenu({
                left: '0',
                top: 'auto',
                bottom: '35px',
                opacity: 1,
                hover: 'over'
            })
		}											 
        
		// init tabs (only one tabset per page currently)
		$('div.tab').wrapAll('<div class="tabset"></div>')
		$('div.tabset').each(function() {
            var tabset = $(this)
            
			// add tabs from h1
            tabheaders = $('h1', tabset)
            var tabs = ['<ol class="tabs inline clearfix">']
            for (var i = 0; i < tabheaders.length; i++) {
				var tab = $(tabheaders[i])
				var id = tab.attr('id') || "tab-" + i
				tab.hide()
                tabs.push('<li id="'+id+'">' + tab.html() + '</li>')
            }
            tabs.push('</ol>')
            tabset.prepend(tabs.join(''))
			
            // add highlight to open tab
			$('ol.tabs li:eq(0)', tabset).addClass('first')
			                             .addClass('tab-highlight')
			                             .dropShadow(dspace.tabshadowconf) 
			// close all except 1st
			$('div.tab:gt(0)', tabset).hide()
			
			// init tab clicks
            $('ol.tabs li', tabset).click(function() {
                if (!$(this).hasClass('tab-highlight')) {
                    var tab = $(this)
					
					// remove old highlight
                    $('.dropShadow', tab.parent()).remove()
                    $('li', tab.parent()).removeClass('tab-highlight')
					
					// set new highlight
                    tab.addClass('tab-highlight')
                       .dropShadow(dspace.tabshadowconf)
					
					// toggle tab content   
					$('div.tab:visible', tabset).hide()
					$('div.tab:eq(' + tab.prevAll('li').size() + ')', tabset).show()
					
					/*
					$('div.tab:visible', tabset).slideUp('fast', function(){
    					$('div.tab:eq(' + tab.prevAll('li').size() + ')', tabset)
						      .slideDown('fast', function(){
						})
					})*/
				}
            })
		})
		// open tab if given in hash: #tab-n with n = [0-tabs.length]
        var hash = document.location.hash
        if (hash) {
            var e
            try {
                e = $(hash)
            }
            catch (ex) {}
            if (e) {
                e.click()
            }
        }		
		
		// init autocomplete
        $('#q').autocomplete(
            dspace.AUTOCOMPLETE_URL, 
			{   delay: 20,
                minChars: 2,
                maxItemsToShow: 10,
				resultsClass: 'ac_results',
				loadingClass: 'ac_loading',
				selectFirst: false,
				width: 200
            }
        );

        $('#qs').hover(
		    function() {
			    this.src = document.search.pfad.value + 'shared/img/images/search_but_' + document.search.language.value + '_hover.jpg'
		    },
			function() {
                this.src = document.search.pfad.value + 'shared/img/images/search_but_' + document.search.language.value + '.jpg'
			}
        )

        // init dropShadows
        $('div.content div.highlight, .main-action').dropShadow(dspace.shadowconf)
		$('.home .tabblock .row .imageteaser').dropShadow(dspace.homeshadowconf)

        // fix Firefox topnav out over search removing topnav menus
        if (!$.browser.msie && !$.browser.opera && !$.browser.safari) {
            $('#q').hover(
                function(){
                    $('.topnav>ul>li>a').removeClass('high')
                    $('.topnav ul ul').hide()
                }, function(){
            })
        }

        // fix IE6 select bug and button hover
        if ($.browser.msie && $.browser.version < 7) {
			// fix button hover
			$('.action').hover(
			    function() { $(this).addClass('action-over') },
				function() { $(this).removeClass('action-over') }
			)
            $('.main-action').hover(
                function() { $(this).addClass('main-action-over') },
                function() { $(this).removeClass('main-action-over') }
            )
			// fix layers
            $('.topnav ul').bgiframe({top: 0, left: 0 });
            $('.browse ul').bgiframe({top: 0, left: 0 });
        }
 
        
        // init click on complete module ?
        /*
        $('div.textlist a.link').each(function() {
            var href = this.href;
            var div = $(this).parent('div.textlist')
            div.css('cursor', 'pointer')
            div.click(function(){
                location.href = href
            })
        })*/        
	}
}

$(dspace.init)


