var browser		= navigator.appName;
var ver			= navigator.appVersion;
var thestart	= parseFloat(ver.indexOf("MSIE"))+1;
var brow_ver	= parseFloat(ver.substring(thestart+4,thestart+7)); 

if ((browser=="Microsoft Internet Explorer") && (brow_ver <= 7)) {
	_gaq.push(['_trackEvent', 'Pages', 'View', 'Old Browser']);
	window.location="http://vainmedia.com/redirect.html";
	exit();
}

var SPACE = 1;
var SKY = 0;
var GROUND = -1;
var LEVEL_HEIGHT = 2100;
var WORLD_HEIGHT = LEVEL_HEIGHT*3;
var CONTACT_HEIGHT = 550;
var LEVEL_ANIMATION_TIME = 500;
var CONTACT_ANIMATION_TIME = 250;
var MAX_CLOUDS = 10;

var active_plane = "";
var contact_visible = false;
var current_level = 0;
var switch_flipped = false;
var num_clouds = 0;
var cloud_generator;
var heli_animation;
var brand_animation;
var timer_count = 0;	
var is_apple = false;

if(	(navigator.userAgent.match(/iPad/i))   ||
	(navigator.userAgent.match(/iPhone/i)) || 
	(navigator.userAgent.match(/iPod/i)))  {
	is_apple = true;
}

/*
window.addEventListener('load', function() {
    setTimeout(scrollTo, 0, 0, 1);
}, false);
*/

// On window resize..
$(window).resize(function() {
	verticallyAlign();
	collapseUniverse();
	realignStripe();
		
	$('#universe').css("height", $(window).height());
	if(!contact_visible)
		$('#contact_container').css("top", $(window).height() + 500);
});


// As soon as page is ready..
$(document).ready(function(){
	verticallyAlign();
	collapseUniverse();
	realignStripe();
	startSkyAnimations();
  	
  	$('#universe').css("height", $(window).height());
	if(!contact_visible)
		$('#contact_container').css("top", $(window).height() + 500);

	$('.brand_image').each(function(index) {
    	$(this).attr("onmouseover", "flip_brand(this)");
    	$(this).css("opacity", "0");
  	});
  	
  	if(is_apple) {
		$('.plane').each(function(index) {
    		$(this).removeAttr("onmouseover");
    		$(this).removeAttr("onmouseout");
  		});
  		$('.banner').each(function(index) {
    		$(this).removeAttr("onmouseover");
    		$(this).removeAttr("onmouseout");
  		});
	}
	
	$('body').removeClass('bigbang');
});

function realignStripe() {
	if(($(window).width()-1024) > 0)
		$('.title_bar').css("left", ($(window).width()-1024)/2 + 15);
	else $('.title_bar').css("left", 15); 
	
	if($(window).height()-768 > 0) {
		$('.level#sky_level .title_bar').css("background-position", "center " + (($(window).height()-768)/2 + (2100-$(window).height())/2 + 25) + "px");
		$('.level#ground_level .title_bar').css("background-position", "center " + (2100-768+25) + "px");
	}
	else {
		$('.level#sky_level .title_bar').css("background-position", "center 691px");
		$('.level#ground_level .title_bar').css("background-position", "center " + (2100-768+25) + "px");
	}
}

// Used to vertically align divs
function verticallyAlign() {
	
	if(contact_visible) {
		var contact_position = getContactPosition();
		$('#contact_container').css('top', contact_position);
		$('#contact_backing').css('top', contact_position);
	}
	
	if(current_level == SKY) {
		var level_position = getLevelPosition(SKY);
		$('#world').css('top', level_position);
	}	
	else if(current_level == GROUND) {
		var level_position = getLevelPosition(GROUND);
		$('#world').css('top', level_position);
	}
}


function collapseUniverse() {
	// Realign ground background
	if(is_apple) {
		$('.level#ground_level').css('background', 'white url(images/sequoia/m_ground_bg.png) no-repeat center bottom');
	} else {
		if($(window).width() <= 1024)
			 $('.level#ground_level').css('background', 'white url(images/sequoia/ground_bg.png) no-repeat -1030px bottom');
		else $('.level#ground_level').css('background', 'white url(images/sequoia/ground_bg.png) no-repeat center bottom');
	}
}


// Animate to level
function goToLevel(level) {
	current_level = level;
	controlNavDisplay(level);
	$('#atmosphere').removeClass('hide');
	var position = 0;
	if(level == SPACE) {
		//move to top
		position = getLevelPosition(SPACE);
	}
	else if(level == SKY) {
		//move to center
		position = getLevelPosition(SKY);
	}
	else if(level == GROUND) {
		//move to baseline
		position = getLevelPosition(GROUND);
	}
	$('#world').animate({
		top: position
	}, LEVEL_ANIMATION_TIME, function() {
		$('#atmosphere').addClass('hide');
	});	
	hideContact();
}


