map.js 6.11 KB
/**
 * @fileOverview map页面JS
 * @author wei.wang@yoho.cn
 * @date 2014-05-21
 */

	var $ = jQuery = require("jquery");
	require("../public");
	require("../plugin/nicescroll");
	var _ui=require("jquery-ui");
	_ui($);
	require("jquery.imagesloaded");
		initShopListHeight();
	$(".city-list").niceScroll();
	var currentZoom=0.85;
	var OriginalWidth=0,OriginalHeight=0,OriginalMarks={};
	exports.init=function(){
	
		$(".map-zoom").show();
		OriginalWidth=$("#citymap-img").width();
		OriginalHeight=$("#citymap-img").height();
		$("#map-zoom-max").click(function(){zoomMax();});
		$("#map-zoom-min").click(function(){zoomMin();});
		$(".big-mark").each(function(index,obj){
			var top=$(this).top();
			var left=$(this).left();
			OriginalMarks[$(this).attr("id")]={left:left,top:top};
		});
		$(window).bind("resize",function(){
			initShopListHeight();
		});
		
		$(".shop").unbind("hover").hover(
			function(){
				$("#citymap").stop();
				$(".mark-tips").hide();//隐藏现有的气泡
				moveShopToView(this);
				
			},
			function(){
				var shopId=$(this).id().replace("shoplist-","mark");
				$("#"+shopId).find(".mark-tips").hide();
			}
		);
		$(".big-mark").hover(
			function(){$(this).find(".mark-tips").css("display","table");},
			function(){$(this).find(".mark-tips").css("display","none");}
		);
		$(".shop-name").click(function(){
			var url=$(this).attr("src");
			window.open(url);
		});
		$(".big-mark").click(function(){
			var href=$("a[val='"+$(this).attr("val")+"']").attr("src");
			window.open(href);
		});
		
		
		var bodyHeight = document.body.clientHeight - 177;
        $(".map-city-content").height(bodyHeight);
        $(window).bind("resize", function() 
        {
            var bodyHeight = document.body.clientHeight - 177;
            $(".map-city-content").height(bodyHeight);
            $("#citymap-img").width($("#citymap").width());
            $("#citymap-img").height($("#citymap").height());
        });
        $("#citymap-img").imagesLoaded(function()
        {
            $(this).width("100%").height("100%");
            $("#citymap").css({width: $(this).width(), height: $(this).height(), position: "relative"}).draggable({
                drag: function() {},
                stop: function() {
                    mapRevert(this);
                }
            });
        });
        var isMouseDown = false;
		
		
	};
	function initShopListHeight(){
		var currentWidth=document.body.clientWidth;
		if(currentWidth>=1190)
		{
			$(".city-list").height(document.body.clientHeight-310);
		}
		else if(currentWidth<1190&&currentWidth>769)
		{
			$(".city-list").height(document.body.clientHeight-260);
		}
		else if(currentWidth<=768)
		{
			$(".city-list").height(document.body.clientHeight-178);
		}
	}
	/*在商店上显示tips*/
	function shopShowTips(obj){
		var shopId=$(obj).id().replace("shoplist-","mark");
		$("#"+shopId).find(".mark-tips").show();
	}
	/*如果店铺不在当前视野范围内,则移动到当前范围*/
	function moveShopToView(obj){
		
		var contentHeight=$(".map-city-content").height();
		var contentWidth=document.body.clientWidth;
		var shopId=$(obj).id().replace("shoplist-","mark");
		var top=$("#"+shopId).offset().top;
		var left=$("#"+shopId).offset().left;
		var absoluteTop=0;
		var absoluteLeft=0;//$("#"+shopId).offset().left+$("#citymap").left();
		if ($("#"+shopId).css("top").indexOf("%")>0) {
			absoluteTop=parseFloat($("#"+shopId).css("top").replace("%",""))/100*$("#citymap").height();
			absoluteLeft=parseFloat($("#"+shopId).css("left").replace("%",""))/100*$("#citymap").width();
		}
		else
		{
			absoluteTop=$("#"+shopId).top();
		 	absoluteLeft=$("#"+shopId).left();
		}
		
		var location=getScreenCenter();
		
		if(top<304&&top>0||top<0||
			top+177>document.body.clientHeight
			||left<$(".city-list").width()
			||left>document.body.clientWidth
			)
		{
			var toTop=location.y-absoluteTop;
			var toLeft=location.x-absoluteLeft;
			if (toLeft>$(".city-list").width()) {
				toLeft=$(".city-list").width();
				
			}
			if (toTop>190) {
				toTop=190;
			}
			$("#citymap").animate({top:toTop,left:toLeft},500,function(){
				$("#"+shopId).find(".mark-tips").show();
			});
		}
		else
		{
			$("#"+shopId).find(".mark-tips").show();
		}
	}
	function zoom(){
		var width=OriginalWidth*currentZoom;
		var height=OriginalHeight*currentZoom;
		$("#citymap").width(width);
		$("#citymap").height(height);
	}
	function zoomMax(){
		if(currentZoom>=1)
		{
			return;
		}
		currentZoom=currentZoom+0.05;
		zoom();
	}
	function zoomMin(){
		if(currentZoom<=0.7)
		{
			return;
		}
		currentZoom=currentZoom-0.05;

		
		zoom();
	}
	function mapRevert(obj) 
        {
            var top = parseFloat($(obj).css("top").replace("px", ""));
            var left = parseFloat($(obj).css("left").replace("px", ""));
            var contentHeight = $(".map-city-content").height();
            var contentWidth = document.body.clientWidth;
            var bottom = contentHeight - top - $(obj).height();
//            var right = contentWidth - left - parseFloat($(obj).width());
//            var rightForbid = contentWidth - $(".city-shop-list").width();
            if (top > 120 && bottom < 0)
            {
                $(obj).animate({top: 120}, 500);
            }
            if (bottom > 0 && top < 120)
            {
                $(obj).animate({top: contentHeight - $(obj).height()}, 500);
            }
            if (left + $(obj).width() <= $(".city-shop-list").width())
            {
                $(obj).animate({left: contentWidth - $(obj).width()}, 500);
            }
            if (left > contentWidth)
            {
                $(obj).animate({left: contentWidth - $(obj).width()}, 500);
            }
        }
	function getScreenCenter(){
		var width=document.body.clientWidth;
		var height=document.body.clientHeight;
		var _left=(width-$(".city-shop-list").width())/2+$(".city-shop-list").width();
		var _headerHeight=$("#maxEnterprise").height()-$(".enterprise-header").height()-12;
		var _top=$(".map-city-content").height()/2;
		//$("<span/>").css({position:"absolute",display:"block",width:20,height:20,"z-index":20,background:"#f00",left:_left,top:_top}).appendTo(document.body);
		return {x:_left,y:_top};
	}