swfobject.registerObject("yTube", "8.0", "/js/expressInstall.swf");

$(document).ready(function () {
	
	bikky = document.cookie;
	
	loadNav();
	loadFooter();					
	
	$(window).load(function () {
		$('#nav,#bgHack').height($('#mainContent').height());
	});
		
	$('#gameScreens a').click(showGameScreens);
	
	$('a.msgthread-link').click(scrollToMsg);
	
	$('.manual-config a').click(scrollToMsg);
	
	if ($('.important')) {
		//blink();
	}
	
});

function getCookie(name) { //Original Cookie function from old menu.js
	name += "="; // append '=' to name string
	var i = 0; // index of first name=value pair
	while (i < bikky.length) {
  		var offset = i + name.length; // end of section to compare with name string
  		if (bikky.substring(i, offset) == name) { // if string matches
   			var endstr = bikky.indexOf(";", offset); // locate end of name=value pair
			if (endstr == -1) endstr = bikky.length;
    		return unescape(bikky.substring(offset, endstr)); // return cookie value section
  		}
  		i = bikky.indexOf(" ", i) + 1; // move i to next name=value pair
  		if (i == 0) break; // no more values in cookie string
	}
	return null; // cookie not found
}

function loadNav() {

	var LoggedIn = getCookie("session") || 0;
	
	$('#nav').load('/includes/nav.html #navContent', function() {
		if (LoggedIn == 0) { 
			$('#navLoggedOut').show();
		} else {
			$('#navLoggedIn').show();
		}
		$('#cta01,#signUp01,#signUp02').click(accountOS);
		$('.has-subnav').click(showSubNav);
		
		if ((typeof isManual != 'undefined') && (isManual == true)) {
			$('.subnav-manual').show();
		}
		
		if ((typeof isBackstory != 'undefined') && (isBackstory == true)) {
			$('.subnav-backstory').show();
		}
	});
	
}

function loadFooter() {
	$('#footer').load('/includes/footer.html');
}

function showSubNav() {
	/*var subNav = this.className+'-subnav';
	$('.'+subNav).slideToggle('fast');
	return false;*/
	
	var subNav = $(this).next('div.subnav');	
	
	$('div.subnav').each(function() {
		$(this).slideUp();
	});
	
	if (subNav.is(':hidden')) {
		subNav.slideDown();
	} else {
		subNav.slideUp();
	}
}

function accountOS() {
	
	var os;
	
	if (navigator.appVersion.indexOf("Win") != -1 ) {
		os = 'win32';
	} else if (navigator.appVersion.indexOf("Mac") != -1 ) {
		os = 'macos';
	} else if (navigator.appVersion.indexOf("PowerPC") != -1 ) {
		os = 'osx';
	} else if (navigator.appVersion.indexOf("Linux") != -1 ) {
		os = 'linux';
	} else {
		os = 'linux';
	}
	
	window.location='/x/newacct?platform='+os;
	
	return false;

}

function showGameScreens() {
	var position = $(this).position();
	var posLeft = position.left;
	var posRight = position.right;
	
	var screenUrl = $(this).attr('href');
	
	//alert(screenUrl);
	
	$('#fullGameScreen img').attr({
		src: screenUrl
	});
	
	/*$('#screenClose').fadeIn(function() {
		$('#fullGameScreen img').load(function() {
			$('#fullGameScreen').fadeIn();
		});
	});*/
	
	$('#screenClose').fadeIn(function() {
		$('#fullGameScreen').fadeIn();
	});
	
	$('#screenClose').click(function() {
		$('#fullGameScreen').fadeOut(function() {
			$('#screenClose').fadeOut();
			$('#fullGameScreen img').attr({
				src: ''
				});
		})
	})
	
	
	
	return false;
}

function scrollToMsg() {
	var loc = this.href.substring(this.href.indexOf('#')+1);
	var pos = $('a[name="'+loc+'"]').offset();
	$('html,body').animate({scrollTop: (pos.top-25)}, 750);
}



// Youtube API Player Functions (not installed)

function onYouTubePlayerReady(id) {
	ytCh = document.getElementById("wooCh");
	
	//ytCh.cueVideoByUrl('http://www.youtube.com/v/fdulyrAkvX4');
	ytCh.cueVideoById('fdulyrAkvX4');
}

function play() {
	$('#video').fadeOut();
	//$('#yTube').css('z-index',1);
	if (ytCh) {
		ytCh.playVideo();
	}
}

function pause() {
	if (ytCh) {
		ytCh.pauseVideo();
	}
}

function stop() {
	$('#video').fadeIn();
	//$('#yTube').css('z-index',-1);
	if (ytCh) {
		ytCh.stopVideo();
	}
}

function blink() { //The blink html tag is deprecated, and not supported in all the major browsers, so we'll do it here.
	$('.important').show();
	$('.important').fadeOut(1000, blink)
}