// Show contact info
function showContact(level) {
	if(!contact_visible) {
		if(active_plane != "")
 			deactivate_plane(active_plane);
		var position = 0;
		position = getContactPosition();
		$('#contact_backing').css('top', position);
		$('#contact_backing').addClass('show');
		$('#contact_container').animate({
			top: position
		}, CONTACT_ANIMATION_TIME, function() {
			contact_visible = true;
		});
	}
}


// Hide contact info
function hideContact() {
	if(contact_visible) {
		$('#contact_backing').removeClass('show');
		$('#contact_container').animate({
			top: ($(window).height() + 500)
		}, CONTACT_ANIMATION_TIME, function() {
			contact_visible = false;
		});	
	}
}


// Get distance to move world from top
// Set limits to account for universe collapse
function getLevelPosition(level) {
	var position = 0;
	if(level == SKY) {
		position = ($(window).height() - WORLD_HEIGHT) / 2;
		var limit = -(LEVEL_HEIGHT + (LEVEL_HEIGHT - 768)/2);
		if(position < limit) position = limit;
	}	
	else if(level == GROUND) {
		position = $(window).height() - WORLD_HEIGHT;
		var limit = -(LEVEL_HEIGHT * 2 + (LEVEL_HEIGHT - 768));
		if(position < limit) position = limit;
	}
	else position = 0;
	return position;	
}


// Get distance to move contact info from top
function getContactPosition() {
	var position = 0;
	position = ($(window).height() - CONTACT_HEIGHT) / 2;
	if($(window).height() < 768) position = (768 - CONTACT_HEIGHT) / 2;
	return position;
}


// Control display of navigation cues
function controlNavDisplay(level) {
	if(level == SPACE) {
		$('#top_right').css('visibility', 'hidden');
		$('#bot_right').css('visibility', 'visible');
		$('#goto_skytop').css('visibility', 'hidden');
		$('#goto_skybot').css('visibility', 'visible');
		$('#goto_space').css('visibility', 'hidden');
		$('#goto_ground').css('visibility', 'visible');
	}
	else if(level == SKY) {
		$('#top_right').css('visibility', 'visible');
		$('#bot_right').css('visibility', 'visible');
		$('#goto_skytop').css('visibility', 'hidden');
		$('#goto_skybot').css('visibility', 'hidden');
		$('#goto_space').css('visibility', 'visible');
		$('#goto_ground').css('visibility', 'visible');
	}
	else if(level == GROUND) {
		$('#top_right').css('visibility', 'visible');
		$('#bot_right').css('visibility', 'hidden');
		$('#goto_skytop').css('visibility', 'visible');
		$('#goto_skybot').css('visibility', 'hidden');
		$('#goto_space').css('visibility', 'visible');
		$('#goto_ground').css('visibility', 'hidden');
	}
}


// Control sky level animations
function startSkyAnimations() {
	heli_animation = setInterval(function(){

	if(Math.random()*10 < 1) {
	    	var cloud_id = "cloud_" + timer_count;		
	    	$('<div class="cloud" id="'+cloud_id+'" style="left:'+($(window).width()+300)+'px"></div>').appendTo('#sky_level');
	    	num_clouds++;
	
			if(Math.round(Math.random())) $('#'+cloud_id).css("background-image", "url(images/sky/cloud_1.png)");
			else $('#'+cloud_id).css("background-image", "url(images/sky/cloud_2.png)");
			$('#'+cloud_id).css("top", ((Math.random()*(768-158)) + (2100-768)/2));
	    	$('#'+cloud_id).animate({
		    	left: -300
		    }, 7500, 'easeInQuad', function() {
		    	$(this).remove();
		    	num_clouds--;
			});			
		}
		timer_count++;
		
    }, 50);
    

    cloud_generator = setInterval(function(){

	}, 50);
    
    
}
function stopSkyAnimations() {
	clearInterval(heli_animation);
}


function flip_switch() {
	$('.brand_image').stop(true, true);
	clearInterval(brand_animation);
	switch_flipped = !switch_flipped;
	if(switch_flipped) {
		$('.brand_image').css('opacity', '1');
		$('#the_switch').addClass('poweron');
	}
	else {
		$('.brand_image').css('opacity', '0');
		$('#the_switch').removeClass('poweron');
	}
}

