//document.write('<sc'+'ript type="text/javascript" src="http://video.msn.com/js/ch/channels.js"></sc'+'ript>');


//document.write('<sc'+'ript src="http://research.microsoft.com/mapcruncher/scripts/v5.5/CrunchControl.js" type="text/javascript"></sc'+'ript>');

//document.write('<div style="display:none"><a id="CrunchedLayers" href="MapCruncherMetadata.xml">CrunchedLayers</a></div>');

document.write('<div id="aboutDiv" style=" display:none; background:white; width:400px; border: 2px solid #0000c0">');
document.write('<div style="padding:10px"><p> Welcome to your new Mashup -- powered by <a href="http://research.microsoft.com/mapcruncher/">MapCruncher</a> Beta for Microsoft <a href="http://local.live.com/">Virtual Earth</a>! This HTML file is a sample.  It demonstrates how to build a real web page that usesthe map layer you just rendered.To publish this mashup, just move the whole folder to your web server.<p><form action="javascript:searchboxactivate()"><b>Search the map for businesses (e.g., "pizza"):</b><input id="searchbox" type="text" name="searchbox" size="20"><input id="sbutton" type="button" value="Go!" onclick="javascript:searchboxactivate()"></form>');
document.write('<form action="None" name="layerCheckboxForm"><b>Select Mashup Layers:</b><input type="checkbox" id="checkbox:NewLayer" onClick="javascript:ToggleLayer(\'NewLayer\', this.checked);" checked>NewLayer&nbsp;<a href="javascript:crunchedLayerManager.layerList.find(\'NewLayer\').SetDefaultView(map);"><font size="-1">center here</font></a>&nbsp;|</form>');
document.write('</div>');
document.write('</div>');


var div = null;
var map = null;
var latLong = new VELatLong(47, -18, 0, "Default");
var layerid = 1;                  
var crunchedLayerManager = null;
var zoom_min = 1;
var zoom_max = 5;


function GetMap()         
{            
	map = new VEMap('Map');            
	map.LoadMap();
	map.SetCenterAndZoom(latLong, 3);
	map.HideDashboard();
	map.SetScaleBarDistanceUnit(VEDistanceUnit.Kilometers);
	map.SetMapStyle('a');
	map.AttachEvent("onmousewheel", wheelCallback);
	AddMyLayer(VEDataType.VECollection);
	
				
				crunchedLayerManager = new VE.MapCruncher.CrunchedLayerManager(map);
				//alert(map);
                crunchedLayerManager.ImportLayersFromAnchorHRef("CrunchedLayers");
 				
				crunchedLayerManager.controlManager.AddControl("aboutDiv", "About this sample page",new VE.MapCruncher.LayoutSpec("right", 10, "top", 12),true, false);
				
				var permalinkProvided = crunchedLayerManager.ApplyPermalink(document.layerCheckboxForm);
				
                if (!permalinkProvided) {
					
          var layer = crunchedLayerManager.layerList.find('NewLayer');
                    layer.Activate(map);
                    layer.SetDefaultView(map);
                }
				
	
	//traductionfr();
}

function wheelCallback(e)
{
	//returning true to cancel default action.
	
	//window.scrollBy(0,-5*e.mouseWheelChange);
	if(parseInt(map.GetZoomLevel()) > zoom_max-1)
	{
		map.SetZoomLevel(zoom_max);
	}
	
	//return true;
}

function DetectZoom()
{
	//alert(map.GetZoomLevel());
	alert("bla");
		//return false;
	if(parseInt(map.GetZoomLevel()) > zoom_max-1)
	{
		map.SetZoomLevel(zoom_max);
	}
}


function AddMyLayer(type)         
{            
	var l = new VEShapeLayer();            
	var veLayerSpec = new VEShapeSourceSpecification(type, "908BD946413DF3EA!268", l);            
	map.ImportShapeLayerData(veLayerSpec, onFeedLoad);         
}         

function onFeedLoad(feed)         
{            
	//alert('RSS or Collection loaded. There are '+feed.length+' items in this list.');  
	//alert(feed.GetShapeCount());
	//alert(map.GetShapeLayerByIndex(1).GetTitle());

	for(var s=0; s<feed.GetShapeCount(); s++)
	{
		var shape 		= feed.GetShapeByIndex(s);
		var shapeID 	= shape.GetID();
		//alert(shapeID);
		feed.GetShapeByIndex(s).SetCustomIcon("<img src='PictoIndyMap.png'/>");
		
			if(feed.GetShapeByIndex(s).GetDescription().toString().toLowerCase().indexOf("<a ") != -1)
			{
				var description = feed.GetShapeByIndex(s).GetDescription().toString().toLowerCase().split("<a ");
				//alert(description[1]);
				var reste = description[1].split("vid=");
				var guid = reste[1].split('"')[0];
				var lientxt = feed.GetShapeByIndex(s).GetDescription().toString().split(">")[1].split("<")[0];
				//alert("guid : "+guid);
				
				feed.GetShapeByIndex(s).SetDescription(feed.GetShapeByIndex(s).GetDescription().split("<")[0] + "<br /><div id='"+ guid +"' style=\"margin-top:3px; clear:both;\"><img id=pucevideo src=http://www.msn.fr/img/fr/fr-fr/msger/tabs/video/img/puce_noire.gif border=0 align=absmiddle /><a id=\"lien-"+guid+"\" onclick=\"afficheVideo('"+ guid +"');\" style=\"line-height:20px;\">"+ lientxt +"</a></div>");
			}
		
	}
	
	//alert(map.GetCenter())
}





