//All javascript functionality for Hag Portfolio
//Coded by Hag Designs


//Load function for images
jQuery.fn.onImagesLoaded = function(_cb) { 
	return this.each(function() {
	var $imgs = (this.tagName.toLowerCase()==='img')?$(this):$('img',this),
		_cont = this,
			i = 0,
	_done=function() {
	if( typeof _cb === 'function' ) _cb(_cont);
	};

	if( $imgs.length ) {
	  $imgs.each(function() {
		var _img = this,
		_checki=function(e) {
		  if((_img.complete) || (_img.readyState=='complete'&&e.type=='readystatechange') )
		  {
			if( ++i===$imgs.length ) _done();
		  }
		  else if( _img.readyState === undefined ) // dont for IE
		  {
			$(_img).attr('src',$(_img).attr('src')); // re-fire load event
		  }
		}; // _checki \\

		$(_img).bind('load readystatechange', function(e){_checki(e);});
		_checki({type:'readystatechange'}); // bind to 'load' event...
	  });
	} else _done();
	});
};

//Functions to perform on load
$(document).ready(function() {
	//Menu Hover Animation
	$('nav ul li').hover(
		//Over
		function () {
			$(this).fadeTo('fast', 0.6);
		},
		//Off
		function () {
			$(this).fadeTo('fast', 1);
		}
	);
	
	//Image Preload
	if($('.preloadImage').length != 0){
		//Do Loading
		$('.preloadImage').onImagesLoaded(function(_img){
			$(_img).fadeIn(800);
		});
		
	}
	
	//Portfolio Image Hover Animation
	$('.item-image').hover(
		//Over
		function () {
			$(this).prepend('<div class="image-hover"></div>');
			$(this).children('a').fadeTo(300, 0.3);
			$(this).children('.image-hover').fadeIn('slow');
		},
		//Off
		function () {
			$(this).children('a').fadeTo(300, 1);
			$(this).children('.image-hover').fadeOut(300, function() {
				$(this).remove();
			});
		}
	);	
});
