var google_zoomLevelBig = 3;
var google_zoomLevelSmall = 4;
var google_markers = new Array();
var google_datas = new Array();
var google_ct = 0;
var google_id = 0;
var google_flag = false;
var google_type;
var google_width;
var google_height;
var google_pname = "";
var google_pic = "";
var google_url = "";
var google_city ="";
var google_country ="";
var google_ct2 = 0;
var google_currency = "";

Math.rand = function(l,u){return Math.floor((Math.random()*(u-l+1))+l);}

window.recycler=(function(){var t=document.createElement('div');t.id="recycler";return t;})();

//Google Map初始化
function initialize() { 
		var latlng = new google.maps.LatLng(37.09024,-95.712891);//预设位置
		
		if(google.loader.ClientLocation){
			latlng = new google.maps.LatLng(google.loader.ClientLocation.latitude,google.loader.ClientLocation.longitude);
			google_city = google.loader.ClientLocation.address.city;
			if(google.loader.ClientLocation.address.country!="USA"){
				google_country = google.loader.ClientLocation.address.country;
			}
		}
		map = new google.maps.Map2(document.getElementById('google_map'),{ size: new GSize(google_width,google_height) }); 
		infoWindow.prototype = new google.maps.Overlay();
		infoWindow.prototype.initialize = function( map ) {
			this.map = map;
			var container = document.createElement( 'div' );
			container.setAttribute( 'class', 'container' );
			map.getPane( G_MAP_FLOAT_PANE ).appendChild( container );
			this.container = container;
		
			var shadow = document.createElement( 'div' );
			shadow.setAttribute( 'class', 'shadowContainer' );
			map.getPane( G_MAP_FLOAT_SHADOW_PANE ).appendChild( shadow );
			this.shadow = shadow;
		}
		infoWindow.prototype.remove = function() {
			if(this.container&&window.recycler){
				window.recycler.appendChild(this.container);
				window.recycler.innerHTML='';
			}
			if(this.shadow&&window.recycler){
				window.recycler.appendChild(this.shadow);
				window.recycler.innerHTML='';
			}
		}
		infoWindow.prototype.redraw = function( force ) {
			if ( !force ) return;
			var mid = document.createElement('div');
			mid.className = 'google_kuangmid';
			mid.style.height = '110px';
			mid.style.position = 'absolute';
			mid.style.top = '6px';
			mid.innerHTML = this.html;
			var contentWidth = 169;
			var contentHeight = 110;
			var wrapper = document.createElement( 'div' );
			wrapper.setAttribute( 'class', 'mapSurround4' );
			var top = document.createElement('div');
			top.className = 'google_kuangup';
			top.innerHTML = '<img src="/Templates/Site61/Dino/images/google_up.gif"/>';
			top.style.position = 'absolute';
			wrapper.appendChild( top );
			wrapper.appendChild( mid );
			var bot = document.createElement('div');
			bot.className = 'google_kuangxia';
			bot.innerHTML = '<img src="/Templates/Site61/Dino/images/google_xia.gif"/>';
			bot.style.position = 'absolute';
			bot.style.top = '116px';
			wrapper.appendChild(bot);
			var pixelLocation = this.map.fromLatLngToDivPixel( this.marker.getPoint() );
			this.container.style.position = 'absolute';
			this.container.style.left = ( pixelLocation.x - 3 ) + 'px';
			this.container.style.top = ( pixelLocation.y - contentHeight - 25 - this.marker.getIcon().iconSize.height ) + 'px';
			this.container.style.display = 'block';
			this.container.appendChild( wrapper );
			var mapNE = this.map.fromLatLngToDivPixel( this.map.getBounds().getNorthEast() );
			var panX = 0;
			var panY = 0;
			if ( this.container.offsetTop < mapNE.y ) {
				panY = mapNE.y - this.container.offsetTop;
			}
			if ( this.container.offsetLeft + contentWidth + 10 > mapNE.x ) {
				panX = ( this.container.offsetLeft + contentWidth + 10 ) - mapNE.x;
			}
		}
		geocoder = new GClientGeocoder(); 
		
		if(google_type=='small'){
			map.disableDoubleClickZoom();
			map.disableScrollWheelZoom();
			map.setCenter( latlng, google_zoomLevelSmall );
		}else{
			map.addControl(new GMapTypeControl()); 
			map.addControl(new GSmallMapControl());
			map.disableDoubleClickZoom();
			map.disableScrollWheelZoom();
			map.setCenter( latlng, google_zoomLevelBig );
//			GEvent.addListener(map,"moveend",function(){
//				for(i=0;i<google_markers.length;i++){
//					try{
//						google_markers[i].infoWindowInstance.redraw(true);	
//					}catch(e){	
//					}
//				}										  
//			});
//			
			if(google_city!=''){
				document.getElementById("topSelling").src = jsMainSiteUrl+"Templates/Site61/Dino/include/inc_topsellingarea.cfm?city="+google_city+"&cur="+google_currency;	
			}else{
				google_city = '';
				google_country = '';
				document.getElementById("topSelling").src = jsMainSiteUrl+"Templates/Site61/Dino/include/inc_topsellingarea.cfm?cur="+google_currency;
			}
		}
		getProductData();
}

