/*
Scripts for woodwall
*/

function main_init() {
	//manageColorBars();
	
	// *** Resizes base fonts if desired fonts are not available
	//detectFonts(["Calibri, Corbel"]);
	
	// *** Header highlighting setup
	setHighlightTargets.bgWidth = $("div#highlight").width();
	setHighlightTargets();
	$("span#h3").mouseover();
	
	// *** Restyles pages that support Javascript
	backgroundSetup();
	
	// *** Round #sidebar > h3 corners
	if(document.getElementById("sidebar")) {
		$("#sidebar h3:first-child").corner("bl");
		$("#sidebar h3:last").corner("left").css("margin-top","2pc"); // *** CSS set here for IE6-
	}

	// *** Prevent cornering in IE8-
	var navapp = navigator.appVersion.split("MSIE ");
	if(navapp.length < 2 || parseInt(navapp[1]) >= 9)
		$("#super").corner("tlbr");

	
	// *** Ensure bottom_gradient is always at bottom of window if page isn't long enough to fill screen
	/*if($('body').height() < $(window).height())
		$('#bottom_gradient').css({position:"absolute",bottom:0});
	*/
	// red, green, blue
	// red, yellow, blue
	// red, yellow
	// blue, yellow
	// red, blue
	// green, purple
	// yellow, green, blue
	// ??? teal, blue
	// ??? Rainbow order
	// ??? Fill screen, then fade all out?
}

// *** Changes whether the background picture is drawn on the <body> or the #wrapper
// *** This defaults to #wrapper when JS is disabled
// *** Adds a covering black bottom for short pages
function backgroundSetup() {
	$("#wrapper").removeClass("backgroundElement");
	var b = $(document.body).addClass("backgroundElement").height();
	if($(window).height() > b)
		$("<div id='bottom_coverall'/>").appendTo("#wrapper").css("top",b+"px");
}

function setHighlightTargets() {
	$("div#highlight span").mouseover(function () {
		var span = $(this);
		if(span.hasClass("activeHighlight")) return; // *** Prevent effect if already highlighting this word
		pauseColorBars();
		var d = 600;
		$("span.activeHighlight").size() ? $("span.activeHighlight")[0].className = "" : d = 800;
		this.className = "activeHighlight";
		var l = span.position().left;
		$("#cover2").animate({width:setHighlightTargets.bgWidth-(l+span.width())},{duration:d, easing:'swing', queue:false});
		$("#cover1").animate({width:l},{duration:d, easing:'swing', complete:function() {pauseColorBars();}, queue:false});
	});
}

// *** Starts the background color bars.
// *** Pass any value to this function to path this effect to a Codex page instead of a main page
function manageColorBars(forCodex) {
	//if(navigator.appVersion.split("MSIE 6.0").length > 1) return;
	if(arguments.length) wipeIn.forCodex = forCodex;
	var arr = manageColorBars.bars;
	if(arr.length < 3)
		wipeIn();
	else if(arr.length > 6)
		fadeBarOut($(arr.shift()));
	else
		Math.random() > 0.5 ? wipeIn() : fadeBarOut($(arr.shift()));
} manageColorBars.bars = [];
manageColorBars.pause = false;

function pauseColorBars(pause) {
	if(arguments.length)
		manageColorBars.pause = pause;
	else
		manageColorBars.pause = !manageColorBars.pause;
}

function wipeIn() {
	var bar = document.createElement("div");
	bar.className = "colorbar";
	var hgt = $(document.body).height();
	$(bar).height(hgt);
	$("div#bars").append(bar);
	var wide = Math.floor(Math.random()*100);
	wide = wide < 15 ? 15+wide : wide;
	var w = Math.floor(Math.random()*10000) % ($('body').width() - wide);
	var color = Math.floor(Math.random()*10*wipeIn.bgChoices.length) % wipeIn.bgChoices.length;
	/*if(w + wide > $('body').width()) // *** Prevent x-overflow
		w = $('body').width()-wide;*/
	$(bar).css({width:0, left:w + "px","background-image":"url('"+(wipeIn.forCodex?"../../":"")+"images/index2/woodwall_"+wipeIn.bgChoices[color]+".jpg')", "background-position":(-w-500)+"px 0"});//.animate({width:"50px"}, 2000);
	navigator.appName=="Microsoft Internet Explorer" ? fadeBarIn($(bar).css({width:wide,opacity:0})) : changeWidth($(bar), 1, wide);
	manageColorBars.bars.push(bar);
} wipeIn.bgChoices = ["blue","yellow","orange","red","purple","green"/*,"teal","pink"*/];

// *** Primarily for IE's handicapped JS engine
function fadeBarIn(bar) {
	if(manageColorBars.pause) {
		setTimeout(function () { fadeBarIn(bar); },100);
		return;
	}
	var op = parseFloat(bar.css("opacity"));
	if(op < 1) {
		bar.css("opacity",parseFloat(0.05+op));
		setTimeout(function () { fadeBarIn(bar); },60);
	} else {
		setTimeout("manageColorBars()",1200);
	}
}
var ctr =5;

function fadeBarOut(bar) {
	if(manageColorBars.pause) {
		setTimeout(function () { fadeBarOut(bar); },100);
		return;
	}
	//bar = $(bar);
	var op = bar.css("opacity");
	if(op > 0) {
		bar.css("opacity",op-0.05);
		setTimeout(function () { fadeBarOut(bar); },60);
	} else {
		bar.remove();
		setTimeout("manageColorBars()",1200);
	}
}

function changeWidth(el, rate, targ) {
	if(manageColorBars.pause) {
		setTimeout(function () { changeWidth(el, rate, targ); },100);
		return;
	}
	var w = el.width() + rate;
	el.css("width",w);
	if(w != targ)
		setTimeout(function () { changeWidth(el, rate, targ); },60);
	else
		setTimeout("manageColorBars();",1200);
}

function detectFonts(fontNameArray) {
	var detect = $("<span id='fontDetect'>abcdefghijklmnopqrstuvwxyz</span>").prependTo(document.body).css({"font-size":"16px", "font-family":"monospace", position:"absolute", top:"-100px"});
	var monoLen = detect.width();
	for(var i = 0; i < fontNameArray.length; i++) {
		if(detect.css("font-family",fontNameArray[i]+", monospace").width() != monoLen) {
			detect.remove();
			return;
		}
	}
	$("body").css({"font-size":"12px"});
	$("#page_content p, .codex_content").css({"line-height":2,"word-spacing":"1px"});
	$("code").css("font-size","13px");
	detect.remove();
}

function ie6BackgroundManager() {
	$("#super").css("background-position",(Math.floor((document.body.offsetWidth-950)/-2) - 499)+"px -139px");
}

function ie6IconSwap() {
	var icons = $("#icons img").each(function () {
		this.src = this.src.split(".png")[0] + ".gif";
	});
}
