/*
 * General Functionality
 */

$(document).ready(function() {

	/*
	 * Links
	 * - open external links in a new window (standards compliant)
	 */
	$('a[href^="http:"]').not('[href*="kahnertfurs.com"]').attr('target', '_blank').attr('rel', 'external').addClass('external');
	
	/* if this external link is an image, we don't want to show the external link icon */
	$('a.external').each(function() {
		if ($(this).html().substr(0, 4) == '<img') {
			$(this).addClass('noicon');
		}
	});

	/*
   * Contact form in modal popup window
	 */
	$('a.dialog').click(function() {
		var width = 500;
		var height = 300;

		$('<iframe src="' + this.href + '" />').dialog({
	    title: ($(this).attr('title')) ? $(this).attr('title') : '',
	    autoOpen: true,
	    width: width,
	    height: height,
	    modal: false,
	    resizable: true,
			autoResize: true,
	    overlay: {
	      opacity: 0.5,
	      background: 'black'
	    }
	  }).width(width).height(height);
		return false;
	});


	/*
	 * Gallery
	 */
	if ($('#gallery').length) {
		// move the gallery description
		$('#bottom .inner').prepend($('#gallery-description'));
		// add classes to last thumb in row
		$('.thumb:nth-child(4n)').addClass('last');
		
		/* make sure large gallery image top-aligned with thumbnails */
		$('#gallery .col.right').css('top', $('#g-header').outerHeight() + 'px');

		/* the main gallery img is abs positioned. if it overlays the logos, push the logos down */
		var right_col_height = $('#gallery .col.right').height() + $('#gallery .col.right').offset().top;
		var logos_offset = Math.round($('#logos').offset().top);
		//alert(right_col_height + ' - ' + logos_offset);
		if (right_col_height > logos_offset) {
			var diff = (right_col_height - logos_offset) + 20;
			$('#logos').css('margin-top', diff + 'px');
		}
	}

	/* Home (promobox)
	 * scroll page to reveal bottom gallery boxes
	 */
	$('a.show-galleries').click(function() {
		$('html').scrollTop($('#top').outerHeight() + $('#content').outerHeight() - 50);
		return false;
	});

	/*
   * Home (slideshow)
	 * nivo slider: http://nivo.dev7studios.com
	 */
	var slider = $('#slider');

	if (slider.length) {
		slider.nivoSlider({
			effect:'fade',
			slices:1,
			animSpeed:500,
			pauseTime:4000,
			directionNav:true,
			directionNavHide:true,
			controlNav:true,
			keyboardNav:true,
			pauseOnHover:true,
			captionOpacity:0.8,
			beforeChange: function() {
				// remove the prev image bg cause imgs are transparent
				slider.css('background', 'none');
				}
		});
		
		/* combine the next/prev arrows with the num nav */
		slider.find('.nivo-controlNav').before('<div class="controls"></div>');
		slider.find('.controls').append($('#slider .nivo-prevNav'));
		slider.find('.controls').append($('#slider .nivo-controlNav'));
		slider.find('.controls').append($('#slider .nivo-nextNav'));
		
		/* update text for next & prev */
		slider.find('.nivo-prevNav').addClass('arrow prev').attr('title', 'Previous');
		slider.find('.nivo-nextNav').addClass('arrow').attr('title', 'Next');
	}

	/*
   * Home (gallery nav)
	 */
	var gallery = $('#nav-gallery');

	if (gallery.length) {
		$('#nav-gallery .row').hover(
			function() {
				$(this).addClass('active');
				$('#nav-gallery .row').not('.active').css('opacity', '0.5');
			},
			function() {
				$(this).removeClass('active');
				$('#nav-gallery .row').css('opacity', '1');
			}
		);
	}

});