function flip_brand(brand) {
	if((switch_flipped && $(brand).css('opacity') == 1) ||
		(!switch_flipped && $(brand).css('opacity') == 0)) {
		if(switch_flipped) $(brand).css('opacity', '0');
		else $(brand).css('opacity', '1');
		brand_animation = setTimeout(function(){
			var opacity_value = 0;
    		if(switch_flipped) opacity_value = 1;
    		$(brand).animate({
	    		opacity: opacity_value
	    	}, { duration: 1000, easing: 'linear', queue: false });
	    }, 2000);
    }
}

function plane_clicked(plane_id) {
 	var plane = $('.plane_container#'+plane_id); 	
    var payload = $('.plane_container#'+plane_id+' .payload');
	if($(payload).css("visibility") == "hidden") {
 		if(active_plane != "" && active_plane != plane_id)
 			deactivate_plane(active_plane);
		active_plane = plane_id;
		activate_plane(plane_id);
	}
	if($(plane).css("top") == "127px") {
		deactivate_plane(plane_id);
	}
}

function activate_plane(plane_id) {
 	var plane = $('.plane_container#'+plane_id);
    var banner = $('.plane_container#'+plane_id+' .banner');
    var bb = $('.plane_container#'+plane_id+' .banner_border');
    var payload = $('.plane_container#'+plane_id+' .payload');
  
    $(payload).stop(true, true);
    $(payload).css("visibility", "visible");
    $(plane).css("z-index", "10");
    $(function () {
	    $(plane).animate({
	    	top: 127,
	    	left: 170
	    }, { duration: 2000, easing: 'easeOutQuad', queue: false });
	    $(banner).animate({
	    	width: '675px'
	    }, { duration: 1000, queue: false });
	    $(bb).animate({
	    	width: '677px'
	    }, { duration: 1000, queue: false });
	    $(payload).animate({
	    	width: '660px',
	    	height: '530px'
	    }, { duration: 1000, queue: false });
	});
}

function deactivate_plane(plane_id) {
 	if(plane_id != "") {
	 	var plane = $('.plane_container#'+plane_id);
	    var banner = $('.plane_container#'+plane_id+' .banner');
	    var bb = $('.plane_container#'+plane_id+' .banner_border');
	    var payload = $('.plane_container#'+plane_id+' .payload');
		var plane_ox = 0;
		var plane_oy = 0;
		var banner_w = 0; 
		if(plane_id == "algorithms")		{ plane_ox = 110; plane_oy = 215; banner_w = 250; }
		else if(plane_id == "design")		{ plane_ox = 85;  plane_oy = 330; banner_w = 150; }
		else if(plane_id == "games")		{ plane_ox = 65;  plane_oy = 520; banner_w = 150; }
		else if(plane_id == "social")		{ plane_ox = 485; plane_oy = 170; banner_w = 150; }
		else if(plane_id == "mobileapps")	{ plane_ox = 435; plane_oy = 375; banner_w = 150; }
		else if(plane_id == "webapps")		{ plane_ox = 580; plane_oy = 280; banner_w = 150; }
		else if(plane_id == "ux")			{ plane_ox = 310; plane_oy = 465; banner_w = 200; }
		else if(plane_id == "concepts")		{ plane_ox = 525; plane_oy = 555; banner_w = 175; }    
		
		$(plane).css("z-index", "4");
	    $(banner).css("overflow", "visible");
		$(function () {
		    $(plane).stop().animate({
		    	top: plane_oy,
		    	left: plane_ox
		    }, { duration: 2000, easing: 'easeOutQuad', queue: false });
		    $(banner).stop().animate({
		    	width: banner_w
		    }, { duration: 1000, queue: false });
		    $(bb).stop().animate({
		    	width: (banner_w+1)
		    }, { duration: 1000, queue: false });
		    $(payload).stop().animate({
		    	width: (banner_w-20),
		    	height: '0px'
			}, 1000, function() {
				$(payload).css("visibility", "hidden");
				$(plane).css("z-index", "3");
			})
	    });
	    //plane_id = ""; 
    }
}

function close_popups() {
	if(active_plane != "") {
		var plane = $('.plane_container#'+active_plane);
		if($(plane).css("top") == "127px") {
			deactivate_plane(active_plane);
		} 
	}
	hideContact();
}

function rollover_plane(plane_id) {
	$('#'+plane_id+' .banner_border').css('visibility', 'visible');
	$('#'+plane_id+' .plane_border').css('visibility', 'visible');
}

function rolloff_plane(plane_id) {
	$('#'+plane_id+' .banner_border').css('visibility', 'hidden');
	$('#'+plane_id+' .plane_border').css('visibility', 'hidden');
}
