// persistent cart cookie reader
var cartitems = '';
if(location.hostname.match(/\.randomhouse\.com/)) {
	var allcookies = document.cookie;
	var pos = allcookies.indexOf("rhcartitems=");
	if(pos != -1) {
		var start = pos + 12;
		var end = allcookies.indexOf(";",start);
		if(end == -1) end = allcookies.length;
		var value = allcookies.substring(start,end);
		value = unescape(value);
		var ct = 0;
		var items = value.split('&');
			for(var i=0; i < items.length; i++) {
				count = items[i].substring(items[i].indexOf(":")+1,items[i].length);
				ct = parseInt(ct) + parseInt(count);
			}
	}
		if(ct>0) { cartitems = " (" + ct + ")"; }
}

function global_nav_popup_setup(){

	var gauze = getByClass('global_nav_float_gauze')[0];
	if (!gauze){
	
		gauze = document.createElement('div');
		gauze.className = 'global_nav_float_gauze';

		var body = document.getElementsByTagName('body')[0];
		body.appendChild(gauze);
		addtachEvent('resize', function (){ resize_to_screen_height(gauze); });
		resize_to_screen_height(gauze);
	}
}
function activate_global_nav_popup(){
	var popup;
	var gauze = getByClass('global_nav_float_gauze')[0];

	if (!gauze){
		global_nav_popup_setup();
		gauze = getByClass('global_nav_float_gauze')[0];
	}
	gauze.style.display = 'block';

		var body = document.getElementsByTagName('body')[0];
		popup = getByID('global_nav_popup');	

		body.appendChild(popup);

		var close = getByClass('global_nav_popup_close', popup)[0];
		addtachEvent('click', function(){
				popup.style.display = 'none';
				gauze.style.display = 'none';
			}, close);

	align_nav_popup(popup);
	popup.style.display = 'block';

	// return false because this will be triggered by an a tag
	return false;
}

/*******************************************************************************
**  printing functions
*******************************************************************************/

