jQuery(document).ready(function() {
	// easy toggle for categories
	jQuery('#triggerCatID').click(function() {
		jQuery(this).toggleClass('focus');
		jQuery('#headerStrip').animate({ height: 'toggle', opacity: '100'}, 100);
		return false;
	});
	jQuery('#triggerCatID2').click(function() {
		jQuery(this).toggleClass('focus');
		jQuery('#footerStrip').animate({ height: 'toggle', opacity: '100'}, 100);
		return false;
	});

	mk.sweeeeet_sidenav.init();
	mk.portfolio.init();
});

var mk = {
	
	sweeeeet_sidenav: {
		_is_active: false,
		init: function() {
			jQuery('nav#nav a').click(function() { 
				var targ = jQuery(this).attr('href'), pos = jQuery('section'+targ).position().top - 1;
				jQuery('html,body').animate({scrollTop: pos}, 750);
				return false;
			});
			jQuery('div#controller a.backTop').click(function() { 
				jQuery('html,body').animate({scrollTop: 0}, 750);
				return false;
			});
			jQuery('a.upOne').click(function() { return mk.sweeeeet_sidenav.nav('up'); });
			jQuery('a.downOne').click(function() { return mk.sweeeeet_sidenav.nav('dn'); });
			
			jQuery('a.backTop').click(function() {
				jQuery('html,body').animate({ scrollTop: 0 }, 750);
				return false;
			});			
		},
		
		nav: function (direction) {
			if (! mk.sweeeeet_sidenav._is_active) {
				mk.sweeeeet_sidenav._is_active = true;
			var positions = []; var offset = 1;
			var targ_pos = 0, last_section_pos = 0, current_pos = Math.round(jQuery(window).scrollTop());
			jQuery('section').each(function() { positions.push( Math.round(jQuery(this).position().top) ); });
			for (i = 0; i < positions.length; i++) {
			   var pos = positions[i];
			   if ('up' == direction) {
				   if (pos <= current_pos) { last_section_pos = pos; }
				   else if (pos > current_pos) { targ_pos = last_section_pos; }
				   if (pos <= current_pos && positions.length - 1 == i) { targ_pos = pos; }
			   }
				else if ('dn' == direction) {
				   if (current_pos + offset < pos) { targ_pos = pos; break; }
			   }
		   }
		   
		   jQuery('html,body').animate({ scrollTop: targ_pos - offset}, 750,function() { mk.sweeeeet_sidenav._is_active = false; });
		  }
		  return false;
		}
	},
	
	
	
	portfolio: {
		_is_loading:        false,
		_is_active_gallery: false,
		
		init: function() {
			jQuery('#portfolioNav a').each(function(index) {
				jQuery(this).click( function() { 
					mk.portfolio.gallery.load( (index+1) );
					jQuery(this).parents('ul').find('a').removeClass('selectedPortfolio'); jQuery(this).addClass('selectedPortfolio');
				});
			});
			jQuery('#portfolioNav a:first').click();
		},
		gallery: {
			load: function(id) {
				if (!mk.portfolio._is_loading && mk.portfolio._is_active_gallery.gid != id) {				
					mk.portfolio._is_loading = true;

					jQuery.post(
						'/work-asynch', { nextgen: true, asynch: true, gid: id },
						function(data){
							var o = eval('('+data+')');
							if (o.pieces && o.pieces.length > 0) {
								mk.portfolio._is_active_gallery = o;
								mk.portfolio.thumbnails.load(o);
								mk.portfolio.piece.load(o.pieces[0].pid);
							}
							mk.portfolio._is_loading = false;
						}
					);
				}
			},
			navigate: function() {
				
			}
		},
		thumbnails: {
			load: function(o) {
				var selector = 'ul.ngg-gallery-list';
				// reset
				jQuery(selector).find('li[id^=ngg-image-]').remove();
				// populate new
				var html = '';
				for (var i=0; i<o.pieces.length; i++) {
					var piece = o.pieces[i];
					html += '<li id="ngg-image-'+piece.pid+'" class="ngg-thumbnail-list '+(i==0?'selected':'')+'">' + "\n";
					html += '<a href="javascript:;" onclick="mk.portfolio.piece.load('+piece.pid+');"';
					html += ' title="'+piece.alttext+'">';
					html += '<img src="'+piece.thumbURL+'" alt="'+piece.alttext+'"';
					html += ' width="'+piece.dimensions.thumbnail.width+'" height="'+piece.dimensions.thumbnail.height+'" />' + "\n";
					html += '</a></li>' + "\n";
				}
				jQuery(selector).append(html); //output
			}
		},
		piece: {
			load: function(pid) {
				var selector = 'div.ngg-galleryoverview';
				var o = false;
				for (var i=0; i<mk.portfolio._is_active_gallery.pieces.length; i++) {
					if (mk.portfolio._is_active_gallery.pieces[i].pid == pid) {
						o = mk.portfolio._is_active_gallery.pieces[i]; break;
					}
				}
				if (o) {
					jQuery(selector).find('div.description h5').html(o.alttext);
					jQuery(selector).find('div.description p:first').html(o.description);
					var html = '<a href="'+o.external_url+'" title="'+o.alttext+'" target="_tat">';
					html += '<img src="'+o.imageURL+'" alt="'+o.alttext+'"';
					html += ' width="'+o.dimensions.main.width+'" height="'+o.dimensions.main.height+'" /></a>' + "\n";
					
					// adjust thumbnail selection
					jQuery(selector).find('li#ngg-image-'+o.pid).siblings().removeClass('selected').end().addClass('selected');					
					jQuery(selector).find('div.pic').html(html); //output
				}				
			}
		}	
	}
}
