| 
									
										
										
										
											2012-07-24 14:40:15 +04:00
										 |  |  | /*********************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  |  * The folowing two functions will get the vertical and horizontal  | 
					
						
							|  |  |  |  * distance components between the pints a and A, centers of the small | 
					
						
							|  |  |  |  * and large squares respectively. | 
					
						
							|  |  |  |  * One of the squares is .field and the other is .container,  | 
					
						
							|  |  |  |  * which is small or big is not important. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *      +---------------+-------+ | 
					
						
							|  |  |  |  *      |               |       | | 
					
						
							|  |  |  |  *      |               |       | | 
					
						
							|  |  |  |  *      |       + a . . | . . . | . + | 
					
						
							|  |  |  |  *      |       .       |       |   +- getCurrentVerticalOffset(...) | 
					
						
							|  |  |  |  *      |       .   + A | . . . | . + | 
					
						
							|  |  |  |  *      +---------------+       | | 
					
						
							|  |  |  |  *      |       .   .           | | 
					
						
							|  |  |  |  *      |       .   .           | | 
					
						
							|  |  |  |  *      |       .   .           | | 
					
						
							|  |  |  |  *      +-----------------------+ | 
					
						
							|  |  |  |  *              .   . | 
					
						
							|  |  |  |  *              +-+-+ | 
					
						
							|  |  |  |  *                +------------------- getCurrentHorizontalOffset(...) | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Adding this distance to margins of one of the sqares will effectively  | 
					
						
							|  |  |  |  * allign the two points. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * NOTE: neither function accunts for field margins. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // get the vertical offset of the center of square from center of container
 | 
					
						
							|  |  |  | // NOTE: this does not account for field margins
 | 
					
						
							| 
									
										
										
										
											2012-07-24 15:38:54 +04:00
										 |  |  | function getCurrentVerticalOffset(image){ | 
					
						
							|  |  |  | 	if(image == null){ | 
					
						
							|  |  |  | 		image = $('.image.current') | 
					
						
							| 
									
										
										
										
											2012-07-24 14:40:15 +04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-29 02:54:22 +04:00
										 |  |  | 	//var zoom = $('.field').css('zoom')
 | 
					
						
							|  |  |  | 	var zoom = getElementScale($('.field')) | 
					
						
							| 
									
										
										
										
											2012-07-24 14:40:15 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	var ribbons = $('.ribbon') | 
					
						
							| 
									
										
										
										
											2012-07-24 15:38:54 +04:00
										 |  |  | 	var ribbon = image.parents('.ribbon') | 
					
						
							|  |  |  | 	var images = ribbon.children('.image') | 
					
						
							| 
									
										
										
										
											2012-07-24 14:40:15 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// vertical...
 | 
					
						
							|  |  |  | 	var H = $('.container').height() | 
					
						
							|  |  |  | 	var h = ribbons.outerHeight(true) | 
					
						
							|  |  |  | 	// margin...
 | 
					
						
							|  |  |  | 	var mh = h - ribbons.outerHeight() | 
					
						
							|  |  |  | 	// current ribbon position (1-based)
 | 
					
						
							|  |  |  | 	var rn = ribbons.index(ribbon) + 1 | 
					
						
							|  |  |  | 	// XXX compensating for margin error buildup... really odd!
 | 
					
						
							|  |  |  | 	//	...why is this still different for the first three ribbons?!
 | 
					
						
							|  |  |  | 	//		....sub-pixel error?
 | 
					
						
							|  |  |  | 	// relative position to field... 
 | 
					
						
							|  |  |  | 	// XXX is there a better way to get this?
 | 
					
						
							|  |  |  | 	var t = rn * (h - mh/2) | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	return -t + H/2 + h/2 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // get the horizontal offset of the center of square from center of container
 | 
					
						
							|  |  |  | // NOTE: this does not account for field margins
 | 
					
						
							| 
									
										
										
										
											2012-07-24 15:38:54 +04:00
										 |  |  | function getCurrentHorizontalOffset(image){ | 
					
						
							|  |  |  | 	if(image == null){ | 
					
						
							|  |  |  | 		image = $('.image.current') | 
					
						
							| 
									
										
										
										
											2012-07-24 14:40:15 +04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-29 02:54:22 +04:00
										 |  |  | 	//var zoom = $('.field').css('zoom')
 | 
					
						
							|  |  |  | 	var zoom = getElementScale($('.field')) | 
					
						
							| 
									
										
										
										
											2012-07-24 14:40:15 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-24 15:38:54 +04:00
										 |  |  | 	var ribbon = image.parents('.ribbon') | 
					
						
							|  |  |  | 	var images = ribbon.children('.image') | 
					
						
							| 
									
										
										
										
											2012-07-24 14:40:15 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	var W = $('.container').width() | 
					
						
							| 
									
										
										
										
											2012-07-24 15:38:54 +04:00
										 |  |  | 	var w = images.outerWidth(true) | 
					
						
							| 
									
										
										
										
											2012-07-24 14:40:15 +04:00
										 |  |  | 	// margin...
 | 
					
						
							|  |  |  | 	// XXX do we need this?
 | 
					
						
							| 
									
										
										
										
											2012-07-24 15:38:54 +04:00
										 |  |  | 	var mw = w - images.outerWidth() | 
					
						
							| 
									
										
										
										
											2012-07-24 14:40:15 +04:00
										 |  |  | 	// current square position (1-based)
 | 
					
						
							| 
									
										
										
										
											2012-07-24 15:38:54 +04:00
										 |  |  | 	var sn = images.index(image) + 1 | 
					
						
							| 
									
										
										
										
											2012-07-24 14:40:15 +04:00
										 |  |  | 	var l = sn * (w - mw/2) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return -l + W/2 + w/2 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function centerSquare(){ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	$('.field').css({ | 
					
						
							|  |  |  | 		'margin-top': getCurrentVerticalOffset() | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// horizontal...
 | 
					
						
							|  |  |  | 	alignRibbon() | 
					
						
							| 
									
										
										
										
											2012-07-29 02:54:22 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-31 15:25:05 +04:00
										 |  |  | 	centerCurrentImage() | 
					
						
							| 
									
										
										
										
											2012-07-24 14:40:15 +04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-24 15:38:54 +04:00
										 |  |  | function alignRibbon(image, position){ | 
					
						
							| 
									
										
										
										
											2012-07-24 14:40:15 +04:00
										 |  |  | 	// default values...
 | 
					
						
							| 
									
										
										
										
											2012-07-24 15:38:54 +04:00
										 |  |  | 	if(image == null){ | 
					
						
							|  |  |  | 		image = $('.image.current') | 
					
						
							| 
									
										
										
										
											2012-07-24 14:40:15 +04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if(position == null){ | 
					
						
							|  |  |  | 		position = 'center' | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-24 15:38:54 +04:00
										 |  |  | 	var ribbon = image.parents('.ribbon') | 
					
						
							| 
									
										
										
										
											2012-07-24 14:40:15 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// account for margined field...
 | 
					
						
							|  |  |  | 	// NOTE: this enables us to cheat and shift all the ribbons just
 | 
					
						
							|  |  |  | 	//       by changing field margin-left...
 | 
					
						
							|  |  |  | 	var cml = parseFloat($('.field').css('margin-left')) | 
					
						
							|  |  |  | 	if(!cml){ | 
					
						
							|  |  |  | 		cml = 0 | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2012-07-24 15:38:54 +04:00
										 |  |  | 	var h_offset = getCurrentHorizontalOffset(image) - cml | 
					
						
							|  |  |  | 	var w = $('.image').outerWidth(true) | 
					
						
							| 
									
										
										
										
											2012-07-24 14:40:15 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	switch(position){ | 
					
						
							|  |  |  | 		case 'before': | 
					
						
							| 
									
										
										
										
											2012-07-31 17:38:50 +04:00
										 |  |  | 			ribbon.css({'margin-left': h_offset - w/2}) | 
					
						
							| 
									
										
										
										
											2012-07-24 14:40:15 +04:00
										 |  |  | 			return true | 
					
						
							|  |  |  | 		case 'center': | 
					
						
							|  |  |  | 			ribbon.css({'margin-left': h_offset}) | 
					
						
							|  |  |  | 			return true | 
					
						
							|  |  |  | 		case 'after': | 
					
						
							| 
									
										
										
										
											2012-07-31 17:38:50 +04:00
										 |  |  | 			ribbon.css({'margin-left': h_offset + w/2}) | 
					
						
							| 
									
										
										
										
											2012-07-24 14:40:15 +04:00
										 |  |  | 			return true | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return false | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-29 02:54:22 +04:00
										 |  |  | /* Set the transform-origin to the center of the current view... | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | // XXX this appears to be wrong....
 | 
					
						
							|  |  |  | function centerOrigin(){ | 
					
						
							|  |  |  | 	var mt = parseFloat($('.field').css('margin-top')) | 
					
						
							|  |  |  | 	var ml = parseFloat($('.field').css('margin-left')) | 
					
						
							|  |  |  | 	var cml = parseFloat($('.current.ribbon').css('margin-left')) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var t = parseFloat($('.field').css('top')) | 
					
						
							|  |  |  | 	var l = parseFloat($('.field').css('left')) | 
					
						
							|  |  |  | 	var w = $('.field').width() | 
					
						
							|  |  |  | 	var h = $('.field').height() | 
					
						
							|  |  |  | 	var W = $('.container').width() | 
					
						
							|  |  |  | 	var H = $('.container').height() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	//var ot = mt + H/2 + t
 | 
					
						
							|  |  |  | 	//var ol = ml + W/2 + l
 | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	var ot = -getCurrentVerticalOffset() + H/2 - t | 
					
						
							|  |  |  | 	var ol = -ml + W/2 - l | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	$('.field').css({ | 
					
						
							|  |  |  | 		'transform-origin': ol + 'px ' + ot + 'px', | 
					
						
							|  |  |  | 		'-o-transform-origin': ol + 'px ' + ot + 'px', | 
					
						
							|  |  |  | 		'-moz-transform-origin': ol + 'px ' + ot + 'px', | 
					
						
							|  |  |  | 		'-webkit-transform-origin': ol + 'px ' + ot + 'px', | 
					
						
							|  |  |  | 		'-ms-transform-origin': ol + 'px ' + ot + 'px' | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// XXX for debugging...
 | 
					
						
							|  |  |  | 	$('.origin-marker').css({ | 
					
						
							|  |  |  | 		'top': ot, | 
					
						
							|  |  |  | 		'left': ol | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-24 14:40:15 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*********************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // XXX need to make this work for % values...
 | 
					
						
							|  |  |  | // XXX make this usable as an event handler for .resize(...) event...
 | 
					
						
							| 
									
										
										
										
											2012-07-31 15:25:05 +04:00
										 |  |  | // XXX this does not account for zoom correctly...
 | 
					
						
							| 
									
										
										
										
											2012-07-24 14:40:15 +04:00
										 |  |  | function fieldSize(W, H){ | 
					
						
							|  |  |  | 	var oW = $('.container').width() | 
					
						
							|  |  |  | 	var oH = $('.container').height() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-29 02:54:22 +04:00
										 |  |  | 	//var zoom = $('.field').css('zoom')
 | 
					
						
							|  |  |  | 	var zoom = getElementScale($('.field')) | 
					
						
							| 
									
										
										
										
											2012-07-24 14:40:15 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	$('.container').css({ | 
					
						
							|  |  |  | 		'width': W, | 
					
						
							|  |  |  | 		'height': H | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// shift the field...
 | 
					
						
							|  |  |  | 	$('.field').css({ | 
					
						
							|  |  |  | 		'margin-top': (parseFloat($('.field').css('margin-top')) + (H-oH)/2),  | 
					
						
							|  |  |  | 		'margin-left': (parseFloat($('.field').css('margin-left')) + (W-oW)/2) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-31 15:25:05 +04:00
										 |  |  | function centerCurrentImage(){ | 
					
						
							|  |  |  | 	$('.field') | 
					
						
							|  |  |  | 		.css({ | 
					
						
							|  |  |  | 			'top': 0, | 
					
						
							|  |  |  | 			'left': 0 | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 		// do this after animations are done...
 | 
					
						
							|  |  |  | 		.one("webkitTransitionEnd oTransitionEnd msTransitionEnd transitionend", centerOrigin) | 
					
						
							|  |  |  | 	// this is repeated intentionally...
 | 
					
						
							|  |  |  | 	// ...needed for small shifts, while the after-animation event 
 | 
					
						
							|  |  |  | 	// is for large moves.
 | 
					
						
							|  |  |  | 	centerOrigin() | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-24 14:40:15 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*********************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-30 16:13:42 +04:00
										 |  |  | // NOTE: this will only return a single scale value...
 | 
					
						
							| 
									
										
										
										
											2012-07-29 02:54:22 +04:00
										 |  |  | function getElementScale(elem){ | 
					
						
							| 
									
										
										
										
											2012-07-30 16:13:42 +04:00
										 |  |  | 	//var transform = elem.css('transform')
 | 
					
						
							|  |  |  | 	var vendors = ['o', 'moz', 'ms', 'webkit'] | 
					
						
							| 
									
										
										
										
											2012-07-29 02:54:22 +04:00
										 |  |  | 	var transform = elem.css('transform') | 
					
						
							|  |  |  | 	var res | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-30 16:13:42 +04:00
										 |  |  | 	// go through vendor prefixes... (hate this!)
 | 
					
						
							|  |  |  | 	if(!transform || transform == 'none'){ | 
					
						
							|  |  |  | 		for(var i in vendors){ | 
					
						
							|  |  |  | 			transform = elem.css('-' + vendors[i] + '-transform') | 
					
						
							|  |  |  | 			if(transform && transform != 'none'){ | 
					
						
							|  |  |  | 				break | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2012-07-29 02:54:22 +04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2012-07-30 16:13:42 +04:00
										 |  |  | 	// no transform is set...
 | 
					
						
							|  |  |  | 	if(!transform || transform == 'none'){ | 
					
						
							|  |  |  | 		return 1 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// get the scale value -- first argument of scale/matrix...
 | 
					
						
							|  |  |  | 	return parseFloat((/(scale|matrix)\(([^,]*),.*\)/).exec(transform)[2]) | 
					
						
							| 
									
										
										
										
											2012-07-29 02:54:22 +04:00
										 |  |  | } | 
					
						
							|  |  |  | // XXX
 | 
					
						
							|  |  |  | function setElementScale(elem, scale){ | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // XXX this appears to be broken -- for some reason the current scale does not change...
 | 
					
						
							| 
									
										
										
										
											2012-07-24 14:40:15 +04:00
										 |  |  | function zoomContainerBy(factor){ | 
					
						
							| 
									
										
										
										
											2012-07-29 02:54:22 +04:00
										 |  |  | 	var zoom = getElementScale($('.field'))*factor  | 
					
						
							| 
									
										
										
										
											2012-07-24 14:40:15 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	setContainerZoom(zoom) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function setContainerZoom(zoom){ | 
					
						
							|  |  |  | 	var H = $('.container').height() | 
					
						
							|  |  |  | 	var W = $('.container').width() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	$('.field').css({ | 
					
						
							| 
									
										
										
										
											2012-07-29 02:54:22 +04:00
										 |  |  | 		'transform': 'scale('+zoom+', '+zoom+')', | 
					
						
							|  |  |  | 		'-moz-transform': 'scale('+zoom+', '+zoom+')', | 
					
						
							|  |  |  | 		'-o-transform': 'scale('+zoom+', '+zoom+')', | 
					
						
							|  |  |  | 		'-ms-transform': 'scale('+zoom+', '+zoom+')', | 
					
						
							|  |  |  | 		'-webkit-transform': 'scale('+zoom+', '+zoom+')', | 
					
						
							| 
									
										
										
										
											2012-07-24 14:40:15 +04:00
										 |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function fitImage(){ | 
					
						
							|  |  |  | 	var H = $('.container').height() | 
					
						
							|  |  |  | 	var W = $('.container').width() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-24 15:38:54 +04:00
										 |  |  | 	var h = $('.image.current').height() | 
					
						
							|  |  |  | 	var w = $('.image.current').width() | 
					
						
							| 
									
										
										
										
											2012-07-24 14:40:15 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	var f = Math.min(H/h, W/w) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	setContainerZoom(f) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function fitThreeImages(){ | 
					
						
							|  |  |  | 	var H = $('.container').height() | 
					
						
							|  |  |  | 	var W = $('.container').width() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-24 15:38:54 +04:00
										 |  |  | 	var h = $('.image.current').height() | 
					
						
							| 
									
										
										
										
											2012-07-24 14:40:15 +04:00
										 |  |  | 	// XXX cheating, need to get three widths...
 | 
					
						
							| 
									
										
										
										
											2012-07-24 15:38:54 +04:00
										 |  |  | 	var w = $('.image.current').width()*3 | 
					
						
							| 
									
										
										
										
											2012-07-24 14:40:15 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	var f = Math.min(H/h, W/w) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	setContainerZoom(f) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 |