var g_previewWindow=null;
var g_downloadWindow=null;
var g_cardViewerWindow=null;
var g_fileViewerWindow=null;
var g_arrThumnails = new Array();
var g_arrWatches = new Array();
var g_uidActiveWatch = null;
var g_arrProcessThumbnails = new Array();
var arrGroupId = new Array();
var arrBoxId = new Array();
var _ajax = new Array();
var g_gallImages = new Array();
var g_gallDisplays = new Array();
var g_mainImg = null;
var g_mainImgName = null;
var g_imgBack = null;
var g_imgNext = null;
var g_imgGallBack = null;
var g_imgGallNext = null;
var g_intSelectedIndex = -1;
var g_bolHomepage=false;
var g_bolLoginNode=false;
var g_bolDownloadTDInit = false;
var g_arrEditorsName = new Array();
var g_arrEditors = new Array();
var objLeft = null;
var bolLoaded = false;

  

  
function previewFile(uidCard, uidFile){
  var url='/tools/file_preview/index.lbl?uidCard='+uidCard+'&uidFile='+uidFile;
  g_previewWindow = window.open(url,"FilePreview","left=5,top=5,toolbar=0,directories=0,menubar=0,scrollbars=1,resizable=1,width="+(screen.width-75)+",height="+(screen.height-75));
  g_previewWindow.focus(); 
}

function openImageDownloadForm(uidFile){
  var url='/popups/image-download-form/index.lbl?uidFile='+uidFile;
  g_downloadWindow = window.open(url,"ImageDownloadForm","toolbar=0,directories=0,menubar=0,scrollbars=0,resizable=0,width=600,height=600");
  g_downloadWindow.focus();
}

function downloadFile(uidFile){
  var url='/filedownload.lbl?uidFile='+uidFile;
  g_downloadWindow = window.open(url,"FileDownload","toolbar=0,directories=0,menubar=0,scrollbars=0,resizable=0,width=40,height=40");
}

function showCard(uidCard){
  var url = '/tools/card_viewer/index.lbl?uidCard='+uidCard;
  g_downloadWindow = window.open(url,"CardViewer","left=5,top=5,toolbar=0,directories=0,menubar=0,scrollbars=1,resizable=1,width=480,height="+(screen.height-75));
  g_downloadWindow.focus();
}

function viewFile(uidFile){
  var url = '/fileviewer.lbl?uidFile='+uidFile;
  g_fileViewerWindow = window.open(url,"FileViewer","toolbar=0,directories=0,menubar=0,scrollbars=1,resizable=1,width=480,height="+(screen.height-75));
  g_fileViewerWindow.focus();
}

function resizePreviewWindow(){
  var objImg = document.getElementById("img");
  if(objImg){
    var intWidth = objImg.width;
    var intHeight = objImg.height;
    if(intWidth<screen.width && intHeight<screen.height)
    {
      window.resizeTo(intWidth+250, intHeight+250);
    }
  }
}

function isDir(objPicture){
  return objPicture.bolDir;
}

function changePicture(objImg){
  var intIndex = objImg.intIndex;
  changePictureIndex(intIndex);
}

function changePictureIndex(intIndex){
  var selectedPicture = getImageFromIndex(g_intSelectedIndex);
  if(selectedPicture){
    selectedPicture.style.borderColor=g_EXTRANET_RIGHT_BG;
  }
  
  var objPicture = g_arrPictures[intIndex];
  if(!isDir(objPicture)){
    g_mainImg.src="/_images/spacer.gif";
    setW(g_mainImg, objPicture.intWidth);
    g_mainImg.src=objPicture.strUrl;
    g_mainImgName.innerText=objPicture.strName;
    
    var thisImg = getImageFromIndex(intIndex);
    if(thisImg){
      thisImg.style.borderColor=g_STR_IMAGE_VIEWER_BORDER;
    }
    g_intSelectedIndex = intIndex;
    enableDownload();
  }else{
    openExDir(objPicture);
  }
}

function enableDownload(){
  if(!g_bolDownloadTDInit){
    var td = getE("downloadTD");
    td.innerHTML = g_downloadTDContent;
  }
  g_bolDownloadTDInit = true;
}

   
function exDownload(){
  var uid = g_arrPictures[g_intSelectedIndex].uid;
  var url = "/filedownload.lbl?uid="+uid;
  window.location=url;
}

function openExDir(objDir){
  var offset = (objDir.bolBack)?-1:1;
  var intLevel = g_intLevel+offset;
  
  var strUrl = g_self;
  if(intLevel>0){
    strUrl += "?uf="+objDir.uid+"&l="+intLevel;
  }
  window.location=strUrl;
}


function binit(){
  bolLoaded = true;
  
  init();
  
  if(typeof(initPortfolios)=="function"){
    initPortfolios();
  }
  if(typeof(basketInit)=="function"){
    basketInit();
  }
  if (typeof(initZ)=="function"){
    initZ();
  }
  
  initThumbnails();

  g_objMainForm = getE("extranetMainForm");
  var l = g_arrEditorsName.length;
  for(var i=0; i<l; i++) {
    g_arrEditors[g_arrEditorsName[i]] = new HTMLEditor(g_arrEditorsName[i]);
  }
  
  if(typeof(linit)=="function"){
    linit();
  }
  
  var divTopBar = getE("divTopBarMenu");
  if (divTopBar!=null){
    setX(divTopBar, 10);
    setY(divTopBar, 60);
    showE(divTopBar);
  }
}

