/*******************************************************************************
 * Description:
 * Error handler. Used for debugging only.
 ******************************************************************************/
function handleErr(msg,url,l)
{
   var txt=""
   txt="There was an error on this page.\n\n"
   txt+="Error: " + msg + "\n"
   txt+="URL: " + url + "\n"
   txt+="Line: " + l + "\n\n"
   txt+="Click OK to continue.\n\n"
   alert(txt)
   return true
} /* END HandleErr */


/*******************************************************************************
 * Description:
 * This function creates the AJAX's XMLHttpRequest object.
 ******************************************************************************/
function GetXmlHttpObject()
{
   var xmlHttp=null;
   try
   {
      // Firefox, Opera 8.0+, Safari
      xmlHttp=new XMLHttpRequest();
   }
   catch (e)
   {
      // Internet Explorer
      try
      {
         xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch (e)
      {
         xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
   }
   return xmlHttp;
} /* END GetXmlHttpObject */

/*******************************************************************************
 * Description:
 * This function requests an update of all the gallery elements.
 * It must be called to select a new gallery.
 *
 * Parameters:
 * gal: 
 *   the gallery to display
 * lang:
 *   the language
 ******************************************************************************/
function SelectGal( gal, lang )
{
   xmlHttp=GetXmlHttpObject();
   if (xmlHttp==null)
   {
      alert ("Your browser does not support AJAX! Please update your browser.");
      return;
   }
   var url="../../shared/assets/scripts/selectgal.php";
   url=url+"?gal="+gal+"&lang="+lang+"&sid="+Math.random();
   xmlHttp.onreadystatechange=UpdatePageElements;
   xmlHttp.open("GET",url,true);
   xmlHttp.send(null);
} /* END SelectGal */

/*******************************************************************************
 * Description:
 * This function requests an update of all the page specific elements within
 * a specified gallery .
 * It must be called to select a new page in a given gallery.
 *
 * Parameters:
 * gal: 
 *   the gallery the new page belongs to
 * page:
 *   the page within the gallery to be displayed
 * pictId:
 *   the index of the picture to be displayed withing the specified page
 * lang:
 *   the language
 ******************************************************************************/
function SelectPage( gal, page, pictIdx, lang )
{
   xmlHttp=GetXmlHttpObject();
   if (xmlHttp==null)
   {
      alert ("Your browser does not support AJAX! Please update your browser.");
      return;
   } 
   var url="../../shared/assets/scripts/selectpage.php";
   url=url+"?gal="+gal+"&page="+page+"&pictIdx="+pictIdx+"&lang="+lang+"&sid="+Math.random();
   xmlHttp.onreadystatechange=UpdatePageElements;
   xmlHttp.open("GET",url,true);
   xmlHttp.send(null);
} /* END SelectPage */

 
/*******************************************************************************
 * Description:
 * This function requests an update of all the picture specific elements.
 * It must be called to select a new picture in a gallery page.
 *
 * Parameters:
 * gal: 
 *   the gallery the picture belongs to 
 * pictId:
 *   the unique ID of the picture
 * lang:
 *   the language 
 ******************************************************************************/
function SelectPict( gal, pictId, lang )
{
   xmlHttp=GetXmlHttpObject();
   if (xmlHttp==null)
   {
      alert ("Your browser does not support AJAX! Please update your browser.");
      return;
   } 
   var url="../../shared/assets/scripts/selectpict.php";
   url=url+"?gal="+gal+"&pictId="+pictId+"&lang="+lang+"&sid="+Math.random();
   xmlHttp.onreadystatechange=UpdatePageElements;
   xmlHttp.open("GET",url,true);
   xmlHttp.send(null);
} /* END SelectPict */

/*******************************************************************************
 * Description:
 * This function updates all the elements of the gallery page.
 * It waits for the xml page which was generated using AJAX to be ready, and 
 * when it is, it performs the update.
 * Each part of the page can be selectively updated by the xml generator 
 * simply by setting or not the following three xml elements:
 *  - <update_pict_elements> : to update picture specific elements
 *  - <update_page_elements> : to update gallery page specific elements
 *  - <update_page_elements> : to update gallery specific elements
 ******************************************************************************/
function UpdatePageElements()
{
   if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
   { 
      xmlDoc=xmlHttp.responseXML;
      /* Deal with the picture specific elements first as we want to start
       * uploading the picture's main image first */
      if( 0 != xmlDoc.getElementsByTagName("update_pict_elements")[0].childNodes[0].nodeValue )
      {
         /* update the pictures source as soon as possible starting with the 
          * main picture */
         var pictId = xmlDoc.getElementsByTagName("pict_id")[0].childNodes[0].nodeValue;
         document.getElementById("pict_image").src = "../../shared/assets/images/"+pictId+"m.jpg";
/*         document.getElementById("pict_image").src = eval(pictId+".src");*/
         var languagePict = xmlDoc.getElementsByTagName("lang_pict")[0].childNodes[0].nodeValue;
         document.getElementById("pict_title").innerHTML = xmlDoc.getElementsByTagName("pict_title")[0].childNodes[0].nodeValue;
         document.getElementById("pict_location").innerHTML = xmlDoc.getElementsByTagName("pict_location")[0].childNodes[0].nodeValue;
         document.getElementById("pict_map").src = xmlDoc.getElementsByTagName("pict_map_src")[0].childNodes[0].nodeValue;

         /* Get on with the rest */
         
         /* Picture selection arrows */
         document.getElementById("next_pict_sel").style.cursor = xmlDoc.getElementsByTagName("next_pict_sel_cursor")[0].childNodes[0].nodeValue;
         if(!(document.getElementById("next_pict_sel").src.match("../assets/graphics/pict_sel_right_sel.png") &&
            xmlDoc.getElementsByTagName("next_pict_sel_src")[0].childNodes[0].nodeValue.match("../assets/graphics/pict_sel_right.png") ))
         {         
            document.getElementById("next_pict_sel").src = xmlDoc.getElementsByTagName("next_pict_sel_src")[0].childNodes[0].nodeValue;
         }
         document.getElementById("next_pict_sel").onmouseover = new Function(eval(xmlDoc.getElementsByTagName("next_pict_sel_mouseover")[0].childNodes[0].nodeValue));
         document.getElementById("next_pict_sel").onmouseout = new Function(eval(xmlDoc.getElementsByTagName("next_pict_sel_mouseout")[0].childNodes[0].nodeValue));
         document.getElementById("prev_pict_sel").onclick = new Function(eval(xmlDoc.getElementsByTagName("prev_pict_sel_onclick")[0].childNodes[0].nodeValue));
         
         document.getElementById("prev_pict_sel").style.cursor = xmlDoc.getElementsByTagName("prev_pict_sel_cursor")[0].childNodes[0].nodeValue;
         if(!(document.getElementById("prev_pict_sel").src.match("../assets/graphics/pict_sel_left_sel.png") &&
            xmlDoc.getElementsByTagName("prev_pict_sel_src")[0].childNodes[0].nodeValue.match("../assets/graphics/pict_sel_left.png") ))
         {         
            document.getElementById("prev_pict_sel").src = xmlDoc.getElementsByTagName("prev_pict_sel_src")[0].childNodes[0].nodeValue;
         }
         document.getElementById("prev_pict_sel").onmouseover = new Function(eval(xmlDoc.getElementsByTagName("prev_pict_sel_mouseover")[0].childNodes[0].nodeValue));
         document.getElementById("prev_pict_sel").onmouseout = new Function(eval(xmlDoc.getElementsByTagName("prev_pict_sel_mouseout")[0].childNodes[0].nodeValue));
         document.getElementById("next_pict_sel").onclick = new Function(eval(xmlDoc.getElementsByTagName("next_pict_sel_onclick")[0].childNodes[0].nodeValue));

         /* Picture information */
         document.getElementById("pict_desc").innerHTML = xmlDoc.getElementsByTagName("pict_desc")[0].childNodes[0].nodeValue;
         document.getElementById("pict_camera").innerHTML = xmlDoc.getElementsByTagName("pict_camera")[0].childNodes[0].nodeValue;
         document.getElementById("pict_lens").innerHTML = xmlDoc.getElementsByTagName("pict_lens")[0].childNodes[0].nodeValue;
         document.getElementById("pict_iso").innerHTML = xmlDoc.getElementsByTagName("pict_iso")[0].childNodes[0].nodeValue;
         document.getElementById("pict_speed").innerHTML = xmlDoc.getElementsByTagName("pict_speed")[0].childNodes[0].nodeValue;
         document.getElementById("pict_apperture").innerHTML = xmlDoc.getElementsByTagName("pict_apperture")[0].childNodes[0].nodeValue;
         document.getElementById("pict_filters").innerHTML = xmlDoc.getElementsByTagName("pict_filters")[0].childNodes[0].nodeValue;
         var galleryPict  = xmlDoc.getElementsByTagName("gallery_pict")[0].childNodes[0].nodeValue;
         if( null != galleryPict.match("panoramics") )
         {
            document.getElementById("pict_pano").innerHTML = xmlDoc.getElementsByTagName("pict_pano")[0].childNodes[0].nodeValue;
         }
         document.getElementById("pict_holding").innerHTML = xmlDoc.getElementsByTagName("pict_holding")[0].childNodes[0].nodeValue;
         
         var pictFormat = xmlDoc.getElementsByTagName("pict_format")[0].childNodes[0].nodeValue;
         var orderPrintLnk = "../../" + languagePict + "/services/prints.php?" + pictId + "?" + pictFormat + "?" + galleryPict + "#order";
         document.getElementById("a_order_print_lnk1").href = orderPrintLnk;
         document.getElementById("a_order_print_lnk2").href = orderPrintLnk;
         document.getElementById("pict_ref").innerHTML = pictId;

         galFile= xmlDoc.getElementsByTagName("gal_file")[0].childNodes[0].nodeValue
         document.getElementById("a_en_version").href = galFile+"?gal="+galleryPict+"&pictId="+pictId+"&lang=en";
         document.getElementById("a_fr_version").href = galFile+"?gal="+galleryPict+"&pictId="+pictId+"&lang=fr";
      }

      /* Next deal with the page specific elements as it contains the next big
       * image to upload, i.e. the pictures selection bar */
      if( 0 != xmlDoc.getElementsByTagName("update_page_elements")[0].childNodes[0].nodeValue )
      {
         var gallery           = xmlDoc.getElementsByTagName("gallery")[0].childNodes[0].nodeValue;
         var language          = xmlDoc.getElementsByTagName("lang")[0].childNodes[0].nodeValue;
         /* Update the 15 page selection images details */
         for(pg=1;pg<=15;pg++)
         {
            document.getElementById("page_sel_"+pg).onclick = new Function(eval(xmlDoc.getElementsByTagName("page_"+pg+"_onclick")[0].childNodes[0].nodeValue));
            document.getElementById("page_sel_"+pg).onmouseout = new Function(eval(xmlDoc.getElementsByTagName("page_"+pg+"_mouseout")[0].childNodes[0].nodeValue));
            document.getElementById("page_sel_"+pg).src = xmlDoc.getElementsByTagName("page_"+pg+"_src")[0].childNodes[0].nodeValue;
            document.getElementById("page_sel_"+pg).style.visibility = xmlDoc.getElementsByTagName("page_"+pg+"_visibility")[0].childNodes[0].nodeValue;
            document.getElementById("page_sel_"+pg).style.paddingLeft = xmlDoc.getElementsByTagName("page_"+pg+"_paddingleft")[0].childNodes[0].nodeValue;
            document.getElementById("page_sel_"+pg).style.paddingRight = xmlDoc.getElementsByTagName("page_"+pg+"_paddingright")[0].childNodes[0].nodeValue;
            document.getElementById("page_sel_"+pg).width = xmlDoc.getElementsByTagName("page_"+pg+"_width")[0].childNodes[0].nodeValue;
         }

         document.getElementById("prev_page_sel").onclick = new Function(eval(xmlDoc.getElementsByTagName("prev_page_sel_onclick")[0].childNodes[0].nodeValue));
         document.getElementById("next_page_sel").onclick = new Function(eval(xmlDoc.getElementsByTagName("next_page_sel_onclick")[0].childNodes[0].nodeValue));

         document.getElementById("prev_page_sel").style.cursor = xmlDoc.getElementsByTagName("prev_page_sel_cursor")[0].childNodes[0].nodeValue;
         if(!(document.getElementById("prev_page_sel").src.match("../assets/graphics/page_sel_prev_sel.png") &&
            xmlDoc.getElementsByTagName("prev_page_sel_src")[0].childNodes[0].nodeValue.match("../assets/graphics/page_sel_prev.png") ))
         {         
            document.getElementById("prev_page_sel").src          = xmlDoc.getElementsByTagName("prev_page_sel_src")[0].childNodes[0].nodeValue;
         }
         document.getElementById("prev_page_sel").onmouseover  = new Function(eval(xmlDoc.getElementsByTagName("prev_page_sel_mouseover")[0].childNodes[0].nodeValue));
         document.getElementById("prev_page_sel").onmouseout   = new Function(eval(xmlDoc.getElementsByTagName("prev_page_sel_mouseout")[0].childNodes[0].nodeValue));
         document.getElementById("next_page_sel").style.cursor = xmlDoc.getElementsByTagName("next_page_sel_cursor")[0].childNodes[0].nodeValue;
         if(!(document.getElementById("next_page_sel").src.match("../assets/graphics/page_sel_next_sel.png") &&
            xmlDoc.getElementsByTagName("next_page_sel_src")[0].childNodes[0].nodeValue.match("../assets/graphics/page_sel_next.png") ))
         {         
            document.getElementById("next_page_sel").src          = xmlDoc.getElementsByTagName("next_page_sel_src")[0].childNodes[0].nodeValue;
         }
         document.getElementById("next_page_sel").onmouseover  = new Function(eval(xmlDoc.getElementsByTagName("next_page_sel_mouseover")[0].childNodes[0].nodeValue));
         document.getElementById("next_page_sel").onmouseout   = new Function(eval(xmlDoc.getElementsByTagName("next_page_sel_mouseout")[0].childNodes[0].nodeValue));

         for(idx=1;idx<=13;idx++)
         {
            if(0 != xmlDoc.getElementsByTagName("pict_id_"+idx)[0].childNodes[0].nodeValue)
            {
               document.getElementById("pict_sel_"+idx).onclick = new Function("SelectPict('"+gallery+"','"+xmlDoc.getElementsByTagName("pict_id_"+idx)[0].childNodes[0].nodeValue+"','"+language+"');");
               /*document.getElementById("pict_sel_"+idx).src = "../assets/images/"+xmlDoc.getElementsByTagName("pict_id_"+idx)[0].childNodes[0].nodeValue+"v.jpg";*/
               document.getElementById("pict_sel_"+idx).src = eval(xmlDoc.getElementsByTagName("pict_id_"+idx)[0].childNodes[0].nodeValue+"v.src");
               document.getElementById("pict_sel_"+idx).style.display="inline";
            }
            else
            {
               document.getElementById("pict_sel_"+idx).onclick = "SelectPict()";
               document.getElementById("pict_sel_"+idx).src = "";
               document.getElementById("pict_sel_"+idx).style.display="none";
            }
            document.getElementById("pict_sel_"+idx).style.marginLeft=xmlDoc.getElementsByTagName("pict_margleft")[0].childNodes[0].nodeValue;
         }
      }

      /* Finally deal with the gallery specific elements */
      if( 0 != xmlDoc.getElementsByTagName("update_gallery_elements")[0].childNodes[0].nodeValue )
      {
         document.title = xmlDoc.getElementsByTagName("page_title")[0].childNodes[0].nodeValue;

         document.getElementById("submenu_panoramics").src     = xmlDoc.getElementsByTagName("submenu_panoramics")[0].childNodes[0].nodeValue;
         document.getElementById("submenu_panoramics").onmouseout  = new Function(eval(xmlDoc.getElementsByTagName("submenu_panoramics_mouseout")[0].childNodes[0].nodeValue));
         document.getElementById("panoramas").onmouseout  = new Function(eval(xmlDoc.getElementsByTagName("submenu_panoramics_mouseout")[0].childNodes[0].nodeValue));

         document.getElementById("submenu_latest_pano").src    = xmlDoc.getElementsByTagName("submenu_latest_pano")[0].childNodes[0].nodeValue;
         document.getElementById("submenu_latest_pano").onmouseout  = new Function(eval(xmlDoc.getElementsByTagName("submenu_latest_pano_mouseout")[0].childNodes[0].nodeValue));
         document.getElementById("submenu_pano_ch_vd").src        = xmlDoc.getElementsByTagName("submenu_pano_ch_vd")[0].childNodes[0].nodeValue;
         document.getElementById("submenu_pano_ch_vd").onmouseout  = new Function(eval(xmlDoc.getElementsByTagName("submenu_pano_ch_vd_mouseout")[0].childNodes[0].nodeValue));
         document.getElementById("submenu_pano_ch_vs").src        = xmlDoc.getElementsByTagName("submenu_pano_ch_vs")[0].childNodes[0].nodeValue;
         document.getElementById("submenu_pano_ch_vs").onmouseout  = new Function(eval(xmlDoc.getElementsByTagName("submenu_pano_ch_vs_mouseout")[0].childNodes[0].nodeValue));
         document.getElementById("submenu_pano_ch_others").src        = xmlDoc.getElementsByTagName("submenu_pano_ch_others")[0].childNodes[0].nodeValue;
         document.getElementById("submenu_pano_ch_others").onmouseout  = new Function(eval(xmlDoc.getElementsByTagName("submenu_pano_ch_others_mouseout")[0].childNodes[0].nodeValue));
         document.getElementById("submenu_pano_eur").src       = xmlDoc.getElementsByTagName("submenu_pano_eur")[0].childNodes[0].nodeValue;
         document.getElementById("submenu_pano_eur").onmouseout  = new Function(eval(xmlDoc.getElementsByTagName("submenu_pano_eur_mouseout")[0].childNodes[0].nodeValue));
         document.getElementById("submenu_pano_usa").src       = xmlDoc.getElementsByTagName("submenu_pano_usa")[0].childNodes[0].nodeValue;
         document.getElementById("submenu_pano_usa").onmouseout  = new Function(eval(xmlDoc.getElementsByTagName("submenu_pano_usa_mouseout")[0].childNodes[0].nodeValue));



         document.getElementById("submenu_others").src         = xmlDoc.getElementsByTagName("submenu_others")[0].childNodes[0].nodeValue;
         document.getElementById("submenu_others").onmouseout  = new Function(eval(xmlDoc.getElementsByTagName("submenu_others_mouseout")[0].childNodes[0].nodeValue));
         document.getElementById("others").onmouseout  = new Function(eval(xmlDoc.getElementsByTagName("submenu_others_mouseout")[0].childNodes[0].nodeValue));
         document.getElementById("submenu_latest_others").src  = xmlDoc.getElementsByTagName("submenu_latest_others")[0].childNodes[0].nodeValue;
         document.getElementById("submenu_latest_others").onmouseout  = new Function(eval(xmlDoc.getElementsByTagName("submenu_latest_others_mouseout")[0].childNodes[0].nodeValue));

         document.getElementById("submenu_std_ch_vd").src      = xmlDoc.getElementsByTagName("submenu_std_ch_vd")[0].childNodes[0].nodeValue;
         document.getElementById("submenu_std_ch_vd").onmouseout  = new Function(eval(xmlDoc.getElementsByTagName("submenu_std_ch_vd_mouseout")[0].childNodes[0].nodeValue));
         document.getElementById("submenu_std_ch_vs").src      = xmlDoc.getElementsByTagName("submenu_std_ch_vs")[0].childNodes[0].nodeValue;
         document.getElementById("submenu_std_ch_vs").onmouseout  = new Function(eval(xmlDoc.getElementsByTagName("submenu_std_ch_vs_mouseout")[0].childNodes[0].nodeValue));
         document.getElementById("submenu_std_ch_others").src      = xmlDoc.getElementsByTagName("submenu_std_ch_others")[0].childNodes[0].nodeValue;
         document.getElementById("submenu_std_ch_others").onmouseout  = new Function(eval(xmlDoc.getElementsByTagName("submenu_std_ch_others_mouseout")[0].childNodes[0].nodeValue));
         document.getElementById("submenu_std_eur").src      = xmlDoc.getElementsByTagName("submenu_std_eur")[0].childNodes[0].nodeValue;
         document.getElementById("submenu_std_eur").onmouseout  = new Function(eval(xmlDoc.getElementsByTagName("submenu_std_eur_mouseout")[0].childNodes[0].nodeValue));
         document.getElementById("submenu_std_usa").src      = xmlDoc.getElementsByTagName("submenu_std_usa")[0].childNodes[0].nodeValue;
         document.getElementById("submenu_std_usa").onmouseout  = new Function(eval(xmlDoc.getElementsByTagName("submenu_std_usa_mouseout")[0].childNodes[0].nodeValue));
         document.getElementById("submenu_std_patagonia").src          = xmlDoc.getElementsByTagName("submenu_std_patagonia")[0].childNodes[0].nodeValue;
         document.getElementById("submenu_std_patagonia").onmouseout  = new Function(eval(xmlDoc.getElementsByTagName("submenu_std_patagonia_mouseout")[0].childNodes[0].nodeValue));

      }
   } /* ENDIF xmlHTTP ready */
} /* END UpdatePageElements */