function print_global_nav(imprint_name,imprint_url) {
	document.writeln('<div id="global_nav">');
	document.writeln('<div id="global_nav_inner">');
	document.writeln('	<img src="http://www.randomhouse.com/art/bw06/base/global_nav/logo.gif" style="float: left;" />');
	document.writeln('	<div id="global_nav_right">');
	document.writeln('		<p><b><a href="https://ecommerce.randomhouse.com/account.do?from=randomhouse">Account</a></b> | <b><a href="http://ecommerce.randomhouse.com/cart.do?from=randomhouse">Cart' + cartitems + '</a></b> | <a onclick="feedbackOpen(); return false;" href="http://www.randomhouse.com/cgi-bin/feedback/feedback.php?loc=' + window.location.href + '"><b>Feedback</b></a> | <b><a href="http://www.randomhouse.com/about/faq/">Help</a></b> | <b>Search:</b></p>');
	document.writeln('		<form action="http://www.randomhouse.com/gm/results.pperl" method="GET">');
	document.writeln('			<input type="text" name="title_subtitle_auth_isbn" id="global_nav_input" value="Search Random House" onfocus="this.value=\'\';" />');
	document.writeln('			<input type="image" src="http://www.randomhouse.com/art/bw06/base/global_nav/btn_go.gif" />');
	document.writeln('		</form>');
	document.writeln('	</div>');
	document.writeln('	<p><b><a href="http://www.randomhouse.com/">Random House, Inc.</a></b> | ');
	if((imprint_name) && (imprint_url)) {
		document.writeln('<b><a href="' + imprint_url + '">' + imprint_name + '</a></b> | ');
	}
	document.writeln('<b><a href="#" onclick="activate_global_nav_popup(); return false">More Sites</a></b></p><a href="#" onclick="activate_global_nav_popup(); return false"><img src="http://www.randomhouse.com/art/bw06/base/global_nav/btn_plus.gif" hspace="0" id="global_nav_popup_open" /></a>');
	document.writeln('</div>');
	document.writeln('</div>');
	document.writeln('<div class="global_nav_more_sites" id="global_nav_popup">');
	document.writeln('	<table cellpadding="0" cellspacing="0" border="0">');
	document.writeln('	<tr>');
	document.writeln('		<td class="global_nav_float_l"></td>');
	document.writeln('		<td >');
	document.writeln('			<div class="global_nav_more_sites_inner">');
	document.writeln('				<div class="global_nav_more_sites_top">');
	document.writeln('					<div class="global_nav_popup_close"><b>More Sites</b><img src="http://www.randomhouse.com/art/bw06/base/global_nav/btn_minus.gif" /></div>');
	document.writeln('				</div>');
	document.writeln('				<div class="global_nav_more_sites_content">');
	document.writeln('					<div class="global_nav_more_sites_content_column1">');
	document.writeln('						<b>Categories</b><br />');
	document.writeln('						<a href="http://www.randomhouse.com/category/afam/">African-American</a><br />');
	document.writeln('						<a href="http://www.randomhouse.com/category/biomem/">Biography/Memoir</a><br />');
	document.writeln('						<a href="http://www.randomhouse.com/category/business/">Business/Economics</a><br />');
	document.writeln('						<a href="http://www.randomhouse.com/kids/">Children\'s Books</a><br />');
	document.writeln('						<a href="http://www.randomhouse.com/category/classics/">Classic Literature</a><br />');
	document.writeln('						<a href="http://www.randomhouse.com/category/cooking/">Cooking</a><br />');
	document.writeln('						<a href="http://www.randomhouse.com/category/fiction/">Fiction/Literature</a><br />');
	document.writeln('						<a href="http://www.randomhouse.com/category/health/">Health/Fitness</a><br />');
	document.writeln('						<a href="http://www.randomhouse.com/houseofcollectibles/">House of Collectibles</a><br />');
	document.writeln('						<a href="http://www.randomhouse.com/category/mystery/">Mystery</a><br />');
	document.writeln('						<a href="http://www.randomhouse.com/category/romance/">Romance</a><br />');
	document.writeln('						<a href="http://www.randomhouse.com/category/scifi/">Science Fiction</a><br />');
	document.writeln('						<a href="http://www.randomhouse.com/category/">More...</a><br />');
	document.writeln('					</div>');
	document.writeln('					<div class="global_nav_more_sites_content_column2">');
	document.writeln('						<b>Stores</b><br />');
	document.writeln('						<a href="http://www.booksontape.com/">Books on Tape</a><br />');
	document.writeln('						<a href="http://www.fodors.com/">Fodor\'s</a><br />');
	document.writeln('						<a href="http://www.livinglanguage.com/">Living Language</a><br />');
	document.writeln('						<a href="http://www.primagames.com/">Prima Games</a><br />');
	document.writeln('						<a href="http://www.randomhouse.com/princetonreview/">The Princeton Review</a><br />');
	document.writeln('						<a href="http://www.randomhouse.com/sylvanlearningbookstore/">Sylvan Learning Bookstore</a><br />');
	document.writeln('						<b>Formats</b><br />');
	document.writeln('						<a href="http://www.randomhouse.com/audio/">Audiobooks</a><br />');
	document.writeln('						<a href="http://www.randomhouse.com/audio/about/audiobookdownloadfaq.html">Audiobook Downloads</a><br />');
	document.writeln('						<a href="http://www.randomhouse.com/category/ebooks/">eBooks/Digital Downloads</a><br />');
	document.writeln('						<a href="http://www.randomhouse.com/largeprint/">Large Print Books</a><br />');
	document.writeln('						<b>Our Publishers</b><br />');
	document.writeln('						<a href="http://www.randomhouse.biz/ourpublishers/">See all here</a>');
	document.writeln('					</div>');
	document.writeln('				</div>');
	document.writeln('			</div>');
	document.writeln('		</td>');
	document.writeln('		<td class="global_nav_float_r"></td>');
	document.writeln('	</tr>');
	document.writeln('	<tr>');
	document.writeln('		<td class="global_nav_float_bl"></td>');
	document.writeln('		<td class="global_nav_float_b"></td>');
	document.writeln('		<td class="global_nav_float_br"></td>');
	document.writeln('	</tr>');
	document.writeln('	</table>');
	document.writeln('</div>');
}

