/*
  * @copyright Art. Lebedev Studio (http://artlebedev.ru)
  * @author Kotelnikov Dmitriy (dimonnot@design.ru)
  */

(function(){
	/*
	  * Класс лупы
	  * @param loup {jQueryElement} Элемент лупы
	  */

	function Loup( loup, image, fullImagePath ){

		var MIN_LEFT = -70,
			MIN_TOP = -65,
			MAX_LEFT = image[0].offsetWidth - loup[0].offsetWidth - 130,
			MAX_TOP = image[0].offsetHeight - loup[0].offsetHeight - 130,
			LOUP_SIZE = 171,
			ZOOM_WIDTH = 3000,
			ZOOM_HEIGHT = 1860,
			ZOOM = ZOOM_WIDTH / image[0].clientWidth,
			loupX = 924,
			loupY = 490,			
			loupImage = new Image();
		var fullImage = new Image();

		/*
		 * Инициализация полотная для рисования
		 */

		function initCanvas( isSecond ){
			if( $.browser.msie ){
				var canvasContainer = document.createElement('v:oval');
				canvasContainer.strokeweight = '0px';
				canvasContainer.style.width = LOUP_SIZE;
				canvasContainer.style.height = LOUP_SIZE;
				canvas = document.createElement('v:fill');
				canvas.type = 'tile';
				canvas.src = loupImage.src;
				canvas.size = (ZOOM_WIDTH / LOUP_SIZE) + "," + (ZOOM_HEIGHT/ LOUP_SIZE);
				loup.append(canvasContainer);
				canvasContainer.appendChild(canvas);
			}
			else{
				var canvasContainer = document.createElement('div');
				canvasContainer.className = 'canvas';
				canvas = document.createElement('canvas');
				canvas.width = LOUP_SIZE;
				canvas.height = LOUP_SIZE;
				canvasContainer.appendChild(canvas);
				loup.append(canvasContainer);
				canvas = canvas.getContext('2d');
			}
		}

		/*
		 * Навешиваем обработчики событий
		 */

		function initEvents(){
			loup.mousedown(
				function( event ){
					isMoving = 1;
					mouseX = event.pageX;
					mouseY = event.pageY;
					return false;
				}
			);

			$(document).mousemove(
				function( event ){
					if( !isMoving )
						return true;

					loupX += event.pageX - mouseX;
					loupY += event.pageY - mouseY;
					drawLoup();
					mouseX = event.pageX;
					mouseY = event.pageY;
					return false;
				}
			).mouseup(
				function(){
					isMoving = 0;
				}
			);
		}

		/*
		 * Функция перерисовки лупы
		 */
		function drawLoup(){
			if( loupX < MIN_LEFT )
				loupX = MIN_LEFT;

			if( loupX > MAX_LEFT )
				loupX = MAX_LEFT;

			if( loupY < MIN_TOP )
				loupY = MIN_TOP;

			if( loupY > MAX_TOP )
				loupY = MAX_TOP;

			if( $.browser.msie ){
				canvas.position = ((-loupX * ZOOM - 167) / LOUP_SIZE) + "," + ((-loupY * ZOOM - 177)/ LOUP_SIZE);
			}
			else{
				canvas.beginPath();
				canvas.arc(LOUP_SIZE/2, LOUP_SIZE/2, LOUP_SIZE/2, 0, Math.PI*2, true);
				canvas.clip();

				try{
					if (fullImage && fullImage.width) {
						canvas.fillStyle = "#ffffff";
						canvas.fillRect(0, 0, ZOOM_WIDTH, ZOOM_HEIGHT);
					}
					canvas.drawImage(loupImage, Math.round(-loupX * ZOOM) - 167, Math.round(-loupY * ZOOM) - 177, ZOOM_WIDTH, ZOOM_HEIGHT);
				}catch(e){}
			}

			loup.css({
				left:loupX + 'px',
				top:loupY + 'px'
			});
		}

		var canvas, isMoving = 0, mouseX, mouseY;

		$(loupImage).load(
			function(){
				initCanvas();
				initEvents();
				drawLoup();
				loup.show();
				$(fullImage).load(
					function(){
						loupImage = fullImage;
						if( $.browser.msie ){
							canvas.src = fullImage.src;
						}
						drawLoup();
					}
				);
				fullImage.src = fullImagePath;
			}
		);
		loupImage.src = image.attr('src');
	}

	$(
		function(){
			$('#new_year_photo a.photo').click(
				function(){
					return false;
				}
			).each(
				function(){
					var that = this;
					Loup($('#loup'), $(that).find('img'), that.href);
				}
			);

			if( /msie (6|5)/.test(navigator.userAgent.toLowerCase()) ){
				$('#loup img').each(
					function(){
						this.style.width = this.clientWidth +'px';
						this.style.height = this.clientHeight +'px';
						var src = this.src;
						this.src = 'e.gif';
						this.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='crop')";
					}
				);
			}
			
		}
	);
	
	if($.browser.msie && !document.namespaces["v"]){
		document.namespaces.add("v", "urn:schemas-microsoft-com:vml");
		var ss = document.createStyleSheet();
		if( /msie 8/.test( navigator.userAgent.toLowerCase() ) )
			ss.cssText = "v\\:image,v\\:line, v\\:fill, v\\:oval{behavior:url(#default#VML);display:inline-block}";
		else
			ss.cssText = "v\\:* {behavior:url(#default#VML);display:inline-block}";
	}
})();