function getProductData(){
	$.ajax({
		type:"GET",
		dataType:"json",
		url:jsMainSiteUrl+"WatchShoppingAjax.cfm",
		data:"id=0",
		error:function(XMLHttpRequest, textStatus, errorThrown){
							AlertInfoAdd("WatchShopping Get ProductData AJAX Error."+textStatus+"-"+errorThrown,36,0);
						},
		success:function(result){
			google_datas = new Array();
			google_datas = result;
			if(google_datas.length>0)startMapShow();
		}
	});	
}

function startMapShow(){
//	if(google_type=='small'){
//		for(i=0;i<15;i++){
//			google_datas[i].filename = google_url;
//			google_datas[i].itemName = google_pname;
//			google_datas[i].pic = google_pic;
//		}
//		google_ct = Math.rand(0,14);
//		creatPointSmall();
//	}else{
		if(google_url!=""&&google_pname!=""&&google_pic!=""){
			for(i=0;i<15;i++){
				google_datas[i].filename = google_url;
				google_datas[i].itemName = google_pname;
				google_datas[i].pic = google_pic;
			}
			google_ct = Math.rand(0,14);	
		}
		creatPoint();
//	}
}

//小图坐标点创建
function creatPointSmall(){
	obj = google_datas[google_ct];
	if(typeof(obj)!='undefined'&&null!=obj.country&&""!=obj.country){
		var address = obj.country + ' ' + obj.city;
		geocoder.getLocations( 
			address,
			function(rep) { 
			  if (!rep || rep.Status.code != 200) {
				  google_ct++;
			  } else { 
			  place = rep.Placemark[0];
			  point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
			  	if(google_markers.length==0){
					google_markers[0] = createMarker( point, createHtmls(google_datas[google_ct]) );
				}else{
					google_markers[0].infoWindowInstance.remove();
					google_markers[0].infoWindowInstance.html = '';
					google_markers[0].remove();
				}
				google_markers[0] = {};
				google_markers[0] = createMarker( point, createHtmls(google_datas[google_ct]) );
				google_ct++;

				if(google_ct>=google_datas.length){
					for(i=0;i<google_markers.length;i++){
						google_markers[i].infoWindowInstance.remove();
						google_markers[i].remove();
						google_markers[i] = {};
					}
					google_ct = 0;
					//window.clearInterval(id);
					//getProductData('small');
				}
			  }
			  obj = "";
			  window.clearTimeout(google_id);
			  google_id = window.setTimeout(creatPointSmall,5000);
			} 
		); 
	}else{
		google_ct++;	
	}
}
  
//创建坐标点
function creatPoint(){
	obj = google_datas[google_ct];

	if(typeof(obj)!='undefined'&&null!=obj.country&&""!=obj.country){
		var address = obj.country + ' ' + obj.city;
		geocoder.getLocations( 
			address,
			function(rep) { 
			  if (!rep || rep.Status.code != 200) {
				  google_ct++;
			  } else { 
			  place = rep.Placemark[0];
			  point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
			  	if(google_flag){
					google_ct2 = 0;
					google_markers[0].infoWindowInstance.remove();
					google_markers[0].remove();
					for(i=0;i<3;i++){
					    google_markers[i] = google_markers[i+1];		
					}
					google_markers[3] = {};
					google_markers[3] = createMarker( point, createHtmls(google_datas[google_ct]) );
				}else{
					google_markers[google_ct2] = createMarker( point, createHtmls(google_datas[google_ct]) );
					google_ct2++;
				}
				google_ct++;
				if(google_markers.length>4&&!google_flag)google_flag = true;
				if(google_ct>=google_datas.length){
					window.location.reload();
					/*for(i=0;i<google_markers.length;i++){
						google_markers[i].infoWindowInstance.remove();
						google_markers[i].remove();
						google_markers[i] = {};
					}
					google_markers = new Array();
					google_ct = 0;
					google_flag = false;*/
				}
			  }
				obj = "";
				window.clearTimeout(google_id);
				google_id = window.setTimeout(creatPoint,5000);
			} 
		); 
	}else{
		google_ct++;	
	}
}

