$(document).ready(function(){
	// pause
	$.fn.pause = function(milli,type) {
		milli = milli || 1000;
		type = type || "fx";
		return this.queue(type,function(){
			var self = this;
			setTimeout(function(){
				$.dequeue(self);
			},milli);
		});
	};
	
	$.fn.clearQueue = $.fn.unpause = function(type) {
		return this.each(function(){
			type = type || "fx";
			if(this.queue && this.queue[type]) {
				this.queue[type].length = 0;
			}
		});
	};
		
	// tabs
	$('#tabs').tabs({
		select: function() {
			$('.tooltip').hide(0);
		}
	});
	// tooltips
	
	
	var toolTipContainer = $('<div class="tooltip"><div class="top"><div class="close"></div></div><div class="main"></div><div class="bottom"></div></div>');
	$('body').append(toolTipContainer)
	toolTipContainer.find('.close').click(function(){toolTipContainer.hide(0)})
	var toolTimer = null;
	$('.toolTipC').mouseover(function(e){
			var _l = e.pageX + 40 /*$(this).position().left + 40*/;
			var _t = e.pageY - 40 /*$(this).position().top + -40*/;
			toolTipContainer.css({
				left: _l,
				top: _t
			}) 
			toolTipContainer.find('.main').html($(this).siblings('.toolTipContent').html());
			if($(this).hasClass('sticky')) 
				toolTipContainer.find('.close').show(0);
			else
				toolTipContainer.find('.close').hide(0);
			if($(this).hasClass('sticky')) 
				toolTimer = window.setTimeout(function() { toolTipContainer.show(0); toolTimer = null; }, 400);
			else
				toolTipContainer.show(0);
	}).mouseleave(function(e) {
		if(!$(this).hasClass('sticky'))
			toolTipContainer.hide(0);
		if(toolTimer != null) {
			window.clearTimeout(toolTimer);
			toolTipContainer.hide(0);
		}
	}).mousemove(function(e) { 
        toolTipContainer.css({
			left: e.pageX + 15
		}) 
    });
	
	
	
	
	
	
	
	// drucken
	$('.pageFootLeftPrint, .shoppingPrint, .printTable').click(function() {
		window.print();
		return false;
	})
	// top
	$(".pageFootLeftTop").click(function(){
		$('html, body').animate({ scrollTop: $("#wrapper").offset().top }, 'slow');
		return false;
	});	
    // schulung finden
	if ($('.findSchool, .findSchool1, .navigationTopSearchInput').length > 0)
		$('.findSchool, .findSchool1, .navigationTopSearchInput').selectreplace();
    
    if ($('.trainingExams').length > 0) {
        $('.trainingExams').selectreplace();
        var lis = $('#trainingExams .form-select').find('.replace ul').find('li');
        lis.each(function() {
            var txt = $(this).find('a').html();
            txt = txt.replace(/\*\*/, '<img src="/ecomaXL/pics/star_garantie.png" alt="Termingarantie" title="Termingarantie" width="15" height="15" />');
            $(this).find('a').html(txt);
        });
    }
    //if ($('#colMain.tableDetail select').length > 0)
		//$('#colMain.tableDetail select').selectreplace();
	// navi
	if ($('select#thema').length > 0) {
        $('select#thema').siblings('.findSchoolS').click(function() {
            if($('select#thema').val() == '')
                return false;
        });
    }
	
	
	$('#navigationSub li').find('ul').each(function() {
		$(this).hide(0);
	});
	
	var navTimer = null;
	$('#navigationSub li').hover(function(e) {
		ae = $(this);
		if(!ae.find('ul').is('animated')) {
			navTimer = window.setTimeout(function() { ae.find('ul').fadeIn('fast') }, 300);
		}
	},function() {
		$(this).find('ul').fadeOut('slow');
	}).mouseleave(function() {
		window.clearTimeout(navTimer);
	}); 
	
	
	

	
	
	
	
	
	
	
	
	
	
	// extern
	$("a[rel^='extern']").attr('target', '_blank');
	
	// start submit 
	$('.findSchoolS').css('margin','-26px 0 0 180px');
	
	// divs ausblenden
	$('#toggle_lit_liefer_2, #toggle_r_liefer_2').css("display","none");	
	// lieferadresse
	$('#lit_liefer_2').click(function(){
		if($('#toggle_lit_liefer_2:hidden').length > 0) {
			$('#toggle_lit_liefer_2').slideDown('slow');
		}
	});
	$('#lit_liefer_1').click(function(){
		if($('#toggle_lit_liefer_2').length > 0) {
			$('#toggle_lit_liefer_2').slideUp('slow');
		}
	});
	// rechnungsadresse
	$('#r_liefer_2').click(function(){
		if($('#toggle_r_liefer_2:hidden').length > 0) {
			$('#toggle_r_liefer_2').slideDown('slow');
		}
	});
	$('#r_liefer_1').click(function(){
		if($('#toggle_r_liefer_2').length > 0) {
			$('#toggle_r_liefer_2').slideUp('slow');
		}
	});
	// Schlagwortsuche
	$('#s').focus( function() {
		if($(this).val() == 'Schlagwortsuche') {
			$(this).attr('value', '');
		}
	});
	$('#s').blur( function() {
		if($(this).val() == '') {
			$(this).attr('value', 'Schlagwortsuche');
		}
	});
	if($.browser.opera) {
		$('.replace').css('margin', '10px 0 0');
	}
	
	
	// produktberater
	$('#produkt').hover( function() {
		$('#produktberater').stop(true, true).pause(200).fadeIn('fast');
	}, function() {
		$('#produktberater').stop(true, true).fadeOut('slow');
	});
	$('#produktberater li').hover( function() {
		$(this).find('ul').show(0);
		$(this).find('.hover').addClass('over');
	}, function() {
		$(this).find('ul').hide(0);
		$(this).find('.hover').removeClass('over');
	});
	
    
    // tabellensortierung
    if ($.tablesorter) {
        $.tablesorter.addParser({
            // set a unique id 
            id: 'max-price',
            is: function(s){
                // return false so this parser is not auto detected 
                return false;
            },
            format: function(s){
                // format your data for normalization
                return $.tablesorter.formatFloat(s.replace(/\./, '').replace(/,/, '.'));
            //return s.toLowerCase().replace(/good/,2).replace(/medium/,1).replace(/bad/,0); 
            },
            // set type, either numeric or text 
            type: 'numeric'
        });
        $.tablesorter.addParser({
            id: 'max-date',
            is: function(s){
                return false;
            },
            format: function(s){
                s = s.replace (/^\s+/, '').replace (/\s+$/, '');
                if (/^(\d{2})[\.](\d{2})[\.](\d{2})$/.test(s)) 
                    s = s.replace(/^(\d{2})[\.](\d{2})[\.](\d{2})$/, "20$3/$2/$1");
                else if (/^(\d{2})[\.](\d{2})[\.][\-](\d{2})[\.](\d{2})[\.](\d{2})$/.test(s)) 
                    s = s.replace(/^(\d{2})[\.](\d{2})[\.][\-](\d{2})[\.](\d{2})[\.](\d{2})$/, "20$5/$2/$1");
                return $.tablesorter.formatFloat(Date.parse(s));
            },
            type: 'numeric'
        });
    }
    if ($('table.sortable').length > 0) {
        $('table.sortable').tablesorter({textExtraction: 'complex'});
        $('table.sortable th a[title=A-Z], table.sortable th a[title=Z-A]').click(function(e) {
            var dir = ($(this).attr('title') == 'A-Z') ? 0 : 1;
            var column = $('table.sortable th').index($(this).parents('th').eq(0));
            var sort = [[column, dir]];
            $('table.sortable').trigger('sorton', [sort]);
            e.preventDefault();
            e.stopPropagation() ;
        });
    }
    if ($('table.sortable2').length > 0) {
        $('table.sortable2').tablesorter({
            textExtraction: 'complex',
            headers: {
                4: {
                    sorter: 'digit'
                },
                5: {
                    sorter: false
                }

            }
        });
        $('table.sortable2 th span.sort a').click(function(e) {
            var dir = ($(this).attr('title') == 'A-Z') ? 0 : 1;
            var column = $('table.sortable2 th').index($(this).parents('th').eq(0));
            var sort = [[column, dir]];
            $('table.sortable2').trigger('sorton', [sort]);
            e.preventDefault();
            e.stopPropagation() ;
        });
    }
    if ($('table.mocNumbers, table.jobTa').length > 0) {
        $('table.mocNumbers, table.jobTa').tablesorter({
            textExtraction: 'complex'
        });
        $('table.mocNumbers th span.sort a, table.jobTa th span.sort a').click(function(e) {
            var dir = ($(this).attr('title') == 'A-Z') ? 0 : 1;
            var column = $('table.mocNumbers th, table.jobTa th').index($(this).parents('th').eq(0));
            var sort = [[column, dir]];
            $('table.mocNumbers, table.jobTa').trigger('sorton', [sort]);
            e.preventDefault();
            e.stopPropagation() ;
        });
    }
    if ($('table.referenz').length > 0) {
        $('table.referenz').tablesorter({
            textExtraction: 'complex',
            headers: {
                0: {
                    sorter: false
                }
            }            
        });
        $('table.referenz th span.sort a').click(function(e) {
            var dir = ($(this).attr('title') == 'A-Z') ? 0 : 1;
            var column = $('table.referenz th').index($(this).parents('th').eq(0));
            var sort = [[column, dir]];
            $('table.referenz').trigger('sorton', [sort]);
            e.preventDefault();
            e.stopPropagation() ;
        });
    } 
	$('#navigationTopSearch ul li a').click(function() {
		if($(this).attr('href') != '') {
			var href = $(this).attr('href')
			setTimeout("self.location.href='"+href+"'",0);
		}
	});
    
    if($("form#inhouse").length > 0) {
    	$('#offen').bind('click', function() {
    		if($(this).attr('checked')==true) {
    			$('#pruefung').val('');
    		}
    	});
    	$('#pruefung').bind('change', function() {
    		if($(this).val()=='') $('#offen').attr('checked', 'checked');
    		else $('#offen').attr('checked', false);
    	});
    	
    	$('#date').bind('focus', function() {
    		if($(this).val()=='MM/YYYY') {
    			$(this).val('');
    		}
			$('#offen2').attr('checked', false);    		
    	}).bind('blur', function() {
    		if($(this).val()=='') {
    			$(this).val('MM/YYYY');
    			$('#offen2').attr('checked', 'checked');
    		}
    	});
    	$('#offen2').bind('click', function() {
    		if($(this).attr('checked')==true) {
    			$('#date').val('MM/YYYY');
    		}
    	});
    }
});

