jQuery(document).ready(function() {
	
	jQuery(".section_thumbs a img").each(function (i) {
    var img_src = this.src;
    
   jQuery(this).attr("src", img_src.replace("images/", "images/small/tn_"));
    
  });
  
	jQuery(".section_thumbs img").load( function(){
		
		jQuery(".section_thumbs img").each(function (i) {
	
			jQuery(this).css({"margin-top": ((77 - this.height) / 2)});

		});
	
	});
	
	
	var	num = 1;
	jQuery(".section_thumbs a").each(function (i) {

		jQuery(this).attr("id", "anchor_img_" + num++);

	});
	
	
	jQuery(".content_copypage .section_thumbs, .content-first .section_thumbs").css({"width":"auto"});
 

	jQuery("#jgallery_count").val(jQuery(".maincontent .section_thumbs a, .content-first .section_thumbs a").size());
	jQuery(".jgallery_left").css({"visibility":"hidden"});
	
	jQuery(".maincontent .section_thumbs a, .content-first .section_thumbs a").attr("href","JavaScript:void(0);");
	
	jQuery(".maincontent .section_thumbs, .content-first .section_thumbs").css({"width": jQuery(".maincontent .section_thumbs a, .content-first .section_thumbs a").size() * 138});
	
	
	if(jQuery("#jgallery_count").val() <= 5){
		jQuery(".jgallery_right").css({"visibility":"hidden"});
	}
	
	if(jQuery(".maincontent .section_thumbs a, .content-first .section_thumbs a").size() == 0){
		jQuery(".jgallery_right").css({"display":"none"});
	}
	
	if(jQuery(".maincontent .section_thumbs a, .content-first .section_thumbs a").size() <= 0){
		//$("#jgallery_thumbs").remove();
	}
	
	jQuery(".product_image").append("<img id=\"ajax_fixed\" style=\"width: 1px; position: absolute; visibility: hidden; border:medium none;\" src=\"template/ajax-loader.gif\">")
			
	jQuery(".product_image #ajax_fixed").css({"height": jQuery(".product_image img:first").height()});
			
	jQuery(".maincontent .section_thumbs a, .content-first .section_thumbs a").click( function(){
		
		
		
		
		
		var image_location = jQuery("#" + this.id + " img")[0].src;
		
		
	
		var image_location = image_location.replace("rorygibigstdkid_mg_t2", "rorygibigstdkid_mg_lg");
		var image_location = image_location.replace("lattice", "@-");
		var image_location = image_location.replace("beautiful", "@&");
		var image_location = image_location.replace("action", "@$");
		var image_location = image_location.replace("rustic", "@!");
		var image_location = image_location.replace("tiamo", "@@");
		var image_location = image_location.replace("ti_amo", "@%");
		var image_location = image_location.replace("lentini", "@*");
		var image_location = image_location.replace("potting", "@+");
		var image_location = image_location.replace("patio", "@#");
		var image_location = image_location.replace("ti", "lg");
		var image_location = image_location.replace("@#", "patio");
		var image_location = image_location.replace("@+", "potting");
		var image_location = image_location.replace("@*", "lentini");
		var image_location = image_location.replace("@@", "tiamo");
		var image_location = image_location.replace("@%", "ti_amo");
		var image_location = image_location.replace("@!", "rustic");
		var image_location = image_location.replace("@$", "action");
		var image_location = image_location.replace("@&", "beautiful");
		var image_location = image_location.replace("@-", "lattice");
		var image_location = image_location.replace("/small/tn_", "/");
		
		var image_alt = jQuery("#" + this.id + " img")[0].alt;
		var image_height = jQuery(".product_image img");
		
			jQuery(".product_image").css({ "height" : image_height.height()});		
			jQuery(".product_image #ajax_fixed").css({"height": jQuery(".product_image img:first").height()});
		
		setTimeout(function(){
		
			jQuery(".product_image img:first").fadeOut(500, function(){
				
				jQuery("#ajax_loader").remove();
				
				jQuery(".product_title").css({"visibility":"hidden"});
				
				jQuery(".product_image").before("<img id=\"ajax_loader\" style=\"width: 100px; position: absolute; margin: " + (image_height.height() / 2 - 40) + "px 0px 0px 320px; display: block; border:medium none;\" src=\"template/ajax-loader.gif\">")
				
				jQuery(".product_image img").attr("src", image_location);
				
				jQuery(".product_title").html(image_alt);
				
			}).load( function(){
				jQuery("#ajax_loader").remove();
				jQuery(".product_title").css({"visibility":"visible"});
				jQuery(".product_image img, .product_title").fadeIn(1000);
				jQuery(".product_image").css({ "height" : image_height.height()});	
			})
			
		},100);

		return false;
	});
	
	
	jQuery(".jgallery_left").click( function(){
		
			jQuery(".jgallery_right").css({"visibility":"visible"});
		
			
			if(jQuery("#jgallery_count").val() >= (jQuery(".maincontent .section_thumbs a, .content-first .section_thumbs a").size() - 5) ){
				jQuery(".jgallery_left").css({"visibility":"hidden"});
			}
			else{
				jQuery(".jgallery_left").css({"visibility":"visible"});
			}
			
			jQuery("#jgallery_count").val(parseInt(jQuery("#jgallery_count").val()) + 5 );
			jQuery(".maincontent .section_thumbs, .content-first .section_thumbs").animate({"left": "+=690px"}, 1500);
			
	});
	
	jQuery(".jgallery_right").click( function(){
		
			jQuery(".jgallery_left").css({"visibility":"visible"});
		
			if(jQuery("#jgallery_count").val() <= 10 ){
				jQuery(".jgallery_right").css({"visibility":"hidden"});
			}
			else{
				jQuery(".jgallery_right").css({"visibility":"visible"});
			}
			
			jQuery("#jgallery_count").val(parseInt(jQuery("#jgallery_count").val()) - 5 );
			jQuery(".maincontent .section_thumbs, .content-first .section_thumbs").animate({"left": "-=690px"}, 1500);
	});
  	  	
});