/*
 * D2W JavaScript Library v5b3
 * http://www.arcantel.ch/
 *
 * Copyright (c) 2010 Luc Duchosal
 *
 * Date: 20100623
 * Revision: 5b3
 */

var JJQuery = jQuery.noConflict();
var adresse = window.location.href;
var spliturl = adresse.split("#");
var anchorName = (spliturl[1] == undefined) ? '' : spliturl[1];

JJQuery(document).ready(jquery_ready);

function jquery_ready() {
   //d2wcl_willload();
   d2wcl_load();
   d2wcl_didload();
}

function d2wcl_didload() {
   
   if(typeof d2wcl_ready != 'function') { 
      return;
   }
   var notloaded = JJQuery(".D2WContentLoader[d2wclurl][d2wclstatus!='loaded']").length;
   //alert('initCount: '+ initCount +' loadingCount: '+ loadingCount);
   if (notloaded == 0) {
       d2wcl_ready(); 
   } else {
      setTimeout("d2wcl_didload();", 500);
   }
   
}

function d2wcl_load() {
   JJQuery(".D2WContentLoader[d2wclurl][d2wclstatus='init']").each(d2wcl_each);
}

///
/// Pour chaque information à charger du D2W,
/// faire une requête AJAX pour aller chercher le contenu
///
function d2wcl_each() {
   
   var id = "#" + this.id;
   var url = JJQuery(id).attr('d2wclurl');
   url = d2wcl_decode(url);

   var status = JJQuery(id).attr('d2wclstatus');
   if (status != 'init') {
      return;
   }
   
   JJQuery(id).html('<a href="'+ url +'" target="_blank"><img src="/d2w/Includes/ContentLoader/loading.gif" border="0" alt="'+ url +'"/></a><br/>');
   //JJQuery(id).html('<a href="'+ url +'" target="_blank">loading...</a><br/>');
   JJQuery(id).css('visibility', 'visible');
   JJQuery(id).attr('d2wclstatus', 'loading');
   JJQuery.ajax ({
      url: url,
      cache: true,
      ifModified: true,
      async: true,
      processData: false,
      success: function(html, status) { jquery_success(id, html, status); },
      error: function(xhr, ajaxOptions, thrownError){ jquery_error(xhr, ajaxOptions, thrownError, id); }
   });
   
}

function d2wcl_decode(str) {
   var decode = str;
   decode = decode.replace(/XXG7TXX/g, '"');
   decode = decode.replace(/XXS3HXX/g, '/');
   decode = decode.replace(/XXS3EXX/g, ' ');
   return decode;
}

function jquery_error(xhr, ajaxOptions, thrownError, id) {

   JJQuery(id).attr('d2wclstatus', 'error');
   JJQuery(id).html('An error occured<br>' + xhr.status + '. ' + xhr.statusText +'<br>'+ xhr.responseText );
   
   //alert(thrownError);

}

function jquery_success(id, html, status) { 				

   if (status != 'success') {
      alert('status: '+ status);      
   }

   JJQuery(id).attr('d2wclstatus', 'loaded');
   JJQuery(id).empty().append(html);
   //JJQuery(id).append(html);
   
   // Il faut le mettre que sur le dernier element
   if (anchorName != '') {
      document.location.hash  = anchorName;
   }
   
   d2wcl_load();

}


function isD2WClCompatible() {
   var bw = new lib_bwcheck();
   var clcompat = (bw.ie6 || bw.ie7 || bw.ie8 || bw.ie9 || bw.ie10 || bw.ff3 || bw.ff4 || bw.ff5 || bw.ff6 || bw.opera9 || bw.opera10 || bw.opera11);

   return clcompat == true || clcompat == 1;
}

function redirectCL() {
   var isCompatible = isD2WClCompatible();
   if (isCompatible) {
      if (document.location.href.indexOf("www2") >= 0) {
         document.location.href=document.location.href.replace("www2", "www3");
      }
   }
}

