$(document).ready(function() {              
       // nav
    if($.browser.msie) {
        $('#pageHead div ul.topNav li ul li').css('background','url(/ecomaXL/pics/bg_topNav_sub_ie.png) repeat-y');
        $('#pageHead div ul.topNav li ul').css('background','url(/ecomaXL/pics/bg_topNav_sub_bottom_ie.png) no-repeat left bottom');
    }
    $('ul.topNav li ul').each(function() { $(this).show(0); $(this).hide(0); });
    var navTimer = null;
    $('ul.topNav li').hover(function(e) {
        ae = $(this);
        if(!ae.find('ul').is('animated')) {
            navTimer = window.setTimeout(function() { ae.find('ul').fadeIn('fast') }, 200);
        }
    },function() {
        $(this).find('ul').fadeOut(200);
    }).mouseleave(function() {
        window.clearTimeout(navTimer);
    }); 

    // news switcher
    if($('.newsSwitch').length > 0 && $('.newsSwitch').find('div').length > 1) {
        $('.newsSwitch').find('ul').prepend('<li class="left prev"><a href="#prev" title="'+Translate.zurueck+'">'+Translate.zurueck+'</a></li><li class="left next"><a href="#next" title="'+Translate.weiter+'">'+Translate.weiter+'</a></li>');
    }
    $('.newsSwitch ul li.left').click(function() {
        var ae = $(this);
        var ul = ae.parents('ul:first');
        if(!ul.parent().find('div').is(':animated')) {
            var old = ul.siblings('div:visible');
            if(ae.hasClass('prev')) {
                if(old.prev('div').length > 0) {
                    var act = old.prev('div');
                } else {
                    var act = old.parent().find('div:last');
                }
            }
            if(ae.hasClass('next')) {
                if(old.next('div').length > 0) {
                    var act = old.next('div');
                } else {
                    var act = old.parent().find('div:first');
                }
            }
            ul.find('li.right a').attr('href', act.find('a:first').attr('href'));
            old.fadeOut(0, function() {
                act.fadeIn(0);
            });
        }
        return false;
    });
    // back
    $('input.back, p.back a').click(function() {
        history.back();
        return false;
    });
    // formvalidate
    if($('form.anfrage').length > 0) {
        $('form.anfrage').formvalidate();
    }
    // lightbox
	
    if($('a[rel^="lightbox"]').length > 0) {
        $('a[rel^="lightbox"]').colorbox({ maxWidth: 1000 });
    }
    if($('a[rel^="inlineLightbox"]').length > 0) {
        $('a[rel^="inlineLightbox"]').each(function() {
            var $t = $(this);
            $t.colorbox({ inline:true, href: $t.attr('href') });
        });
	}
	
	if($('a[rel^="iframeLightbox"]').length > 0) {
		$('a[rel^="iframeLightbox"]').colorbox({ iframe:true, innerWidth:910, innerHeight:635 });
    }
	
    // tabs
     $("#tabs").tabs();
    
    $('a.external').attr('target', '_blank');
    
    // styled title-tags
    // todo: als plugin auslagern
    var jqueryTitle = $('<div id="jqueryTitle"></div>');
    var jqueryTitleWait = null;
    $('body').append(jqueryTitle);
    $('!*[title^=""]').hover(function(e) {
        var ae = $(this);
        var title = ae.attr('title');
        ae.attr('title', '');
        ae.data('titleText', title);
        jqueryTitle.html(title);
        jqueryTitlePosition(e.pageX, e.pageY);
        jqueryTitle.css({'opacity': '0.9' });   
        jqueryTitleWait = window.setTimeout(function() {
            jqueryTitle.stop(true, true).fadeIn(100);
        }, 500);
    }, function() {
        var ae = $(this); 
        jqueryTitle.stop(true, true).fadeOut(0);
        var title = ae.data('titleText');
        ae.attr('title', title);
    }).mousemove(function(e) {
        jqueryTitlePosition(e.pageX, e.pageY);
    }).mouseleave(function() {
        window.clearTimeout(jqueryTitleWait);
    });
    $('!*[alt^=""]').each(function() {
        if($.browser.msie) {
            $(this).attr('alt','');
        }
    });
    function jqueryTitlePosition(left, top) {
        var _w = 'auto';
        var _t = top + 15;
        var _l = left + 15;
        if($(window).scrollLeft() + $(window).width() < _l + jqueryTitle.outerWidth()) {
            _l = left - jqueryTitle.width() - 20;
            if(0 > _l) {
                _l = 0;
            }
        }
        if($(window).scrollTop() + $(window).height() < _t + jqueryTitle.outerHeight()) {
            _t = top - jqueryTitle.height() - 20;
        }
        jqueryTitle.css({ 'top':_t, 'left':_l, 'width': _w });
    }
    // !styled title-tags
    
    
    
    // quickfinder
    if($('.call-quickfinder').length > 0) {
        $('.call-quickfinder').click(function(e) {
            if ($.browser.msie && parseInt($.browser.version) < 7) {
                alert('Browser ist zu alt!');
                return false;
            }
            
            var qf = $('#quickfinder');
            var qfC = $('#quickfinderContainer');
            var qfBG = $('#quickfinderBG');
            
            if (qf.length == 0) return false;
            
            if (qf.children().length == 0) {
                qf.finder();
                qf.find('.close').live('click', function() {
                    qfC.stop(true, false).animate({ left: -908 }, 550).removeClass('open');
                    qfBG.stop().fadeTo(550, 0, function() { $(this).hide(0); });
                });
            }
            
            var _l = ($(window).width() / 2) - (qf.width() / 2);
            if (_l < 0) _l = 0;
            
            if (qfC.hasClass('open') && qf.find('.close').length > 0) {
                qf.find('.close').click();
            }
            else {
                qfC.animate({ left: _l }, 550).addClass('open');
                qfBG.stop().fadeTo(550, 0.5);
            }
            e.preventDefault();
            return false;
        });
    }
 // Ie8 Pruefstand Grafik laden
$('div.pruf').css('display','block');

});