//标签内容HTML初始化
function createHtmls(product){
	var content = "";
	content += "<div class='google_kuangmid_left'>";
        content += "<div class='google_kuangmid_left1'>";
		kidTemp = "?gmap="+product.kid;
		content +="<a href='"+product.filename+kidTemp+"' title='"+ product.itemName +"' alt='"+product.itemName+"' target='_blank'><img src='"+product.pic+"' border='0' onerror='ImageOnError(this);'/></a></div>";
		var time = product.time;
		if(time >= 24*60){
			if(time >= 7*24*60){
				if(time >= 30*24*60){
					content += "<div class='google_kuangmid_left2'>";
					content += parseInt(time/60/24/30);
					content += " month ago</div>";
				}else{
					content += "<div class='google_kuangmid_left2'>";
					content += parseInt(time/60/24/7);
					content += " weeks ago</div>";
				}
			}else{
				content += "<div class='google_kuangmid_left2'>";
				content += parseInt(time/60/24);
				content += " days ago</div>";
			}
		}else{
        	content += "<div class='google_kuangmid_left2'>"+ product.time +" minutes ago</div>";
		}
        content += "</div>";
        content += "<div class='google_kuangmid_right'>";
        content += "<div class='google_kuangmid_right1'>";
		var address = product.country + '<br />' + product.city;
		if(product.country.toLowerCase(product.country)=='united states')address = 'USA&nbsp;' + product.city;
        content += "<div class='google_kuangmid_right1_zi'>Someone in "+ address +"</div>";
        content += "<div class='google_kuangmid_right1_flag'><img src='/Templates/Site61/Dino/Images/flag/"+product.country+".gif'/></div>";
        content += "</div>";
        content += "<div class='google_kuangmid_right2'>";
        content += "<ul>";
        content += "<li>Bought</li>";
		var name = product.itemName;
		if(product.itemName.length>20){
			name = product.itemName.substring(0,48) + '...';
		}
        content += "<li class='zi'><a href='"+product.filename+kidTemp+"' title='"+ product.itemName +"' alt='"+product.itemName+"' target='_blank'>"+name+"</a></li>";
        content += "<li>with free shipping</li>";
        content += "</ul>";
        content += "</div>";
        content += "</div>";
	return content;
 }
 
 //绘制Tooltip标签
 function createMarker( pPoint, pHtml ) {
    var tinyIcon = new GIcon();
    tinyIcon.image = '/Templates/Site61/Dino/images/mm_20_red.png';
    tinyIcon.shadow = '/Templates/Site61/Dino/images/mm_20_shadow.png';
    tinyIcon.iconSize = new GSize( 12, 20 );
    tinyIcon.shadowSize = new GSize( 22, 20 );
    tinyIcon.iconAnchor = new GPoint( 6, 20 );
    tinyIcon.infoWindowAnchor = new GPoint( 4, 1 );
    mOptions = { icon: tinyIcon };
    var gMarker = new GMarker( pPoint, mOptions );
    fromLatLng = map.fromLatLngToDivPixel( pPoint );
    fromLatLng.x += 80;
    fromLatLng.y -= 85;
    
    newPoint = map.fromDivPixelToLatLng( fromLatLng );

    map.panTo( newPoint );
	
    map.addOverlay( gMarker );
    if ( gMarker.infoWindowInstance == null ) {
		gMarker.infoWindowInstance = new infoWindow( gMarker, pHtml );
		map.addOverlay( gMarker.infoWindowInstance );
    }
    return gMarker;
}

//重写Tooltip标签样式
function infoWindow( marker, html, width ) {
    this.html = html;
    this.width = '169px';
    this.marker = marker;
}
