var curr_first = 0;
var count_pic = 0;
var stoper = 0;
var prelImg = new Image();
prelImg.src = "/img/preloader.gif";

$(document).ready(function(){ 
	count_pic = $("#thumbs").children().size();
	if (count_pic > 6)
		count_pic -= 10; 
	stoper = 1;
	curr_first = parseInt($("#thumbs").css("left").substr(1))/125;
	
	var q = parseInt($(".img_sel").attr("id").substr(3));
	rep(q);
});

function scroll_left() {	
	if (stoper) {
		stoper = 0;
		if (curr_first < 5) {
			curr_first = count_pic + curr_first;
			$("#thumbs").css({"left": "-" + curr_first*125 + "px"});
		} 
		stoper = 1;
		scroll_line(-5);				
	}
}

function scroll_right() {
	if (stoper) {
		stoper = 0;
		if (curr_first >= count_pic) {
			curr_first = curr_first - count_pic;
			$("#thumbs").css({"left": "-" + curr_first*125 + "px"});
		}
		stoper = 1;
		scroll_line(5);
	}
}

function scroll_line (dist) {
	if (stoper) {
		stoper = 0;
		var dif = - dist*125;
		curr_first = curr_first + dist;
		
		$("#thumbs").animate({"left": "+=" + dif + "px"}, "slow", null, function() {
			if (curr_first < 5) {
				curr_first = curr_first + count_pic;
				$("#thumbs").css({"left": "-" + curr_first*125 + "px"});
			}
			else
				if (curr_first >= count_pic + 5) {
					curr_first = curr_first - count_pic;
					$("#thumbs").css({"left": "-" + curr_first*125 + "px"});
				}
			stoper = 1;
		});
	}
}

function scroll_round (dist, side) {
	if (side == "<--") {
	}
}

function scroll_to (id_int) {
	
	if (((id_int >= curr_first)&&(id_int < curr_first + 6))||((id_int + count_pic >= curr_first)&&(id_int + count_pic < curr_first + 6))) return true;
	//alert(curr_first + "    " + id_int);
	var to_first = 2;
	
	var sc_l, sc_r;
	if (curr_first < id_int) {
		
		//sc_l = curr_first + to_first - 5 + count_pic - id_int;
		sc_r = id_int - (curr_first + to_first);
		//if (sc_l < sc_r) alert("in other way");
		//else 
			scroll_line(sc_r);
	}
	else {
		
		sc_l = curr_first + to_first - id_int;
		//sc_r = id_int - 5 + count_pic - (curr_first + to_first);
		//if (sc_r < sc_l) alert("in other way");
		//else 
			scroll_line(- sc_l);
	}
	
}

function sel_to_unsel(t) {
	var sel_img_src = $("#img_" + t).attr("src");
	if (sel_img_src) {
		var a_href = sel_img_src.substr(sel_img_src.lastIndexOf('/') + 5,sel_img_src.length);
		$("#li_" + t).replaceWith("<li class='thumb' id='li_" + t + "'><a href='" + a_href + "' id='a_" + t + "' onclick='rep(" + t + ");return false;' ><img src='" + sel_img_src + "' alt='' id='img_" + t +"' /></a></li>");
	}
}

function unsel_to_sel(t) {
	$("#li_" + t).replaceWith("<li class='img_sel' id='li_" + t + "'>" + $("#a_" + t).html() + "</li>");
}

