
//image rollover
function swapimages(which1,what1,which2,what2,which3,what3,which4,what4,which5,what5,which6,what6){
if (document.getElementById(''+which1+'')){
	document.getElementById(''+which1+'').src=''+what1+'';
}
if (document.getElementById(''+which2+'')){
	document.getElementById(''+which2+'').src=''+what2+'';
}
if (document.getElementById(''+which3+'')){
	document.getElementById(''+which3+'').src=''+what3+'';
}
if (document.getElementById(''+which4+'')){
	document.getElementById(''+which4+'').src=''+what4+'';
}
if (document.getElementById(''+which5+'')){
	document.getElementById(''+which5+'').src=''+what5+'';
}
if (document.getElementById(''+which6+'')){
	document.getElementById(''+which6+'').src=''+what6+'';
}
}

//replace image
function movepic(img_name,img_src) {
document[img_name].src=img_src;
}

// search stuff

function textClear (input) { if ( input.value == input.defaultValue ) { input.value = ""; } }
function textRestore (input) { if ( input.value == "" ) { input.value = input.defaultValue;	} }


function JSFX_FloatTopDiv(div_name)
{
return;
}

// Long-in stuff

function textClear (input)
{
	if ( input.value == input.defaultValue ) 
	{ 
		input.value = ""; 
	} 
}
function textRestore (input)
{
	if ( input.value == "" ) 
	{
		input.value = input.defaultValue;	
	} 
}

 function changeBox()
 {
    document.getElementById('div1').style.display='none';
    document.getElementById('div2').style.display='';
    document.getElementById('password').focus();
 }
 function restoreBox()
 {
    if(document.getElementById('password').value=='')
    {
      document.getElementById('div1').style.display='';
      document.getElementById('div2').style.display='none';
    }
 }
 
function change_highlighted(trigger, new_highlighted, new_mem_level)
{
	// set colors
	mouseover_highlight = '#FAD8C4';
	click_highlight = '#FAD8C4';
	//

	if(trigger == 'click')
	{
		// change mem level radio button
		radioObj = document.forms['join_form'].elements['mem_level'];	

		// set all other elements background color to none
		for(i = 0; i < radioObj.length; i++) 
		{
			el_name = 'plan_'+(i+1);
			document.getElementById(el_name).style.backgroundColor = '';
			document.getElementById(el_name).setAttribute('clicked', 0);
		}
		
		new_highlighted.style.backgroundColor = click_highlight;
		new_highlighted.setAttribute('clicked', 1);
		
		for(i = 0; i < radioObj.length; i++)
		{
			if(radioObj[i].value == new_mem_level)
			{
				return radioObj[i].checked = true;
			}
		}

}
	
	// if mousing over and this item is not already clicked
	// set mouseover color normally
	else if(trigger == 'mouseover' && new_highlighted.getAttribute('clicked') != 1)
	{
		new_highlighted.style.backgroundColor = mouseover_highlight;
	}
	
	// if mousing out of unclicked element
	// set background to nothing
 	else if (trigger == 'mouseout' && new_highlighted.getAttribute('clicked') != 1)
	{
		new_highlighted.style.backgroundColor = '';
	}
}



