$(document).ready
(
    function()
    {
    	
    	
	    $('.callback input').keydown(function(e){
	        if (e.keyCode == '13') {
	        	$('.callback .submit').click();
	            return false;
	        }
	    });
        
        callBackValidate();
        
        $('.tx-jppageteaser-pi1-list-entry a').hover
        (
            function()
            {
                $(this).closest('.tx-jppageteaser-pi1-list-entry').contents().find('a').addClass('hovered');
            },
            function()
            {
                $('.tx-jppageteaser-pi1-list-entry a.hovered').removeClass('hovered');
            }
        );

        $('.tx-jppageteaser-pi1-list-entry-image img').each
        (
            function()
            {
                posTop = Math.round((100 - $(this).height())/2) + 'px';
                posLeft = Math.round((100 - $(this).width())/2) + 'px';
                $(this).css
                ({
                    'position': 'relative',
                    'top': posTop,
                    'left': posLeft
                });
            }
        );        

        $('#content .subcategory .tx-jppageteaser-pi1-list-entry').each
        (
            function()
            {
                child = $(this).children('.tx-jppageteaser-pi1-list-entry-contents');
                h = $(this).height();
                hContents = child.height();
                contentsTop = Math.round((h - hContents)/2) + 'px';
                
                child.css
                ({
                    'position': 'relative',
                    'top': contentsTop
                });
            }
        );


        
   
        /*Reichwald Handwerkersuche*/
        $('#content .tx-reichwaldcraftsmen-pi1 table tr td').each
        (
            function()
            {
                h = $(this).height();
                $(this).children().height(h);
            }
        );
            
        $('#content .tx-reichwaldcraftsmen-pi1 table tr td a').hover
        (
            function ()
            {
                $(this).parents('tr').children().find('a').addClass('hovered');
            },
            function ()
            {
                $('#content .tx-reichwaldcraftsmen-pi1 table tr td a.hovered').removeClass('hovered');
            }
        );
		
		/*Reichwald Handwerkersuche - Partner*/
		$('.tx-reichwaldcraftsmen-pi1-list-entry-image img').each(function(){
			posTop = Math.round((130 - $(this).innerHeight()) / 2) + 'px';
			posLeft = Math.round((130 - $(this).innerWidth()) / 2) + 'px';
			$(this).css({
				'position': 'relative',
				'top': posTop,
				'left': posLeft
			});
		});
	
		$('.tx-reichwaldcraftsmen-pi1 #partner-details .tx-reichwaldcraftsmen-pi1-detail-image img').each(function(){
			posTop = Math.round((205 - $(this).innerHeight()) / 2) + 'px';
			posLeft = Math.round((205 - $(this).innerWidth()) / 2) + 'px';
			$(this).css({
				'position': 'relative',
				'top': posTop,
				'left': posLeft
			});
		});

		
		$('#contact-data .callback input')
			.val('Rückruf-Service')
			.focus(function () {
		        $(this).attr('value','');
		    });
		
    }    
);

function callBackInit()
{
    $.callbackLabels = {};
    
    $('#footer .mailform li label').each(function(i){
        $.callbackLabels[$(this).next().attr('id')] = $(this).text();
        $(this).text('').next().val($.callbackLabels[$(this).next().attr('id')]).focus(function () {$(this).attr('value','');});
    });
    
};

function callBackValidate()
{
    $.callbackErrors = {};
    callBackInit();
    
    $('#footer .submit').parent().prepend('<span id="callback-errors"></span>');
    
    $('#footer .submit').click(function(event){
        callbackErrors = '';
        for(key in $.callbackLabels) {
            if( (($('#' + [key]).val() == $.callbackLabels[key]) && ($.callbackLabels.hasOwnProperty(key))) || ($('#' + [key]).val() == '')  )
            {
                event.preventDefault();
                $.callbackErrors[key] = $.callbackLabels[key];//$('#' + [key]).val();
                //console.log($.callbackErrors);
            }
            else
            {
                delete $.callbackErrors[key];
                console.log($.callbackErrors);
            }
        };
        for(key in $.callbackErrors) {
            callbackErrors += '<p class="error">"' + $.callbackErrors[key] + '" bitte ausfüllen.</p>';
        };
        $('#callback-errors').children('.error').remove()
        $('#callback-errors').prepend(callbackErrors);
    });
};
