// onload funcs 
Event.observe(window, 'load', function(event) {

	// search prefill 
	var default_text='Enter a Book Title or Author or ISBN';
	if($F('search_k')==''){$('search_k').value=default_text;}else if($F('search_k') != default_text){$('search_k').addClassName('black');};
	Event.observe('search_k','focus',function(event){
		if($F('search_k')==default_text){
			$('search_k').value='';
			$('search_k').addClassName('black');
		}
	});
	Event.observe('search_k','blur',function(event){
		if($F('search_k')==''){
			$('search_k').value=default_text;
			$('search_k').removeClassName('black');
		}
	});
	
	$$('a.voucherbuy').each(function(s){
		// we put the id in the name field, extract it 
		var product_id=parseInt(s.name.replace("voucher_",""));
		Event.observe(s, 'click', function(event){
			// stop the non-js add link running 
			Event.stop(event);
			// do the js version of the add 
			addVoucher(product_id);
		});
	});
	
	// attach event to buy button(s) 
	$$('a.productbuy').each(function(s){
		// we put the id in the name field, extract it 
		var product_id=parseInt(s.name.replace("product_",""));
		Event.observe(s, 'click', function(event){
			// stop the non-js add link running 
			Event.stop(event);
			// do the js version of the add 
			addBasket(product_id);
		});
	});

});

// login slide
function show_login(){
	if($('login_abs').visible()){
		Effect.SlideUp('login_abs',{ duration: 0.2 });
		$('login_href').innerHTML='Login';
	}else{
		Effect.SlideDown('login_abs',{ duration: 0.2 })
		setTimeout("$('login_email').focus()",300);
		$('login_href').innerHTML='Hide';
	}
}

var loading_basket = false;

// basket
function addBasket(product_id){

    if (!loading_basket) {
        new Ajax.Updater('basket_top','/shopping/basket_action/'+product_id,{parameters:
            {
                'ajax':1,
                'shopping.action':'add'
            },
            onLoading:function(request){
                $('ajax1').show();
                loading_basket = true;
                $$('a.productbuy img').each(function(s){
                    s.writeAttribute('src', '/images/pbo/buttons/add_to_basket_disabled.gif');
                });
            },
            onComplete:function(request){
                toggleAdded();
                loading_basket = false;
                $$('a.productbuy img').each(function(s){
                    s.writeAttribute('src', '/images/pbo/buttons/add_to_basket.gif');
                });
            }
        });        
    }

	
}

function toggleAdded(){
	$('ajax1').hide();
	$('product_added').show();
	setTimeout("$('product_added').fade({duration:2.0})",5000)
}

// basket
function addVoucher(voucher_id){
	new Ajax.Updater('basket_top','/shopping/basket_action/'+voucher_id,{parameters:
	{
		'ajax':1,
		'shopping.action':'add_voucher'
	},
	onLoading:function(request){$('ajax1').show()},
	onComplete:function(request){toggleAddedVoucher()}
});}

function toggleAddedVoucher(){
	$('product_added').show();
    setTimeout("$('product_added').fade({duration:2.0})",5000);
}

