jQuery(function($)
{
	// animation bei hover
    $('#navimain #slide .no1').each(
        function()
        {
            $(this)
            .hover
            (
                function() {
                    
                    $(this).children('.a1')
                    .animate
                    (
                        {
                            'padding-bottom': '60'
                        },
                        {
                            'duration':150, // same speed as hover out, see below (less flicker -> much better usability)
                            'queue' : false
                        }
                    );
                },
                function() {
                    $(this).children('.a1')
                    .animate
                    (
                        {
                            'padding-bottom': '0'
                        },
                        {
                            'duration':150, // same speed as hover in, see above (less flicker -> much better usability)
                            'queue' : false
                        }
                    );
                }
            )
        }
        
    );

    // styles for exclusively for IE6
    if($.browser.msie  && (jQuery.browser.version.substr(0,3)=="6.0")){
        $('#head').hover
        (
            function ()
            {
                $(this).addClass('hover');
            },
            function ()
            {
                $(this).removeClass('hover');
            }
        );
    }

    // navigation animation sensation
    // call navistretch function
    // set class on hover
    $('#navimain li').hover
    (
        function ()
        {
            $(this).addClass('hover');
        },
        function ()
        {
            $(this).removeClass('hover');
        }
    );
    // adjust position for 3rd level 
    $('#navimain li li.hover .lev2').each
    (
        function()
        {
            positionLeft = $(this).prevAll('a').outerWidth();
            $(this).css
            ({
                'left': positionLeft
            });
        }
    );

    //naviStretch('#navi-main');

    $('#unique-name').append('<a class="thickbox" id="unique-name-clicklink" href="#"></a>');

    var isSupersizeImgLoaded = false;
    var $supersizeLoadingImg = null;

    // Adjust supersize image size
    function resizenow()
    {
        var browserheight = $(window).height();
        var browserwidth  = $(window).width();
    
        var $supersize = $('#supersize');
        var $supersizeimg = $supersize.find('img');
    
        var imagewidth  = $supersizeimg.attr('width');  // read attr and not width() because invisible images have width() 0
        var imageheight = $supersizeimg.attr('height'); // dito.
        var imagewidth  = $supersizeLoadingImg.width();
        var imageheight = $supersizeLoadingImg.height();
        var imageratio  = imageheight/imagewidth;
    
        $supersize.height(browserheight);
        $supersize.width(browserwidth);
    
        //Resize image to proper ratio
        if ((browserheight/browserwidth) > imageratio)
        {
            var childrenheight = browserheight;
            var childrenwidth  = browserheight / imageratio;
        }
        else
        {
            var childrenwidth  = browserwidth;
            var childrenheight = browserwidth * imageratio;
        }
    
        $supersizeimg.css({
            width: childrenwidth,
            height: childrenheight,
            left: 0, // centered: (browserwidth - childrenwidth)/2,
            top:  0  // centered: (browserheight - childrenheight)/2
        });
    };
    
    // create dummy img tag to trigger loading of supersize img (currently visible, load event would never fire)
    // important: assign img object to variable first, do not chain with bind('load', ...) etc! (otherwise this variable
    // would be set too late: after src has been set which triggers load-handler)
    $supersizeLoadingImg = $('<img id="supersize-loading" />');
    $supersizeLoadingImg
        .bind('load', function()
        {
            isSupersizeImgLoaded = true;
            resizenow(); // resize _before_ making visible, to avoid flicker
            $('#supersize').fadeIn('fast');
        })
        .hide().appendTo('body') // append (hidden) to dom, otherwise loading wouldn't be triggered in ie-browsers
        .attr('src', $('#supersize img').attr('src'));

    $(window).bind('resize', function()
    {
        if (isSupersizeImgLoaded)
        {
            resizenow();
        }
    });

    $('.button-links .mailform label').each(
        function() {
            $(this).hide().next().val($(this).text() + '...').focus(function() { $(this).val(''); });
        }
    );
    
    // links with icons in content
    $('.phoneorder-link').click
    (
        function(event)
        {
            event.preventDefault();
            $(this).next().children().toggle();
        }
    );
    
    // "unsere wochenkarte"
    // #flashTest1 is the selector
    $('#unsere-wochenkarte')
    .flash(
    {
        // test.swf is the flash document
        swf: 'fileadmin/layouts/main/public/swf/karte_flashvars.swf',
        params:
        {
            wmode: 'transparent',
            base: 'http://www.dasnaschwerk.de/'
        },
        flashvars:
        {
            url1: 'index.php?id=28'
        },
        width: 83,
        height: 109
    })
    .find('img').hide();
    
    // image gallery
    $('#MyFlowGallery').append('<div class="btn-right"></div>');
    $('#MyFlowGallery').append('<a class="thickbox" id="MyFlowGallery-clicklink" rel="imageflow" href="#"></a>');
    $('#MyFlowGallery').prepend('<div class="btn-left"></div>');
    $('#MyFlowGallery .btn-right').bind("click", function() { $('#MyFlowGallery_next').trigger('click') });
    $('#MyFlowGallery .btn-left').bind("click", function() { $('#MyFlowGallery_previous').trigger('click') });

    // lightbox
    $('.lightbox').fancybox({
        'titlePosition'  : 'inside'
    });

    $.fn.media.defaults.mp3Player = '/fileadmin/layouts/main/public/swf/mp3player.swf';
    mp3Files();
    
    $('#search-input').bind(
        'focus',
        function()
        {
            $(this).val('');
        }
    );
    
    if($('#col-100 .news-list-container').size())
    {
    	ajaxNews();
    }
    
});

