

$(window).load(function() {



	
	// flexDiv = $('#newsFeed');

	// if (flexDiv.fleXcroll) {
	// 	flexDiv.fleXcroll.setScrollPos(false,100);
	// 	flexDiv.fleXcroll.scrollContent(0, '.5p');
	// };
	
	// fleXenv.fleXcrollMain("#newsFeed");
	// fleXenv.updateScrollBars();
	

	var	hbtc = false,
			clickd = 'ground';
	
	
	scrollPage = function(){
		
		
		
		// fleXenv.updateScrollBars();
		
		var curDiv = $('#' + clickd),
				curOffset = curDiv.offset(),
				curH = curDiv.height(),
				totalH = $(window).height(),
				curDif = (totalH - curH)/2,
				windowPos = $(window).scrollTop(),
				divPos = curDiv.position(),
				mainTransTime = 2000,
				mtt4 = mainTransTime * .25,
				mtt34 = mainTransTime * .75,
				mtt5 = mainTransTime / 5;
		
		animNavTop = function(){
			
			$('ul#mainNav').animate({
				'top' : '-5%'
			}, mtt4, 'easeOutBack', function(){
				$('ul#mainNav').delay(mtt34).animate({
					'top' : '5%',
					'opacity' : '1'
				}, mtt5, 'easeOutBack');
			});
			
		};
		
		animNavBottom = function(){
		
			$('ul#mainNav').animate({
				'top' : '18%'
			}, mainTransTime, 'easeOutBack', function(){
				$('ul#mainNav').animate({
					'top' : '5%'
				}, mtt5, 'easeOutBack');
			});
			
		};
	
		if(curDif <= 0 && curOffset.top > 0){
			
			var bigDif = curH - totalH;
			
			$('html,body').animate({
				scrollTop: curOffset.top + bigDif
			}, mainTransTime);
			
			animNavTop(top);
		
			
		} else if(curDif < 0 && curOffset.top <= 0){
			$('html,body').animate({
				scrollTop: curOffset.top
			}, mainTransTime);
			
			animNavBottom();
				
		} else {
			$('html,body').animate({
				scrollTop: curOffset.top - curDif
			}, mainTransTime);
			
			if(windowPos < divPos.top) {
				animNavTop();
			} else {
				animNavBottom();
			};
			
		};
		
	};  // END SCROLL PAGE FUNCTION
	
	scrollPage();
	
	
	
	$('#portfolio').slideDown(1000);
	
	navChange = function(){
		$('.workItemFull').slideUp('slow');
		$('#portfolio').animate({
			'padding-top' : '225px'
		});
		$('.activeNav').removeClass('activeNav');
	};
	
	
	//BUTTON DEFINITIONS
	
	$('a.home').click(function(ev){
		clickd = 'ground';
		woopraTracker.pushEvent({name:'home'});
		navChange();
		$(this).addClass('activeNav');
		scrollPage();
		window.location.hash = '';
		ev.preventDefault();
 	   	ev.stopPropagation();
	});
	$('a.about').click(function(ev){
		clickd = 'cave';
		woopraTracker.pushEvent({name:'about'});
		navChange();
		$(this).addClass('activeNav');
		scrollPage();
		window.location.hash = 'about';
		ev.preventDefault();
 	   	ev.stopPropagation();
	});
	$('a.work').click(function(ev){
		clickd = 'space';
		woopraTracker.pushEvent({name:'work'});
		navChange();
		$(this).addClass('activeNav');
		scrollPage();
		window.location.hash = 'work';
		ev.preventDefault();
 	   	ev.stopPropagation();
	});
	$('a.contact').click(function(ev){
		clickd = 'core';
		woopraTracker.pushEvent({name:'contact'});
		navChange();
		$(this).addClass('activeNav');
		scrollPage();
		window.location.hash = 'contact';
		hbtc = true;
		ev.preventDefault();
 	   	ev.stopPropagation();
 	   	
	});
	
	//OUR WORK STUFF
	
	var curWork = 0;
	
	displayWork = function(cnt){
	
		var winHeight = $(window).height(),
				headHeight = 56,
				workDif = (winHeight - headHeight);
	
		$('ul#mainNav').fadeOut('slow');
		$('.workItem' + cnt).slideDown('slow');
		$('.workItemFull').tinyscrollbar();
		
		//alert(workDif);
		
		/*$('.scrollAble .track').css({
			'height' : workDif + 'px'
		});*/
		
		$('.scrollAble .viewport').css({
			'height' : workDif + 'px'
		});
		
		$('html,body').animate({
				scrollTop: 390
			}, 1000);
		$('#portfolio').animate({
			'padding-top' : '440px'
		});
		curWork = cnt;
	};
	
	
	$('a.closeBtn').click(function(ev){
		
		$('.workItemFull').slideUp('slow');
		$('#portfolio').animate({
			'padding-top' : '225px'
		});
		$('html,body').animate({
				scrollTop: 0
			}, 1000);
		
		ev.preventDefault();
 	   	ev.stopPropagation();
 	   	curWork = 0;
 	   	$('ul#mainNav').fadeIn('slow');
 	   	
 	   	$('.scrollAble .viewport').css({
			'height' : '400px'
		});
	});
	
	
	
	$('.work1 a').click(function(cnt){ // BTFE
		displayWork(1);
	});
	$('.work2 a').click(function(cnt){ // TBRC
		displayWork(2);
	});
	$('.work3 a').click(function(cnt){ // Red Wing
		displayWork(3);
	});
	$('.work4 a').click(function(cnt){ // Stunt Puppt
		displayWork(4);
	});
	$('.work5 a').click(function(cnt){ // Living with Dogs
		displayWork(5);
	});
	$('.work6 a').click(function(cnt){ // Join My Village
		displayWork(6);
	});
	
	//RANDOM SETTINGS
	
	$('#downToCore').click(function(ev){
		clickd = 'core';
		$('.activeNav').removeClass('activeNav');
		$('a.contact').addClass('activeNav');
		scrollPage();
		hbtc = true;
		ev.preventDefault();
 	   	ev.stopPropagation();
	});
	$("select").sb({
		animDuration: 250,
    	fixedWidth: true
	});
	
	$('#magicFlower').mouseenter(function(){
		$('#magicFlower img').fadeIn('slow');
	});
	$('#magicFlower').mouseleave(function(){
		$('#magicFlower img').fadeOut('slow');
	});
	$('#magicFlower').click(function(){
		clickd = 'phonebooth';
		$('.activeNav').removeClass('activeNav');
		scrollPage();
	});
	$('#phonebooth').mouseenter(function(){
		$('#phonebooth img').fadeIn('slow');
	});
	$('#phonebooth').mouseleave(function(){
		$('#phonebooth img').fadeOut('slow');
	});
	$('#happyStar').mouseenter(function(){
		$('#happyStar img').fadeIn('slow');
	});
	$('#happyStar').mouseleave(function(){
		$('#happyStar img').fadeOut('slow');
	});
	
	$('#happyStar').click(function(){
		
		
		if (hbtc) {
			$('#dudeContain').animate({
				'rotate': -50,
			}, 1800, 'easeOutElastic', function(){
				$('#dudeContain').animate({
					'top' : '-200px',
					'left' : '-200px',
					'width' : '20px',
					'height' : 'auto'
				}, 2800, 'easeOutBack');
			});
			$('ul#mainNav').fadeIn('slow');
		} else {
			$('#dudeContain').animate({
				'rotate': -50,
			}, 1800, 'easeOutElastic', function(){
				$('#dudeContain').animate({
					'top' : '0px',
					'left' : '0px'
				}, 450, 'easeOutElastic', function(){
					$('#dudeContain').animate({
						'rotate' : 50,
						'top' : '35px',
						'left' : '75px'
					}, 2200, 'easeOutBack', function(){
						$('#dudeContain').delay(350).animate({
							'rotate' : -40,
							'top' : '120%',
							'left' : '250px',
							'width' : '862px',
							'height' : 'auto'
						}, 500, function(){
							$('#dudeContain').css({'display' : 'none'});
							clickd = 'core';
							$('.activeNav').removeClass('activeNav');
							$('a.contact').addClass('activeNav');
							scrollPage();
							hbtc = true;
							$('ul#mainNav').fadeIn('slow');
						});
					});
				});
			});
		};
		
		$('#happyStar').css({'display' : 'none'});
		
	});
	
	
	
	//$('a.lightbox').lightBox();
	/*$('a.lightbox').click(function(ev){
		
		$('body').css({
			'overflow-y' : 'scroll'
		});
		ev.preventDefault();
 	   	ev.stopPropagation();
	});
	$('#lightbox-secNav a#lightbox-secNav-btnClose').click(function(ev){
		alert('closed');
		ev.preventDefault();
 	   	ev.stopPropagation();
	});*/
					
	
	init_map();
	init_contact();
	

	//$('a.lightbox').lightBox(); // Select all links with lightbox class
	
	$('#copyright').animate({
		'opacity' : '1'
	}, 2000);
	
	/*$('input#contact-submit').click(function(){
		$('form#contact').slideUp(500);
		
		//if it worked
		//$('#contactThanks').fadeIn(1000);
		//if it didn't
		$('#contactFailure').fadeIn(1000);
		
	});*/

	$('a.continue').click(function(ev){
		$('form#contact').slideDown(500);
		ev.preventDefault();
 	   	ev.stopPropagation();
 	   	$('.contMessage').fadeOut(500);
	});
	
	
	
	
	
	
	
	$('.slideshow').cycle({
		fx: 'fade',
		timeout: 7000,
		speed: 2000
	});
	
	
	
	/*window.onbeforeunload = function (ev) {
	//return false;
	//location.href="http://www.ideapark.com/beta";
		clickd = 'ground';
		navChange();
		$('a.home').addClass('activeNav');
		scrollPage();
		ev.preventDefault();
 	   	ev.stopPropagation();
		return false;
};*/
	
	

});