function rep(i) {
	if (stoper) {
		stoper = 0;
		
		var imgW, imgH;
		
		//alert(i);
		var path = $("#a_" + i).attr("href");
		if (!path) path = $("#img_" + i).attr("src");
		var largePath = "/content/pictures/" + path.substr(path.length - 36);
		var largeAlt = $("#img_" + i).attr("alt");
		
		
		$(".sold").fadeOut(1000);
		$(".description").fadeOut(1000, function() {$("#preloader_d").css({"display" : "block"});});
		$("#visible").fadeOut(1000, function () {
			$("#preloader").css({"display" : "block"});
			$.post("/controllers/ajax.php", { 'act': "change_description", 'hash': path.substr(path.length - 36, 32) }, function(res) {
				//alert(path.substr(path.length - 36, 32));
				
				var img = document.createElement('img');
				img.onload = function () {
					$("#preloader").css({"display" : "none"});
					//alert ("hello");
					imgW = this.width;
					imgH = this.height;
					
					$(".sold").css({"display" : "none"});
					
					if (res.indexOf("Already SOLD") > 0) {
						$(".sold").css({"top" : 210 - (imgH + 1)/2 + "px", "right" : 261 - (imgW + 1)/2 + "px"});
						$(".sold").fadeIn(1000);
					}
					
					
					$("#invisible").html("<div class='inner'><img src='" + largePath + "' alt='" + largeAlt + "' /></div>");
					$("#invisible").fadeIn(1000, function(){
						$("#invisible").attr("id", "temp"); 
						$("#visible").attr("id", "invisible"); 
						$("#temp").attr("id","visible");
						stoper = 1;
					});
				};
				$(img).attr('src', largePath);
				
				$("#preloader_d").css({"display" : "none"});
				$(".description").html(res);
				$(".description").fadeIn(1000);
			});
			
		});
		
		var j = parseInt($(".img_sel").attr("id").substr(3));

		sel_to_unsel(j);
		if (count_pic > 6) {
			if (j < 10) 						
				sel_to_unsel(j + count_pic);
			if (j >= count_pic) 
				sel_to_unsel(j - count_pic);
			if (j + count_pic < 10)
				sel_to_unsel(j + 2*count_pic);
		}
			
		unsel_to_sel(i);
		if (count_pic > 6) {
			if (i < 10) 
				unsel_to_sel(i + count_pic);
			if (i >= count_pic) 
				unsel_to_sel(i - count_pic);
			if (i + count_pic < 10)
				unsel_to_sel(i + 2*count_pic);
			if (i - 2*count_pic >= 0)
				unsel_to_sel(i - 2*count_pic);
		}
		
		return false;
	}
}
		
function prev_big() {
	if (stoper) {
		stoper = 0;
		var j = parseInt($(".img_sel").attr("id").substr(3));
		//alert(j);
		if (count_pic > 6) {
			if (j < 5) j += count_pic;
			
			if (count_pic > 6) {
				var f = j;
				stoper = 1;
				if ((f == curr_first)||(f + count_pic == curr_first)) scroll_left();
				else scroll_to(f - 1);
			}
		}
		
		if (j == 0)
			j = count_pic;
		
		stoper = 1;
		rep(j-1);
	}		
}

function next_big() {
	if (stoper) {
		stoper = 0;
		var j = parseInt($(".img_sel").attr("id").substr(3));
		if (count_pic > 6) {
			if (j < 5) j += count_pic;
			
			if (count_pic > 6) {
				var f = j - 5;
				stoper = 1;
				if ((f == curr_first)||(f + count_pic == curr_first)) scroll_right();
				else scroll_to(f + 6);
			}				
		}
		
		if (j == count_pic - 1)
			j = -1;	
		stoper = 1;
		rep(j+1);
	}
}

function add_to_cart() {
	if (stoper) {
		stoper = 0;
		if ($(".description").html().indexOf("Already SOLD") > 0) {
			$("#alr_sold").fadeIn(500, function () {$("#alr_sold").fadeOut(500); stoper = 1;});
		}
		else {
			var sel_img_src = $("#img_" + $(".img_sel").attr("id").substr(3)).attr("src");
			var int_hash = sel_img_src.substr(sel_img_src.length - 36, 32);
			
			$.post("/controllers/ajax.php", { act: "add_to_cart", hash: int_hash }, function(res) {
				//alert(res);
				switch (res) {
				case '1':
					$(".big_border").append("<div class='outer' id='virtual' style='z-index: 3'>" + $("#visible").html() + "</div>");
					$("#virtual").css({"opacity": "1"});
					$("#virtual").animate({"left": "650px", "top": "-60px", "width": "0px", "height": "0px", "opacity": "0"}, "slow", null, function() {
						$("#virtual").remove();
						var byed;
						if ($("#cart").text().substr(0,1) != "(")
							byed = 1;
						else byed = parseInt($("#cart").text().substr(1,3)) + 1;
						$("#cart").text("(" + byed + ") Shopping cart");
						stoper = 1;
					});
					break;
				case '2':
					$("#alr_incart").fadeIn(500, function () {$("#alr_incart").fadeOut(500); stoper = 1;});
					break;
				default:
					alert("O.o");
					break;
				}
			});
			
		}
	}
}