// define the images and image maps you need to use for the swappings here
arr_ads = [];
arr_ads[1] = [];
arr_ads[1]['ad_img'] = 'paperback_book_plans.jpg';
arr_ads[1]['button_img'] = 'button_book_plans_highlight.jpg';
arr_ads[1]['button_img_off'] = 'button_book_plans_off.jpg';
arr_ads[1]['usemap'] = '#books_map';
arr_ads[1]['button_id'] = 'books';
arr_ads[1]['selected'] = 0;
arr_ads[2] = [];
arr_ads[2]['ad_img'] = 'children_book_plans.jpg';
arr_ads[2]['button_img'] = 'button_kid_rentals_highlight.jpg';
arr_ads[2]['button_img_off'] = 'button_kid_rentals_off.jpg';
arr_ads[2]['usemap'] = '#kids_map';
arr_ads[2]['button_id'] = 'kids';
arr_ads[2]['selected'] = 0;
arr_ads[3] = [];
arr_ads[3]['ad_img'] = 'cd_audio_book_plans.jpg';
arr_ads[3]['button_img'] = 'button_audiobook_plans_highlight.jpg';
arr_ads[3]['button_img_off'] = 'button_audiobook_plans_off.jpg';
arr_ads[3]['usemap'] = '#cds_map';
arr_ads[3]['button_id'] = 'cds';
arr_ads[3]['selected'] = 0;
arr_ads[4] = [];
arr_ads[4]['ad_img'] = 'mp3_audio_book_plans.jpg';
arr_ads[4]['button_img'] = 'button_mp3_plans_highlight.jpg';
arr_ads[4]['button_img_off'] = 'button_mp3_plans_off.jpg';
arr_ads[4]['usemap'] = '#mp3_map';
arr_ads[4]['button_id'] = 'mp3s';
arr_ads[4]['selected'] = 0;
arr_ads[5] = [];
arr_ads[5]['ad_img'] = 'combo_plans.jpg';
arr_ads[5]['button_img'] = 'button_combo_plans_highlight.jpg';
arr_ads[5]['button_img_off'] = 'button_combo_plans_off.jpg';
arr_ads[5]['usemap'] = '#combo_map';
arr_ads[5]['button_id'] = 'combos';
arr_ads[5]['selected'] = 0;

ad_timer = setTimeout('var test;',60000);
current_ad = 0;
stop_swap_done = 0;
function swap_next_ad(swap_ad)
{
	if(swap_ad)
	{
		stop_swap_done = 1;
		clearTimeout(ad_timer);

		if(swap_ad == current_ad)
		{
			document.getElementById(arr_ads[swap_ad]['button_id']).src= 'http://images.booksfree.com/images/' + arr_ads[swap_ad]['button_img'];
			return false;
		}
		
		current_ad = swap_ad;
		
		this_ad_key = swap_ad;
		this_ad = arr_ads[swap_ad];
	}
	else if(stop_swap_done)
	{
		return;
	}
	else
	{
		current_ad++;
		if(current_ad > 5)
		{
			current_ad = 1;
		}
		
		this_ad_key = current_ad;
		this_ad = arr_ads[current_ad];
	}

	arr_ads[this_ad_key]['selected'] = 1;

	// change clickable button
	document.getElementById(this_ad['button_id']).src= 'http://images.booksfree.com/images/' + this_ad['button_img'];


	new_ad=document.createElement('img');
	new_ad.src = 'http://images.booksfree.com/images/' + this_ad['ad_img'];
	new_ad.setAttribute('id', 'ad');
	new_ad.style.position = 'absolute';
	new_ad.style.border = '0px';
	new_ad.setAttribute('alt', "Rent Unlimited Paperback or Audio Books");
	new_ad.style.width = '668px';
	new_ad.style.height = '252px';
	new_ad.setAttribute('usemap', this_ad['usemap']);
	new_ad.useMap = this_ad['usemap'];

	// transparent object
	var object = new_ad.style;
	object.opacity = (0);
	object.MozOpacity = (0);
	object.KhtmlOpacity = (0);
	object.filter = "alpha(opacity=" + 0+ ")";

	fade_time = 500;

	old_ad = document.createElement('img');
	old_ad.src = document.getElementById('ad').src;
	old_ad.style.position = 'absolute';
	old_ad.style.border = 0;
	old_ad.style.width = '668px';
	old_ad.style.height = '252px';

	document.getElementById('ad_div').insertBefore(old_ad,document.getElementById('ad'));
	document.getElementById('ad_div').replaceChild(new_ad,document.getElementById('ad'));
	opacity('ad',0,100,fade_time);
	
	if(!stop_swap_done)
	{
		clearTimeout(ad_timer);
		ad_timer = setTimeout("swap_next_ad();", 8000);
	}
		
	// switch all other button images back to their original (non-selected) position
	for(i=1;i<6;i++)
	{
		if(i !== this_ad_key)
		{
			document.getElementById(arr_ads[i]['button_id']).src= 'http://images.booksfree.com/images/' + arr_ads[i]['button_img_off'];
			arr_ads[i]['selected'] = 0;
		}
	}




}

function stop_swap(el)
{
return;
}