var g_intResizeTimeout=null;

function resize() {
  if(g_intResizeTimeout) clearTimeout(g_intResizeTimeout);
  g_intResizeTimeout=setTimeout("resizeDone()",200);
}

function resizeDone() {
  g_intResizeTimeout=null;
  if(typeof(initBoxes)=="function"){
    initBoxes();
  }
}


function linit(){
  initBoxes();
  if (typeof initSlideShows=="function") initSlideShows();
}

/***************************************************************
* PORTFOLIOS MANAGEMENT
***************************************************************/
function downloadArchive(id){
  var win = openPopup("/popups/image-download-form/index.lbl?user_action=downloadArchive&id_portfolio="+id, 10, 10);
}

function deleteArchive(id){
  window.location="/tools/download-manager/index.lbl?user_action=deletePortfolio&id_portfolio="+id;
}

/****************************************************************
* THUMBNAILS
****************************************************************/
function initThumbnails(){
  if (!g_arrThumnails) return;
  var len = g_arrThumnails.length;
  var watchname;
  var uid;
  var strMode;
  var idthumb;
  
  _ajax = new Array();
  for (var i=0; i<len; i++){
    if (g_arrThumnails[i]==null) return;
    idthumb=g_arrThumnails[i].idthumb;
    watchname=g_arrThumnails[i].watchname;
    if (watchname==null || watchname=="undefined") watchname="";
    uid=g_arrThumnails[i].uid;
    if (uid==null || uid=="undefined") uid="";
    strMode=g_arrThumnails[i].strMode;
    if (strMode==null || strMode=="undefined") strMode="";
    _ajax[idthumb] = new XMLHttpRequest();
    // if thumb isnt already created, call "setInterval" for check thumbnail until it's been created
    g_arrProcessThumbnails[idthumb] = new Object();
    var url = '/tools/thumbnails_download.lbl';
    g_arrProcessThumbnails[idthumb].process = 'ajax_checkThumb("'+uid+'", "'+watchname+'", '+g_arrThumnails[i].width+', '+g_arrThumnails[i].height+', "'+strMode+'", "'+idthumb+'")';
    _ajax[idthumb].open("GET", url, true);
    var id=idthumb.replace(/-/g, "","-");
    eval("_ajax['"+idthumb+"'].onreadystatechange = treatThumb_"+id+";");
    _ajax[idthumb].send("&action=check&uid="+encode(uid)+"&watchname=" + encode(watchname)+"&w="+g_arrThumnails[i].width+"&h="+g_arrThumnails[i].height+"&m="+strMode);
  }
}

function launchProcess(id){
  if (id==null) return;
  g_arrProcessThumbnails[id].process = setInterval(g_arrProcessThumbnails[id].process, 1500);
  g_arrProcessThumbnails[id].count = 0;
}

function ajax_checkThumb( uid, watchname, width, height, strMode, idthumb ) {
  var objAjax = _ajax[idthumb];
  if (g_arrProcessThumbnails[idthumb].count>20){
    clearInterval(g_arrProcessThumbnails[idthumb].process);
    var img=getE(idthumb);
    if (img!=null){
      img.src=img.src+"&action=abort";
    }
  }
  else g_arrProcessThumbnails[idthumb].count++;

  var url = '/tools/thumbnails_download.lbl'+"?&action=check&uid="+encode(uid)+"&watchname=" + encode(watchname)+"&w="+width+"&h="+height+"&m="+strMode;
  objAjax.open("GET", url, true);
  objAjax.onreadystatechange = function(){
    if ( objAjax.readyState == 4 ) {
      if (objAjax.responseText=="yes"){
        clearInterval(g_arrProcessThumbnails[idthumb].process);
        var img=getE(idthumb);
        if (img!=null){
          var strTmp=img.src;
          img.src="/_images/_spacer.gif";
          img.src=strTmp+"&t=renew"; // the "t=renew" param is to force the browser to download the generated image (changing it's url) and avoid its cache...
        }
      }
    }
  }
  objAjax.send("&action=check&uid="+encode(uid)+"&watchname=" + encode(watchname)+"&w="+width+"&h="+height+"&m="+strMode);
}

function ajax_downloadDocument(strParams) {
  var objAjax = new XMLHttpRequest();
  var url = '/popups/image-download-form/index.lbl';
  objAjax.open("POST", url, true);
  objAjax.onreadystatechange = function(){
    if ( objAjax.readyState == 4 ) {
      if (objAjax.responseText=="yes"){
      }
    }
  }
  objAjax.send(strParams);
}

function ajax_addToPortfolio(strParams){
  var objAjax = new XMLHttpRequest();
  var url = '/popups/image-download-form/index.lbl';
  objAjax.open("POST", url, true);
  objAjax.onreadystatechange = function(){
    if ( objAjax.readyState == 4 ) {
      alert(objAjax.responseText);
    }
  }
  objAjax.send(strParams);
}


function encode( uri ) {
  if (encodeURIComponent) return encodeURIComponent(uri);
  if (escape) return escape(uri);
}