/*

	ILG FRAMEWORK
	
	GENERAL JS FUNCTIONS
	
	CREATED BY RYAN ILG
	HTTP://RYANILG.COM
		
	CREATED: Wed May  5 17:44:03 PDT 2010
	LAST UPDATE: Mon Oct 25 17:36:13 PDT 2010
	
*/

	$(document).ready(function() {
		
		var current = 0;
		
		$('html').addClass('js');
		
		/* DEFAULT BUTTON ACTIONS
		+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */
		
			$('.collection')
				.append('<ul class="controls"><li><a href="#" class="close">Close</a></li><li><a href="#" class="zoom zoom2x">Zoom</a></li></ul><div class="zoom zoom1x"><a href="#">Zoom</a></div>')
				.click(function(e){ $(this).triggerHandler('open'); e.preventDefault(); })
				
		
		/* OPEN A COLLECTION 
		+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */
		
			$('.collection').bind('open', function() { 
								
				// CLOSE ANY OPEN COLLECTIONS
				$(this).siblings('.x1, .x2').css('z-index', '10');//.find('.close').each(function(){ $(this).triggerHandler('close1x'); });
				
				// ADD THE ZOOM CLASS AND HIDE THE ZOOM ICON
				$(this).addClass("x1").find('.zoom1x').hide(); 
				
				// ANIMATE THE COLLECTION CONTAINER
				$(this).stop().animate({ height: '600px', width: '600px' }, 300, function() 
					{ 
						// DISABLE CLICK BIND
						$(this).unbind('click'); 
						
						// ANIMATE THE SECOND STAGE
						$(this).animate({ width: '900px' }, 600); 
						
						// SCROLL CONTENT INTO VIEW
						$.scrollTo($(this), 1000, { axis: 'y', easing: 'easeInOutCubic', offset: -50 }); 
						
						// SHOW THE CONTROLS
						$(this).find('.controls').animate({ right: '0' }, 600);
						
						// ENABLE BUTTONS
						$(this).find('.close').click(function(e){ $(this).triggerHandler('close1x'); e.preventDefault(); });
						$(this).find('.zoom2x').click(function(e){ $(this).triggerHandler('zoom2x'); e.preventDefault(); });
					}
				);
				
				// ANIMATE THE HERO IMAGE & ROW
				$(this).find('.hero').animate({ height: '600px', width: '600px' }, 250);
				$(this).parents('.row').animate({ height: '600px' }, 400);
				
			});
		
		
		/* CLICKING A CLOSE BUTTON (1X)
		+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */
		
			$('.close').bind('close1x', function(e) {
				$(this).parents('.collection').stop().animate({ width: '600px' }, 600, function() 
					{
						// DISABLE CLICK BIND
						$(this).unbind('click');
					
						// SHOW THE ZOOM ICONS
						$(this).find('.zoom1x').show();
						$(this).find('.zoom2x').show(300);
						
						// ANIMATE THE COLLECTION CONTAINER
						$(this).animate({ height: '450px', width: '450px' }, 300);
					
						// ANIMATE THE HERO IMAGE
						$(this).find('.hero').animate({ height: '450px', width: '450px' }, 400);
						
						// HIDE THE CONTROLS
						$(this).find('.controls').animate({ right: '-75px' }, 400);
						
						// ANIMATE THE ROW
						$(this).parents('.row').animate({ height: '450px' }, 250, function() {
							
							// REMOVE THE ZOOM CLASS
							$(this).find('.collection').removeClass('x1').removeClass('x2')
							
							// ENABLE THE CLICK ACTIONS
							.click(function(e){ $(this).triggerHandler('open'); e.preventDefault(); });
							
						});
					}
				);
			});
		
		
		/* CLICKING A ZOOM BUTTON (2X)
		+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */
		
			$('.zoom2x').bind('zoom2x', function() {
				
				// ANIMATE THE HERO IMAGE
				$(this).parents('.collection').find('.hero').fadeOut(300);
				
				// REPLACE THE CLOSE BUTTON ACTIONS
				$(this).parents('.collection').find('.close').unbind('click').click(function(e){ $(this).triggerHandler('close2x'); e.preventDefault(); });
				
				// HIDE THE ZOOM BUTTON
				$(this).animate({ left: 100 }, 600, function(){
					
					// ADD THE ZOOM CLASS
					$(this).parents('.collection').removeClass('1x').addClass('2x');
					
				});
			});
			
		
		/* CLICKING A CLOSE BUTTON (2X) 
		+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */

			$('.close').bind('close2x', function(){
				
				// ANIMATE THE HERO IMAGE
				$(this).parents('.collection').find('.hero').fadeIn(600, function(){
					
					// SHOW THE ZOOM BUTTON
					$(this).parents('.collection').find('.zoom2x').animate({ left: 0 }, 400);
					
					// ADD THE ZOOM CLASS
					$(this).parents('.collection').addClass('x1').removeClass('x2');
					
				});
				
				// REPLACE THE CLOSE BUTTON ACTIONS
				$(this).unbind('click').click(function(e){ $(this).triggerHandler('close1x'); e.preventDefault(); });
			});
		
			
		/* ADD SCROLL FOLLOWING TO BRANDING & NAVIGATION 
		+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */
		
			$('#branding, #navigation').scrollFollow({ speed: 500, easing: 'easeInOutCubic' });
		
						
		/* CONTACT 
		+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */
		
			$('.contact a').attr('href', '#TB_inline?height=290&amp;width=490&amp;inlineId=contact');
		
			
		/* ABOUT 
		+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */

			$('#about').height(0);
			$('.about').click(function(e) {
				
				// HIDE THE ABOUT NAV BUTTON
				$(this).stop().animate({ left: "-30px" }, 500, function() { 
					$(this).hide();
						
					// SHOW THE GRID NAV BUTTON
					$('.grid').stop().show().animate({ left: "0" }, 500);
				});
				
				// SHOW THE ABOUT WINDOW
				$('#about').stop().animate({height: '900px'}, 300, function(){
					
					// HIDE THE MAIN CONTENT
					$('#content').hide(300);
				});
				
				// SCROLL CONTENT INTO VIEW
				$.scrollTo($('body'), 1000, { axis: 'y', easing: 'easeInOutCubic' });
				
				// DISABLE DEFAULT ACTIONS
				e.preventDefault();
				
			});
			
			$('.grid').click(function(e) {
				
				// HIDE THE ABOUT NAV BUTTON
				$(this).stop().animate({ left: "-30px" }, 500, function() { 
					$(this).hide();
					
					// SHOW THE ABOUT NAV BUTTON
					$('.about').stop().show().animate({ left: "0" }, 500);
				});
				
				// SHOW THE GRID CONTENT
				$('#content').show(300, function() {
					
					// HIDE THE ABOUT WINDOW
					$('#about').stop().animate({ height: "0" }, 300);
				});
				
				// SCROLL CONTENT INTO VIEW
				$.scrollTo($('body'), 1000, { axis: 'y', easing: 'easeInOutCubic' });
				
				// DISABLE DEFAULT ACTIONS
				e.preventDefault();
				
			});
		
	});
	
	/* CLICKING THE UP OR DOWN ARROW 
	+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */
	
	$(function() {
		function scroll(direction) {

			var scroll, i, positions = [], here = $(window).scrollTop(), collection = $('.row');
			
			// SAVE THE COLLECTION POSTIONS
			collection.each(function() {
				positions.push(parseInt($(this).offset()['top'], 10)-50);
			});
			
			// CALCULATE WHERE WE ARE GOING
			for (i = 0; i < positions.length; i++) {
				if (direction == 'down' && positions[i] > here) { scroll = collection.get(i); break; }
				if (direction == 'up' && i > 0 && positions[i] >= here) { scroll = collection.get(i-1); break; }
			}
			
			// SCROLL
			if (scroll) { $.scrollTo(scroll, { duration: 900, axis: 'y', easing: 'easeInOutCubic', offset: -50 }); }
			
			return false;
		}
		
		// ENABLE THE BUTTONS
		$(".up, .down").click(function() {
			return scroll($(this).attr('class'));
		});

	});