function ad_button_mouseout(ad_id)
{
	if(arr_ads[ad_id]['selected'] == 1)
	{
			document.getElementById(arr_ads[ad_id]['button_id']).src= 'http://images.booksfree.com/images/' + arr_ads[ad_id]['button_img'];
	}
	else
	{
			document.getElementById(arr_ads[ad_id]['button_id']).src= 'http://images.booksfree.com/images/' + arr_ads[ad_id]['button_img_off'];
	}
}


var fade_inprogress;
function changeOpac(opacity, id,opacEnd)
{
	//window.alert(id);
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
   // fading complete, notify everyone
	if(opacEnd && opacity == opacEnd)
	{
		fade_inprogress = '';
	}
}
function opacity(id, opacStart, opacEnd, millisec)
{
	if(fade_inprogress !== 1)
	{
		fade_inprogress = 1;
		//speed for each frame
		var speed = Math.round(millisec / 100);
		var timer = 0;

		//determine the direction for the blending, if start and end are the same nothing happens
		if(opacStart > opacEnd) {
			for(i = opacStart; i >= opacEnd; i--) {
				setTimeout("changeOpac(" + i + ",'" + id + "', "+opacEnd+")",(timer * speed));
				timer++;
			}
		} else if(opacStart < opacEnd) {
			for(i = opacStart; i <= opacEnd; i++)
				{
				setTimeout("changeOpac(" + i + ",'" + id + "', "+opacEnd+")",(timer * speed));
				timer++;
			}
		}
	}
	else
	{
		// wait a half second for the other fade to complete
		setTimeout('opacity("'+id+'", '+opacStart+', '+opacEnd+', '+millisec+');', 500);
	}
}


/**
 *	Whatever:hover - V1.42.060206 - hover & active
 *	------------------------------------------------------------
 *	(c) 2005 - Peter Nederlof
 *	Peterned - http://www.xs4all.nl/~peterned/
 *	License  - http://creativecommons.org/licenses/LGPL/2.1/
 *
 *	Whatever:hover is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU Lesser General Public
 *	License as published by the Free Software Foundation; either
 *	version 2.1 of the License, or (at your option) any later version.
 *
 *	Whatever:hover is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *	Lesser General Public License for more details.
 *
 *	Credits and thanks to:
 *	Arnoud Berendsen, Martin Reurings, Robert Hanson
 *
 *	howto: body { behavior:url("csshover.htc"); }
 *	------------------------------------------------------------
 */

