$(document).ready(function() {
    // {{ Fancybox

    $('a[rel="fancybox"]').fancybox();
    $('a.iframe').fancybox({frameWidth: 800, frameHeight: 550});

	// }}


	// {{ Search field

	var needleText = 'Suchbegriff eingeben...';
	$('#searchpanel #q').val(needleText);
    $('#searchpanel #q').bind('focus', function () {
    	if ($(this).val() == needleText)
    		$(this).val('');
    });

    $('#searchpanel #q').bind('blur', function () {
    	if ($(this).val() == '')
    		$(this).val(needleText);
    });

    // }}


    // {{ Color up li-elements

    $('#content ul, #content ol').not('.employees').not('.partners').not('.form_errors').find('li').wrapInner('<span class="colored" />');

    // }}


    // {{ Auto submit for selectbox on homepage

    $('form#serviceselector select').bind('change', function () {
    	if (parseInt($('form#serviceselector select').val()))
    		$('form#serviceselector').submit();
    });

    // }}


    // {{ Auto length 3rd level navigation elements
    
    var max_widths = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0);
    var col_count  = $('#content ul.third li').length;

    function distribute(col_count) {
    	var twidth = 0;

    	for (var i = 0; i < max_widths.length; i++) {
    		max_widths[i] = 0;
    	}

        $('#content ul.third li').each(function(index, value) {
        	max_widths[index % col_count] = Math.max($(this).width(), max_widths[index % col_count]);
        });

        for (i = 0; i < col_count; i++) {
        	twidth += max_widths[i];
        }

        return twidth;
    }

    while (distribute(col_count) > 500) {
    	col_count--;

    	if (col_count == 1)
    		break;
    }

    $('#content ul.third li').each(function(index, value) {
    	if (index % col_count == 0) {
    		$(this).css('clear', 'both');
    	}

    	$(this).width(max_widths[index % col_count]);
    });

    // }}


    // {{ Theme handling

	if (typeof window['changeTheme'] == 'function') {
		changeTheme(cur_theme_index, false);
		beginWalkingThemes();
	}

    // }}


	// {{ Manipulate external links

	$("a[href^='http://']").addClass('external');

	// }}


	// {{ Dropdown panel handling

	$('a.dropdownpanel_trigger').click(
		function () {
			$(this).blur();
			$('.dropdownpanel').fadeIn('fast');
		}
	);

	$('.dropdownpanel').hover(
		function () {},
		function () {
			$('.dropdownpanel').fadeOut('slow');
		}
	);

	$('.dropdownpanel').click(function () {
		$('.dropdownpanel').fadeOut('slow');
	});

	// }}


	// {{ Tag cloud

    swfobject.addDomLoadEvent(function() {
    	swfobject.embedSWF(
			"s/text_and_image_cloud.swf",
			"tagcloud",
			"300",
			"265",
			"9",
			"s/expressInstall.swf",
			{
				cloud_data: "../clouddata.xml.php",
				tcolor: "0x999999",
				tcolor2: "0x666666",
				hicolor: "0x494949",
				tspeed: "85",
				fontFace: "Arial"
			},
			{
				wmode: "window",
				menu: "false",
				quality: "best"
			}
        );
    });

    // }}


    // {{ Handle ajax loading of contactpersons

    $('a[rel^="contactperson_"]').each(function () {
    	$(this).attr('href', '#').bind('click', function () {
        	var cid = parseInt($(this).attr('rel').substring(14));

        	$('#contactperson_data').animate({opacity:0}, '1000', 'swing', function() {
            	$('#contactperson_data').load('loadcontactperson.php?cid=' + cid, '', function () {
            		$('#contactperson_data').animate({opacity:1}, '1000', 'swing');
            	});
        	});
    	});
    });

    // }}


    // {{ No-spam protection

    $('a.email').nospam({
        replaceText: true,
        filterLevel: 'normal'
    });

    // }}
});

