/*
 * 2meter3 scripts
 */

// hide blog navigation (older/newer posts) if emtpty
// wordpress outputs the wrapper even if no navigation is required
if ($('.blog-navigation a').length == 0) {
	$('.blog-navigation').hide();
}

// show feed options in footer
$('#footer .feeds').hover(function() {
	$(this).find('ul').show();
}, function() {
	$(this).find('ul').hide();
});

// show featured projects from projects page in a rotating container
$.fn.featuredProjects = function() {
	return $(this).each(function() {
		var $wrapper = $(this),
			$container = $('<div class="container"/>').appendTo($wrapper),
			$projects,
			projectsSelector = ' .featured',
			projectsUri = 'http://' + location.host +  '/show/my-projects ' + projectsSelector,
			position = 0,
			duration = 7000,
			interval,
			play = true;
		
		// load featured projects
		$container.load(projectsUri, function(x) {
			$projects = $container.find(projectsSelector);
			
			if ($projects.length < 2) {
				return;
			}
			
			// show container
			$wrapper.removeClass('hidden');
			$container.animate({height: '94px'}, 600);
			
			// clone first porject and append at the end
			$projects.filter(':first').clone().appendTo($container);
			
			// make sure scrollTop is set to 0
			$container.attr('scrollTop', 0);
			
			// start rotation
			interval = window.setInterval(rotate, duration);
			
			// stop rotation if mouse is hovering
			$container.hover(function() {
				window.clearInterval(interval);
			}, function() {
				rotate();
				interval = window.setInterval(rotate, duration);
			});
			
			// bind image swaps
			$projects.find('img.preview').imageSwapper();
		});
		
		// rotator function
		function rotate() {
			if (play) {
				// block more animation triggers (mouseout)
				play = false;
				
				// animate scrollTop
				$container.animate({scrollTop: '+=103'}, 400, function() {
					// reset to 0, if at the end
					if (position == ($projects.length - 1)) {
						$container.attr('scrollTop', '0');
					}
					
					// increment position
					position = ++position % $projects.length;
					
					// reset
					play = true;
				});
			}
		};
	});
}

// swap src and custom data-orig-image attributes of an image
$.fn.imageSwapper = function() {
	function swapImages(elem) {
		var tmp = elem.src;
		
		elem.src = elem.getAttribute('data-orig-image');
		elem.setAttribute('data-orig-image', tmp);
	}
	
	return $(this).each(function() {
		// show original images on hover
		$(this).hover(function() {
			swapImages(this)
		}, function() {
			swapImages(this);
		});
	});
}

// initialize featured projects and image swapper
$('#featured-projects').featuredProjects();
$('img.preview').imageSwapper();

// initialize colorbox
if ($.fn.colorbox) {
	$('.gallery').each(function(i) {
		 $('a', this).colorbox({rel:'group-' + i});
	});
}

// initialize puzzle
if ($.fn.jqPuzzle) {
	$('#portrait').jqPuzzle({
		shuffle: true,
		control: {
			shufflePieces: false,
			toggleNumbers: false,
			toggleOriginal: false,
			counter: false,
			timer: false
		},
		success: {
			fadeOriginal: false
		},
		style: {
			gridSize: 1
		}
	});
}

// initialize street view substitution
$('#streetview-container').one('click', function() {
	$(this).empty().append('<iframe width="648" height="380" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.de/maps?f=q&amp;source=embed&amp;hl=de&amp;q=Tenerife,+Spanien&amp;sll=48.226854,16.352602&amp;sspn=0.010235,0.021865&amp;ie=UTF8&amp;cd=1&amp;geocode=FeyxrwEddkIC_w&amp;split=0&amp;hq=&amp;hnear=Tenerife,+Spanien&amp;t=h&amp;layer=c&amp;cbll=28.326852,-16.855092&amp;panoid=QpgRflU-nqLQFGSpgZilRg&amp;cbp=12,60.44,,1,4.54&amp;ll=28.290947,-16.628215&amp;spn=0.001247,0.013883&amp;z=16&amp;iwloc=A&amp;output=svembed"></iframe><p><a href="http://maps.google.de/maps?f=q&amp;source=embed&amp;hl=de&amp;q=Tenerife,+Spanien&amp;sll=48.226854,16.352602&amp;sspn=0.010235,0.021865&amp;ie=UTF8&amp;cd=1&amp;geocode=FeyxrwEddkIC_w&amp;split=0&amp;hq=&amp;hnear=Tenerife,+Spanien&amp;t=h&amp;layer=c&amp;cbll=28.326852,-16.855092&amp;panoid=QpgRflU-nqLQFGSpgZilRg&amp;cbp=12,60.44,,1,4.54&amp;ll=28.290947,-16.628215&amp;spn=0.001247,0.013883&amp;z=16&amp;iwloc=A" class="map-more">Größere Kartenansicht anzeigen <span class="en">Show bigger map</span></a></p>');
});