//-------- Fonctions Map Cruncher
		function ToggleLayer(name, active)
		{
				if (active == true)
				{
						crunchedLayerManager.layerList.find(name).Activate(map);
				}
				else
				{
						crunchedLayerManager.layerList.find(name).Deactivate(map);
				}
		}

		function searchboxactivate()
		{
			txt = document.getElementById('searchbox').value;
			map.Find(txt, "");
		}        

        function GetSize()
        {
                var myWidth = 0, myHeight = 0;

                if( typeof( window.innerWidth ) == 'number' ) {
                    //Non-IE
                    myWidth = window.innerWidth;
                    myHeight = window.innerHeight;
                } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
                    //IE 6+ in 'standards compliant mode'
                    myWidth = document.documentElement.clientWidth;
                    myHeight = document.documentElement.clientHeight;
                } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
                    //IE 4 compatible
                    myWidth = document.body.clientWidth;
                    myHeight = document.body.clientHeight;
                }

                if (myWidth > 0)
                {
                        var result = new Array();
                        result[0] = myWidth;
                        result[1] = myHeight;
                        return result;
                }
                else
                {
                        return null;
                }
        }

        function SetMapSize()
        {
                var browserSize = GetSize();

                var div = document.getElementById("Map");

                /*
				if (browserSize != null)
                {
                        div.style.width = (browserSize[0] - 25) + "px";
                        div.style.height = (browserSize[1] - 25) + "px";

                }
                else
                {
                        div.style.width = "800px";
                        div.style.height = "600px";
                }
				*/
                div.style.overflow = "hidden";
                div.style.position = "relative";

				if (crunchedLayerManager!=null)
				{
					crunchedLayerManager.controlManager.UpdateControlLayout();
				}
        }

        function MapModeChanged(e)
        {
                if (map.GetMapMode() == Msn.VE.MapActionMode.Mode3D &&
                    window.location.protocol == "file:")
                {
                        alert("MapCruncher tiles will only display in 3D mode " +
                        "if your map is hosted on a web server (using http://).\n" +
                        "Currently, your tiles are coming from a local machine (file://) " +
                        "so only 2D mode is available.");
                }
        }

        function DetectZoom(zoomLevel)
		{
			//alert(map.GetZoomLevel());
				//return false;
			if(parseInt(map.GetZoomLevel())==7)
			{
				map.SetZoomLevel(map.GetZoomLevel()-1);
			}
		}
		function LoadPage()
        {
                SetMapSize();
                window.onresize = SetMapSize;

                map = new VEMap('Map');
                map.LoadMap();
                map.AttachEvent("oninitmode", MapModeChanged);
                map.SetMapStyle('a');
				map.SetZoomLevel(3);
				map.AttachEvent("onmousewheel", DetectZoom);
				

				
				crunchedLayerManager = new VE.MapCruncher.CrunchedLayerManager(map);
				crunchedLayerManager.controlManager.AddControl(
						"aboutDiv", "About this sample page",
						new VE.MapCruncher.LayoutSpec("right", 10, "top", 12),
						true, false);
                crunchedLayerManager.ImportLayersFromAnchorHRef("CrunchedLayers");
                var permalinkProvided = crunchedLayerManager.ApplyPermalink(document.layerCheckboxForm);

                if (!permalinkProvided) {
          var layer = crunchedLayerManager.layerList.find('NewLayer');
                    layer.Activate(map);
                    layer.SetDefaultView(map);
                }
        }


// Players
function afficheVideo(guid)
{
	var idPlayer = guid;
	
	Msn.Video.Build(idPlayer,"Installez la derniere version d\'Adobe Flash Player (gratuite).Pour obtenir de l\'aide sur Flash, consultez la page de support d\'Adobe Flash Player.",{skin:"0",mkt:"fr-fr",mk:"fr-fr",fr:"inline-divertissements-specials",fg:"SpecialVengeancedanslapeauDVD-carte",ad:"false",ap:"true",partnerlogo:"false",timePlaying:"180",c:"v",v:idPlayer,p:"IV2_fr-fr_VengeancedanslapeauDVD-carte",rv:"false",t:"c1788"},300);
	
	document.getElementById(idPlayer+"_t").style.display="none";
	document.getElementById(idPlayer+"_m").style.display="none";
	document.getElementById(idPlayer+"_a").style.display="none";
	
}



window.onload = GetMap;