var ready_to_show = new Object();
var gallery = new Object(); 




var top_frame = 0;
var top_img = 0;
var bottom_frame = 0;
var bottom_img = 0;
var top_frame_parent = 0;
var bottom_frame_parent = 0;

var scroller_body = document.getElementById("scroller_body");
var step = 1;
var move_direction = 0;
var total_innerHTML = '';
var first_move = 1;
var total = 0;
var pics = new Array();

if (user_pics) {
	for(i in user_pics) {
		pics.push(i);
		total++;
	}
}

var numbers = new Object();
numbers["/"] = "http://pics."+path_domain+"/pics/modern/albums/slash.gif";
for(i=0; i<=9; i++) {
	numbers[i]="http://pics."+path_domain+"/pics/modern/albums/"+i+".gif";
}

var max_position = total - 1;
var cur_position = 0;
var real_position = 0;
var old_direction = 0;
var spec = 0;
var rp = 0;
var ztop = 0;
var zbottom = 0;
var pressed = 0;
var init_scroll = new Array();
var debug = 0;

if (!user_pics[cur_img_id]) cur_img_id = 0;
for(var i=0; i<=max_position; i++) {
	if (!cur_img_id) cur_img_id = pics[i];
	if (pics[i]==cur_img_id) cur_position = i; // find out current position if pic ID
}

if (!cur_position && !cur_img_id) { // find out current position if NO pic ID
	if (total>5) cur_position = 3;
	else cur_position = Math.round(total/2);
}

var total = String(total);
var total_len = total.length;
for(i=0; i<=total_len; i++) {
	var curr_char = total.charAt(i);
	if (curr_char) total_innerHTML="<img src=\'"+numbers[curr_char]+"\' >"+total_innerHTML;
}
real_position = cur_position;

function create_scroller() {
	if (total>6) {
		scroller_body.style.height = "296px";
		var par_table = document.getElementById("scroller_table");
		if (par_table) {
			par_table.style.height = "330px";
		}

		var start = (cur_position>3) ? (cur_position-3) : 0;
		var end = (cur_position<(total-4)) ? (cur_position+3) : max_position;
		if (end==max_position) start = max_position - 5;
		if (start==0) end = 5;
		var num_moves = 0;
		if ( (cur_position+3>max_position) || (cur_position-3<0) ) {
			// we need to create right init_scroll
			if (cur_position+3>max_position) {
				var n = (cur_position - 3 );
				for(i=n; i<=max_position; i++) init_scroll.push(i); // standart
				var n = 6 - init_scroll.length;
				for(i=0; i<=n; i++) init_scroll.push(i);
				
				var loop_dir = 1;
				var t = max_position - cur_position;
				switch(t){
					case 0: num_moves = 3; break;
					case 1: num_moves = 2; break;
					case 2: num_moves = 1; break;
				}
				cur_position = max_position-3;
			} else if (cur_position-3<0) {
				var n = parseInt(max_position) + parseInt(cur_position-2); // actually -, not +
				n = Math.abs(n);
				for(i=n; i<=max_position; i++) init_scroll.push(i);
				var n = 6 - init_scroll.length;
				for(i=0; i<=n; i++) init_scroll.push(i);// standart

				var loop_dir = 0;
				switch(cur_position){
					case 0: num_moves = 3; break;
					case 1: num_moves = 2; break;
					case 2: num_moves = 1; break;
				}
				cur_position = 3;
			}
		} else {
			var n = cur_position - 3;
			var n2 = cur_position + 3;
			for(i=n; i<=n2; i++) init_scroll.push(i);
		}
		for(i in init_scroll) {
			hide = (i==0 || i==6) ? 'hide':'nohide';
			create_element(init_scroll[i], init_scroll[0], init_scroll[6], 0, parseInt(init_scroll[i])+1, hide);
		}
		// compensate
		for(i=0; i<num_moves; i++) up_loop(loop_dir);
	} else { 
		
		var top_arrow = document.getElementById("top_arrow");
		var bottom_arrow = document.getElementById("bottom_arrow");

		top_arrow.src = "http://pics."+path_domain+"/pics/modern/album_filmstrip_up_gray.gif";
		bottom_arrow.src = "http://pics."+path_domain+"/pics/modern/album_filmstrip_down_gray.gif";
		top_arrow.setAttribute("onmousedown", "return false;", 0);
		bottom_arrow.setAttribute("onmousedown", "return false;", 0);
		top_arrow.style.cursor = 'default';
		bottom_arrow.style.cursor = 'default';
		cur_position = 0;
		for(var i=0; i<=max_position; i++) create_element(i, 0, max_position, 0, i+1);
	}
	setTimeout("show_body()", 1000);
}

function show_body() {	scroller_body.style.display = "block"; }

