var timer;
var moving_up = false;
var moving_down = false;
var img_table = null;
var img_table_height;
var img_container_body = null;
var g_direction;
var speed_coeficient = 80;
var move_offset = 1;
var speed = false;
var sd_timer;
var image = new Image();
var root_url = null;
var shadow = null;
var big_image_area = null;

function init() {
	img_table = document.getElementById('mg_preview_table');
	img_container_body = document.getElementById('mg_container_body');
	shadow = document.getElementById("mg_shadow");
	big_image_area = document.getElementById("mg_big_image_area");
	img_table.style.top = '0px'; 
	mg_start_move('down');
	root_url = document.getElementById('root_url').value;
  
}

function get_page_scroll(){
	var xScroll, yScroll;
	
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;
	}
	
	arrayPageScroll = new Array(xScroll,yScroll)
	return arrayPageScroll;
}

function mg_start_move(direction){
	if(direction == 'up' && moving_up == false){
		moving_up = true;
		moving_down = false;
		
		mg_move(direction);	
	}else if(direction == 'down' && moving_down == false){
		moving_up = false;
		moving_donw = true;
		
		mg_move(direction);	
	}
	
	
	
}

function mg_speed_up(direction){
	speed = true;
	clearTimeout(sd_timer);
	mg_move(direction);
	if(is_firefox){
		move_offset = 6;
		speed_coeficient = 20;	
	}else if(is_ie){
		move_offset = 4;
		speed_coeficient = 1;		
	}else{
		move_offset = 2;
		speed_coeficient = 1;	
	}
	
	
	
}

function mg_slow_down(){
	
	if(speed_coeficient < 80){
		
		speed_coeficient = speed_coeficient + 3;
		sd_timer = setTimeout("mg_slow_down()",20);		
	
		
				
	}else{
		
		clearTimeout(sd_timer);	
	}
	move_offset = 1;
	speed = false;
	
	
	
}

image.onload = function(){
	
	var imgHeight = image.height;
	var imgWidth = image.width;
	
	if(imgWidth>imgHeight){
		var newImgHeight = 600 * (imgHeight / imgWidth);
		var newImgWidth = 600;		
	}
	
	if(imgHeight>imgWidth){
		var newImgHeight = 600;
		var newImgWidth = 600 * (imgWidth / imgHeight);		
	}
	
	if(imgHeight == imgWidth){
		var newImgHeight = 600;
		var newImgWidth = 600;	
	}
	
	var newImgTop = (600/2) - (newImgHeight/2) + 'px';
	var newImgLeft = (700/2) - (newImgWidth/2) + 'px';
	
	big_image_area.innerHTML = '<img src="'+image.src+'" style="width:'+newImgWidth+';height:'+newImgHeight+';position:absolute;top:'+newImgTop+';left:'+newImgLeft+'" />';
	
}

function mg_show_big_image(obj){
	var source = obj.src;
	var index = source.indexOf('/thumbs/');
	var img_name = source.substr(index + 8);
	
	
	
	//alert(document.body.clientHeight);
	var shadow = document.getElementById("mg_shadow");
	var big_image_area = document.getElementById("mg_big_image_area");
	
	//alert(document.body.clientHeight + 'px');
	
	
	shadow.style.height = document.body.clientHeight + 'px';
	shadow.style.width = document.body.clientWidth + 'px';
	
	shadow.style.top = get_page_scroll()[1] + 'px';
	shadow.style.left = '0px';
	
	shadow.style.display = 'block';
	
	
	
	big_image_area.style.top = (document.body.clientHeight / 2) - (600 / 2) + 'px';
	big_image_area.style.left = (document.body.clientWidth / 2) - (700 / 2) + 'px';
	
	document.getElementById('mg_close_button').style.position = 'absolute';
	document.getElementById('mg_close_button').style.top = (document.body.clientHeight / 2) - (600 / 2) + 'px';
	document.getElementById('mg_close_button').style.left = (document.body.clientWidth / 2) + (700 / 2) - 60 + 'px'; 
	
	loadingTop = (600 / 2) - (300 / 2) + 'px';
	loadingLeft = (700 / 2) - (494 / 2) + 'px'; 
	
	big_image_area.innerHTML = '<img src="'+root_url+'images/loading.gif" style="position:absolute;left:'+loadingLeft+';top:'+loadingTop+'" />';
	
	image.src = root_url + 'MINI_GALERIE/' + img_name;  
	
	
	
	
}

function mg_move(direction){
	g_direction = direction;
	if(direction == 'up'){
		if(parseInt(img_table.style.top)  > 0){
			if(speed == false){
				speed_coeficient = 80;
				move_offset = 1;
				direction = 'down';
				g_direction = direction;			
			}
						
		}else{
			img_table.style.top = parseInt(img_table.style.top)+ move_offset +'px';			
		}
		
		
		//document.getElementById('test_text').innerHTML = parseInt(img_table.style.top) + img_table.offsetHeight ;
			
	}
	
	if(direction == 'down'){
		
		if(parseInt(img_table.style.top) + img_table.offsetHeight < img_container_body.offsetHeight){
			if(speed == false){
				speed_coeficient = 80;
				move_offset = 1;
				direction = 'up';
				g_direction = direction;				
			}
					
		}else{
			img_table.style.top = parseInt(img_table.style.top)- move_offset +'px';			
		}
		
		
		//document.getElementById('test_text').innerHTML = parseInt(img_table.style.top) + img_table.offsetHeight ;
		
		
			
	}
	clearTimeout(timer);
	timer = setTimeout("mg_move('"+direction+"')",speed_coeficient);
	g_direction = direction;
																																																					
}

function mouse_over(){
	clearTimeout(timer);
	
	moving_up = false;
	moving_down = false;
}

function mouse_out(){
	//alert(g_direction);
	mg_start_move(g_direction);
}

function mg_close_big_image(){
	shadow.style.display = 'none';
	
}


 
 
window.onload = init;

