/***************************************
 * Main.js
 * jQuery functions for all/most pages in the site
 ***************************************/

/*
 * Styleswitcher
 * Change page styles using cookies/click, change banner text for geocities theme, and refresh cufon.
 */
function styleSwitcher(){
	// Change header text
	function bannerSwitcher() {
		if($("body").attr('class') == "geocities") {
			$("#banner").replaceWith('<h2 id="banner"><span class="scroller"><span class="lineOne">Bad design</span><br /><span class="lineTwo">speaks</span><br /><span class="lineThree">volumes</span></span><span class="scroller"><span class="lineOne">Bad design</span><br /><span class="lineTwo">speaks</span><br /><span class="lineThree">volumes</span></span></h2>');
			//$("#banner .lineOne").replaceWith('<span class="lineOne">Bad design</span>');
		} else {
			$("#banner").replaceWith('<h2 id="banner"><span class="lineOne">Good design</span><br /><span class="lineTwo">speaks</span><br /><span class="lineThree">volumes</span></h2>');
			//$("#banner .lineOne").replaceWith('<span class="lineOne">Good design</span>');
		}
	}
	// If cookie exists, use cookie value
	if($.cookie("styleSwitcherCookie")) {
		$("body").removeClass().addClass($.cookie("styleSwitcherCookie"));
		$("#styleSwitcher li a").removeClass("on").each(function(){
			if($(this).attr('id') == $.cookie("styleSwitcherCookie")) {
				$(this).addClass("on");
			}
		});
		bannerSwitcher();
	}
	// Toggle html class on click
	$("#styleSwitcher li a").click(function(){
		$("#styleSwitcher li a").removeClass("on");
		$("body").removeClass().addClass($(this).attr('id'));
		$(this).addClass("on");
		$.cookie("styleSwitcherCookie",$(this).attr('id'), {expires: 365, path: '/'});
		bannerSwitcher();
	});
}


/* 
 * Nav hover ribbon slideout effect
 * Animated transition for header nav link descriptionss. Vertical offset when hidden = -250px, Vertical offset when shown = -20px
 */
function navHover() {
	/* Nav hover description effect */
	$('header nav ul a .ribbon').css({'top':'-250px'}); // initially set the vertical offset in order to override the external CSS.
	$('header nav ul a').hover(
		function () {
			$(this).children('header nav ul a .ribbon').stop().animate({'top':'-20px'},400/*,'easeOutBack'*/);
		},
		function () {
			$(this).children('header nav ul a .ribbon').stop().animate({'top':'-250px'},300/*,'easeInQuad'*/);
		}
	);
}


/* 
 * Nav link vertical scroller 
 * Animated transition for header nav links. Vertical offset = 60, Time = 1000
 */
function verticalScroller() {
	function rwScroller(event){
		var $anchor = $(this);
		$('html, body').stop().animate({
			scrollTop: ($($anchor.attr('href')).offset().top) - 60
		}, 1000);
		event.preventDefault();
	}
	$('ul#nav a').bind('click',rwScroller);
	$('.scroll').bind('click',rwScroller);
}


/* 
 * Header opacity function 
 * Fade out header if page is scrolled down and header is not being hovered.
 */
function headerOpacity() {
	if(!(($.browser.msie) && ($.browser.version < 9.0))) { // turn it off in IE < 9
		var $hover = false;
		$(window).scroll(function(){
			var scrollTop = $(window).scrollTop();
			if(scrollTop != 0 && !$hover) {
				$('header').stop().animate({'opacity':'0.2'},400);
			} else {
				$('header').stop().animate({'opacity':'1'},200);
			}
		});
		$('header').hover(
			function (e) {
				var scrollTop = $(window).scrollTop();
				if(scrollTop != 0){
					$('header').stop().animate({'opacity':'1'},100);
				}
				$hover = true;
			},
			function (e) {
				var scrollTop = $(window).scrollTop();
				if(scrollTop != 0){
					$('header').stop().animate({'opacity':'0.2'},400);
				}
				$hover = false;
			}
		);
	}
}


/* 
 * Link New-Window Toggler
 * Make links open in new window when option is checked
 */
function linkToggler(){
	// Main toggle links function
	function toggleCheckbox(){
		if ($("#linkSwitcher li a").hasClass("off")) {
			// set cookie and class to "on"
			$.cookie("linkSwitcherCookie","on", {expires: 365, path: '/'});
			$("#linkSwitcher li a").removeClass().addClass("on");
		} else {
			// set cookie and class to "off"
			$.cookie("linkSwitcherCookie","off", {expires: 365, path: '/'});
			$("#linkSwitcher li a").removeClass().addClass("off");
		}
	}
	function toggleLinks(){
		if ($("#linkSwitcher li a").hasClass("on")) {
			// add external class and target="_blank" to all external links
			$('a').filter(function() {
			   return this.hostname && this.hostname !== location.hostname;
			}).addClass("external").attr({target: "_blank"});
		} else {
			// remove external class and target="_blank" from all external links
			$('a').filter(function() {
			   return this.hostname && this.hostname !== location.hostname;
			}).removeClass("external").attr({target: "_self"});
		}
	}
	// If cookie exists, use cookie value
	if($.cookie("linkSwitcherCookie")) {
		$("#linkSwitcher li a").removeClass().addClass($.cookie("linkSwitcherCookie"));
		toggleLinks();
	}
	// Toggle html class on click
	$("#linkSwitcher li a").click(function(){
		toggleCheckbox();
		toggleLinks();
	});
}


/* 
 * Konami Code
 * Add a class to the body when the Konami Code has been activated
 */
function konamiCode() {
	$.fn.konami = function(callback, code) {
		if(code == undefined) code = "38,38,40,40,37,39,37,39,66,65";
		return this.each(function() {
			var kkeys = [];
			$(this).keydown(function(e){
				kkeys.push( e.keyCode );
				if ( kkeys.toString().indexOf( code ) >= 0 ){
					$(this).unbind('keydown', arguments.callee);
					callback(e);
				}
			}, true);
		});
	}
	$(window).konami(function(){
		alert('Konami Code Activated');
		$("html").toggleClass("konami");
	});
}

/*
 * Guide Toggler
 * Add/remove photoshop guides by pressing Ctrl + H or Ctrl + ;.
 */
// If cookie exists, use cookie value
if($.cookie("guideCookie")) {
	$("html").removeClass("guides").addClass($.cookie("guideCookie"));
}
// Toggle html class on key press
function guideToggler(event){
	if (event.ctrlKey && (event.keyCode == 72 || event.keyCode == 59)) {
		if ($("html").hasClass("guides")) {
			$("html").removeClass("guides");
			$.cookie("guideCookie","", {expires: 365, path: '/'});
		} else {
			$("html").addClass("guides");
			$.cookie("guideCookie","guides", {expires: 365, path: '/'});
		}
		return false;
	}
}

/* 
 * Website config box
 */
// Enable config button popout effect
function popoutToggle() {
	if ($("#config").css("top") == "-520px") {
		$("#config").stop().animate({top: "100px"}, "fast");
		$("#configOverlay").stop().fadeIn("fast");
	} else {
		$("#config").stop().animate({top: "-520px"}, "medium");
		$("#configOverlay").stop().fadeOut("medium");
	}
}
function configPopout(){
	$(".navConfig").toggle(popoutToggle, popoutToggle);
}
