/**
 * @author Peter
 */
(function(){
    (function preserveTextsInInputs(elm,btn){
        var origValue = 'Search new and antiquarian books';
        function clear(){
            if (elm.val() == origValue) {
                elm.val('');
                elm.removeClass('origvalue').addClass('userinput');
				btn.removeAttr('disabled');
            }
        }
        function reset(){
            if (elm.val() == '' || elm.val() == origValue) {
                elm.removeClass('userinput').addClass('origvalue');
                elm.val(origValue);
				btn.attr({
					'disabled': 'disabled'
				});	
            }
        }
		elm.click(clear).bind('enter',clear).bind('focus',clear).bind('blur',reset).bind('cancel',reset);
		
		reset();
    })($('#searchbox'),$('#searchbutton'));
    
	$('a.zoom').bind('click', function(e){
		e.preventDefault();
		$('b', this).toggleClass('out');
		var originalSrc = $.data($('img', this)[0],'originalSrc');
		if (originalSrc) {
			$.data($('img', this)[0],'originalSrc', $('img', this).attr('src'));
			$('img', this).attr('src', originalSrc);
		} else {
			$.data($('img', this)[0],'originalSrc', $('img', this).attr('src'));
			var href = $(this).attr('href');
			$('img', this).removeAttr('width');
			$('img', this).removeAttr('height');
			$('img', this).attr('src', href);
		}
	});
	
	(function(){
		var elm = $('.highlights .mask');
		var step = ($('ul li:first', elm).width()+30);
		var w = $('ul li', elm).length * step;
		$('.highlights').append('<input type="button" class="next" title="Next" value="Next"></input><input type="button" class="prev" title="Previous" value="Previous"></input>');
		elm.css({overflow:'hidden'});
		
		$('ul', elm).width(w);
		
		function checkPrevNext(){
			if (elm.scrollLeft() >= ($('ul', elm).outerWidth() - elm.outerWidth())) {
				$('.highlights .next').stop().fadeOut();
			} else {
				$('.highlights .next').stop().fadeIn();
			}
			if (elm.scrollLeft() <= 0) {
				$('.highlights .prev').stop().fadeOut();
			} else {
				$('.highlights .prev').stop().fadeIn();
			}
		}
		
		(function(){
			var isAnimating = false;
			var speed = 400;
			$('.highlights .next').bind('click', function(){
				if (isAnimating) {
					return;
				}
				isAnimating = true;
				elm.animate({
					scrollLeft: elm.scrollLeft() + step
				}, speed, function(){
					isAnimating = false
					checkPrevNext();
				});
			});
			$('.highlights .prev').bind('click', function(){
				if (isAnimating) {
					return;
				}
				isAnimating = true;
				elm.animate({
					scrollLeft: elm.scrollLeft() - step
				}, speed, function(){
					isAnimating = false;
					checkPrevNext();
				});
			});
		})();
		
		checkPrevNext();
		
	})();
	
	typeof Garp === 'undefined' ? Garp = {} : true;
	
	Garp.animateFlashMessage = function() {
		var fm = $('#flashMessage');
		fm.fadeOut(0.001,function(){
			fm.css({opacity:.95});
			fm.fadeIn(400, function(){
				setTimeout(function(){
					fm.fadeOut(800, function(){
						fm.remove();
					});
				}, 2500);
			});
		});
	};
})();	