// this javascript file includes custom functions required across the entire indiebound.org site, including functions for the disappearing text in the email updates box and facebox ajax form for buy this book

function toggleInputText(el) {
		$(el).each(function() {
			var old = $(this).val();

			$(this).focus(function() {
				if ($(this).val() === old) {
					if ($(this).val() == 'Your E-mail Address' ||
							$(this).val() == 'Title, Author, or ISBN #' ||
							$(this).val() == 'Your Zip Code' ||
							$(this).val() == 'Enter Your Address or ZIP')
								$(this).val('');	
				}
			});

			$(this).blur(function() {
				if ($(this).val() === '') {
					$(this).val(old);
				}
			});
		});
	}

		$('.input-text').livequery(function() {
		    toggleInputText(this);
	    });

// these functions are for the zip code ajax form
function transferMe(url) {
	window.location=url;
}
	
function zipTransfer(url,store_id) {
	if($('#cookset'+store_id).attr('checked')==true) { // if user asked to set cookie, then SET THE USER'S COOKIE!!
		document.cookie ='indiebound_store_select='+store_id+'; expires=; path=/';
	}
	$.post('/zip-ajax-genxml.php', { storeurl: url, store: store_id });
	$('#zip-content').html('<img src="/sites/all/themes/indiebound/images/loading.gif" onLoad="transferMe(\''+url+'\')" /><br />Transferring you to your store...');
}

function searchStores(zipsearch, storelimit, isbn) {
	if (zipsearch == 0) { zipsearch = $('#zipsearch').val(); }
	$.get('/zip-ajax-genxml.php?zip='+zipsearch+'&isbn='+isbn+'&storelimit='+storelimit, function(xml) {
		var zipcontent = '<center><table align="center">';
		var morestores = storelimit+5;
		$(xml).find('item').each(function() {
			transferUrl = $(this).attr('urlstring');
			zipcontent+='<tr><td width="120" style="padding: 4px"><input name="zip-url" id="zip-url" type="hidden" value='+transferUrl+' /><img style="margin-bottom: 4px" src="/sites/all/themes/indiebound/images/btn-select-store.gif" class="store-select-btn" onClick="zipTransfer(\''+transferUrl+'\',\''+$(this).attr('id')+'\')" alt="Select This Store" /><br /><input name="cookset" id="cookset'+$(this).attr('id')+'" type="checkbox" value="1" /> <span class="tinytext">Remember my choice</span></td><td style="padding: 4px"><strong>'+$(this).attr('name')+'</strong><br />'+$(this).attr('street1')+'<br />'+$(this).attr('city')+', '+$(this).attr('state')+' '+$(this).attr('zip')+'<br /></td></tr>';
 		});
		zipcontent += '<tr><td colspan="2"><p style="text-align: center; cursor: pointer; color: #ba131a" onclick="searchStores(\''+zipsearch+'\', '+morestores+', \''+isbn+'\')">Show 5 more stores...</p></td></tr></table></center>';
		$('#zip-content').html(zipcontent);
	});
}
		
$('.book-link').livequery(function(){
    $(this).facebox();
});