function create_element(i, start, end, up_flag, position, hide) {
	// i must be position to draw on frame(i+1)
	var tcreate_numbers = create_numbers;
	var div_dummy = document.createElement("DIV");
	if (div_dummy) {
		div_dummy.setAttribute("id", "frame_td_"+pics[i], 0);
		div_dummy.setAttribute("position", position, 0);
		div_dummy.style.overflow = "hidden";
		div_dummy.style.height = "60px";
		if ( (!hide) || (hide=='hide') ) {
			if ( (total>6) && ((i<=start && i>0) || (i>=end && i<max_position) || hide=='hide') ) {
				div_dummy.style.height = "1px";
				div_dummy.style.display = "none";
			}
		}

		var div = document.createElement("DIV");
		if (div) {
			div.style.overflow = "hidden";
			if (cur_img_id) {
				if (pics[i] == cur_img_id) div.className = "albumFilmstripActive";
				else div.className = "albumFilmstripPassive";
			} else cur_img_id = pics[i];
			div.setAttribute("id", "frame_div_"+pics[i], 0);
			div.style.position = "relative";
			var img = create_photo(user_pics[pics[i]], i, pics[i], 0);
			if (img) {
				img.style.cursor = "pointer";
				img.style.width = "60px";
				img.style.height = "60px";
				img.style.position = "relative";
				img.style.verticalAlign = "top";
				img.style.left = "15px";
				div.appendChild(img);
				div_numbers = tcreate_numbers(position, total);
				if (div_numbers) div.appendChild(div_numbers);
			}

			div_dummy.appendChild(div);
			if (!up_flag) scroller_body.appendChild(div_dummy);
			else scroller_body.insertBefore(div_dummy, scroller_body.childNodes[0]);
		}
	}
}
function up_loop(direction) {
	if (direction) {// DOWN (left)
		var elem = pics.shift(); // get first
		pics.push(elem); //put last
	} else { // UP (right)
		var elem = pics.pop(); // get last element
		pics.unshift(elem); //put first
	}
}

function tmove( direction ) {
	var new_frame_created = spec = 0;
	if (!move_direction) {
		var add = (spec && (direction!=old_direction)) ? 1 : 0; // for continious scrolling
		var bm = (direction!=old_direction) ? 1 : 0;
		old_direction = direction;
		move_direction = (direction) ? 1 : 2;

		//--------------------//
		// esli list zakonchilsja i iest dvizgenije v tom zge napravlenii
		if (direction && zbottom==max_position && !first_move && !bm) { // when list is ended / DOWN
			up_loop(direction); //down
		} else if (!direction && ztop==0 && !first_move && !bm) { // when list is ended / UP
			up_loop(direction); //up
		}
		//--------------------//
		if (direction) { // down
			cur_position = ((cur_position+2)<max_position) ? (cur_position+1) : (max_position-2); //next position
			ztop = cur_position - 3;
			zbottom = cur_position + 2;				
			if (real_position==max_position) real_position = 0; // drop to 0
			else real_position++;
			rp = real_position + 3;
			if (rp > total) rp = parseInt(rp) - parseInt(total);
		} else { // up
			cur_position = ((cur_position-2)>0) ? (cur_position-1) : 2;
			ztop = cur_position - 2;
			zbottom = cur_position + 3;				
			if (real_position==0) real_position = max_position;
			else real_position--;				
			rp = real_position - 1;
			if (rp <= 0) rp = parseInt(rp) + parseInt(total);
		}
		// check whether the next frame exists
		top_frame = document.getElementById("frame_div_"+pics[ztop]);
		bottom_frame = document.getElementById("frame_div_"+pics[zbottom]);

		//*************************************************************//

		// create next frame if it not existed yet
		if ( !top_frame || !bottom_frame) {
			var dt = new Date();
			new_frame_created = 1;

			if (!top_frame) {
				if (rp>=0) { // normal move up
					create_element(ztop, zbottom, zbottom, 1, rp, 1); // add at the top of image list
				}
			}

			if (!bottom_frame) {
				if (zbottom<=max_position) { // normal move down
					create_element(zbottom, 0, 0, 0, rp, 1);
				}
			}
		}
		//*************************************************************//
		top_frame = document.getElementById("frame_div_"+pics[ztop]);
		top_frame_parent = top_frame.parentNode;				
		top_img = document.getElementById("frame_img_"+pics[ztop]);
		if (!top_img.src) top_img.setAttribute("src", user_pics[pics[ztop]], 0); // current image preload

		bottom_frame = document.getElementById("frame_div_"+pics[zbottom]);

		bottom_frame_parent = bottom_frame.parentNode;
		bottom_img = document.getElementById("frame_img_"+pics[zbottom]);
		if (!bottom_img.src) bottom_img.setAttribute("src", user_pics[pics[zbottom]], 0); // current image preload

		//****************************//
		// # esli list zakonchilsja i nuzgniy element sozdan no trebujutsja perenesti ego v protivopolozgnuju poziciju
		// frame loop moving start	
		
		if (direction && zbottom==max_position && !first_move && !new_frame_created) { // when list is ended / down
			spec = 1;
			var div = scroller_body.childNodes[0]; // first frame
			if (div) {
				if (div && div.getAttribute) {
					var id = div.getAttribute('id');
					if (id) {
						id = id.replace('frame_td_', '');
						if (id==pics[zbottom]) { // only than loop frames
							var sub_div = div.childNodes[0];
							var img = sub_div.childNodes[0];
							div.style.top = "0px";
							sub_div.style.top = "0px";
							scroller_body.removeChild(div);
							scroller_body.appendChild(div);
						}
					}
				}
			}
		} else if (!direction && ztop==0 && !first_move && !new_frame_created) { // when list is ended / up
			var div = scroller_body.childNodes[total-1]; //last frame
			if (div) {
				if (div && div.getAttribute) {
					var id = div.getAttribute('id');
					if (id) {
						id = id.replace('frame_td_', '');
						if (id==pics[ztop]) { // only than loop frames
							var sub_div = div.childNodes[0];
							var img = sub_div.childNodes[0];
							div.style.top = "0px";
							sub_div.style.top = "0px";
							scroller_body.removeChild(div);
							scroller_body.insertBefore(div, scroller_body.childNodes[0]);
						}
					}
				}
			}
		}

		top_frame_parent.style.display = "block";
		bottom_frame_parent.style.display = "block";
		if (direction) move_down();
		else move_up();

		first_move = 0;
	}
	return false;
}

