
// INIT - - - - - - - - - - - - - - - - - - - - - - - -
function init() {
	preloadImages();
	//init_style();
}


// PRELOAD IMAGES - - - - - - - - - - - - - - - - - - -

var imsrcs = new Array(
		'min_hover.jpg',
		'max_hover.jpg',
		'res_hover.jpg',
		'close_hover.jpg',

		'prev_hover.jpg',
		'next_hover.jpg',
		'tick.gif',
		'tick_hover.gif',
		'gobutton_hover.png',
		'osx/menu_hover.png',
		'osx/menu_hover_active.png'
);

function preloadImages() {
	var ims = new Array();
	var imsSilver = new Array();
	var imsOSX = new Array();
	for(var i=0; i<4; i++) {
		ims[i] = new Image;
		ims[i].src = '/wp-content/themes/notepress/images/royale/buttons/'+imsrcs[i];
		imsSilver[i] = new Image;
		imsSilver[i].src = '/wp-content/themes/notepress/images/silver/buttons/'+imsrcs[i];
		imsOSX[i] = new Image;
		imsOSX[i].src = '/wp-content/themes/notepress/images/osx/buttons/'+ imsrcs[i].replace(/\.jpg$/, '.png');
	}
	for(var i=4; i<imsrcs.length; i++) {
		ims[i] = new Image;
		ims[i].src = '/wp-content/themes/notepress/images/'+imsrcs[i];
	}
}


// WINDOWING - - - - - - - - - - - - - - - - - - - - - - -

function winMin(id) {
	winClose(id);
}

function winMax(id) {
	document.getElementById(id+'MaxButton').style.visibility = 'hidden';
	document.getElementById(id+'ResButton').style.visibility = 'visible';
	document.getElementById(id+'Wrapper').style.width = '100%';
	document.getElementById(id+'Wrapper').style.marginTop = '0';
}

function winRes(id) {
	document.getElementById(id+'ResButton').style.visibility = 'hidden';
	document.getElementById(id+'MaxButton').style.visibility = 'visible';
	document.getElementById(id+'Wrapper').style.width = '700px';
	document.getElementById(id+'Wrapper').style.marginTop = '25px';
}

function winClose(id) {
	if (id=='viewer')
		window.location = '/photos/';
	else
		document.getElementById(id+'Wrapper').style.display = 'none';
}

/*ondblclick="winOpen()"
function winOpen(id) {
	document.getElementById(id+'Wrapper').style.display = 'block';
}*/


// MENUS - - - - - - - - - - - - - - - - - - - - - - -

var currentMS;
var currentStub;
var shadowS;

function menuInit() {
	shadowS = document.getElementById('menuShadow').style;
}

function toggleMenu(mID,stub) {
	var mS = document.getElementById(mID+'Menu').style;
	if (mS.display=='block')
		closeCurrentMenu();
	else {
		closeCurrentMenu();
		openMenu(mID,stub);
	}
}

function mouseOverStub(mID,stub) {
	if ( !currentMS || stub==currentStub )
		return;
	closeCurrentMenu();
	openMenu(mID,stub);
}

function blurStub(s) {
	if (currentMS && s==currentStub)
		setTimeout( 'closeCurrentMenu()', 200 );
}

function closeCurrentMenu() {
	if (currentMS) {
		currentMS.display = 'none';
		currentStub.className = '';
		shadowS.display = 'none';
		currentMS = null;
	}
}

function openMenu(mID,stub) {
	var mS = document.getElementById(mID+'Menu').style;
	mS.left = (stub.offsetLeft+4)+'px';
	mS.display = 'block';
	stub.className = 'current';
	currentMS = mS;
	currentStub = stub;
	stub.focus();
	var m = document.getElementById(mID+'Menu');
	shadowS.left = (m.offsetLeft+4)+'px';
	shadowS.top = (m.offsetTop+4)+'px';
	shadowS.width = (m.offsetWidth-2)+'px';
	shadowS.height = (m.offsetHeight-2)+'px';
	shadowS.display = 'block';
}


// SEARCH - - - - - - - - - - - - - - - - - - - - - - -

function blogSearch() {
	var term = prompt('Hello, I am the HAL 9000 blog search. What would you like to find today?','');
	if (term)
		window.location = '/?s='+ term;
}


// ADDRESS BAR - - - - - - - - - - - - - - - - - - - -

function addressbarGo() {
	var addrSel = document.getElementById('selAddress');
	window.location = addrSel[addrSel.selectedIndex].value;
}


// SWITCH THEME - - - - - - - - - - - - - - - - - - - -

function switch_theme(title) {
	title = title.replace(/\+/, ' '); // revert + to [space]
	setActiveStyleSheet(title);
	setStyleMenuitems(title);
	createCookie("bartleby_theme", title, 365);
}

function setStyleMenuitems(title) {
	var styles = [ 'royale', 'silver', 'inferno', 'plasma', 'r18', 'osx' ];
	for(var i=0; i<styles.length; i++) {
		document.getElementById(styles[i] +'menuitem').className = '';
	}
	document.getElementById(title.toLowerCase().replace(/ /, '') +'menuitem').className = 'enableditem';
}

/*
Respect! to Bernie Zimmerman
http://www.bernzilla.com/
Based on:
http://web.archive.org/web/20040605212154js_/www.bernzilla.com/inc/magic.js
*/

function setActiveStyleSheet(title) {
	var i, s;
	for(i=0; (s = document.getElementsByTagName("link")[i]); i++) {
		if(s.getAttribute("rel").indexOf("style") != -1 && s.getAttribute("title")) {
			if(s.getAttribute("title") == title)
				s.disabled = false;
			else
				s.disabled = true;
		}
	}
}
	
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
