jQuery.noConflict();

    function slideSwitch(fadeTime) {
        var $active = jQuery('#fadingImages div.active');
        
        if ( $active.length == 0 ) $active = jQuery('#fadingImages div:last');
        
        var $next =  $active.next('div').length ? $active.next('div')
            : jQuery('#fadingImages div:first');
        
        $active.removeClass('active').addClass('last-active');
        $next.addClass('active')
            .fadeIn(fadeTime, function() {
                $active
                    .removeClass('last-active')
                    .hide();
            });
    }
    
/**
********************************************************************************
* language vars
********************************************************************************
*/

/**
* fancybox *********************************************************************
*/
    var fancyCountSep;
    var fancyClose;
    
    var fancyCountSep_de = 'von';
    var fancyClose_de = 'schliessen';
    var fancyCountSep_en = 'from';
    var fancyClose_en = 'close';

/**
* rzgooglemaps *****************************************************************
*/
    var linkGoogleMaps;
    
    var linkGoogleMaps_de = 'Link zu Google Maps';
    var linkGoogleMaps_en = 'Link to Google Maps';
    

jQuery(function($) {
	
/**
* check language ***************************************************************
*/
    var lang = jQuery('meta[name="language"]').attr('content');
    switch(lang) {
        case "de":
            fancyCountSep = fancyCountSep_de;
            fancyClose = fancyClose_de;
            linkGoogleMaps = linkGoogleMaps_de;
        break;
        case "en":
            fancyCountSep = fancyCountSep_en;
            fancyClose = fancyClose_en;
            linkGoogleMaps = linkGoogleMaps_en;
        break;
        case "fr":
            fancyCountSep = fancyCountSep_fr;
            fancyClose = fancyClose_fr;
            linkGoogleMaps = linkGoogleMaps_fr;
        break;
        default:
            fancyCountSep = fancyCountSep_de;
            fancyClose = fancyClose_de;
            linkGoogleMaps = linkGoogleMaps_de;
    }

    
/**
********************************************************************************
* link the whole teaser
********************************************************************************
*/
    // link a whole block (e.g. li)
    var linkAllItems = $("#col1 .teaserElement");
    linkAllItems.css("cursor","pointer");
    
    linkAllItems.click(function(e){
        var newHref = $('a:first', this).attr('href');
        document.location = '/'+newHref;
        return false;
    }); 


/**
********************************************************************************
* header fading animation 
********************************************************************************
*/

//  console.log(headerBgs);

  	var headerBgsDiv;
  	var divPre = '<div id="fadingImage';
  	var divMid = '" class="fadingImage" style="background-image: url(';
  	var divApp = ')"></div>';
  	
  	// if there are more then one header-images in ressources
    if (headerBgs[1] != "") {
      	// delete the first img in array, cause it's already inserted
    	headerBgs.shift();
        // check the headerBgs-Array
        $.each(headerBgs, function(index, value) {
            var bgImage = value;
            var bgImageSrc = $(bgImage).attr('src');
            
            // to add new bg-divs
            var headerBgsDiv = $('.fadingImage:last');
            // add 1 to the index because the first image already has "0"
            index = index + 1;
            if (value != "") {
                // build new bg-div
                var newBgDiv = divPre + index + divMid + bgImageSrc + divApp;
                // add new bg div to the dom
                headerBgsDiv.after(newBgDiv);
                // hide new one for later use
                $('#fadingImage' + index).hide();
            }
        });
        
        setInterval( "slideSwitch(2000)", 10000 );
    }


/**
********************************************************************************
* header panorama animation
********************************************************************************
*/
    
    function bgScroll(headerBg, tempImageWidth) {
        var maxBgPos = -tempImageWidth;
        var bgPos = headerBg.css('backgroundPosition');
        var bgPosNew = parseInt(bgPos);
        
        // to prevent max bg-position value
        // check for width and take it as the max value
        if (maxBgPos < bgPosNew) {
            bgPosNew = bgPosNew-1;
        } else {
            bgPosNew = 0;
        }
        
        bgPosNew = bgPosNew+'px 0';
        headerBg.css('backgroundPosition', bgPosNew);
    }
    
    var headerBg = $('#fadingImage0');
    
    // it's just one header-image in ressources
    if (headerBgs[1] == "") {
        var bgImage = $('#fadingImage0').css("background-image").replace(/"/g,"").replace(/url\(|\)$/ig, "");
		bgImage = '<img src="'+bgImage+'" />';
        var $bgImage = $(headerBgs[0]);
        
        // IE's need to have the image inside the body/display block to get with
        var bgImageWidth = 
            $bgImage.css({visibility:'hidden', display:'block'}).appendTo('body').attr('width');
        $bgImage.remove();

        // IE8 needs this re-rendering of the site
        // hate these f*** IE's!
        $('body').css('padding','0');
        
        // if image is wider then 2500px scroll it as panorama
        if (bgImageWidth > 2500) {
            // set the funtion which gets started by interval
            var intervalFn = function () { bgScroll(headerBg, bgImageWidth); };
            
            // start the interval
            var intervalScroll = setInterval(intervalFn, 45 );
            // stop the interval on mouseover, continue on mouseout
            $('#header').hover(function() {
                clearInterval(intervalScroll);
            }, function() {
                intervalScroll = setInterval( intervalFn, 45 );
            });
        }
    }



/**
********************************************************************************
* gallerys
********************************************************************************
*/
    
    $('#fancybox-close').text(fancyClose);
    
    var fancyboxSettings = {
        'transitionIn' : 'none',
        'transitionOut' : 'none',
        'cyclic' : 'true',
        'padding' : 20,
        // should be: --- 'padding' : '20', --- but thats a current bug
        'overlayOpacity' : '0.7',
        'overlayColor' : '#000',
        'titlePosition' : 'inside',
        
        'titleFormat' : function(title, currentArray, currentIndex, currentOpts) {
            var title  = '<span id="imagePager">' + (currentIndex + 1) + ' ' + fancyCountSep + ' ' + currentArray.length + '</span>'
                       + '<span id="imageTitle">' + title + '</span>';
            return title;
        }
    };
    
    function verticalAlign(images) {
        $.each(images, function(key, value) { 
    //      console.log(key + ': ' + value);
            
            // get the get the height of the picture itself
            // and the height of its parent
            var imgHeight = $(this).height();
            var parentHeight = $(this).parent().height();
            
            // if the picture-height is smaller then the parent height, we have to calculate the top margin
            // and then add the top-margin to the picture
            if (parentHeight > imgHeight) {
                var marginTop = (parentHeight-imgHeight)/2;
                marginTop = parseInt(marginTop);
                $(this).css('marginTop', marginTop);
            }
        });
    }

/**
* standard gallery *************************************************************
*/
    $(".gallery a").fancybox(fancyboxSettings);
    
    // only align pictures vertically
    var images = $('.gallery img');
    verticalAlign(images);
    
    
/**
* inline gallery ***************************************************************
*/
    var galInWrap = 'div.galleryInlineWrap';
    var galIn = $('ul.galleryInline');
    
    //for each inline-gallery
	$.each( galIn, function(i, l){
		
	    var galInLinks = $('a', this);
	    galInLinks.fancybox(fancyboxSettings); 
	    
	    // wrap for the inline-gallery
	    $(this).wrapAll('<div class="galleryInlineWrap" class="clearfix" />');
	    
	    // change position of big images from list to wrap
	    $thisWrap = $(this).parent(galInWrap);
	    galInLinks.detach().prependTo($thisWrap);

	    // align all images
	    var images = $('img', $thisWrap);
	    verticalAlign(images);
	    
	    // put the first big image on top
	    $('a:eq(0)', $thisWrap).css('z-index','1');
	    
	    // find all the picture-pairs
	    var thumbLinks = $('li', this);
	    
	    // function for the li's to show a associated big image
	    function changeThumbOver() {
	        var thisIndex = thumbLinks.index(this);
	        var $thisParentWrap = $(this).parents(galInWrap);
	        var bigImages = $('a', $thisParentWrap);
	        bigImages.css('z-index','0');
	        var assocBigImage = $('a:eq(' + thisIndex + ')', $thisParentWrap);
	        assocBigImage.css('z-index','1');

	    }
	    function changeThumbOut() {}
	    
	    // set the funtion for the assoc image with indent
	    thumbLinks.hoverIntent(changeThumbOver, changeThumbOut);
	    
	    // set mouseover for clickaction without intent
	    thumbLinks.mouseover(function() {
	        var thisIndex = thumbLinks.index(this);
	        var $thisParentWrap = $(this).parents(galInWrap);
	        $(this).css('cursor','pointer');
	        $(this).click(function() {
	            $('a:eq(' + thisIndex + ')', $thisParentWrap).trigger('click');
	            return false;
	        });
	    });
	    
	}); //each
    
    
});
