$(document).ready(function(){
		/*counter = 1;*/
		if( $("body").is(".pg2") ) {
			counter = 2;
			}
		else if( $("body").is(".pg3") ) {
			counter = 3;
			}
		else if( $("body").is(".pg4") ) {
			counter = 4;
			}
		else if( $("body").is(".pg5") ) {
			counter = 5;
			}
		else {
			counter = 1;
			}
		$(".gallerynav").replaceWith('<p class="gallerynav"><a href="#" id="back" class="back" >previous page</a><em class="back">previous page</em> | <a href="#" id="next" class="next" >next page</a><em class="next">next page</em></p>');
		
		
		if ( $("#gallery").is(".last") ) {
			$("a.next").css({'left' : '-5000px', 'position' : 'absolute'});
			$("em.back").css({'left' : '-5000px', 'position' : 'absolute'});
		}
		else if ( $("#gallery").is(".mid") ) {
			$("em.next").css({'left' : '-5000px', 'position' : 'absolute'});
			$("em.back").css({'left' : '-5000px', 'position' : 'absolute'});
		}
		
		else {
			$("em.next").css({'left' : '-5000px', 'position' : 'absolute'});
			$("a.back").css({'left' : '-5000px', 'position' : 'absolute'});
		}
		
		var pheight = $(".prodbox").eq(0).height();
		var pheight = pheight+pheight+80;
		var startheight = pheight*counter-pheight;
		$("#slider").animate( {top: "-="+startheight+"px"}, {queue:true, duration:1});
		$("#gallery").css({'position' : 'relative', 'overflow' : 'hidden'});
		$("#slider").css({'position' : 'absolute'});
		$("#gallery").css({'height' : pheight});
		kids = $("#slider").children().length;
		if (kids<7) {
			$("a.next").css({'left' : '-5000px', 'position' : 'absolute'});
			$("a.back").css({'left' : '-5000px', 'position' : 'absolute'});
			$("em.next").css({'left' : '0px', 'position' : 'relative'});
			$("em.back").css({'left' : '0px', 'position' : 'relative'});
		}
		kids=kids/6;
		pages=Math.ceil(kids);
    $("a#next").click(function() {
			counter++;
			$("#slider").animate( {top: "-="+pheight+"px"}, {queue:true, duration:1});
			if (counter==pages) {$("a.next").css({'left' : '-5000px', 'position' : 'absolute'}); $("em.next").css({'left' : '0px', 'position' : 'relative'});};
			if (counter!=pages) {$("a.next").css({'left' : '0px', 'position' : 'relative'}); $("em.next").css({'left' : '-5000px', 'position' : 'absolute'});};
			if (counter!=1) {$("a.back").css({'left' : '0px', 'position' : 'relative'}); $("em.back").css({'left' : '-5000px', 'position' : 'absolute'});};
			return false;	
		});
		$("a#back").click(function() {
			counter--;
			$("#slider").animate( {top: "+="+pheight+"px"}, {queue:true, duration:1});
			if (counter==1) {$("a.back").css({'left' : '-5000px', 'position' : 'absolute'}); $("em.back").css({'left' : '0px', 'position' : 'relative'});};
			if (counter!=pages) {$("a.next").css({'left' : '0px', 'position' : 'relative'}); $("em.next").css({'left' : '-5000px', 'position' : 'absolute'});};
			return false;	
		});
   });