/**
 * 2003 Apr 20
 *
 * $Id: ache.js,v 1.5.2.1 2008/10/02 05:53:09 epark Exp $
 *
 * The main JavaScript library for the Achewood site.
 */

function openLinkNewWindow(link) {
	if(!link) {
		link = this;
	}
	var url = link.href;
	var name = link.target || '_blank';
	var win = window.open(url, name);
	win.focus();

	return false;
}

function popLink(link, name, width, height) {

	var win, params;

	if(!name) {
		name = 'popup';
	}
	if(!width) {
		width = 550;
	}
	if(!height) {
		height = 700;
	}

	params  = "width=" + width;
	params += ",height=" + height;
	params += ",toolbar=0";
	params += ",scrollbars=1";
	params += ",location=0";
	params += ",statusbar=0";
	params += ",menubar=0";
	params += ",resizable=1";

	win = window.open(link.href, name, params);
	win.focus();

	return false;
}

function openDialog(html) {
	var d = $("#dialog");
	if(d.length > 0) {
		$('#dialog_inset').empty();
	} else {
		$('body').prepend('<div id="dialog"><div id="dialog_inset"></div></div>');
		d = $('#dialog');
	}

	var oldTid = d.attr('timeoutId');
	if(oldTid) {
		clearTimeout(oldTid);
		d.attr('timeoutId', null);
	}

	$('#dialog_inset')
	 .append(html)
	 .append("<p><button onclick=\"closeDialog();\">Close</button></p>");

	var scrollYOffset = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;

	d.css('top', scrollYOffset + 100)
	 .hide()
	 .slideToggle('slow');

	d.attr('timeoutId',
	  setTimeout("$('#dialog').slideUp('slow', function(){$(this).remove()})", 5000)
	);

	return d.get(0);
}

function closeDialog() {
	$('#dialog').slideUp('slow', function(){
		var oldTid = $(this).attr('timeoutId');
		if(oldTid) {
			clearTimeout(oldTid);
			$(this).attr('timeoutId', null);
		}
		$(this).remove();
	});
}

function openOverlay(html) {
	var overlay = document.createElement('div');

	$(overlay)
	  .attr('id', 'dialog_overlay')
	  .html(
	      '<div class="mask"></div><div class="content">'
	    + '<button id="overlay_close" title="Close" onclick="closeOverlay()">x</button>'
	    + (html ? html : '') + '</div>'
	  )
	  .prependTo('body');

	var isIE6 = navigator.userAgent.indexOf('MSIE') >= 0
	         && navigator.appVersion.replace(/.*MSIE.([\d\.]+).*/, '$1') == 6;

	if(isIE6) {
		var scrollYOffset = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
		$('.content', overlay).css('top', scrollYOffset + 175)

		$('html').css('overflow', 'hidden');
		$('select').css('visibility', 'hidden');
	}

	return overlay;
}

function closeOverlay() {
	var isIE6 = navigator.userAgent.indexOf('MSIE') >= 0
	         && navigator.appVersion.replace(/.*MSIE.([\d\.]+).*/, '$1') == 6;

	if(isIE6) {
		$('html').css('overflow', '');
		$('select').css('visibility', '');
	}
	$('#dialog_overlay').remove();
}

function initImageEnlarge() {
	$('a[rel=enlarge]').click(function(){
		var jImg = $('img', this);
		var jThis = $(this);
		var caption = jThis.attr('title');
		var html = '<p class="image"><img src="'+jThis.attr('href')+'" alt="'+jImg.attr('alt')+'"/></p>';

		if(caption) {
			html += '<p class="caption">'+caption+'</p>';
		}

		// Preload images so that the width of the image can
		// be determined and then used to set the width of the
		// DIV.content element

		var tmpImg = new Image();

		$(tmpImg).load(function() {
			var o = openOverlay(html);
			$(o).addClass('image-enlarge')
			  .find('.content').width(this.width);
		});

		tmpImg.src = jThis.attr('href');

		return false;
	});
}

function fixIESiteNav() {
	fixIENav('site_navigation');
}

function fixIEShopNav() {
	fixIENav('shop_navigation');
}

function fixIENav(navId) {
	var exc;
	try {
		var navList = document.getElementById(navId).getElementsByTagName('ul').item(0).getElementsByTagName('li');
		for(var i=0, navLen=navList.length; i < navLen; i++) {
			navList.item(i).innerHTML = "&nbsp;::&nbsp;" + navList.item(i).innerHTML;
		}
	} catch(exc) {}
}
