/* =========== Begin Plugins =========== */

/** jQuery Shuffle (http://mktgdept.com/jquery-shuffle) * A jQuery plugin for shuffling a set of elements * v0.0.1 - 13 November 2009 * Copyright (c) 2009 Chad Smith (http://twitter.com/chadsmith) * Dual licensed under the MIT and GPL licenses. * http://www.opensource.org/licenses/mit-license.php * http://www.opensource.org/licenses/gpl-license.php
 * Shuffle elements using: $(selector).shuffle() or $.shuffle(selector) **/
(function(d){d.fn.shuffle=function(c){c=[];return this.each(function(){c.push(d(this).clone(true))}).each(function(a,b){d(b).replaceWith(c[a=Math.floor(Math.random()*c.length)]);c.splice(a,1)})};d.shuffle=function(a){return d(a).shuffle()}})(jQuery);

/* =========== End Plugins =========== */

$(document).ready(function(){
	KZA.Initialize();
});

Cufon.replace('nav a', { hover: true });

var KZA = {
	Initialize: function() {
		KZA.Add_Cufon();
		KZA.Homepage_Background.Initialize();
		KZA.Prepare_Project();
		KZA.Prepare_Project_List();
		KZA.Prepare_Complete_Project_List();
	},
	
	Add_Cufon: function() {
		// done outside of the main site engine to let Cufon do it's thang
	},
	
	Homepage_Background: {
		Speed: {
			In: 500,
			Pause: 4000,
			Out: 500
		},
		Timeout: 0,
		
		Initialize: function() {
			$('.home').each(function() {
				$(this).prepend('<ul id="background"/>');
				var Output = '';
				$('.ngg-galleryoverview .ngg-gallery-thumbnail a').each(function() {
					Output += '<li><img src="' + $(this).attr('href') + '" /></li>';
				});
				$('#background').html( Output );
				$('#background li').shuffle();
				
				$('.ngg-galleryoverview').remove();
				
				KZA.Homepage_Background.Check_Loaded();
				
				window.onresize = function() {
					KZA.Homepage_Background.Resize();
				};
				
				$('#background li img').click(function(){
					window.location.href = '/projects/';
				});

			});
		},
		
		Check_Loaded: function() {
			var Loaded = true;
			$('#background li img').each(function() {
				if( !this.complete ) {
					Loaded = false;
					return false;
				}
			});
			
			if( Loaded ) {
				KZA.Homepage_Background.Resize();
				
				$('#background').css('left', '0');
				$('#background li').hide();
				$('#background li:first-child').fadeIn( KZA.Homepage_Background.Speed.In, function() {
					KZA.Homepage_Background.Timeout = setTimeout( KZA.Homepage_Background.Cycle, KZA.Homepage_Background.Speed.Pause );
				});
			} else {
				setTimeout( KZA.Homepage_Background.Check_Loaded, 300 );
			}
		},
		
		Resize: function() {
			$('#background li img').each(function() {
				$(this).css('width', window.innerWidth + 'px');
				$(this).css('height', 'auto');
				
				if( $(this).height() < window.innerHeight ) {
					$(this).css('width', 'auto');
					$(this).css('height', window.innerHeight + 'px');
				}
			});
		},
		
		Cycle: function() {
			clearTimeout( KZA.Homepage_Background.Timeout );
			$('#background li:first-child').fadeOut( KZA.Homepage_Background.Speed.Out, function() {
				$('#background').append( $(this) );
				$('#background li:first-child').fadeIn( KZA.Homepage_Background.Speed.In, function() {
					KZA.Homepage_Background.Timeout = setTimeout( KZA.Homepage_Background.Cycle, KZA.Homepage_Background.Speed.Pause );
				});
				
			});
		}
	},
	
	Prepare_Project: function() {
		$('.page-template-project-details-php blockquote').each(function() {
			$(this).after( $('#project-title') );
			
			if( $(this).find('img').length > 1 ) {
				$(this).append('<a href="#previous" id="slideshow-previous"><span>&lt;</span></a><a href="#next" id="slideshow-next"><span>&gt;</span></a>');
				$('#slideshow-previous').click(function() {
					$('blockquote img:first').hide().appendTo('blockquote p').fadeIn('slow');
					return false;
				});
				$('#slideshow-next').click(function() {
					$('blockquote img:last').fadeOut('slow', function() {
						$(this).prependTo('blockquote p').show();
					});
					return false;
				});
			}
			
			$('#project-details').before( $(this) );
			
			$(this).find('img:last-child').delay(500).fadeIn('slow', function() {
				$('.page-template-project-details-php blockquote img').show();
			});
			
			// if there's no content in the sidebar, spread the content area out
			if( $('#project-sidebar').children().length == 0 ) {
				$('#project-details').css('width','auto');
			}

		});
	},
	
	Prepare_Project_List: function() {
		KZA.Filter_Project_List();
		$('#project-category').change(function(){
			KZA.Filter_Project_List();
		});
	},
	
	Filter_Project_List: function() {
		var Selected_Category = $('#project-category').val();
		$('#project-list article').removeClass('row').hide();
		$('#project-list article[rel~=' + Selected_Category + ']').each(function(i){
			if( i % 3 == 0 ) {
				$(this).addClass('row');
			}
			$(this).delay(i * 100).fadeIn('slow');
		});
		
		$('#project-list article:visible').each(function(i) {
			
			//$(this).fadeIn('slow');
		});
	},
	
	Prepare_Complete_Project_List: function() {
		KZA.Filter_Complete_Project_List();
		$('#project-category-complete').change(function(){
			KZA.Filter_Complete_Project_List();
		});
	},
	
	Filter_Complete_Project_List: function() {
		var Selected_Category = $('#project-category-complete').val();
		$('#project-list-complete li').hide();
		$('#project-list-complete li[rel~=' + Selected_Category + ']').show();
	}
	
	
	
}
