$(document).ready(function(){
	//	Randomize Search Values
		var searchTerms = new Array("War and Peace", "Michael Pollan", "0324271123", "Art History", "\"civil war generals\"");
		var termNumber = 1;
		var switcher;
		function switchTerm() {
			$('.keyword').fadeOut("fast",function(){
				$(this).val(searchTerms[termNumber]).fadeIn("fast");
				if(termNumber >= searchTerms.length-1) {
					termNumber = 0;
				} else {
					termNumber++;
				}
			});
		}
		for(i=0;i<searchTerms.length;i++){
			if($('.keyword').val() == searchTerms[i]){
				switcher = setInterval(switchTerm, 7000);
			}
		}
	//	Search Field Focus/Blur handling
		var userValue;
		$('.keyword').focus(function(){
			$(this).select();
			clearInterval(switcher);
		});
		$('.keyword').blur(function(){
			for(i=0;i<searchTerms.length;i++){
				if($('.keyword').val() == searchTerms[i]){
					switcher = setInterval(switchTerm, 7000);
					userValue = false;
				}
				else { userValue = true; }
			}
		});

		
	//	Baking cookies
		function setCookie(name, value, expires) {
			document.cookie = name + "=" + escape(value) + "; path=/" + ((expires == null) ? "" : "; expires=" + expires.toGMTString());
		}
		var expdate = new Date ();
		expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000*365)); // 1 year
	
	//	Banner function
		$('#bannerclose').click(function(event){
			event.preventDefault();
			$('#banner').slideToggle("normal");
			setCookie('HasVisited', 'Yes', expdate);
		});
	
	//	Tabs
		$('#index #buybooks a').click(function(event){
			event.preventDefault();
			$('#index').attr('class', 'buy');
			if (userValue) {
				$('#buy .searchfield input').val($('#sell .searchfield input').val());
			}
			if (document.location.search || isIE) { document.location.search = "tab=buy"; }
		});
		$('#index #sellbooks a').click(function(event){
			event.preventDefault();
			$('#index').attr('class', 'sell');
			if (userValue) {
				$('#sell .searchfield input').val($('#buy .searchfield input').val());
			}
			if (document.location.search || isIE) { document.location.search = "tab=sell"; }
		});
		$('#index #browse a').click(function(event){
			event.preventDefault();
			$('#index').attr('class', 'browse');
			if (document.location.search || isIE) { document.location.search = "tab=browse"; }
				
		});
	
	//	Large Image pop-up
		function popUpLarge(imageLink) {
			$('body').append("<div class=\"popupoverlay\"></div>");
			$('<img />')
			    .attr('src', imageLink)
			    .load(function(){
			        $('.popupoverlay').after( "<div class=\"popup\"><a href=\"#\"><img src=\""+imageLink+"\" /></a></div>" );
					$('.popupoverlay').css('height', $('#container').height());
					$('.popup').click(function(event){
						event.preventDefault();
						closePopUp();
					});
			        $('.popup a').css("width",$('.popup img').width()).click(function(event){
						event.preventDefault();
						closePopUp();
					});
			    });
			$('.popupoverlay').click(function(){
				closePopUp();
			})
		}
		function closePopUp() {
			$('.popupoverlay').remove();
			$('.popup').remove();
		}
		$('#enlargeimage').click(function(event){
			event.preventDefault();
			popUpLarge($(this).attr("href"));
		});
	//	"Loading" Pop-up
		var loaderimg = $('<img />').attr('src', '/images/priceloader.gif').attr('class', 'loader');
		function popUpLoader() {
			$('#container').append("<div id=\"popupoverlay\"</div>");
			$('#popupoverlay').css('height', $('#container').height());
			$('#popupoverlay').after("<div class=\"popuploader trans\"><div class=\"popupcontent\">Please Wait <small>Loading Seller Information & Prices</small></div></div>");
			$('.popupcontent').append(loaderimg);
			if(isIE) { 
				$(".popuploader").css("top", $(window).scrollTop() + $(window).height()/2 + "px");
				$(window).scroll(function() {
				    $(".popuploader").css("top", $(window).scrollTop() + $(window).height()/2 + "px");
				});
			 }
		}
		$('.buy .item a').click(function(){
			popUpLoader();
		})
	
	//	Pretty-up price format
		var singlePrice = $('#singlelisting .price .price').text();
		$('#singlelisting .price .price').text(singlePrice.replace('$', ''));
		
	//	Add long description to singleresult pages
		$('#singlelisting .description .trim .ellipsis').append(" <a href=\"#\" class=\"readmore\">Read More &rsaquo;</a>");
		$('#singlelisting .description .trim .readmore').click(function(event){
			event.preventDefault();
			$('#singlelisting .description .trim').hide('fast');
			$('#singlelisting .description .full').show('normal');
		});
		$('#singlelisting .description .full').append("<p><a href=\"#\" class=\"shorten\">Shorten Up</a></p>");
		$('#singlelisting .description .full .shorten').click(function(event){
			event.preventDefault();
			$('#singlelisting .description .full').hide('fast');
			$('#singlelisting .description .trim').show('normal');
		})
		
	//	Fun Form Stuff
		$('form#new_listing input').focus(function(){
			$(this).prev("label").addClass("selected");
		});
		$('form#new_listing input').blur(function(){
			$(this).prev("label").removeClass("selected");
		});
		$('form#new_listing .sellingprice input').focus(function(){
			$(this).parent().prev("label").addClass("selected");
		});
		$('form#new_listing .sellingprice input').blur(function(){
			$(this).parent().prev("label").removeClass("selected");
		});
		$('form#new_listing textarea').focus(function(){
			$(this).prev("label").addClass("selected");
		});
		$('form#new_listing textarea').blur(function(){
			$(this).prev("label").removeClass("selected");
		});
});