$(window).load(function () {
    // Calling naviStretch here as FF 9.0.1 WIN has problems with call in document-ready-fct:
    // styles for everyone except IE6
    if(!($.browser.msie  && (jQuery.browser.version.substr(0,3)=="6.0"))){
        $('body.nojs').removeClass('nojs');
        naviStretch('#navimain');
    }
});

/******************************   functions   *************************************/

function ajaxNews()
{
	$('.browseLinksWrap a').each(
	    function()
	  	{
	   		$(this).bind
	   		(
				'click',
				function(e)
	    	    {
					e.preventDefault();
	    	        $('#col-100').load
	    	        (
	    	        	$(this).attr('href') +' #col-100 > div',
	    	        	function()
	    	        	{
	    	        		ajaxNews();
	    	        	}
	    	        );
	    	    }
	   		);
	  	}
	);
}

function setLinkForFancybox(linkTarget, linkTitle) {
    var attributes = {
        href: linkTarget
    }
    if (linkTitle)
    {
        attributes.title = linkTitle;
    }
    $('#MyFlowGallery-clicklink').attr(attributes);
}

function mp3Files() {
    $('#content-inner .csc-texttext a.download').each(function(i) {
        var $a = $(this);
        var href = $a.attr('href');
        
        // get the extension from the href
        var hrefArray = href.split('.');
        var extension = hrefArray[hrefArray.length - 1];
        
        if (extension == 'mp3') {
            $(this).media( {
                width: 824,
                height: 35,
                flashvars: { soundFile: href, playerID: 'audioplayer' + i }
            } );
        }
    });
    //Cufon.replace('#content-inner div.download div');
}
 
function getNaviItems()
{
    var a = [];
    $('#navimain .a0').each
    (
        function()
        {
            a.push
            ({
                'element' : this,
                'width'   : $(this).outerWidth()
            });
        }
    );
    return a;
}
 
function getNaviItemsWidth(a) {
    var naviItemWidth = 0;
    for (var i = 0; i < a.length; i++) {
        naviItemWidth += a[i]['width'];
    }
    return naviItemWidth;
}

function naviStretch( naviclass, paddingOuter ) {
    var a = getNaviItems();
    var naviWidthIs = getNaviItemsWidth(a);
    var naviWidthShould = $(naviclass).outerWidth();
    var paddingAll = 0;
    
    var padding = Math.floor( (naviWidthShould - naviWidthIs) / (2 * a.length ) );
    for (var i = 0; i < a.length; i++)
    {
        $(a[i]['element']).css
        ({
            //paddingTop: 0,
            //paddingBottom: 0,
            'padding-left': padding + 'px',
            'padding-right': padding + 'px'
        });
        paddingAll += (padding*2);
    }
    

}

