function resizeWindow() {

}

$(window).bind("resize", resizeWindow);
$(document).ready(function(){
	
	var img = new Image();
	
	if ($(".portfolio").length == 0) {
		$('body').hide();
	}
	
	$("#email-thanks-message").delay(500).fadeOut(500);
	
	if (document.URL == "http://surface51.com/") {
		if (navigator.platform.indexOf("iPhone") != -1 || navigator.platform.indexOf("iPod") != -1) {
			if(confirm('Load our mobile website?')) {
				window.location = "http://m.surface51.com";
			} else { 
			
			}
		}
	}
		
	konami = new Konami();
	
	konami.code = function() {
		$.getScript('/_shared/js/asteroids.js', function() {
		
		});
	}
	
	
	 konami.load();
	
resizeWindow();

/*
var emailOpen = false;
$(".email a, #nevermind, #closeform a").click(function() {
	
	if ( $.browser.msie && $.browser.version == 8) {
		window.location = "/contact-form"
		return false;
	}
	
	if (emailOpen) {
		$("#footer").removeClass("override");
		$(".contact-info").animate({marginTop:"0px", height:"88px", paddingTop:0});
		$(".contact-info .backgroundarea").fadeOut();
		$("#closeform").animate({opacity:0}).hide();
		emailOpen = false;
	} else {
		$("#footer").addClass("override");
		
		$(".contact-info").animate({marginTop:"-420px", height:"508px", paddingTop:"10px"});
		$(".contact-info .backgroundarea").fadeIn();
		$("#closeform").delay(500).show().animate({opacity:1});
		
		emailOpen = true;
	}
	return false;
});
*/
if (isiPhone()) {
	var image = '<img src="/_shared/images/feature_iphone.jpg" />';
	var newSlide = '<div class="slide"><div class="slidecontent"><a href="/" class="button1">desktop version <span class="icon"></span></a><a href="http://m.surface51.com" class="button2">mobile version <span class="icon"></span></a>'+image+'</div></div>';
	
	if (navigator.platform.indexOf("iPad") != -1) {
		image = '<img src="/_shared/images/feature_ipad.jpg" />';
		newSlide = '<div class="slide"><div class="slidecontent ipad"><a href="/" class="button1">desktop version <span class="icon"></span></a><a href="http://m.surface51.com" class="button2">mobile version <span class="icon"></span></a>'+image+'</div></div>';
	}
	
		
	$("#slides #firstslide").after(newSlide);
	
	var newSlideMenu = '<a href="#" alt="">Mobile Layout</a>';
	$("#slideshow #menu #firstmenu").after(newSlideMenu);
}

$(".eightball a").click(
	function() {
		if ($('html').hasClass('ie7') || $('html').hasClass('ie8')) {
			return true;
		}
				
		if ($("#darkness").length == 0) {
			$("body").prepend("<div id='darkness'><div id='thickbox'></div><div id='eightballstuff'></div><div id='everythingelse'></div></div>");
		}
		
		
		$("#eightballstuff").load("/8ball/?id="+Math.random()+" #pagecontent", function() {
			$("#darkness").animate({opacity:1}, 500, "easeInOutExpo");
			$("#eightballstuff").addClass("loaded");
			initEightBall();
			
			$("#everythingelse, a#closeeightball, #thickbox").click(function() {
				$("#darkness").animate({opacity:0}, 500, "easeInOutExpo", function() {
					$("#darkness").remove();
				});
				return false;
			});
			
		});
		return false;
	}
);


$("a#map").click(
	function() {
		
	
		
		if ($("#darkness").length == 0) {
			$("body").prepend("<div id='darkness'><div id='thickbox'></div><div id='eightballstuff'></div><div id='everythingelse'></div></div>");
		}
		
		$("#eightballstuff").load("/map/1dasdsdfef #pagecontent", function() {
			$("#darkness").animate({opacity:1}, 500, "easeInOutExpo");
			$("#eightballstuff").addClass("loaded");
			$("#everythingelse, a#closeeightball, #thickbox").click(function() {
				
				$("#darkness").animate({opacity:0}, 500, "easeInOutExpo", function() {
					$("#darkness").remove();
				});
				return false;
			});
			
			$("#mapclip").gMap({ markers: [{ address: "309 S Neil Champaign, IL",
			                              icon: { image: "/_shared/images/map_pin.png",
										                                      iconsize: [46, 61],
										                                      iconanchor: [20,61],
										                                      infowindowanchor: [12, 0] } }],
			                  address: "309 S Neil Champaign, IL",
			                  zoom: 15 });			
		});
		return false;
	}
);

letterSpacing = {
	' ': '75',
	A: '25',
	B: '-50',
	C: '-125',
	D: '-200',
	E: '-275',
	F: '-350',
	G: '-425',
	H: '-500',
	I: '-575',
	J: '-650',
	K: '-725',
	L: '-800',
	M: '-875',
	N: '-950',
	O: '-1025',
	P: '-1100',
	Q: '-1175',
	R: '-1250',
	S: '-1325',
	T: '-1400',
	U: '-1475',
	V: '-1550',
	W: '-1625',
	X: '-1700',
	Y: '-1775',
	Z: '-1850',
	0: '25',
	1: '-50',
	2: '-125',
	3: '-200',
	4: '-275',
	5: '-350',
	6: '-425',
	7: '-500',
	8: '-575',
	9: '-650'
};

var words = ['   GRACE50',
			'  SANITY49',
			'   SHARP48',
			'ORIGINAL47',
			'ARTISTIC46',
			'ELEGANCE45',
			'    LOVE44',
			'  WARMTH43',
			' MEANING42',
			'   MOXIE41',
			'  REASON40',
			'  WISDOM39',
			'   ORDER38',
			'   VERVE37',
			'   HONOR36',
			' FINESSE35',
			' HARMONY34',
			'   TASTE33',
			'  PURITY32',
			'   CHARM31',
			'  ALLURE30',
			'   HUMOR29',
			'     JOY28',
			'   UNITY27',
			'  ENERGY26',
			'INVITING25',
			'   FOCUS24',
			'   TRUTH23',
			'   PEACE22',
			' EMOTION21',
			'   DRIVE20',
			'  COFFEE19',
			' CLARITY18',
			'  DESIRE17',
			'   FAITH16',
			' INSIGHT15',
			' BALANCE14',
			'     WIT13',
			'  SPIRIT12',
			' PASSION11',
			'    MIND10',
			'   HEART09',
			'  VISION08',
			'  WONDER07',
			'   STYLE06',
			' MESSAGE05',
			'    SOUL04',
			'  BEAUTY03',
			' THOUGHT02',
			'     FUN01'];


var currentWord = null;
var logoInterval = null;


var $letters = $('#logo span');
var pageTitle = "NONE";

init = function() {
	
	
	if ($.cookie('surface') != null) {
		$("#mainclass").addClass($.cookie('surface'));
		var string = $.cookie('surface');
		$("#switcher .active").removeClass("active");
		$("#switcher #"+string).addClass("active");
	}
	
	$('.switcher').find("a").each(function() {
		$(this).click(function(){
			$('.switcher').find("a").each(function() {
				$(this).removeClass("active");
			});
			$("#mainclass").removeClass("one");
			$("#mainclass").removeClass("two");
			$("#mainclass").removeClass("three");
			$("#mainclass").removeClass("four");
			$("#mainclass").removeClass("five");
			$("#mainclass").removeClass("six");
			$("#mainclass").removeClass("seven");
			$("#mainclass").addClass($(this).attr("id"));
			$(this).addClass("active");
			$.cookie('surface',$(this).attr("id"), { path: "/" });
			$('html, body').animate({scrollTop:0}, 'slow');
			

			
			return false;
		});
	});
	
	$('#logo').hover(
		function() {
			setLogoWord();
			logoInterval = setInterval("setLogoWord()", 3000);
		},
		function() {
			clearInterval(logoInterval);
			setLogoWord(' SURFACE51');
		}
		
	);
	
	var defaultMessage = $("#message h3").html();
	var currentMessage = "three";
	
	$("#quote-control a").click(
		function () {
			$("#quote-control a.active").removeClass("active");
			$(this).addClass("active");
			var thisNum = ($(this).index() * 121 * -1) - 18;
			$("#message h3 blockquote:first-child").stop().animate({marginTop: thisNum}, 500, "easeInOutExpo");
			return false;
		}
	);
	
	
	$('#messages a').click(
		function(){
			if (currentMessage != $(this).attr("alt")) {
				currentMessage = $(this).attr("alt");				
				if ($(this).html() != "") {
					var newString = String($(this).html()).replace("<em", "<a");
					newString = newString.replace("</em>", "</a>");
					$("#message h3:nth-child(2)").html(newString);
				} else {
					$("#message h3:nth-child(2)").html(defaultMessage);
				}
				$("#message .message-viewer").append("<h3></h3>");
				if (currentMessage == "one") {
					var thisNum = ($("#quote-control a.active").index() * 121 * -1) - 18;
					$("#message h3 blockquote:first-child").css({marginTop: thisNum});
					$("#quote-control").show().addClass("active").css({opacity:0}).animate({opacity:1}, 125);
				//	$("#message h3:eq(2)").css({borderLeft:"7px solid rgba(149, 64, 19, .6)"});
				} else if (currentMessage == "two") {
					$("#quote-control").removeClass("active").animate({opacity:0}, 125);
				//	$("#message h3:nth-child(2)").css({borderLeft:"7px solid rgba(0, 122, 166, .3)"});
				} else if (currentMessage == "three") {
					$("#quote-control").removeClass("active").animate({opacity:0}, 125);
				//	$("#message h3:nth-child(2)").css({borderLeft:"7px solid rgba(0, 0, 0, .6)"});
				}
				$("#message h3:nth-child(1)").stop().animate({marginLeft: '-568px'}, 1000, "easeInOutExpo").delay(100, 
					function(){
						$(this).remove();
					}
				);
			}
			return false;
		}
	);
	
	$('#footer-bottom-tab').click(
		function() {
			if ($('#footer-bottom').position().top > 500) {
				$('#footer-bottom').stop().animate({bottom: '0px', leaveTransforms:true}, 500);
			} else {
				$('#footer-bottom').stop().animate({bottom: '-705px', leaveTransforms:true}, 250);
			}
			return false;
		}
	);
	
	/*
	$('a.transition').click(
		function() {
			
			pageTitle = $(this).attr("title");
			transitionPage($(this).attr("href"), "none", false);
			return false;
		}
	);
	*/
	if ($("#desktop").css("display") == "block") {
		runSlideShow($('#slideshow'));
	}
	
	$(".transition-hide").hide().fadeIn(255);

}

transitionPage = function(href, title, triggerChange) {
	var thisHref = href;
	$("#loading h2").text("Loading "+pageTitle);
	$(".transition-hide").stop().animate({opacity:0}, 250, "easeInOutExpo");
	$("#loading").stop().animate({opacity:1}, 500, "easeInOutExpo");
	$("#fakefooter").stop().animate({marginTop:'-420px'}, 500, "easeInOutExpo");
	$("#pagecontent").stop().animate({height: '40px'}, 500, "easeInOutExpo", function (){
		$(this).load(thisHref +" #pagecontent", function(response, status, xhr) {
			if (status == "error") {
				$(this).html("<div id='error' class='center-grid'><h3>Sorry.</h3><p>The page you selected does not exist.</div>");
			}

			$("#loading").stop().animate({opacity:0}, 500, "easeInOutExpo");
			$("#pagecontent").animate({height: '455px'}, 500, "easeInOutExpo");
			$("#fakefooter").stop().animate({marginTop:'0'}, 500, "easeInOutExpo");
			$(".transition-hide").hide().delay(500).fadeIn(255);
		});
		
	});
}

setLogoWord = function(logoWord) {
	if (logoWord == null) {
		var newWord = null;
		do {
			newWord = Math.floor(Math.random()*words.length);
		} while (newWord == currentWord);
		currentWord = newWord;
		logoWord = words[currentWord];
	}

	$.each(logoWord, function(key, value){
		$($letters[key]).stop().animate({top: letterSpacing[value]+'px', leaveTransforms:false}, 1000);
	});
}


runSlideShow = function(target) {
	$(".slide").hide();
	var currentIndex = 0;
	var currentAnim = false;
	var currentInterval = null;
	
	$('.slide:eq(0)').show();
	
	
	
	$("#menu a:eq(0)").addClass("active");
	
	$('#menu a').click(
		function() {
			if (currentIndex == $(this).index()) { return false; }
			clearInterval(slideshowInterval);
			goToSlide($(this).index());
			return false;
		}
	);
	

	var s51SlideTimeout = null;
	s51SlideAnimate = function(idx){
		$(".slidecontent h5 span").eq(idx).stop().animate({opacity:1, marginTop:"-5px"},800,"easeInOutBack");
		var delay=175;
		if (idx+2 == $(".slidecontent h5 span").length) { delay = 675; }
		s51SlideTimeout = setTimeout(function() {s51SlideAnimate(idx+1)}, delay);
	}
	
	startS51SlideAnimation = function() {
		clearTimeout(s51SlideTimeout);
		$(".slidecontent h5 span").stop().css({opacity:0, marginTop:"12px"});
		s51SlideTimeout = setTimeout(function() {s51SlideAnimate(0)}, 1000);
	}

	startS51SlideAnimation();

	nextSlide = function(thisTarget, index) {
		goToSlide((currentIndex + 1) % $(".slide").length);
	};
	
	function toggleTopSlide () {
		if ($("#topslide div").length == 0) $("#topslide").hide();
		else $("#topslide").show();
	}
	
	var slideClosing = false;
	goToSlide = function(index) {
		var $newSlide = $("#slides .slide:eq("+index+")");
		var $oldSlide = $("#slides .slide:eq("+currentIndex+")");
		$("#menu a:eq("+index+")").addClass("active").siblings().removeClass("active");

		var zebra = 1;
		if (currentIndex == 0) slideClosing = true;
		
		if ($('html').hasClass('ie7')) { 
			$newSlide.show();
			$oldSlide.hide();
		} else {
			if ($('#topslide .closing').length > 0){
				$('#topslide .closing').each(function(index) {
					zebra = 2 * (0.5 - index%2);
					$(this).delay(50 * $(this).data('index')).animate({top: zebra * 336+'px'}, 250, "easeInExpo",
						function() {
							$(this).remove();
							toggleTopSlide();
						});
				});
			}
			$('#topslide').show();
			if (slideClosing) { $animated = $newSlide; }
			else { $animated = $oldSlide; $newSlide.show(); $oldSlide.hide(); }


			for( i=0; i < 15; i++){
				var sliceWidth = 960 / 15;
				var thisClip = $animated.clone().hide();

				thisClip
					.css({width: sliceWidth+'px', overflow:'hidden', left:i*sliceWidth+"px", top:0})
					.data('index', i)
					.addClass('slide'+index)
					.prependTo($("#topslide"))
					.find(".slidecontent").css({marginLeft: -i*sliceWidth+"px"});
				
				if (slideClosing) thisClip.css({top: zebra * 336+'px'}).addClass('closing');
				
				thisClip.show();
	
				if (slideClosing) {
					thisClip.delay(50 * i).animate({top:0}, 250, "easeInExpo", 
					function() {
						if ($(this).index() == 0) {
							slideClosing = false;
							$newSlide.show();
							$('#topslide .slide'+index).remove();
							toggleTopSlide();
						}
					});
				} else {
				    thisClip.delay(50 * i).animate({top: zebra * 336+'px'}, 250, "easeInExpo",
					function() {
						$(this).remove();
						toggleTopSlide();
					});
				}
			  	zebra = zebra * -1;
			}
		}

		if (index == 0) {
			startS51SlideAnimation();
		}
	
		currentIndex = index;
		return false;
	}
	
	//slideshowInterval = setInterval("nextSlide()", 8000);
}

init();

});

function iPhoneDebug(msg) {
	if (isiPhone()) {
		alert(msg);
	}
}

function isiPhone(){
    return (
        (navigator.platform.indexOf("iPhone") != -1) ||
        (navigator.platform.indexOf("iPod") != -1) ||
		(navigator.platform.indexOf("iPad") != -1)
    );
}

var slideshowInterval = null;

$(window).load(function() {
	slideshowInterval = setInterval("nextSlide()", 10000); 
//	$('html').css('background','url(/_shared/images/styles/1_new.jpg) top center repeat-y'); // fade in the elements when the image loads
    $('body').fadeIn();
	
} );



