﻿$(function() {
	$('#catalog_image img').hide();
	$('#catalog_zoom .minus').hide();
		   
	var ua =navigator.userAgent;
	var zoomin = false;
	var _width = 1056;
	var _height = 1250;
	if(ua.indexOf("MSIE")!=-1 && ua.indexOf("Trident/4.0")!=-1){ //IE8
		$('#catalog_image').css('width', '526px').css('height', '624');
	}
	
	var _width2 = _width/2;
	var _height2 = _height/2;
	var _width3 =  _height/4;
	var _height3 = _width/4;
	var mx = 0;
	var my = 0;
	
	// loader init
	//$('#catalog_image img').animate({width:_width2, height:_height2, left: 0, top: 0}, 0).hide();
	var timer;
	$(window).bind("load", function() {
		timer = setInterval(doThis, 500);
	});
	function doThis() {
		clearInterval(timer);
		$('#catalog_image img:hidden').eq(0).fadeIn(500);
	}
	
	// !! iphone ipad ipod
	if(ua.indexOf('iPhone') > -1 || ua.indexOf('iPad') > -1 || ua.indexOf('iPod')  > -1){
		var move  = "touchmove";
		var start = "touchstart";
		var end   = "touchend";
	}else{
		var start = "mousedown";
		var move  = "mousemove";
		var end   = "mouseup";
	}

	// zoom mouse event
	$('#catalog_zoom').mouseover(function(e) {
        $(this).css('cursor', 'pointer');
	});
	$('#catalog_zoom').mouseout(function(e) {
        $(this).css('cursor', 'auto');
	});

	// image event method start
	// zoom event
	$('#catalog_zoom').click(function(e) {
		if (zoomin) {
			$('#catalog_image img').fadeTo(0, 0.5).animate({width:_width2, height:_height2, left: 0, top: 0}, {duration:500, complete:function(){
					$('#catalog_image img').fadeTo(200, 1);
				}
			});
			// IE6 //$('#catalog_zoom img').attr("src", "images/plus.png");
			$('#catalog_zoom .plus').show();
			$('#catalog_zoom .minus').hide();
			
			$('#catalog_image img').css('cursor', 'auto');
			zoomin = false;
		} else {
			var x = 0;
			if (x < 0) x = 0;
			if (x > _width2) x = _width2;
			var y = 0;
			if (y < 0) y = 0;
			if (y > _height2) y = _height2;
			
			$('#catalog_image img').fadeTo(0, 0.5).animate({width:_width, height:_height, left:-_width3, top:-_height3}, {duration:500, complete:function(){
					$('#catalog_image img').fadeTo(200, 1);
				}
			});
			
			// IE6 //$('#catalog_zoom img').attr("src", "images/minus.png");
			$('#catalog_zoom .plus').hide();
			$('#catalog_zoom .minus').show();
			
			$('#catalog_image img').css('cursor', 'move');
			mx = _width3;
			my = _height3;
			zoomin = true;
		}
	});
	// drag event
    $('#catalog_image img').bind(start, function(e) {
		if(zoomin){
			if(!e.pageX)e = event.touches[0];
			var bx = e.pageX;
			var by = e.pageY;
			$().bind(move, function(e) {
				if(!e.pageX)e = event.touches[0];
				mx += bx - e.pageX;
				bx = e.pageX;
				if (mx < 0) mx = 0;
				if (mx > _width2) mx = _width2;
				my += by - e.pageY;
				by = e.pageY;
				if (my < 0) my = 0;
				if (my > _height2) my = _height2;
				
				$('#catalog_image img').animate({left: -mx, top: -my}, 0);
				return false;
			}).one(end, function() {
				$().unbind(move);
			});
			return false;
		};
    });
});
