﻿(function($){$.fn.vnMapComponent=function(options){this.each(function(){var target=$(this);if(target.data("vnMapComponent"))return target;var vnMapComponent=new VNMapComponent(target,options);target.data("vnMapComponent",vnMapComponent)})};function VNMapComponent($target,options){var $currentLi,timeoutID,slideAnimID,curPopupPoi,curPopupLayer,defaultLayer=0,currentLayer=0,mapData={},slidePos=-1,$popup=$('<div class="mc_popup"/>').appendTo("body"),$pois=$target.find(".mc_pois"),$poimenu=$target.find(".mc_poimenu"),
$bgMap=$target.find(".mc_map"),$bgLayer=$target.find(".mc_layer"),isFrontPage=$target.parents(".c_frontpageright").length;options=$.extend({},{jsonpath:"layers.json",method:"POST",pageId:0,languageCode:"en",isMice:false,enableBooking:false,hoverdelay:100,popupdirection:"left",fadeduration:250,showduration:4E3,citybreakpoint:387,shiftx:-28,shifty:-34,enableslideshow:false},options);function init(){$target.show();initPopup();if(typeof options.jsonpath!="string"){layers=options.jsonpath;renderData()}else loadData()}
init();function initPopup(){if($target.parents(".c_frontpageright").length)options.popupdirection="right";if(options.popupdirection=="right")$popup.addClass("mc_popup_right");$popup.hover(function(){window.clearTimeout(timeoutID);mouseOverHandler($currentLi)},function(){hidePopup()})}function loadData(){var paramChar=options.jsonpath.indexOf("?")==-1?"?":"&";$.ajax({url:options.jsonpath,dataType:"json",type:options.method,contentType:"application/json; charset=utf-8",data:"{ "+"pageId: "+options.pageId+
", "+'languageCode: "'+options.languageCode+'", '+"isMice: "+options.isMice+", "+"enableBooking: "+options.enableBooking+" }",success:function(data){mapData=data.d;renderData()},error:function(x,e){$target.hide();if(x.status==0)log("You are offline",this,arguments);else if(x.status==404)log("Requested URL not found",this,arguments);else if(x.status==500)log("Internel Server Error",this,arguments);else if(e=="parsererror")log("Parsing JSON Request failed",this,arguments);else if(e=="timeout")log("Request Time out",
this,arguments);else log("Unknown Error: "+x.responseText,this,arguments)}})}function renderData(){if(!window.isMobile)renderMap();renderMenu()}function renderMap(){var i,len;$.views.registerTags({popupwidth:function(arr){return arr.length>5?"wide":"narrow"},labelDirection:function(poix){return poix>options.citybreakpoint?"label_left":"label_right"},poiStyle:function(position,poiclass){if(poiclass=="city"&&position.x>options.citybreakpoint)return"top:"+position.y+"px;"+"right:"+($bgMap.width()-position.x+
11)+"px";return"top:"+position.y+"px;"+"left:"+position.x+"px"},iconStyle:function(iconclass,imgicon,imgsize){if(iconclass)return"";else if(imgicon)return"background-image:url("+imgicon+");"+"width:"+imgsize.w+"px;"+"height:"+imgsize.h+"px"}});for(i=0,len=mapData.layers.length;i<len;i++)if(mapData.layers[i].isdefault){currentLayer=defaultLayer=i;break}$bgMap.css("background-image","url("+mapData.layers[defaultLayer].img+")");$bgLayer.css("background-image","url("+mapData.layers[defaultLayer].img+
")")}function renderMenu(){var bodyMaxHeight,menuHeadHeight=0,heightAdjust=26;window.isMobile=$("#mobilehead").is(":visible");$poimenu.html($("#poiMenuTemplate").render(mapData));if(!window.isMobile)renderLayer(currentLayer);else currentLayer=-1;$poimenu.find(".mc_menu_head").each(function(index,el){$(el).click(function(){stopSlideshow();if(window.isMobile)renderLayer(index);else if(currentLayer!=index){defaultLayer=currentLayer=index;renderLayer(index)}return false});menuHeadHeight+=$(el).height()});
bodyMaxHeight=$target.find(".mc_content").height()-menuHeadHeight-heightAdjust-$target.find(".mc_poimenu_heading").height();$poimenu.find(".mc_menu_body_inner").css("max-height",bodyMaxHeight+"px");$poimenu.find(".mc_menu_body").each(function(layerIndex,layerEl){$(layerEl).find("li").each(function(poiIndex,poiEl){$(poiEl).find("a").hover(function(){stopSlideshow();mouseOverHandler($pois.find("li").eq(poiIndex))},function(){mouseOutHandler()})})})}function startSlideshow(){if(!options.enableslideshow||
mapData.layers[currentLayer].pois.length<2||slidePos!=-1||window.isMobile)return;slideAnimID=setInterval(switchSlide,options.showduration);switchSlide()}function stopSlideshow(){clearInterval(slideAnimID);if($currentLi)$currentLi.removeClass("poiHover");$popup.fadeOut(0)}function switchSlide(){hidePopup(options.fadeduration,function(){slidePos=(slidePos+1)%mapData.layers[currentLayer].pois.length;$currentLi=$pois.find("li").eq(slidePos);showPopup(options.fadeduration)})}function renderLayer(idx){var $visibleBodies;
if(!window.isMobile){stopSlideshow();$popup.hide();$pois.fadeOut("normal",function(){$bgMap.css("background-image",$bgLayer.css("background-image"));$bgLayer.hide().css("background-image","url("+mapData.layers[idx].img+")").fadeIn("normal",function(){renderPois(idx);$pois.fadeIn("normal",startSlideshow)})})}$visibleBodies=$poimenu.find(".mc_menu_body:visible");if(window.isMobile){$poimenu.find(".mc_menu_head").eq(idx).toggleClass("current");$poimenu.find(".mc_menu_body").eq(idx).slideToggle("fast")}else{if(!isFrontPage)if($visibleBodies.length)$poimenu.find(".mc_menu_body:visible").slideUp("fast",
function(){$poimenu.find(".mc_menu_body").eq(idx).slideDown("fast")});else $poimenu.find(".mc_menu_body").eq(idx).slideDown("fast");$poimenu.find(".mc_menu_head").removeClass("current").eq(idx).addClass("current")}}function renderPois(idx){$pois.html($("#poiTemplate").render(mapData.layers[idx].pois)).find("li").mouseover(function(){mouseOverHandler($(this))}).mouseout(function(){mouseOutHandler()})}function mouseOverHandler($li){stopSlideshow();$currentLi=$li;$currentLi.addClass("poiHover");if(window.isMobile)return;
window.clearTimeout(timeoutID);if(curPopupPoi==$li.index()&&curPopupLayer==currentLayer)$popup.fadeIn(0);else timeoutID=window.setTimeout(function(){showPopup()},options.hoverdelay)}function mouseOutHandler(){window.clearTimeout(timeoutID);hidePopup()}function showPopup(fadeTime){$currentLi.addClass("poiHover");fadeTime=fadeTime||0;var pos=$currentLi.offset(),top,xAdd,left;if(!pos||pos.left==0)return;top=Math.floor(pos.top+$currentLi.height()/2);xAdd=options.popupdirection=="left"?$currentLi.width():
0;left=Math.floor(pos.left+xAdd);$popup.html($("#popupTemplate").render(mapData.layers[currentLayer].pois[$currentLi.index()]));$popup.fadeIn(fadeTime).css({top:top+"px",left:left+"px"});curPopupPoi=$currentLi.index();curPopupLayer=currentLayer}function hidePopup(fadeTime,callBack){fadeTime=fadeTime||0;if($currentLi)$currentLi.removeClass("poiHover");if(fadeTime)$popup.fadeOut(fadeTime,callBack);else{$popup.hide();if(callBack)callBack()}}}})(jQuery);