/*******************************************************************************
**  general reusable functions
*******************************************************************************/

function align_nav_popup(div){

	var adjust_offset_left = -62;

	var open_btn = getByID('global_nav_popup_open');

	if(open_btn.offsetParent.offsetParent)
		adjust_offset_left += open_btn.offsetParent.offsetParent.offsetLeft;
	if(open_btn.offsetParent)
		adjust_offset_left += open_btn.offsetParent.offsetLeft;
	adjust_offset_left += open_btn.offsetLeft;


	div.style.left = adjust_offset_left + 'px';
	div.style.display = 'block';

}
function resize_to_screen_height(div){

	var height, width;
	var test1 = document.body.scrollHeight;
	var test2 = document.body.offsetHeight

	if (test1 > test2) {
		height = document.body.scrollHeight;
		width = document.body.scrollWidth;
	}
	else {
		height = document.body.offsetHeight;
		width = document.body.offsetWidth;
	}

	//alert ('resize to ' + height);

	div.style.height = height + 'px';
	div.style.width = width + 'px';
}

function getByID(ID){
    if (document.getElementById){
        return document.getElementById(ID);
    }
    else if ((document.all) && (eval ('document.all.' + ID))){
        return eval ('document.all.' + ID);
    }
}
function getByClass(cls, top) {
    var elements;
    var elements_list = [];
    var pattern = new RegExp("(^|\\s)"+cls+"(\\s|$)");

    (top) || (top = document);
    elements = top.getElementsByTagName('*')

    for (var i=0; i<elements.length; i++){
        if (pattern.test(elements[i].className)){
            elements_list.push(elements[i]);
        }
    }
    return elements_list;
}
function addtachEvent(evt, daFunction, obj){
    (obj) || (obj = window);
    if (window.addEventListener){
        obj.addEventListener(evt, daFunction, false);
    }
    else if (window.attachEvent){
        obj.attachEvent( "on" + evt, daFunction);
    }
}
function makeDraggable(bar, obj){
	addtachEvent('mousedown', function(event){
		dragDown(bar, obj, event);
	}, bar);
	addtachEvent('mouseup', function(event){
		dragUp(bar, obj, event);
	}, document);
	addtachEvent('mousemove', function(event){
		dragMove(bar, obj, event);
	}, document);
}
function dragDown(bar, obj, evt){
	evt = (evt) ? evt : ((event) ? event :null);
	bar.dragging = 1;

	if (!document.all){
	   bar.startY = evt.screenY;
	   bar.startX = evt.screenX;
	}
	else {
	   bar.startX = evt.clientX;
	   bar.startY = evt.clientY;
	}

	bar.startLeft = extractNumber(obj.style.left);
	bar.startTop = extractNumber(obj.style.top);
}
function dragMove(bar, obj, evt){
	if (bar.dragging){
		evt = (evt) ? evt : ((event) ? event :null);

		var moveX, moveY;
		if (!document.all){
			moveY = evt.screenY - bar.startY;
			moveX = evt.screenX - bar.startX;
		}
		else{
			moveY = evt.clientY - bar.startY;
			moveX = evt.clientX - bar.startX;
		}

		obj.style.left = (bar.startLeft + moveX) + 'px';
		obj.style.top = (bar.startTop + moveY) + 'px';

		// prevent text selection
		document.body.focus();
		document.onselectstart = function () { return false; };
		return false;
	}
}
function dragUp(bar, obj, evt){
	if (bar.dragging){
		evt = (evt) ? evt : ((event) ? event :null);
		bar.dragging = null;
		document.onselectstart = null;
	}
}
function extractNumber(value) {
	var n = parseInt(value);
	return n == null || isNaN(n) ? 0 : n;
}
function feedbackOpen() {
	var href = "http://www.randomhouse.com/cgi-bin/feedback/feedback.php?loc=" + window.location.href;
	window.open(href, '450_cover', 'height=620,width=500,resizable=yes,scrollbars=no')
}

addtachEvent('load', function (){ global_nav_popup_setup(); });