function move_up() {
	if ((step<59) && (move_direction==2)) {
		if (step<35) var delta = 12;
		else if (step<45) var delta = 8;
		else if (step<55) var delta = 4;
		else var delta = 1;
		step+=delta;

		top_frame.style.top = (step-59) + "px";
		top_frame_parent.style.height = step + "px";				
		bottom_frame_parent.style.height = (60 - step) + "px";
		setTimeout("move_up()", 20);
	} else {
		// move is finished, finalize
		bottom_frame_parent.style.display = "none";
		move_direction = step = 0;
		if (pressed) setTimeout("tmove(0)", 20);
	}
}

function move_down() {
	if ((step<59) && (move_direction==1)) {
		if (step<35) var delta = 12;
		else if (step<45) var delta = 8;
		else if (step<55) var delta = 4;
		else var delta = 1;
		step+= delta;
		bottom_frame_parent.style.height = step + "px";
		top_frame_parent.style.height = ( 60 - step ) + "px";
		top_frame.style.top = ( - step) + "px";
		setTimeout("move_down()", 20);
	} else {
		// move is finished, finalize
		top_frame_parent.style.display = "none";
		move_direction = step = 0;
		if (pressed) setTimeout("tmove(1)", 20);
	}
}
function create_photo(img_path, img_num, id) {
	var img = document.createElement("IMG");
	if (img && img_path) {
		img.setAttribute("src", "http://pics."+path_domain+"/pics/modern/albums/loading.gif", 0);
		img.setAttribute("src", img_path, 0);
		img.setAttribute("id", "frame_img_"+id, 0);
		img.style.border = "1px solid white";				
		img.onclick = function () {image_selected(id);}
	}
	return img;
}
function image_selected(id) {
	if (cur_img_id != id) {
		var old = document.getElementById("frame_img_"+cur_img_id);
		var img = document.getElementById("frame_img_"+id);
		img.parentNode.className = "albumFilmstripActive";
		img.style.cursor = "wait";
		old.parentNode.className = "albumFilmstripPassive";
		old.style.cursor = "pointer";
		cur_img_id = id;
		if (textFileType =='club') {
			document.location.href = "http://www." + path_domain + "/club/"+club_name+"/photo/"+cur_img_id;
		} else{
			document.location.href = "http://www." + path_domain + "/albums/"+username+"/p"+cur_img_id;
		}
	}
}
function create_numbers(position, total) {
	var position = String(position);
	var n_len = position.length;
	var div = document.createElement("div");
	div.style.position = "relative";
	div.style.left = "80px";
	div.style.top = -36 -(n_len*7)+"px";
	div.style.width = "10px";
	div.style.margin = "0px";
	div.style.padding = "0px";

	
	var data = '';
	// create first numbers
	for(i=0; i<=n_len; i++) {
		var curr_char = position.charAt(i);
		if (curr_char) data="<img src=\'"+numbers[curr_char]+"\' >"+data;
	}
	// add slash
	data="<img src=\'"+numbers["/"]+"\' >"+data;
	// create second (total) numbers
	div.innerHTML = total_innerHTML + data;
	return div;
}