var csshoverReg = /(^|\s)(([^a]([^ ]+)?)|(a([^#.][^ ]+)+)):(hover|active)/i,
currentSheet, doc = window.document, hoverEvents = [], activators = {
	onhover:{on:'onmouseover', off:'onmouseout'},
	onactive:{on:'onmousedown', off:'onmouseup'}
}

function parseStylesheets() {
	if(!/MSIE/.test(navigator.userAgent)) return;
	window.attachEvent('onunload', unhookHoverEvents);
	var sheets = doc.styleSheets, l = sheets.length;
	for(var i=0; i<l; i++) 
		parseStylesheet(sheets[i]);
}
	function parseStylesheet(sheet) {
		if(sheet.imports) {
			try {
				var imports = sheet.imports, l = imports.length;
				for(var i=0; i<l; i++) parseStylesheet(sheet.imports[i]);
			} catch(securityException){}
		}

		try {
			var rules = (currentSheet = sheet).rules, l = rules.length;
			for(var j=0; j<l; j++) parseCSSRule(rules[j]);
		} catch(securityException){}
	}

	function parseCSSRule(rule) {
		var select = rule.selectorText, style = rule.style.cssText;
		if(!csshoverReg.test(select) || !style) return;

		var pseudo = select.replace(/[^:]+:([a-z-]+).*/i, 'on$1');
		var newSelect = select.replace(/(\.([a-z0-9_-]+):[a-z]+)|(:[a-z]+)/gi, '.$2' + pseudo);
		var className = (/\.([a-z0-9_-]*on(hover|active))/i).exec(newSelect)[1];
		var affected = select.replace(/:(hover|active).*$/, '');
		var elements = getElementsBySelect(affected);
		if(elements.length == 0) return;

		currentSheet.addRule(newSelect, style);
		for(var i=0; i<elements.length; i++)
			new HoverElement(elements[i], className, activators[pseudo]);
	}

function HoverElement(node, className, events) {
	if(!node.hovers) node.hovers = {};
	if(node.hovers[className]) return;
	node.hovers[className] = true;
	hookHoverEvent(node, events.on, function() { node.className += ' ' + className; });
	hookHoverEvent(node, events.off, function() { node.className = node.className.replace(new RegExp('\\s+'+className, 'g'),''); });
}
	function hookHoverEvent(node, type, handler) {
		node.attachEvent(type, handler);
		hoverEvents[hoverEvents.length] = { 
			node:node, type:type, handler:handler 
		};
	}

	function unhookHoverEvents() {
		for(var e,i=0; i<hoverEvents.length; i++) {
			e = hoverEvents[i]; 
			e.node.detachEvent(e.type, e.handler);
		}
	}

function getElementsBySelect(rule) {
	var parts, nodes = [doc];
	parts = rule.split(' ');
	for(var i=0; i<parts.length; i++) {
		nodes = getSelectedNodes(parts[i], nodes);
	}	return nodes;
}
	function getSelectedNodes(select, elements) {
		var result, node, nodes = [];
		var identify = (/\#([a-z0-9_-]+)/i).exec(select);
		if(identify) {
			var element = doc.getElementById(identify[1]);
			return element? [element]:nodes;
		}
		
		var classname = (/\.([a-z0-9_-]+)/i).exec(select);
		var tagName = select.replace(/(\.|\#|\:)[a-z0-9_-]+/i, '');
		var classReg = classname? new RegExp('\\b' + classname[1] + '\\b'):false;
		for(var i=0; i<elements.length; i++) {
			result = tagName? elements[i].all.tags(tagName):elements[i].all; 
			for(var j=0; j<result.length; j++) {
				node = result[j];
				if(classReg && !classReg.test(node.className)) continue;
				nodes[nodes.length] = node;
			}
		}	
		
		return nodes;
	}
	
// DH 2009-07-15 automatically display sign in or out link
function sign_in_out()
{
	if(typeof(document.getElementById('sign_in_out')) != "undefined" && document.getElementById('sign_in_out'))
	{
		sign_in_out = document.getElementById('sign_in_out');
		signed_in = 0;
		// check cookies
		
		var ck_name=new Array(12)
		ck_name[0]="BFmem";
		ck_name[1]="BFcdmem";
		ck_name[2]="BFmp3mem";
		ck_name[3]="BFcombo";
		ck_name[4]="BFkids";
		ck_name[5]="BFmemsession";
		ck_name[6]="BFcdmemsession";
		ck_name[7]="BFmp3memsession";
		ck_name[8]="BFcombosession";
		ck_name[9]="BFkidssession";
		ck_name[10]="BFalacarte";
		ck_name[11]="BFalacartesession";
		
		var x=0;
		for(x=0; x<12; x++)
		{
			if(signed_in != 1) {
				var ck_data = readCookie(ck_name[x]);
				if(ck_data != '000' && ck_data){signed_in = 1;} // they are signed in, or at least are pretending to be
			}
		}
		
		if(signed_in)
		{
		sign_in_out.href= "http://www.booksfree.com/cgi-bin/logout.cgi";
		sign_in_out.innerHTML = "Sign Out";
		}
		else
		{
		sign_in_out.href= "http://www.booksfree.com/mem_login_home.shtml";
		sign_in_out.innerHTML = "Member Sign-In";		
		}		
	}
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

//dropdown menu form style
function tamingselect()
{
	if(!document.getElementById && !document.createTextNode){return;}
	
// Classes for the link and the visible dropdown
	var ts_selectclass='turnintodropdown'; 	// class to identify selects
	var ts_listclass='turnintoselect';		// class to identify ULs
	var ts_boxclass='dropcontainer'; 		// parent element
	var ts_triggeron='activetrigger'; 		// class for the active trigger link
	var ts_triggeroff='trigger';			// class for the inactive trigger link
	var ts_dropdownclosed='dropdownhidden'; // closed dropdown
	var ts_dropdownopen='dropdownvisible';	// open dropdown
/*
	Turn all selects into DOM dropdowns
*/
	var count=0;
	var toreplace=new Array();
	var sels=document.getElementsByTagName('select');
	for(var i=0;i<sels.length;i++){
		if (ts_check(sels[i],ts_selectclass))
		{
			var hiddenfield=document.createElement('input');
			hiddenfield.name=sels[i].name;
			hiddenfield.type='hidden';
			hiddenfield.id=sels[i].id;
			hiddenfield.value=sels[i].options[0].value;
			sels[i].parentNode.insertBefore(hiddenfield,sels[i])
			var trigger=document.createElement('a');
			ts_addclass(trigger,ts_triggeroff);
			trigger.href='javascript:void(0);';
			trigger.style.color = "#777777";
			trigger.onclick=function(){
				ts_swapclass(this,ts_triggeroff,ts_triggeron)
				ts_swapclass(this.parentNode.getElementsByTagName('ul')[0],ts_dropdownclosed,ts_dropdownopen);
				return false;
			}
			trigger.appendChild(document.createTextNode(sels[i].options[0].text));
			sels[i].parentNode.insertBefore(trigger,sels[i]);
			var replaceUL=document.createElement('ul');
			for(var j=0;j<sels[i].getElementsByTagName('option').length;j++)
			{
				if(sels[i].getElementsByTagName('option')[j].value)
				{
					var newli=document.createElement('li');
					var newa=document.createElement('a');
					newli.v=sels[i].getElementsByTagName('option')[j].value;
					newli.elm=hiddenfield;
					newli.istrigger=trigger;
					newa.href='javascript.void(0);';
					newa.appendChild(document.createTextNode(
					sels[i].getElementsByTagName('option')[j].text));
					newli.onclick=function(){ 
						this.elm.value=this.v;
						ts_swapclass(this.istrigger,ts_triggeron,ts_triggeroff);
						ts_swapclass(this.parentNode,ts_dropdownopen,ts_dropdownclosed)
						this.istrigger.firstChild.nodeValue=this.firstChild.firstChild.nodeValue;
						return false;
					}
					newli.appendChild(newa);
					replaceUL.appendChild(newli);
				}
			}
			ts_addclass(replaceUL,ts_dropdownclosed);
			var div=document.createElement('div');
			div.appendChild(replaceUL);
			ts_addclass(div,ts_boxclass);
			sels[i].parentNode.insertBefore(div,sels[i])
			toreplace[count]=sels[i];
			count++;
		}
	}
	
/*
	Turn all ULs with the class defined above into dropdown navigations
*/	

	var uls=document.getElementsByTagName('ul');
	for(var i=0;i<uls.length;i++)
	{
		if(ts_check(uls[i],ts_listclass))
		{
			var newform=document.createElement('form');
			var newselect=document.createElement('select');
			for(j=0;j<uls[i].getElementsByTagName('a').length;j++)
			{
				var newopt=document.createElement('option');
				newopt.value=uls[i].getElementsByTagName('a')[j].href;	
				newopt.appendChild(document.createTextNode(uls[i].getElementsByTagName('a')[j].innerHTML));	
				newselect.appendChild(newopt);
			}
			newselect.onchange=function()
			{
				window.location=this.options[this.selectedIndex].value;
			}
			newform.appendChild(newselect);
			uls[i].parentNode.insertBefore(newform,uls[i]);
			toreplace[count]=uls[i];
			count++;
		}
	}
	for(i=0;i<count;i++){
		toreplace[i].parentNode.removeChild(toreplace[i]);
	}
	function ts_check(o,c)
	{
	 	return new RegExp('\\b'+c+'\\b').test(o.className);
	}
	function ts_swapclass(o,c1,c2)
	{
		var cn=o.className
		o.className=!ts_check(o,c1)?cn.replace(c2,c1):cn.replace(c1,c2);
	}
	function ts_addclass(o,c)
	{
		if(!ts_check(o,c)){o.className+=o.className==''?c:' '+c;}
	}
}

window.onload=function()
{
	tamingselect();
	sign_in_out();
	// add more functions if necessary
}
