	
	var img_index = 0;
	var page_index = 0;
	var total_pages = 0;
	var th_width = 0;
	var page_width = 0;
	var start_left = 0;
	var over = null;
	var col_unit = 0;
	var visible_cols = 5;
	var reverseLoop = false;
	
	function removeStr(s,r){
		return s.substr(0,s.indexOf(r));
	}
	
	function removeThumbRef(s){
		var start = s.substr(0,s.indexOf('.thumb'));
		var suffix = s.substr(s.lastIndexOf('.'));
		return start+suffix;
	}
	
	$(document).ready(function(){
							   
		
		$(".th_ctr").css("width","9999px");
		$(".th_ctr").css("position","relative");
		$(".th_mask").css("overflow","hidden");
		$(".th a").each(function(){
			var index = $(".th a").index(this);
			$(this).attr("target","");
			$(this).attr("href", "#"+ $(".th a:eq("+index+") img").attr("alt").replace(" ","-"));
		});
							   
		total_pages = Math.ceil($(".th_ctr img").length / visible_cols);
		if(total_pages>1){
			$("#page_nav").css("visibility","visible");	
		}
		var ths = $('.th');
		//alert($(ths[ths.length-1]).css("width"));
		th_width = $('.th').length > 0 ? $('.th:eq(0)').css("width") : 0;
		
		//th_width = th_width * 5;
		th_width = th_width.substr(0, th_width.indexOf("px"));
		
		over = ($('.th').length)%visible_cols;
		//alert(over);
		col_unit = $('.th:eq(0)').css("width");
		col_unit = removeStr(col_unit,'px');
		
		page_width = $('.th_mask').css('width');
		page_width = removeStr(page_width,'px');
		start_left = $('.th_ctr').css('left');
		start_left = Number(removeStr(start_left,'px'));
		
		$(".th a").click(function(){
										var index = $(".th a").index(this);
										var sel_img = $(this).get(0).getElementsByTagName("img")[0];
										var s = sel_img.src;
										update(index, s, $(this));
		});
		
		$(".next a").click(function(){
										nextPage();
										moveNext();
										updateTracker();
		});
		
		$(".prev a").click(function(){
										previousPage();
										movePrev();
										updateTracker();
		});
		
		updateTracker();
		
	});
	
	function movePrev(){
		var rev = reverseLoop;
		previousPage();
		updateTracker();
		//alert(rev);
		if(page_index==0){
			var check_width = 0;
		}else if(page_index!=0 && reverseLoop){
			//alert("dog");
			var check_width = (page_index*page_width) - ((visible_cols-over) * (Number(col_unit)-10));
		}else{
			//alert('cat');
			var check_width = page_index*page_width;
		}
			var new_margin = -1*((check_width)-start_left) + "px";
		$(".th_ctr").animate({ 
			left:new_margin
		  }, 1000, "swing" );
	}
	
	function moveNext(){
		if(page_index==total_pages-1){
			var check_width = (page_index*page_width) - ((visible_cols-over) * Number(col_unit));
		}else{
			var check_width = page_index*page_width;
			//alert("bottom");
		}
			var new_margin = -1*((check_width)-start_left) + "px";
			//alert(check_width);
			//alert(new_margin);
		$(".th_ctr").animate({ 
			left:new_margin
		  }, 1000, "swing" );
		
	}
	
	
	function update(n,s,l){
		//$(".large_img img").attr('src', removeThumbRef(s,'.thumb'));
		$(".large_img img").attr('src', $(".th:eq("+n+") input").attr('value'));
		if($(l).attr("title").length > 0){
			$(".large_img h2 span").text($(l).attr("title"));
			$(".large_img h2").removeClass("hide");
		}else{
			$(".large_img h2").addClass("hide");
		}
		$(".th a").each(function(){
								 	$(this).attr("class", "");
								 });
		l.attr("class", "active");
	}
	
	function nextPage(){
		if(page_index < total_pages-1){
			page_index++;	
		}
	}
	
	function previousPage(){
		if(page_index > 0){
			page_index--;	
		}
	}
	
	function updateTracker(){
		nextDisplay();
		prevDisplay();
		$(".page_tracker .current_page").text(page_index+1);
	}
	
	function nextDisplay(){
		if(page_index>=total_pages-1){
			fade_btn(".next");
		}else{
			show_btn(".next");	
		}
	}
	
	function prevDisplay(){
		if(page_index<=0){
			fade_btn(".prev");
		}else{
			show_btn(".prev");	
		}
	}
	
	function fade_btn(btn){
		$(btn + " a").css("color","#C2C2C2");
		$(btn + " a").attr("class", "disabled");
	}
	
	function show_btn(btn){
		$(btn + " a").css("color","#1a1a1a");
		$(btn + " a").attr("class", "");
	}