$(document).ready(function(){


	/* External links
	------------------------------------------------------------- */
	$('a').each(function() {
	
		var href = $(this).attr('href');
	  
		// kludgy
		domain = location.hostname;	// current domain
		domain = domain.split('.');
		domain.shift(); // remove subdomain
		domain = domain.join('.');

		// console.log(location.hostname);	  
		// console.log(domain);
				
		if (this.hostname && this.hostname !== location.hostname && this.hostname.indexOf(domain) == -1)
		{
    	$(this)
    		.removeAttr('target')
    		.attr('rel', 'external')	    		
        .attr('title', href)
        .click(function() {
           window.open($(this).attr('href'));
           return false;
       });
		}
  
	});
	
	$('a > img').each(function() {
		
		$(this).parents('a').addClass('image-link');
		
	});
		

	/* Layout fixes
	------------------------------------------------------------- */
	$('.block, .widget, .teaser, .showcase-pager, .list, #color-guide').addClass('clear');

	$('.section, .block .body').each(function() {
		$(this).children('.column:first').addClass('first');
		$(this).children('.column:last').addClass('last');
	});
	$('.list').each(function() {
		$(this).children('.teaser:last').addClass('last');
	});
	$('.nav-sub').each(function() {
		$(this).children('li:last').addClass('last');
	});
  	$('#nav-main li.parent:last').addClass('last');

	// if ( $('a.more').html() == '' )
	// {
	// 	$('a.more').html('&nbsp;');		
	// }




	/* Convert image to background image (for round corner images)
	------------------------------------------------------------- */
	$('.cycle .slide, .widget.cap').each(function() {
		var src = $(this).children('.image').children('img').attr('src');
		// console.log(src);
		$(this).css('background-image', 'url(' + src + ')');
		$(this).children('.image').hide();
	});

	
	$('.project .image').each(function() {
		var src = $(this).children('a').children('img').attr('src');
		// console.log(src);
		$(this).css('background-image', 'url(' + src + ')');
		$(this).children('a').children('img').hide();
	});


	$('.flickr-image').each(function() {			
		$(this).wrap('<span class="image-box"></span>');		
	});


	$('.image-box').each(function() {
		
		box = $(this);
		img = box.find('img');		
		link = box.find('a');
		src = img.attr('src');
		
		box.css('background-image', 'url(' + src + ')');				
		
		img.hide();

		w = img.width();
		h = img.height();
		
		box.width(w);		
		box.height(h);
					
		if (link != 'undefined')
		{
			link.width(w);
			link.height(h);
		}
		
	});
	



	/* Show / hide guide elements
	------------------------------------------------------------- */
	$('#show-sections').click(function(event){
		$('.section').toggleClass('guide');
		$(this).toggleText('Show Sections', 'Hide Sections');
		return false;
	});
	$('#show-columns').click(function(event){
		$('.column').toggleClass('guide');
		$(this).toggleText('Show Columns', 'Hide Columns');		
		return false;
	});
	$('#show-blocks').click(function(event){
		$('.block').toggleClass('guide');
		$(this).toggleText('Show Blocks', 'Hide Blocks');		
		return false;
	});


	/* Sub navigation
	 * http://users.tpg.com.au/j_birch/plugins/superfish/
	------------------------------------------------------------- */	
	$('#nav-main ul').superfish({
		pathClass: 		'active',
		hoverClass: 	'hover',
		autoArrows:   false,
	  	dropShadows:  false,
		// animation: 		{height:'toggle'},
		speed: 				400,
		// disableHI: 		true,
		delay:        800
	}); 
	
	
	/* Add a class to each sub-nav item
	------------------------------------------------------------- */	
	$('#nav-main .nav-sub').each(function() {

		var name = $(this).attr('id');
		
		$(this).children('li').children('a').each(function() {
			  
       var id = (name + '-' + $(this).text().toLowerCase().replace(/\s/g, '-')).replace(/sub-/, '');
			 var img = 'url(' + http_root + '/res/img/sub-nav/' + lang + '_' + id.replace(/nav-/, '').replace(/-/g, '_') + '.gif)';
			 
			 $(this).attr('id', id).css('background-image', img);
      							
		});
		
	});


	/* Banner slideshow
	------------------------------------------------------------- */
	$('#banner .slideshow').cycle({ 
		timeout: 10000, 
		speed: 600,
		fx:     'scrollHorz', 
		easing: 'easeInOutSine',
		startingSlide: selectedSlide,
		prev:   '.prev', 
		next:   '.next',
		pause: 1,
		after: update_banner_link
	});

	// The banner mask graphic hides links attached directly to slide elements
	// This function creates a 'hit' element over the mask with the href of the current slide (if any)
	function update_banner_link() 
	{
		var slide = this;
		
		$('#banner a.hit').remove();

		if (slide.tagName.toUpperCase() == 'A')
		{
			$('#banner').prepend('<a class="hit" href="' + slide.href + '"></a>');
			// console.log(slide.tagName);
			// console.log(slide.href);			
		}
	}



	/* Banner slideshow pause/resume
	------------------------------------------------------------- */
	$('.mask').bind("mouseenter",function(){
		$('#banner .slideshow').cycle('pause')
	}).bind("mouseleave",function(){
		$('#banner .slideshow').cycle('resume')
	});

		
	/* Centerplan menu
	------------------------------------------------------------- */
	$('#centerplan ul').superfish( {
	    pathClass: 'active',
	    hoverClass: 'hover',
	    autoArrows: false,
	    dropShadows: false,
	    // speed: 1,
	    disableHI: true
  	});

	
	/* Cycle widget slideshow
	------------------------------------------------------------- */
	$('.cycle .slideshow').cycle({ 
		timeout: 	7000, 		
		speed: 		600,
		// fx:     	'scrollHorz', 
		fx: 			'fade',
    easing: 	'easeInOutSine',		
		pager:  	'.pager'				
	});

	
	/* Cycle exhibition slideshow
	------------------------------------------------------------- */
	$('.cycle .slideshow-half').cycle({ 
		timeout: 	7000, 		
		speed: 		600,
		// fx:     	'scrollHorz', 
		fx: 			'fade',
   		easing: 	'easeInOutSine',
		pager: '.pager'
	});

	
	/* Accordion widget
	------------------------------------------------------------- */
	$(".accordion").accordion({
		header: '.title',
		autoHeight: false,
		selectedClass: 'active', 
	    	// active: false, 
	    	// alwaysOpen: false, 
	    	animated: false
	});


	/* Maps
	 * http://map.ifies.org/
	------------------------------------------------------------- */
	// var contact_details = $('#contact-details-for-map').html();
	// console.log(contact_details);
	
	$('#contact-map').jmap(
		'init', {'mapType':'map','mapZoom':13, 'mapCenter':[48.309973,14.283514], 'mapShowjMapsIcon':false}
	);
	$('#contact-map').jmap(
		// 'AddMarker', {'pointLatLng':[48.309973,14.283514], 'pointHTML':contact_details}
		'AddMarker', {'pointLatLng':[48.309973,14.283514]}
	);
	
	
	/* Calendar
	------------------------------------------------------------- */

	$('.calendar tr.day:first').addClass('first');
	$('.calendar .description').hide();
		
	$('.calendar .toggle-description').click(function(event) {
		$(this).next('.description').slideToggle();
		return false;
	});

	$('.datepicker').datepicker({ 
		firstDay: 1,
		dateFormat: 'yy-mm-dd',
		onSelect: function(date, instance) 
		{
			 window.location.href = "?date=" + date;
		}
	});
		
	if ( lang == 'de')
	{
		$('.datepicker').datepicker('option', 'dayNamesMin', ['So', 'Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa']);	
		$('.datepicker').datepicker('option', 'monthNames', ['Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember']);		
	}
	
	$('.datepicker').hide();
	
	$('.datepicker-toggle').click(function(event) {
		$('.datepicker').toggle();
		return false;
	});
	

	/* Misc
	------------------------------------------------------------- */
	$('a.print').click( function() { window.print(); });
	
	$('iframe').hide();

	
  /* Press PDF links
  ------------------------------------------------------------- */
  $("a:contains('Lesen Sie mehr')").addClass('more');
	
	$("a:contains('Read more')").addClass('more');
	
});
