var id = FM.Cookie.get("ImdsfsdShFaebMOLJWqol100Meid"); 
var f5lai = FM.Cookie.get("f5lai"); 
var isShowBlog = true;

var pathName = "/f5";
var map_data_url  = "/f5/mwfm/indexsl?ctrl=index&t="+FM.AutoReNote.getRefreshToken();
var map_note_count      = 0;
var map_note_show_count = 0;
var map_note_json         = null;//数据对象
var map_note_timer        = null;//执行对象
var map_note_spacing_time = 5000;//间隔时间
var map_note_maker        = null;//数据点
var map_carousel          = null;//滚动对象
var map_carousel_scroll   = 10;//每次滚动条数
var myIcon         = null;
var accuratelyIcon = null;
var intangiblyIcon = null;
var gmapObj        = null;

if(!isUndefined(id) && id != null && id != "null" && id != "undefined" && id != "" && id != "\"\""){
	if(!isUndefined(f5lai) && f5lai != null && f5lai != "null" && f5lai != "undefined" && f5lai != "" && f5lai != "\"\""){
		window.location = "/f5/mwfm/home?u=" + f5lai;
	}else {
		window.location = "/f5/mwfm/home";
	}
 	isShowBlog = false;
}

var name = FM.Cookie.get("NhjiKUu890ApfdsPKplsMIOe");			 
if(isUndefined(name) || name == null || name == "null" || name == "undefined"){
	name = "";
}
var strChecked = FM.Cookie.get("ImCsfsdShFaebhOLJWqolc0Meked");
if(strChecked != "checked"){
	strChecked = "";
}

FMJS.ready(function(){
	if(!isShowBlog){
		return ;
	}
	
	G("#indexRegister").hover(
		function(){
			G(this).addClass("index_register_hover");
		},
		function(){
			G(this).removeClass("index_register_hover");
		}
	);
	G("#indexRamble").hover(
		function(){
			G(this).addClass("index_ramble_hover");
		},
		function(){
			G(this).removeClass("index_ramble_hover");
		}
	);
	G("#loginBtn").hover(
		function(){
			G(this).addClass("login_btn_hover");
		},
		function(){
			G(this).removeClass("login_btn_hover");
		}
	);
	
	accuratelyIcon = new Mapifies['createIcon']();
	intangiblyIcon = new Mapifies['createIcon']({
		'iconImage': pathName+'/map/images/pin_b.gif'
	});
	
	hs.graphicsDir = pathName+'/scfe/common/highslide/graphics/';
	hs.wrapperClassName = 'highslide-wrapper';

	jQuery('#map').jmap('init', {
		'mapType':G_PHYSICAL_MAP,
		'mapCenter':[39.5420, 116.2329],
		'mapZoom':6,
		'mapControl': 'none',
		'mapEnableType': false,
		'customUI' : false,
		'mapEnableScrollZoom':false
	},function(thisMap, element, options){
		gmapObj = thisMap;
		queryAtten();
		changeMapSize();
	});

});

function changeMapSize(){
	var clientHeight    = screen.height;
	if(clientHeight > 760){
		clientHeight -= 310;
		$(".index_body").animate({ height : clientHeight+"px"}, { queue: true, duration: 200,complete:function(){
			gmapObj.checkResize();
		}});
		$(".index_map").animate({ height : "100%"}, { queue: true, duration: 200,complete:function(){
			gmapObj.checkResize();
		}});
	}
}

function queryAtten(){
	FMJS.getSJSON(map_data_url,'',
		function(json){
			var errorCode = json.error_code;
			if(errorCode == 0 && json.note_list != null){
				map_note_json = json;
				map_note_count = json.note_list.length;
				jQuery('#mycarousel').jcarousel({
					scroll: map_carousel_scroll,
			        itemLoadCallback: mycarousel_itemLoadCallback
			    });
			}
		},
		function(){}
	);
}

function mycarousel_itemLoadCallback(carousel, state){
	map_carousel = carousel;
	var map_acc_photo = "";
    if (state != 'init')	return;
	var lon     = "";
	var lat     = "";
	var aLogo   = "";
	var iCount  = 0;
	for(var i=0;i<map_note_count;i++){
		lon   = map_note_json.note_list[i].longitude;
		lat   = map_note_json.note_list[i].latitude;
		aLogo = map_note_json.note_list[i].account_photo_path;
		if(lon != "" && lat != ""){
			iCount++;
			map_acc_photo = '<img id="map_carousel_logo_'+map_note_json.note_list[i].note_id+'" ' +
					'src="'+map_note_json.note_list[i].account_photo_path+'" ' +
					'width="40" height="40" alt="'+map_note_json.note_list[i].account_name+'" title="'+map_note_json.note_list[i].account_name+'"' +
					'value="'+i+'" onclick="logoClick(this)" class="clickedImg"/>';
			carousel.add(iCount, map_acc_photo);
		}
	}
	carousel.size(iCount);
	setMapData();
}

function mycarousel_nextCallback(){
	map_note_show_count = 0;
	map_carousel.prev();
}

function logoClick(obj){
	map_note_show_count = G(obj).attr("value");
	if(map_note_timer != null)	clearTimeout(map_note_timer);
	setMapData();
}

