$(document).ready(function() {

    /*oHandler = $("body select").msDropDown().data("dd"); geht irgendwie nicht*/

    var $j = jQuery;
    $j.fn.extend({
        toggleValue : function (defaultText) {
            return this.each(function() {
                $j(this).focus(function() {
                    if ($j(this).val() == defaultText) {
                        $j(this).val('');
                    }
                    $j(this).blur(function () {
                        if ($j.trim($j(this).val()) == '') {
                            $j(this).val(defaultText);
                        }
                    });
                });
            });
        }
    });
    
    $("#flags select option").each(function() {
    	$(this).css('backgroundImage', "url('" + $(this).attr('title') + "')");
    });
    
    $("#flags img").attr('src', $("#flags select option:selected").attr('title'));

    $("#flags select").change(function() {
    	$("#flags img").attr('src', $("#flags select option:selected").attr('title'));
        self.location.href = $(this).attr('value');
    });

    $('#login input:text').each(function(){$(this).toggleValue($(this).val());});
    $('div.productsearch input:text').each(function(){
        $(this).val($(this).attr('title'));
        $(this).toggleValue($(this).val());
    });
    $('div.productsearch input:submit').click(function(){
        $('div.productsearch input:text').each(function(){
            if($(this).val() == $(this).attr('title')) {
                $(this).val('');
            }
        });
    });
    $('#login input:submit').click(function(){
        $('#login input:text').each(function(){
            if($(this).val() == $(this).attr('title')) {
                $(this).val('');
            }
        });
    });
	
	$("#tabs").tabs();
	
    if($('#wrapin div.ps').height() < $('#aside').height()) {
        $('#wrapin div.ps').height($('#aside').height());
    }
    if($('#wrapin div.ps2').height() - $('#header').height() < $('#aside').height()) {
        $('#wrapin div.ps2').height($('#aside').height() + $('#header').height());
    }
    /*
    if($('#wrapin div.content').height() < $('aside').height()) {
        $('#wrapin div.content').height($('aside').height());
    }
    if($('#wrapin div').not('.content').not('.ps').height() - $('header').height() < $('aside').height()) {
        $('#wrapin div').not('.content').not('.ps').height($('aside').height() + $('header').height());
    }*/

    /*
    $('div.figure a').lightBox({
        	imageLoading: TPL_URL + 'js/lightbox/images/lightbox-ico-loading.gif',
            imageBtnClose: TPL_URL + 'js/lightbox/images/lightbox-btn-close.gif',
            imageBtnPrev: TPL_URL + 'js/lightbox/images/lightbox-btn-prev.gif',
        	imageBtnNext: TPL_URL + 'js/lightbox/images/lightbox-btn-next.gif',
            imageBlank: TPL_URL + 'js/lightbox/images/lightbox-blank.gif',
            fixedNavigation: true
    });
    */

    $("a[rel=staplerset]").fancybox({
        'transitionIn'		: 'none',
        'transitionOut'		: 'none',
        'titlePosition' 	: 'over',
        'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
            return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
        }
    });

    $("div.productbox > a").show();
    /*$("div.productbox > a:nth-child(2), div.productbox > a:last-child").slideUp('slow');*/
	
	$('form.autosubmit input:submit').hide();
	$('form.autosubmit select').change(function() {$(this).closest("form").submit()});

	$('#pwadvert a').click(function() {
		$.cookie("pwadvert", "false", { expires: 7, path: '/' });
		$('#pwadvert').fadeOut();
	});

	if($.cookie('pwadvert') == 'false') {
		$('#pwadvert').hide();
	}
	
});

$(document).ready(function() {
	var at = / at /;
	var dot = / dot /g;
	$('span.mailme').each(function () {
		var addr = $(this).text().replace(at,"@").replace(dot,".");
		$(this).after('<a class="justprint" href="mailto:'+ addr +'" title="Send an email to '+ addr +'">'+ addr +'</a>');
		$(this).remove();
	});
	$('span.mailmeicon').each(function () {
		var addr = $(this).text().replace(at,"@").replace(dot,".");
		$(this).after('<a href="mailto:'+ addr +'" title="Send an email to '+ addr +'" class="noprint"><img class="noprint" src="./templates/richter/img/email.gif" alt="'+ addr +'" /></a><span class="printonlyi">' + addr + '</span>');
		$(this).remove();
	});
});
