// ------------------------------------------
// CLASS   : amcms_image_zoom
// descr.  : picture zoom
// author  : Richard Lagerström (+46 739 39 19 10)
// website : http://www.empty.se, http://www.mediatroop.se
// date    : 2007-07-12
// ------------------------------------------
function amcms_image_zoom()
{
	// -------------------------------------------
	// properties
	// -------------------------------------------
		/* document || parent.document
		 * the frame object in which the picture resides (if other than current)
		 * this is used by picture_list.php when displaying pictures in parent frame
		 * @var string
		*/
		this.document = document;
		
		
		/* id of image to set zoom level on
		 * @var string
		*/
		this.id = 'pic';
		
		
		/* [optional] id of the <div> that is moved when dragging the picture (the div which contains the <img>)
		 * @var string
		*/
		this.drag_box_id = 'dragBox';
		
		
		/* minimum allowed picture width
		 * this prevents user from zooming out to a width smaller than this
		 * @var string
		*/
		this.min_pic_width 	= 180;
		this.min_pic_height = 210;
		
		
		/* amount of pixels to zoom each step (in or out)
		 * how much in pixels to to zoom in- and out each time user zooms
		 * @var string
		*/
		this.zoom_step = 10;
		
		
		/* container width offset (size of the borders surrounding the image to the left and right, if any)
		 * @var string
		*/
		this.container_width_offset = 20;
		
		
		/* container height offset (size of the borders surrounding the image on top and bottom, if any)
		 * @var string
		*/
		this.container_height_offset = 60;
		
		/* whether to force the use of current frame (default action of the zoom obj is to grab img from parent,
		 * but sometimes (for instance; profile picture crop) we want to grab img from current frame even though
		 * parent exists. so force_current_frame = true forces this action.
		 * @var string
		*/
		this.force_current_frame = false;
		
		/* object container for "image popup"
		 * @var string
		*/
		this.obj_image_popup = '';
		
	// -------------------------------------------
	// methods
	// -------------------------------------------
		/* set new image height (new height is calculated from old w/h values and new width)
		 * @param 
		 * @return void
		*/
		this.set_new_img_height = function(old_w, old_h)
		{
			// grab new width
			var new_w = parseInt(amcmsImgZoom.document.getElementById(amcmsImgZoom.id).style.width);
			
			// get % difference between height/width (so that we can calculate new height based on new width)
			var diff = old_h/old_w;
				
			// get NEW image height
			var new_h = Math.round(new_w * diff);
			
			// set new image height
			amcmsImgZoom.document.getElementById(amcmsImgZoom.id).style.height 					= new_h + "px";
			if(amcmsImgZoom.document.getElementById(amcmsImgZoom.drag_box_id) != undefined && amcmsImgZoom.document.getElementById(amcmsImgZoom.drag_box_id) != 'undefined')
			{
				amcmsImgZoom.document.getElementById(amcmsImgZoom.drag_box_id).style.height 	= new_h + "px";
			}
			
			// set image height into crop form
		    if(document.getElementById('frmSizeH')) document.getElementById('frmSizeH').value = parseInt(amcmsImgZoom.document.getElementById(amcmsImgZoom.drag_box_id).style.height);
		}
		
		
		/* zoom in picture
		 * @param 
		 * @return void
		*/
		this.zoom_in = function()
		{
			if(amcmsImgZoom.document.getElementById(amcmsImgZoom.id))
			{
				// grab old values
				var old_w = parseInt(amcmsImgZoom.document.getElementById(amcmsImgZoom.id).offsetWidth);
				var old_h = parseInt(amcmsImgZoom.document.getElementById(amcmsImgZoom.id).offsetHeight);
				
				// set new width on image
				amcmsImgZoom.document.getElementById(amcmsImgZoom.id).style.width 				= (parseInt(amcmsImgZoom.document.getElementById(amcmsImgZoom.id).offsetWidth) + amcmsImgZoom.zoom_step) + 'px';
				if(amcmsImgZoom.document.getElementById(amcmsImgZoom.drag_box_id) != undefined && amcmsImgZoom.document.getElementById(amcmsImgZoom.drag_box_id) != 'undefined')
				{
					amcmsImgZoom.document.getElementById(amcmsImgZoom.drag_box_id).style.width 	= parseInt(amcmsImgZoom.document.getElementById(amcmsImgZoom.id).style.width) + 'px';
				}
				
				// set image width into crop form
				if(document.getElementById('frmSizeW')) document.getElementById('frmSizeW').value = parseInt(amcmsImgZoom.document.getElementById(amcmsImgZoom.drag_box_id).style.width);
				
				// set new height on image
				amcmsImgZoom.set_new_img_height(old_w, old_h);
				
				// adjust image container size
				if(typeof(this.obj_image_popup) != 'undefined') { if(this.obj_image_popup != '') eval("if('"+this.obj_image_popup+"' != '') "+this.obj_image_popup+".adjust_container(false);"); }
				
				// set fade height
				if(typeof(this.obj_image_popup) != 'undefined') { if(this.obj_image_popup != '') eval(this.obj_image_popup+".setFadeHeight();"); }
			}
			else
			{
				//alert('Could not locate picture to zoom.');
			}
		}
		
		
		/* zoom out picture
		 * @param 
		 * @return void
		*/
		this.zoom_out = function()
		{
			if(amcmsImgZoom.document.getElementById(amcmsImgZoom.id))
			{
				// get CURRENT image width/height
				var imgWidth 	= amcmsImgZoom.document.getElementById(amcmsImgZoom.id).offsetWidth;
				var imgHeight 	= amcmsImgZoom.document.getElementById(amcmsImgZoom.id).offsetHeight;
				
					// get % difference between height/width (so that we can calculate new height based on new width)
					var diff = imgHeight/imgWidth;
					
				// get NEW image width/height
				var imgNewWidth 	= imgWidth - amcmsImgZoom.zoom_step;
				var imgNewHeight 	= Math.round(imgNewWidth * diff);
				
				// make sure NEW width & height does not get smaller than min/max
				if(imgNewWidth >= amcmsImgZoom.min_pic_width && imgNewHeight >= amcmsImgZoom.min_pic_height)
				{
					// make sure CURRENT width & height does not get smaller than min/max
					if(imgWidth >= amcmsImgZoom.min_pic_width && imgHeight >= amcmsImgZoom.min_pic_height)
					{
						// new size ok for picure, set it
							// grab old values
							var old_w = parseInt(amcmsImgZoom.document.getElementById(amcmsImgZoom.id).offsetWidth);
							var old_h = parseInt(amcmsImgZoom.document.getElementById(amcmsImgZoom.id).offsetHeight);
				
							// set new width on image
							amcmsImgZoom.document.getElementById(amcmsImgZoom.id).style.width 			= imgNewWidth + 'px';
							if(amcmsImgZoom.document.getElementById(amcmsImgZoom.drag_box_id) != undefined && amcmsImgZoom.document.getElementById(amcmsImgZoom.drag_box_id) != 'undefined')
							{
								amcmsImgZoom.document.getElementById(amcmsImgZoom.drag_box_id).style.width 	= imgNewWidth + 'px';
							}
								
							// set image width into crop form
				            if(document.getElementById('frmSizeW')) document.getElementById('frmSizeW').value = parseInt(amcmsImgZoom.document.getElementById(amcmsImgZoom.drag_box_id).style.width);
				
							// set new height on image
							amcmsImgZoom.set_new_img_height(old_w, old_h);
				
						// adjust image container size
						if(typeof(this.obj_image_popup) != 'undefined') eval(this.obj_image_popup+".adjust_container(false);");
				
						// set fade height
						if(typeof(this.obj_image_popup) != 'undefined') eval(this.obj_image_popup+".setFadeHeight();");
				
						// reposition picture if it is malplaced
						amcmsImgZoom.reposition();
					}
				}
			}
			else
			{
				alert('Could not locate picture to zoom.');
			}
		}
		
		
		/* reposition dragBox inside container incase it gets malplaced
		 * @param 
		 * @return void
		*/
		this.reposition = function()
		{
			// reposition only if dragBox is available
			if(amcmsImgZoom.document.getElementById(amcmsImgZoom.drag_box_id) != undefined && amcmsImgZoom.document.getElementById(amcmsImgZoom.drag_box_id) != 'undefined')
			{
				// get current image width/height
				var imgWidth 	= parseInt(amcmsImgZoom.document.getElementById(amcmsImgZoom.id).offsetWidth);
				var imgHeight 	= parseInt(amcmsImgZoom.document.getElementById(amcmsImgZoom.id).offsetHeight);
					
				// get current position of image from crop areas top/right/bottom & left
				var fromLeft 		= amcmsImgZoom.document.getElementById(amcmsImgZoom.drag_box_id).style.left ? parseInt(amcmsImgZoom.document.getElementById(amcmsImgZoom.drag_box_id).style.left) : 0;
				var fromLeftMinus 	= fromLeft < 0 ? (fromLeft.toString().replace(/-/g, '')) : 0;
				var fromTop 		= amcmsImgZoom.document.getElementById(amcmsImgZoom.drag_box_id).style.top ? parseInt(amcmsImgZoom.document.getElementById(amcmsImgZoom.drag_box_id).style.top) : 0;
				var fromTopMinus 	= fromTop < 0 ? (fromTop.toString().replace(/-/g, '')) : 0;
				
					// check if background is visible to the right
						if( (imgWidth - fromLeftMinus) > amcmsImgZoom.min_pic_width)
						{
							// ok
						}
						else
						{
							// move position of picture to edge on the right side
								// calculate how much background is visible to the right
								var numPixelsVisible = (amcmsImgZoom.min_pic_width - ((imgWidth - fromLeftMinus)));
								
								// move picture
								amcmsImgZoom.document.getElementById(amcmsImgZoom.drag_box_id).style.left = (fromLeft + numPixelsVisible) + "px";
						}
						
					// check if background is visible at the bottom
						if( (imgHeight - fromTopMinus) > amcmsImgZoom.min_pic_height)
						{
							// ok
						}
						else
						{
							// move position of picture to edge at the bottom
								// calculate how much background is visible at the bottom
								var numPixelsVisible = (amcmsImgZoom.min_pic_height - ((imgHeight - fromTopMinus)));
							
								// move picture
								amcmsImgZoom.document.getElementById(amcmsImgZoom.drag_box_id).style.top = (fromTop + numPixelsVisible) + "px";
						}
			}
		}
		
		
		/* scroll handling (what to do when scrolling up/down)
		 * @param 
		 * @return void
		*/
		this.handle = function(delta)
		{
			if(amcmsImgZoom.document.getElementById(amcmsImgZoom.id))
			{
				// BUGG FIX
				// Bugg descr: 
				//               IE    : scroll up = zoom out 
				//               MOZ   : scroll up = zoom out 
				//               OPERA : scroll up = zoom in (this is the one we would like to reverse to same as above) 
				// Opera browser has opposite values in this calculation, so if we got the browser
				// object available we reverse the check algorithm to make it same as Moz & IE (scroll up = zoom out)
				if(typeof(amcmsBrowser) == 'object')
				{
					if((amcmsBrowser.isOP && delta >= 0) || (!amcmsBrowser.isOP && delta < 0))
					{
						// scrolling down
						amcmsImgZoom.zoom_out();
					}
			        else
			        {
			        	// scrolling up
			        	amcmsImgZoom.zoom_in();
			        }
				}
				else
				{
					if(delta < 0)
					{
						// scrolling down
						amcmsImgZoom.zoom_out();
					}
			        else
			        {
			        	// scrolling up
			        	amcmsImgZoom.zoom_in();
			        }
				}
			}
			else
			{
				alert('Could not locate picture to zoom.');
			}
		}
		
		
		/* event handler for mouse wheel event
		 * @param 
		 * @return void
		*/
		this.wheel = function(event)
		{
			if(window.amcmsImgZoom != undefined && window.amcmsImgZoom != "undefined")
			{
				if(window.amcmsImgZoom != undefined && amcmsImgZoom.document.getElementById(amcmsImgZoom.id))
				{
					var delta = 0;
					if (!event)
					{
						// IE
						event = (parent && !amcmsImgZoom.force_current_frame) ? parent.window.event : window.event;
					}
					
					if (event.wheelDelta)
					{
						// IE/Opera
						delta = event.wheelDelta/120;
						
						// in Opera 9, delta differs in sign as compared to IE
						if(window.opera)
						{
							delta = -delta;
						}
					} 
					else if (event.detail)
					{
						// Mozilla
							// In Mozilla, sign of delta is different than in IE. Also, delta is multiple of 3.
							delta = -event.detail/3;
					}
					
					// If delta is nonzero, handle it.
					// Basically, delta is now positive if wheel was scrolled up,
					// and negative, if wheel was scrolled down.
					if (delta)
					{
						amcmsImgZoom.handle(delta);
					}
					
					// Prevent default actions caused by mouse wheel.
					// That might be ugly, but we handle scrolls somehow
					// anyway, so don't bother here..
					if (event.preventDefault)
					{
						event.preventDefault();
					}
					
					event.returnValue = false;
				}
				else
				{
					//alert('Could not locate picture to zoom.');
				}
			}
		}
		
		
		/* ONMOUSEOVER triggers event handlers which allow user to zoom picture with scroll wheel
		 * @param 
		 * @return void
		*/
		this.trigger_scroll_zoom = function()
		{
			if(amcmsImgZoom != undefined && amcmsImgZoom != "undefined" && amcmsImgZoom.document.getElementById(amcmsImgZoom.id))
			{
				// handle image zoom by scrolling
					// mozilla
					if(window.addEventListener)
					{
						if(amcmsImgZoom.document == document || amcmsImgZoom.force_current_frame)
						{
							// DOMMouseScroll is for mozilla
							window.addEventListener('DOMMouseScroll', amcmsImgZoom.wheel, false);
						}
						else
						{
							// DOMMouseScroll is for mozilla
							parent.window.addEventListener('DOMMouseScroll', amcmsImgZoom.wheel, false);
						}
					}
					
					// ie/opera
					if(amcmsImgZoom.document == document || amcmsImgZoom.force_current_frame)
					{
						window.onmousewheel = amcmsImgZoom.document.onmousewheel = amcmsImgZoom.wheel;
					}
					else
					{
						parent.window.onmousewheel = amcmsImgZoom.document.onmousewheel = amcmsImgZoom.wheel;
					}
			}
			else
			{
				//alert('Could not locate picture to zoom.');
			}
		}
		
		
		/* ONMOUSEOUT untriggers event handlers which allow user to zoom picture with scroll wheel
		 * @param 
		 * @return void
		*/
		this.untrigger_scroll_zoom = function()
		{
			if(amcmsImgZoom != undefined && amcmsImgZoom != "undefined" && amcmsImgZoom.document.getElementById(amcmsImgZoom.id))
			{
				// disable mouse scroll
					// mozilla
					if(window.addEventListener)
					{
						if(amcmsImgZoom.document == document || amcmsImgZoom.force_current_frame)
						{
							// DOMMouseScroll is for mozilla
							window.removeEventListener('DOMMouseScroll', amcmsImgZoom.wheel, false);
						}
						else
						{
							// DOMMouseScroll is for mozilla
							parent.window.removeEventListener('DOMMouseScroll', amcmsImgZoom.wheel, false);
						}
					}
					
					// ie/opera
					if(amcmsImgZoom.document == document || amcmsImgZoom.force_current_frame)
					{
						window.onmousewheel = amcmsImgZoom.document.onmousewheel = null;
					}
					else
					{
						parent.window.onmousewheel = amcmsImgZoom.document.onmousewheel = null;
					}
			}
			else
			{
				//alert('Could not locate picture to zoom.');
			}
		}
		
		this.manual_zoom = function(biggerOrSmaller)
		{
			delta = biggerOrSmaller == 'smaller' ? -10 : 10;
			
			// manual zoom
			if(amcmsImgZoom.document.getElementById(amcmsImgZoom.id))
			{
				// BUGG FIX
				// Bugg descr: 
				//               IE    : scroll up = zoom out 
				//               MOZ   : scroll up = zoom out 
				//               OPERA : scroll up = zoom in (this is the one we would like to reverse to same as above) 
				// Opera browser has opposite values in this calculation, so if we got the browser
				// object available we reverse the check algorithm to make it same as Moz & IE (scroll up = zoom out)
				if(typeof(amcmsBrowser) == 'object')
				{
					if((amcmsBrowser.isOP && delta >= 0) || (!amcmsBrowser.isOP && delta < 0))
					{
						// scrolling down
						amcmsImgZoom.zoom_out();
					}
			        else
			        {
			        	// scrolling up
			        	amcmsImgZoom.zoom_in();
			        }
				}
				else
				{
					if(delta < 0)
					{
						// scrolling down
						amcmsImgZoom.zoom_out();
					}
			        else
			        {
			        	// scrolling up
			        	amcmsImgZoom.zoom_in();
			        }
				}
			}
			else
			{
				alert('Could not locate picture to zoom.');
			}
		}
}

// instantiate image zoom object
var amcmsImgZoom = new amcms_image_zoom();