var objetXHR;
var video;
//--------------------------------------------------------------------------------------------
function createXHR() 
{
    var request = false;
    
        try
        {
            request = new ActiveXObject('Msxml2.XMLHTTP');
        }
        catch (err2)
        {
            try
            {
                request = new ActiveXObject('Microsoft.XMLHTTP');
            }
            catch (err3)
            {
				try
				{
					request = new XMLHttpRequest();
				}
				catch (err1) 
				{
					request = false;
				}
            }
        }
    
    return request;
}
//--------------------------------------------------------------------------------------------
function chargementCadrePrincipal(sto_id)
{
	
	objetXHR = createXHR();
	objetXHR.onreadystatechange  = traitement;
	objetXHR.open("GET", "ajax/getVideobyStoryId.php?sto_id=" + sto_id ,  true); 
	objetXHR.send(null); 
	document.getElementById("StoId").value = sto_id;
	//showBoutList(document.getElementById("offset").value ); 
}
//--------------------------------------------------------------------------------------------
function traitement()
{

	if(objetXHR.readyState  == 4)
	{
		if(objetXHR.status  == 200) 
		{	
			
			var responseText = objetXHR.responseText;
			
			tabVideo = responseText.split(new RegExp("[|]+", "g"));
			//tabVideo = responseText.split(new RegExp("|", "g"));
			

 	 var so = new SWFObject('player/player.swf','mpl','480','300','9');
 	 so.addParam('allowscriptaccess','always');
 	 so.addParam('allowfullscreen','true');
 	 so.addParam("flashvars","&file=../img/" + tabVideo[2] + "&frontcolor=009900&lightcolor=FFFFFF&bufferlength=7&autostart=true");
 	 so.write('player');               
  
    //var linkYouTube =   tabVideo[2] ;               
            var linkIndexMore = "<a href='index_more.php?vdo_id=" + tabVideo[0] + "'><img name='button2_v_off' src='images/btn4-map_on.png' width='110' height='29' border='0' id='button2_v' alt='' /><img src='images/btn6_product_off.gif' width='92' border='0' height='29' /></a>";         
            //document.getElementById("StoryTitle").innerHTML = geocoder.getLatLng("Taipei"); 
            document.getElementById("StoryTitle").innerHTML = tabVideo[1]; 
				//document.getElementById("objetYouTube").innerHTML = '<font color="#FFFFFF">'+tabVideo[1]+'</font>'; 
			document.getElementById("StoryText").innerHTML  = tabVideo[4]; 
			document.getElementById("StoryCamera").innerHTML  = tabVideo[5]; 
			document.getElementById("StoryWriter").innerHTML  = tabVideo[6];
			document.getElementById("extra").innerHTML  = tabVideo[7]; 
			//document.getElementById("lookmap").innerHTML = '<img name="button2_v_off" src="images/btn4-map_on.png" width="110" height="29" border="0" id="button2_v" alt="" />'; 
			//document.getElementById("lookboutique").innerHTML = '<a href="#"><img src="images/btn6_product_off.gif" width="92" border="0" height="29" onClick = "chargementCadreBoutique(\''+ tabVideo[0] +'\');" /></a>'; 
		document.getElementById("cadreboutique").innerHTML = '';
			//document.getElementById("cadreboutique").style.visibility = 'hidden';	
			//document.getElementById("map").style.visibility = 'visible';	
			document.getElementById("cadreboutique").style.visibility = 'visible';	
			document.getElementById("map").style.visibility = 'hidden';	
			chargementCadreBoutique(tabVideo[0]);
      /*
		var map = null;
       var geocoder = null;
    
	
       function load(address) {
         if (GBrowserIsCompatible()) {
			 

           map = new GMap2(document.getElementById("map"));
		  // map.addControl(new TextualZoomControl());
           
       
		  // map.setCenter(new GLatLng(25.04, 121.51), 13);
           map.addControl(new GSmallMapControl());
		    
           //map.addControl(new GMapTypeControl());
           geocoder = new GClientGeocoder();
		   if (geocoder) {
           geocoder.getLatLng(
             address,
             function(point) {
               if (!point) {
                 alert(address + " not found");
               } else {
                 map.setCenter(point, 13);
				
				 //---------------Set up our GMarkerOptions object			 
				// var blueIcon = new GIcon(G_DEFAULT_ICON);
				//blueIcon.image = "ajax/green-dot.png";
				//blueIcon.iconSize = new GSize(20, 20);
				//markerOptions = { icon:blueIcon };
				//----------
                 var marker = new GMarker(point);
				
                 map.addOverlay(marker);
				 
				 //---
				 //map.addOverlay(new GMarker(point, markerOptions));
                 //---
				 //marker.openInfoWindow(address);
               }
             }
           );
         }
         }
       }
	    address = tabVideo[3];
		load(address);
		*/	
			
		}	
		else 
			window.alert("Error code " + objetXHR.status);
	}
         
}
//--------------------------------------------------------------------------------------------
function chargementCadreBoutique(sto_id)
{
	objetXHR = createXHR();
	objetXHR.onreadystatechange  = traitementBoutique;
	objetXHR.open("GET", "ajax/getBoutiquebyStoryId.php?sto_id=" + sto_id ,  true); 
	objetXHR.send(null); 
}
//--------------------------------------------------------------------------------------------
function traitementBoutique()
{

	if(objetXHR.readyState  == 4)
	{
		if(objetXHR.status  == 200) 
		{	
			var responseText = objetXHR.responseText;
			
			tabBout = responseText.split(new RegExp("[|]+", "g"));
			// window.alert('tabBout = ' + tabBout);
			document.getElementById("map").style.visibility = 'hidden';
			document.getElementById("cadreboutique").style.visibility = 'visible';
			function MakeBout(a,b,c,d){
			var bout = '<br><table width="370" border="0" cellspacing="0" cellpadding="0"> <tr><td width="220" align="center"><table border="1" cellpadding="3" cellspacing="0" bordercolor="#FFFFFF"><tr><td><a href="view_things.php?bout_id='+  a +'"><img src="img/'+  d +'" width="150" border="0"  /></a></td> </tr></table><br /></td><td width="221" valign="top" class="style8"><strong><a href="view_things.php?bout_id='+  a +'">'+  b +'</a></strong><br /><br />'+  c +'</td></tr></table>';
			return(bout)
			}
			//window.alert("Error code " + tabBout[0]);
			var bout = '';
			if (tabBout[0] >= 1 ) bout = MakeBout(tabBout[2],tabBout[3],tabBout[4],tabBout[5]) + bout ;
			if (tabBout[0] >= 2 ) bout = MakeBout(tabBout[6],tabBout[7],tabBout[8],tabBout[9]) + bout ;
			if (tabBout[0] >= 3 ) bout = MakeBout(tabBout[10],tabBout[11],tabBout[12],tabBout[13]) + bout ;
			document.getElementById("cadreboutique").innerHTML = bout;
			
			//document.getElementById("cadreboutique").innerHTML = tabProduct[4]; 
			//document.getElementById("cadreboutique").visibility  ='show';
			document.getElementById("lookmap").innerHTML = '<a href="#"><img name="button2_v_off" src="images/btn4-map_on.png" width="110" height="29" border="0" id="button2_v" alt=""  onClick = "chargementMap(\''+ tabBout[1] +'\');" /></a>'; 
			document.getElementById("lookboutique").innerHTML = '<img src="images/btn6_product_off.gif" width="92" border="0" height="29" />'; 
  	
		}	
		else 
			window.alert("Error code " + objetXHR.status);
	}
         
}
//--------------------------------------------------------------------------------------------
function chargementMap(sto_id)
{
	objetXHR = createXHR();
	objetXHR.onreadystatechange  = traitementMap;
	objetXHR.open("GET", "ajax/getVideobyStoryId.php?sto_id=" + sto_id ,  true); 
	objetXHR.send(null); 
}
//--------------------------------------------------------------------------------------------
function traitementMap()
{

	if(objetXHR.readyState  == 4)
	{
		if(objetXHR.status  == 200) 
		{	
			
			var responseText = objetXHR.responseText;
			tabVideo = responseText.split(new RegExp("[|]+", "g"));
			if(tabVideo[2] == '123') tabVideo[2] = '';
			//document.getElementById("cadreboutique").innerHTML = '';
			document.getElementById("lookmap").innerHTML = '<img name="button2_v_off" src="images/btn4-map_on.png" width="110" height="29" border="0" id="button2_v" alt="" />'; 
			document.getElementById("lookboutique").innerHTML = '<a href="#"><img src="images/btn6_product_off.gif" width="92" border="0" height="29" onClick = "chargementCadreBoutique(\''+ tabVideo[0] +'\');" /></a>'; 
			document.getElementById("cadreboutique").innerHTML = '';
			document.getElementById("cadreboutique").style.visibility = 'hidden';	
			document.getElementById("map").style.visibility = 'visible';	

		//-------------------------------
	  //-------------------------------
	 
		//var lat = document.getElementById("boutLat").value;
			var lat = tabVideo[8]; 
		 var lng = tabVideo[9]; 
		/*
		var lat = 24.1507; 
		 var lng = 120.647; 
		 */
		//var lng = document.getElementById("boutLng").value;
	  var map = null;
	 	map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallZoomControl());
		if (lat != 0) 
			pointCenter =new GLatLng(lat,lng);
		else
			pointCenter =new GLatLng(25.04, 121.51);
		map.setCenter(pointCenter, 13);
		//----------------------			
	var orange = 'F09800';
	var myIconOrange = MapIconMaker.createMarkerIcon({width: 36, height: 32, primaryColor: 'F09800'});
	var markerOptionsOrange = {
	icon: myIconOrange
	};
	//----------------------	
		var marker = new GMarker(pointCenter, myIconOrange);
	  	
			map.addOverlay(marker);
	  //-------------------------------
	  //-------------------------------
		/*
		var map = null;
       var geocoder = null;
       function load(address) {
         if (GBrowserIsCompatible()) {
           map = new GMap2(document.getElementById("map"));
		  // map.addControl(new TextualZoomControl());
		  // map.setCenter(new GLatLng(25.04, 121.51), 13);
           map.addControl(new GSmallMapControl());
           //map.addControl(new GMapTypeControl());
           geocoder = new GClientGeocoder();
		   if (geocoder) {
           geocoder.getLatLng(
             address,
             function(point) {
               if (!point) {
                 alert(address + " not found");
               } else {
                 map.setCenter(point, 13);
				
				 //---------------Set up our GMarkerOptions object			 
				// var blueIcon = new GIcon(G_DEFAULT_ICON);
				//blueIcon.image = "ajax/green-dot.png";
				//blueIcon.iconSize = new GSize(20, 20);
				//markerOptions = { icon:blueIcon };
				//----------
                 var marker = new GMarker(point);
				
                 map.addOverlay(marker);
				 //---
				 //map.addOverlay(new GMarker(point, markerOptions));
                 //---
				 //marker.openInfoWindowHtml(address);
               }
             }
           );
         }
         }
       }
		    address = tabVideo[3];
			load(address);
	  */
	  //-------------------------------
	  //-------------------------------
	  
		}	
		else 
			window.alert("Error code " + objetXHR.status);
	}
         
}
//--------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------
//-------------------------
function loadBout(id){
		//window.alert(id);

		if ((id == 1 ) && (document.getElementById("miniBoutLink1")!= ''))
			chargementCadrePrincipal(document.getElementById("miniBoutLink1").value);
		if ((id == 2 ) && (document.getElementById("miniBoutLink2")!= ''))
			chargementCadrePrincipal(document.getElementById("miniBoutLink2").value);
		if ((id == 3 ) && (document.getElementById("miniBoutLink3")!= ''))
			chargementCadrePrincipal(document.getElementById("miniBoutLink3").value);
		if ((id == 4 ) && (document.getElementById("miniBoutLink4")!= ''))
			chargementCadrePrincipal(document.getElementById("miniBoutLink4").value);
	}
