/*
Version 10.12.14
*/

function ZoomSet(options)
{
	this._image_container = null;
	this._image = null;
	this._original_height = null;
	this._original_width = null;
	this._moving_up = false;
	this._moving_down = false;
	this._moving_left = false;
	this._moving_right = false;
	
	if(!!options.image_container)
	{
		this._image_container = options.image_container;
		this._image = this._image_container.getElementsByTagName('img')[0];
	}
		
	this._theme = 'default';
	if(!!options.theme)
		this._theme = options.theme;
	
	this.init();
}

ZoomSet.prototype.setImage = function(img) 
{
	var w, h;
	w = parseInt(this._image_container.style.width);
	h = parseInt(this._image_container.style.height);
	
	img = img.replace('max', '');
	img = img.replace(/w=\d+/,'w='+w);
	img = img.replace(/h=\d+/,'h='+h);
	this._image.src = img;
	this._image.width = w;
	this._image.height = h;
	this._image.style.left = '0px';
	this._image.style.top = '0px';
}

ZoomSet.prototype.init = function()
{
	var img;
	
	this.setTheme(this._theme);
	this._image_container.className += ' ZoomSet_container';
	this._image.className += ' ZoomSet_image';
	this._image_container.style.width = this._image.width+'px';
	this._image_container.style.height = this._image.height+'px';
	this._original_width = this._image.width;
	this._original_height = this._image.height;
	this._image.style.top = '0px';
	this._image.style.left = '0px';
	
	this.ZoomControl = new ZoomControl(this);
}

ZoomSet.prototype.setTheme = function(theme)
{
	if(!!this._themeLinkScreen)
		document.getElementsByTagName('head')[0].removeChild(this._themeLinkScreen);
	if(!!this._themeLinkPrint)
		document.getElementsByTagName('head')[0].removeChild(this._themeLinkPrint);
	
	fileref=document.createElement('link');
	fileref.setAttribute('rel', 'stylesheet');
	fileref.setAttribute('type', 'text/css');
	fileref.setAttribute('href', '/themes/ZoomSet/'+theme+'/theme.css');
	fileref.setAttribute('media', 'screen');
	this._themeLinkScreen = document.getElementsByTagName('head')[0].appendChild(fileref);
	
	fileref=document.createElement('link');
	fileref.setAttribute('rel', 'stylesheet');
	fileref.setAttribute('type', 'text/css');
	fileref.setAttribute('href', '/themes/ZoomSet/'+theme+'/print.css');
	fileref.setAttribute('media', 'print');
	this._themeLinkPrint = document.getElementsByTagName('head')[0].appendChild(fileref);
}

ZoomSet.prototype.startDown = function()
{
	this._moving_down = true;
	this._interval = setInterval(function()
	{
		if(this._moving_down && this.canMoveDown())
		{
			this._image.style.top = (parseInt(this._image.style.top)-15)+'px';
			this.fixOverflow();
		}
		else
			clearInterval(this._interval);
	}.bind(this), 10);
	
	return false;
}

ZoomSet.prototype.startUp = function()
{
	this._moving_up = true;
	this._interval = setInterval(function()
	{
		if(this._moving_up && this.canMoveUp())
		{
			this._image.style.top = (parseInt(this._image.style.top)+15)+'px';
			this.fixOverflow();
		}
		else
			clearInterval(this._interval);
	}.bind(this), 10);
	
	return false;
}

ZoomSet.prototype.startRight = function()
{
	this._moving_right = true;
	this._interval = setInterval(function()
	{
		if(this._moving_right && this.canMoveRight())
		{
			this._image.style.left = (parseInt(this._image.style.left)-15)+'px';
			this.fixOverflow();
		}
		else
			clearInterval(this._interval);
	}.bind(this), 10);
	
	return false;
}

ZoomSet.prototype.startLeft = function()
{
	this._moving_left = true;
	this._interval = setInterval(function()
	{
		if(this._moving_left && this.canMoveLeft())
		{
			this._image.style.left = (parseInt(this._image.style.left)+15)+'px';
			this.fixOverflow();
		}
		else
			clearInterval(this._interval);
	}.bind(this), 10);
	
	return false;
}

ZoomSet.prototype.canMoveDown = function()
{
	return parseInt(this._image.style.top)+this._image.height > parseInt(this._image_container.style.height);
}

ZoomSet.prototype.canMoveUp = function()
{
	return parseInt(this._image.style.top) < 0;
}

ZoomSet.prototype.canMoveRight = function()
{
	return parseInt(this._image.style.left)+this._image.width > parseInt(this._image_container.style.width);
}

ZoomSet.prototype.canMoveLeft = function()
{
	return parseInt(this._image.style.left) < 0;
}

ZoomSet.prototype.zoomIn = function()
{
	var id, w, h, src;
	
	w = this._image.width;
	h = this._image.height;
	
	if(w >= 7 * this._original_width || h >= 7 * this._original_height)
		return;
	
	src = this._image.src;
	
	src = src.replace('max', '');
	src = src.replace(/w=\d+/,'w='+(w*2));
	src = src.replace(/h=\d+/,'h='+(h*2));
	
	this._image.src = src;
	this._image.width = w*2;
	this._image.height = h*2;
	this._image.style.left = ((parseInt(this._image.style.left)*2)-(parseInt(this._image_container.style.width)/2))+'px';
	this._image.style.top = ((parseInt(this._image.style.top)*2)-(parseInt(this._image_container.style.height)/2))+'px';
	
	this.fixOverflow();
	
	return false;
}

