// Title: lifeinmono.net - javascript
// Author: Kalon Edser, http://www.kalonedser.com/
// Updated: 2 July 2011

jQuery(document).ready(function() {
	
	$('body').addClass('js');
	$('.png').ifixpng(); 
	$.preloadImages('http://static.lifeinmono.net/img/prettyPhoto/light_rounded/sprite.png');

	var addressHash = window.location.hash;

	$.fn.imageLoader = function() {
		return this.each(function() {
			var $thisImage = $(this).hide();
			var $loadIcon = $('<div>')
				.addClass('load-icon')
				.width($thisImage.width()).height($thisImage.height())
				.html('<img src="http://static.lifeinmono.net/img/prettyPhoto/light_square/loader.gif" />')
				.insertBefore($thisImage);

			if ($thisImage.hasClass('master')) { $('#imagenav').hide(); }

			function showImage() {
				$loadIcon.hide().remove();
				$thisImage.fadeIn(1000);
				if ($thisImage.hasClass('master')) { $('#imagenav').fadeIn(1500); }
			}

			$thisImage.each(function() {
				if ($thisImage[0].complete) {
					showImage();
				} else {
					$thisImage.bind('load', showImage);
				}
			});
		});
	}

	$('img:not(.gravatar)').imageLoader();
	
	// main nav hover
	$('#nav a, .backtotop a').hover(
	      function() {
	        $(this).css('background-position','0 0').animate({ opacity: 0 }, 'fast' );
	      }, 
	      function() {
	        $(this).animate({ opacity: 1 }, 'fast' );
	      }
	);

	function prevNext() {
		$('a.tooltip').hover(
			function() {
				if ($(this).hasClass('next')) { var $direction = 'right';} else { var $direction = 'left'; }
				$('.tip',this).stop(false,true).hide().show('drop', { direction: $direction, easing:'easeOutBack' }, 'slow');
			}, 
			function() {
				if ($(this).hasClass('next')) { var $direction = 'right';} else { var $direction = 'left'; }
				$('.tip',this).stop(false,true).show().hide('drop', { direction: $direction, easing:'easeInBack' }, 'normal');
			}
		);
	}
	// tooltips for browsers with opacity support
	if ($.support.opacity) {
		prevNext();
	}

	// window scroll effect for anchor links
	$.localScroll({ margin:true, easing:'easeInOutExpo' });
	$.localScroll.hash({ margin:true, easing:'easeInOutExpo' });
	
	function more(type) {
		if (type == 'info') $('#information').animate({ opacity: 1.0 }, 800).toggle('blind', { direction: 'vertical', easing:'easeInOutExpo' }, 1000);
		if (type == 'thumbs') $('#project-thumbs').animate({ opacity: 1.0 }, 800).toggle('blind', { direction: 'vertical', easing:'easeInOutExpo' }, 500);
	}

	// toggle display of information
	if ($('#information')) {
		$('#information').hide();
		$('#toggle').click(function() { more('info'); });
		if (addressHash == '#more') more('info');
	}

	// toggle display of project thumbs
	if ($('#project-thumbs')) {
		$('#project-thumbs').hide();
		$('#toggle2').click(function() { more('thumbs'); });
		if (addressHash == '#project-thumbs') more('thumbs');
	}

	// prettyPhoto controls
	$("a[rel^='large']").prettyPhoto({
		animation_speed: 'normal', /* fast/slow/normal */
		slideshow: 6000, /* false OR interval time in ms */
		autoplay_slideshow: false, /* true/false */
		opacity: 1, /* Value between 0 and 1 */
		show_title: false, /* true/false */
		allow_resize: true, /* Resize the photos bigger than viewport. true/false */
		default_width: 500,
		default_height: 344,
		counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
		theme: 'light_rounded', /* light_rounded / dark_rounded / light_square / dark_square / facebook */
		horizontal_padding: 20, /* The padding on each side of the picture */
		modal: false, /* If set to true, only the close button will close the window */
		deeplinking: true, /* Allow prettyPhoto to update the url to enable deeplinking. */
		overlay_gallery: false, /* If set to true, a gallery will overlay the fullscreen image on mouse over */
		keyboard_shortcuts: true, /* Set to false if you open forms inside prettyPhoto */
		ie6_fallback: true,
		social_tools: false /* html or false to disable */
	});

	// thumbs rotate
	$('.thumbs-rotate').each(function(i) {
		var delay_value = i * 250;
		$(this).cycle({ 
			fx:      'fade', 
			speed:    2500, 
			timeout:  7500,
			random: 1,
			pause: 1,
			delay: delay_value
		});
	});

	// open new window for external links
	$('.flickr .recent-images a').attr({rel: "external"});
	
	$('A[rel="external"]').click( function() {
        window.open( $(this).attr('href') );
        return false;
    });

});