function setMapData(){
	if(map_note_json != null){

		map_note_timer = setTimeout("setMapData()",map_note_spacing_time);
		
		if(map_note_show_count < map_note_count){
			
			var lon     = map_note_json.note_list[map_note_show_count].longitude;
			var lat     = map_note_json.note_list[map_note_show_count].latitude;
			var noteId  = map_note_json.note_list[map_note_show_count].note_id;
			var localSource = map_note_json.note_list[map_note_show_count].localtion_source;
			var pointHTML = getOverlayHtml();
			
			map_note_show_count++;
			
			if((map_note_show_count-1) != 0 && (map_note_show_count-1)%map_carousel_scroll == 0){
				map_carousel.next();
			}
			
			if(map_note_maker != null){
				jQuery('#map').jmap('RemoveMarker',map_note_maker);
			}
			
			if(localSource == FM.EnvParam.iLocalSourceLoginIp || localSource == FM.EnvParam.iLocalSourceLocation || localSource == FM.EnvParam.iLocalSourcePhone){
				myIcon = intangiblyIcon;
			}else if(localSource == FM.EnvParam.iLocalSourceCustomize || localSource == FM.EnvParam.iLocalSourceGPS){
				myIcon = accuratelyIcon;
			}else{
				myIcon = intangiblyIcon;
			}
			
			if(lon != "" && lat != ""){
				jQuery('#map').jmap('MoveTo', {
					'centerMethod': 'pan',
					'mapCenter': [lat, lon]
					}, 
					function(point, options){
						//alert(point.lat());
						G(".clickedImg").removeClass("clickingImg");
						jQuery('#map').jmap('AddMarker', {
							'pointLatLng': [point.lat(), point.lng()],
							'pointHTML': pointHTML,
							'pointIcon': myIcon,
							'pointIsDraggable': false,
							'centerMap': true
							},function(marker, options){
								map_note_maker=marker;
								marker.openInfoWindowHtml(options.pointHTML, {maxContent: options.pointMaxContent, maxTitle: options.pointMaxTitle});
								G("#map_carousel_logo_"+noteId).addClass("clickingImg");
							}
						);
						
				});
				G(".map_note_overlay").hover(
					function(){
						if(map_note_timer != null)	clearTimeout(map_note_timer);
					},
					function(){
						map_note_timer = setTimeout("setMapData()",map_note_spacing_time);
					}
				);
			}
		}else{
			mycarousel_nextCallback();
		}
		
	}
}

function getOverlayHtml(){
	var accountId      = map_note_json.note_list[map_note_show_count].account_id;
	var accountLogo    = map_note_json.note_list[map_note_show_count].account_photo_path;
	var accountName    = map_note_json.note_list[map_note_show_count].account_name;
	var publishDate    = map_note_json.note_list[map_note_show_count].publish_time;
	var publishType    = map_note_json.note_list[map_note_show_count].publish_type;
	var receiveType    = map_note_json.note_list[map_note_show_count].receiver_label;
	var noteMessage    = map_note_json.note_list[map_note_show_count].text_message_tran;
	var noteType       = map_note_json.note_list[map_note_show_count].note_type;
	var linkType       = map_note_json.note_list[map_note_show_count].link_type;
	var attachmentImg  = map_note_json.note_list[map_note_show_count].note_file_path_s;
	var attachmentImgL = map_note_json.note_list[map_note_show_count].note_file_path_l;
	var attachmentLink = map_note_json.note_list[map_note_show_count].link;
	//var attachmentLinkCover = map_note_json.note_list[map_note_show_count].link_cover;
	
	var strHtml = '<div class="map_note_overlay">';
	strHtml += '<div class="map_note_overlay_acc"><a href="'+pathName+'/mwfm/profile?aid='+accountId+'">'+accountName+'：</a></div>';
	strHtml += '<div class="map_note_overlay_body">';
	strHtml += '<div class="map_note_overlay_content">'+noteMessage+'</div>';
	if(noteType == FM.EnvParam.iPhotoNote){
		strHtml += '<div class="note_mess_attachment">';
		strHtml += '<a href="'+attachmentImgL+'" class="highslide" onclick="return hs.expand(this)">';
		strHtml += '<img src="'+attachmentImg+'" class="map_img_src" onload="initImgLogo(this,100)"/></a>';
		strHtml += '</div>';
	}else if(attachmentLink != ""){
		strHtml += '<div class="note_link"><a href="'+attachmentLink+'" target="_blank" class="button">'+attachmentLink+'</a></div>';
		strHtml += '';
	}
	strHtml += '</div>';
	strHtml += '<div class="map_note_overlay_msg">';
	strHtml += '<label>'+publishDate+'</label>';
	strHtml += '<label>'+publishType+'</label>';
	strHtml += '<label>'+receiveType+'</label>';
	strHtml += '</div>';
	strHtml += '</div>';
	
	return strHtml;
}

function goTarget(flag){
	if(flag == "ramble"){
		top.window.location.href = "/f5/mwfm/ramble?ctrl=nn";
	}else if(flag == "register"){
		top.window.location.href = "/f5/jsp/register/register.jsp";
	}
}

function unload (){
	jQuery('#map').jmap('unload',{});
}