ZoomSet.prototype.zoomOut = function()
{
	var id, w, h, src;
	
	w = this._image.width;
	h = this._image.height;
	
	if(w <= this._original_width || h <= this._original_height)
		return;
	
	src = this._image.src;
	
	src = src.replace('max', '');
	src = src.replace(/w=\d+/,'w='+(w/2));
	src = src.replace(/h=\d+/,'h='+(h/2));
	
	this._image.src = src;
	this._image.width = w/2;
	this._image.height = h/2;
	this._image.style.left = ((parseInt(this._image.style.left)+this._image.width/2))+'px';
	this._image.style.top = ((parseInt(this._image.style.top)+this._image.height/2))+'px';
	
	this.fixOverflow();
	
	return false;
}

ZoomSet.prototype.fixOverflow = function()
{
	var left, top, w, h, container_w, container_h;
	left = parseInt(this._image.style.left);
	top = parseInt(this._image.style.top);
	w = parseInt(this._image.width);
	h = parseInt(this._image.height);
	container_w = parseInt(this._image_container.style.width);
	container_h = parseInt(this._image_container.style.height);
		
	if(left + w < container_w)
		left = container_w - w;
	if(top + h < container_h)
		top = container_h - h;
		
	if(left > 0)
		left = 0;
	if(top > 0)
		top = 0;
	this._image.style.left = left+'px';
	this._image.style.top = top+'px';
}

ZoomSet.prototype.end = function()
{
	this._moving_up = false;
	this._moving_down = false;
	this._moving_left = false;
	this._moving_right = false;
}

function ZoomControl(zoom_set)
{
	var control_div, arrow_up_div, arrow_down_div, arrow_left_div, arrow_right_div, zoom_in_div, zoom_out_div;
	
	this._ZoomSet = zoom_set;
	
	control_div = document.createElement('div');
	control_div.className = 'ZoomSet_control';
	
	arrow_down_div = document.createElement('div');
	arrow_down_div.className = 'ZoomSet_down';
	arrow_down_div.onmousedown = this._ZoomSet.startDown.bind(this._ZoomSet);
	arrow_down_div.onmouseup = this._ZoomSet.end.bind(this._ZoomSet);
	arrow_down_div.onmouseout = this._ZoomSet.end.bind(this._ZoomSet);
	
	arrow_up_div = document.createElement('div');
	arrow_up_div.className = 'ZoomSet_up';
	arrow_up_div.onmousedown = this._ZoomSet.startUp.bind(this._ZoomSet);
	arrow_up_div.onmouseup = this._ZoomSet.end.bind(this._ZoomSet);
	arrow_up_div.onmouseout = this._ZoomSet.end.bind(this._ZoomSet);
	
	arrow_left_div = document.createElement('div');
	arrow_left_div.className = 'ZoomSet_left';
	arrow_left_div.onmousedown = this._ZoomSet.startLeft.bind(this._ZoomSet);
	arrow_left_div.onmouseup = this._ZoomSet.end.bind(this._ZoomSet);
	arrow_left_div.onmouseout = this._ZoomSet.end.bind(this._ZoomSet);
	
	arrow_right_div = document.createElement('div');
	arrow_right_div.className = 'ZoomSet_right';
	arrow_right_div.onmousedown = this._ZoomSet.startRight.bind(this._ZoomSet);
	arrow_right_div.onmouseup = this._ZoomSet.end.bind(this._ZoomSet);
	arrow_right_div.onmouseout = this._ZoomSet.end.bind(this._ZoomSet);
	
	zoom_in_div = document.createElement('div');
	zoom_in_div.className = 'ZoomSet_zoom_in';
	zoom_in_div.onmousedown = this._ZoomSet.zoomIn.bind(this._ZoomSet);
	zoom_in_div.onmouseup = function(){return false};
	//zoom_in_div.onmouseup = this._ZoomSet.end.bind(this._ZoomSet);
	//zoom_in_div.onmouseout = this._ZoomSet.end.bind(this._ZoomSet);
	
	zoom_out_div = document.createElement('div');
	zoom_out_div.className = 'ZoomSet_zoom_out';
	zoom_out_div.onmousedown = this._ZoomSet.zoomOut.bind(this._ZoomSet);
	zoom_out_div.onmouseup = function(){return false};
	//zoom_out_div.onmouseup = this._ZoomSet.end.bind(this._ZoomSet);
	//zoom_out_div.onmouseout = this._ZoomSet.end.bind(this._ZoomSet);
	
	control_div.appendChild(zoom_in_div);
	control_div.appendChild(zoom_out_div);
	control_div.appendChild(arrow_up_div);
	control_div.appendChild(arrow_down_div);
	control_div.appendChild(arrow_left_div);
	control_div.appendChild(arrow_right_div);
	
	this._ZoomSet._image_container.appendChild(control_div);
}
/*

<table>
	<tr>
		<td><img src="images/magifier_zoom_out.png" width="16" height="16" /></td>
		<td>&nbsp;</td>
		<td><img src="images/arrow-up.png" width="16" height="16" /></td>
		<td>&nbsp;</td>
	</tr>
	<tr>
		<td>&nbsp;</td>
		<td><img src="images/arrow-left.png" width="16" height="16" /></td>
		<td>&nbsp;</td>
		<td><img src="images/arrow-right.png" width="16" height="16" /></td>
	</tr>
	<tr>
		<td><img src="images/magnifier_zoom_in.png" width="16" height="16" /></td>
		<td>&nbsp;</td>
		<td><img src="images/arrow-down.png" width="16" height="16" /></td>
		<td>&nbsp;</td>
	</tr>
</table>
*/