//-------------------------

//-------------------------
//-------------------------
   function next() {
	  document.getElementById("offset").value ++;
	  showBoutList(document.getElementById("offset").value ); 
   }
//-------------------------
//-------------------------
   function prev() {
	 if  (document.getElementById("offset").value  > 0){
	 document.getElementById("offset").value --;
	  showBoutList(document.getElementById("offset").value ); 
	 }
	  
   }
//-------------------------
//-------------------------
   
   function showBoutList(offset) {
	//fill the data of Boutique
	 GDownloadUrl("include/ajaxGetStoListByOffset.php?offset="+ offset, function(data) {
          var xml = GXml.parse(data);
		  document.getElementById("miniBoutImg1").src = 'images/spacer.gif';
		  document.getElementById("miniBoutImg2").src = 'images/spacer.gif';
		  document.getElementById("miniBoutImg3").src = 'images/spacer.gif';
		  document.getElementById("miniBoutImg4").src = 'images/spacer.gif';


		  document.getElementById("miniBoutLink1").value='';
		  document.getElementById("miniBoutLink2").value='';
		  document.getElementById("miniBoutLink3").value='';
		  document.getElementById("miniBoutLink4").value='';

		  document.getElementById("Layer1").innerHTML=  '';
		  document.getElementById("Layer2").innerHTML=  '';
		  document.getElementById("Layer3").innerHTML=  '';
		  document.getElementById("Layer4").innerHTML=  '';

          var markers = xml.documentElement.getElementsByTagName("boutique");
		  
			  document.getElementById("miniBoutImg1").src= 'img/' + markers[0].getAttribute("pic2");
			  document.getElementById("miniBoutLink1").value=  markers[0].getAttribute("id");
			  document.getElementById("Layer1").innerHTML=  markers[0].getAttribute("name");
			  
			  document.getElementById("miniBoutImg2").src= 'img/' + markers[1].getAttribute("pic2");
			  document.getElementById("miniBoutLink2").value=  markers[1].getAttribute("id");
			  document.getElementById("Layer2").innerHTML=  markers[1].getAttribute("name");
			  
			  document.getElementById("miniBoutImg3").src= 'img/' + markers[2].getAttribute("pic2");
			  document.getElementById("miniBoutLink3").value=  markers[2].getAttribute("id");
			  document.getElementById("Layer3").innerHTML=  markers[2].getAttribute("name");

			  document.getElementById("miniBoutImg4").src= 'img/' + markers[3].getAttribute("pic2");
			  document.getElementById("miniBoutLink4").value= markers[3].getAttribute("id");
  			  document.getElementById("Layer4").innerHTML=  markers[3].getAttribute("name");


			 

        }); // GDownload
   	
    } // fct
//-------------------------
