
var winImgView;

function imagePreview(url, guid, size, mode) {
	if (!mode) mode = '';
  //var left = screen.availWidth - 480;
  var left = (screen.availWidth>700+480)? 700 : screen.availWidth-480;
  var top = 100;
  if (guid!=null) var url = ((url)?url:'')+'zoom.php?type=image&guid='+guid+'&mode='+mode+'&size=zoom';
	var features = 'left='+left+',top='+top+',width=480,height=360,scrollbars=1,location=0,resizable=1,modal=1';
	winImgView = window.open(url, 'viewimage', features).focus();
}

function imagePreview_old(url, guid, size, mode) {
	if (!mode) mode = '';
  //var left = screen.availWidth - 480;
  var left = 650;
  var top = 100;
	if (ie) {
	  if (url) {
	    var s  = '<html><title>image</title>'
	        s += '<body leftmargin=0 topmargin=0 scroll=auto>'
	        s += '<img src="'+url+'" onload="window.opener.resizeImageWindow(window, this.width, this.height)">'
	        s += '</body></html>'
	  }
	  if (guid!=null) {
	    var s  = '<html><title>image</title>';
	        s += '<body leftmargin="0" topmargin="0" bgcolor="#ffffff" scroll="auto" ';
          s += ' onclick="self.close()" style="cursor:hand; cursor:pointer"><center>';
	        s += '<div><img src="view.php?type=image&guid='+guid+'&mode='+mode+'&size=original" ';
          s += ' onload="window.opener.resizeImageWindow(window, this.width, this.height)"></div>';
	        s += '</center></body></html>';
	  }
	  winImgView = window.open('','viewimage','left='+left+',top='+top+',width=480,height=360,scrollbars=1,location=0,resizable=1,modal=1');
	  var o = winImgView.document.open("text/html", "replace");
	  o.write(s);
	} else {
	  if (url!=null) imgurl = url;
	  if (guid!=null) var imgurl = 'view.php?type=image&guid='+guid+'&mode='+mode+'&size=original'
		winImgView = window.open(imgurl,'viewimage','left='+left+',top='+top+',width=480,height=360,scrollbars=1,location=0,resizable=1,modal=1');
	}
}

function resizeImageWindow(win, w, h) {
  var maxW = screen.availWidth * 0.75;
  var maxH = screen.availHeight * 0.75;
  if (w>maxW) w = maxW
  if (h>maxH) h = maxH
  win.resizeTo(w+64, h+64)
  //win.moveTo(screen.availWidth - (w+164), 100)
  win.focus()
}

function getPos(obj) {
  var pos = new Array(0,0);
  while(obj.offsetParent) {
    pos[0] += obj.offsetLeft;
    pos[1] += obj.offsetTop;
    obj = obj.offsetParent;
  }
  return pos;
}

function elem(id) { return document.getElementById(id); }

function gotoAnchor(anchor) {
	self.location = '#'+anchor;
}

function editbar(id, mode) {
  var o = document.getElementById(id); if (!o) return;
  if (mode=='over' || mode=='on' || mode=='show') {
    o.style.display = '';
  }
  if (mode=='out' || mode=='off' || mode=='hide') {
    o.style.display = 'none';
  }
}

function editunit(id, mode, classname, limit) {
  //if (window.editbusy) return;
  if (!classname) classname = 'edit-unit';
  var obj = document.getElementById(id); if (!obj) return;
  var o = (event.srcElement);

  /* OVER */
  if (mode=='over' || mode=='on' || mode=='show') {
    if (obj.contains(event.srcElement) || obj==event.srcElement) return;
    var margin = parseInt(obj.getAttribute('margin'),10); if (margin==null || isNaN(margin)) margin = -40;
    if (ie) {
      var pageY = document.body.scrollTop + event.y;
      var pageX = document.body.scrollLeft + event.x;
    }
    if (moz) {
      var pageY = event.pageY;
      var pageX = event.pageX;
    }
    if (margin!=-9999) obj.style.top = (pageY + margin)+'px';
    obj.style.zIndex = 5;

    event.cancelBubble = true;
    document.body.click();
    document.body.onclick = function() { editunit(id, 'off', classname, limit); }

    if (obj.style.display!='block') obj.style.display = 'block';
    if (!limit) limit = 0;
    if ( limit && pageX < limit && (pageX-obj.offsetWidth)>0 ) {
      var x = (5-obj.offsetWidth)+'px';
      if (obj.style.marginLeft!=x) {
        obj.oldMarginLeft = obj.style.marginLeft;
        obj.style.marginLeft = x;
      }
    } else {
      try { obj.style.marginLeft = obj.oldMarginLeft; } catch(e) {}
    }
  }
  /* OUT */
  if (mode=='out' || mode=='off' || mode=='hide') {
    if (obj.contains(event.toElement) || obj==event.toElement) return;
    obj.style.display = 'none';
  }
}

function editpage(id, mode, classname) {
  if (!classname) classname = 'edit-page';
  var table = document.getElementById('table'+id);
  var block = document.getElementById('block'+id);
  var bar = document.getElementById('bar'+id);

  /* OVER */
  if (mode=='over' || mode=='on' || mode=='show') {
    if (block && block.oldclass!=null) return;
    if (moz) {
      if (window.mouseover) return;
      window.mouseover = event.srcElement;
    }
    window.editbusy = true;
    if (block) {
      block.oldclass = block.className; if (block.className) block.className += ' ';
      block.className += classname+'-body';
    }
    if (bar) bar.style.display = 'block';
    event.cancelBubble = true;
    document.body.onclick = function() { editpage(id, 'off', classname); }
    //document.body.onclick = function() { if (!table.contains(event.srcElement)) editpage(id, 'off', classname); }

    if (ie) {
      bar.style.visibility = 'visible';
      document.getElementById('close'+id).style.display = '';
      bar.style.filter = '';
      table.style.visibility = 'hidden';
      table.style.filter = 'progid:DXImageTransform.Microsoft.Fade(duration=0.25,overlap=0)';
      table.style.filter += " progid:DXImageTransform.Microsoft.Shadow(color='#666666', Direction=135, Strength=5)";
      table.filters[0].Apply();
      table.style.visibility = 'visible';
      table.filters[0].Play();
    }

  }
  /* OUT */
  if (mode=='out' || mode=='off' || mode=='hide') {
    if (moz) {
      var o = window.mouseover;
      if (o.contains(event.toElement)) return;
      window.mouseover = null;
    }
    window.editbusy = false;
    if (block) {
      block.className = block.oldclass;
      block.oldclass = null;
    }
    if (moz) bar.style.display = 'none';
    if (ie) {
      //clipIt(id, 100);
      try { document.getElementById('close'+id).style.display = 'none'; } catch(e) {}
      bar.style.filter = 'progid:DXImageTransform.Microsoft.Fade(duration=0.5,overlap=0)';
      bar.filters[0].Apply();
      bar.style.visibility = 'hidden';
      bar.filters[0].Play();
      table.style.filter = '';
      window.setTimeout("document.getElementById('bar"+id+"').style.display = 'none'", 400);
      window.setTimeout("document.getElementById('bar"+id+"').style.visibility = ''", 400);
      window.setTimeout("document.getElementById('close"+id+"').style.display = ''", 400);
      document.body.onclick = function() {}
    }
  }
}

function clipIt(id, percent) {
  var obj = document.getElementById('table'+id);
  var w = obj.offsetWidth * (1-percent/100);
  var h = obj.offsetHeight * percent/100;
  obj.style.clip = 'rect(0px, '+obj.offsetWidth+'px, '+h+'px, '+w+'px)';
  if (percent>0) { window.setTimeout("clipIt('"+id+"', "+(percent-5)+")", 10); return }
  var bar = document.getElementById('bar'+id);
  bar.style.display = 'none';
  obj.style.clip = 'rect(auto)';
  document.body.onclick = function() {}
}

function highlight(obj, mode, classname) {
  if (!classname) classname = 'highlight';
  if (!obj) return;

  if (mode=='over' || mode=='on' || mode=='show') {
    obj.oldclass = obj.className; if (obj.className) obj.className += ' ';
    obj.className += classname;
    debug.value += obj.className;
  }
  if (mode=='out' || mode=='off' || mode=='hide') {
    obj.className = obj.oldclass;
  }
}

function setCookie (name, value, expires, path, domain, secure) {
  szCookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
  document.cookie = szCookie;
}

function getCookie (name) {
  if ( document.cookie) { // Le cookie est-il valide ?
    index = document.cookie.indexOf( name);
    if ( index != -1) {
      nDeb = (document.cookie.indexOf( "=", index) + 1);
      nFin = document.cookie.indexOf( ";", index);
      if (nFin == -1) {nFin = document.cookie.length;}
      return unescape(document.cookie.substring(nDeb, nFin));
    }
  }
  return null;
}

/*****************************************/
function rnd(a,b) {
	return a + Math.floor(Math.random()*(b-a+1));
}
function rotorLetterOver(id, interval) {
  var obj = document.getElementById(id); if (!obj) return;
  if (event.srcElement!=obj) return;
  rotorLetter(id, interval);
}
function rotorLetter(id, interval, range) {
  if (!interval) interval = 50;
  if (!range) range = 30;
  var obj = document.getElementById(id); if (!obj) return;

  if (obj.oldtext==null) {
    var text = obj.innerText+'';
    var arr0 = new Array();
    var arr1 = new Array();
    for(var i=0; i<text.length; i++) {
      var code = text.charCodeAt(i);
      arr0.push(code);
      arr1.push(code-rnd(0,range));
    }
    obj.oldtext = arr0;
    obj.newtext = arr1;

  } else {
    var arr0 = obj.oldtext;
    var arr1 = obj.newtext;
    var str = '';
    var changed = false;
    for(var i=0; i<arr0.length; i++) {
      if (arr1[i]!=arr0[i]) {
        arr1[i]++;
        changed = true;
        var char = String.fromCharCode(arr1[i]);
        if (arr1[i]<33) char = '*';
        if (arr1[i]>125 && arr1[i]<161) char = '*';
        //char = '*';
      } else {
        var char = String.fromCharCode(arr1[i]);
      }
      str += char;
    }
    obj.innerText = str;
    if (!changed) { obj.oldtext = null; obj.timer = 0; return; }

  }

  obj.timer = window.setTimeout("rotorLetter('"+id+"', "+interval+")", interval);
}


/***** Edition IFrame *****/
function openEditIFrame(id, name, args, params) {
  if (!params) params = new Array();
  var d = new Date();
  var obj = elem(id); if (!obj) { alert("Error: Object '"+id+"' does not exist"); return; }
  obj.style.display = '';
  obj.style.height = params[1] || '2.5em';
  obj.style.position = 'absolute';
  obj.style.zIndex = 0;
  obj.style.margineLeft = '20px';
  if (params[0]) obj.style.width = params[0];
  if (args==null) args = '';
  // Fermeture de la précédente fenêtre d'édition
  if (window.curIFrame && window.curIFrame!=obj) closeEditIFrame(window.curIFrame);
  window.curIFrame = obj;
  // Affichage du message d'attente
  if (ie || moz || opera) obj.contentWindow.document.write(getHTMLWaitMessage());
  // Chargement du formulaire
  obj.src = 'templates/edit/index.php?name='+name+'&'+args+'&var='+Date.parse(new Date());
}
function closeEditIFrame(obj) {
  obj.style.display = 'none';
  obj.src = 'about:blank';
}
function reposEditIFrame(obj) {
  var y = obj.offsetTop + obj.offsetHeight;
  if (moz) {
    var y1 = document.documentElement.scrollTop + document.body.clientHeight;
  } else {
    var y1 = document.body.scrollTop + document.body.offsetHeight;
  }
  if (y1 < y) window.scrollTo(0, document.body.scrollTop + y - y1 + 10);
}
function responseIFrame(behavior) {
  switch(behavior) {
    case 'reload' : window.location.reload(true); break;
    default: window.location.reload(true);
  }
}
function getHTMLWaitMessage() {
  var html = '<body bgcolor="#ffffff" scroll="no" style="margin:5px; font-family:verdana; color:#000000">';
     html += '<table border="0" width="100%"><tr><td style="font-size:12px; font-weight:bold">';
     html += labels['loading-please-wait']+'</td><td width="1%">';
     html += '<a href="javascript:parent.closeEditIFrame(window.frameElement)" style="font-size:11px; color:#666666">';
     html += '['+labels['cmd-close']+']</a></td></tr></table></body>';
  return html;
}
/*****/

/*** Ajax ***/
function ajaxLoadHTML(id, params) { // Charge une page avec les paramètres params
  var content = elem(id); if (!content) return;
  var obj = send(php_self, 'tpl=ajax&'+params, '', 'GET');
  content.style.display = '';
  content.innerHTML = obj.responseText;
}
function ajaxLoadURL(id, url) { // Charge une url (externe)
  var table = elem(id+'__table'); if (!table) return;
  var obj = send(url);
  table.style.display = '';
  var content = elem(id+'__content'); content.innerHTML = obj.responseText;
  var head = elem(id+'__head');
  var body = elem(id+'__body');
  var foot = elem(id+'__foot');
}
function send(address, request, ctype, method) {
	var obj = getHTTPObject();
	if (!method) method = 'POST';
	if (!ctype) ctype = 'application/x-www-form-urlencoded';
	obj.open(method, address, false);
  obj.setRequestHeader("Content-Type", ctype);
  request = (request)? request : '';
  obj.send(request);
  var r = obj.responseText;
  if (r.substring(0,5)=='error') { alert(r); }
  return obj;
}
function send_sync(address, request, ctype) {
  return send(address, request, ctype);
}
function send_async(address, request, ctype, callBack) {
  var obj = getHTTPObject();
  obj.open("POST", address, true);
  obj.setRequestHeader("Content-Type", (ctype)? ctype : 'application/x-www-form-urlencoded');
  if (callBack) obj.onreadystatechange = callBack;
  request = (request)? request : ''
  obj.send(request);
  return obj;
}
function getHTTPObject() {
  var xmlhttp = false;
  /* Compilation conditionnelle d'IE */
  /*@cc_on
  @if (@_jscript_version >= 5)
     try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
		 catch (e) {
        try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
				catch (E) { xmlhttp = false; }
     }
  @else
     xmlhttp = false;
  @end @*/

  /* on essaie de créer l'objet si ce n'est pas déjà fait */
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
     try { xmlhttp = new XMLHttpRequest(); }
     catch (e) { xmlhttp = false; }
  }

	return xmlhttp;
  if (xmlhttp) {
     /* on définit ce qui doit se passer quand la page répondra */
     xmlhttp.onreadystatechange = function()
     {
        if (xmlhttp.readyState == 4) { /* 4 : état "complete" */
           if (xmlhttp.status == 200) { /* 200 : code HTTP pour OK */
              /*
              Traitement de la réponse.
              Ici on affiche la réponse dans une boîte de dialogue.
              */
              alert(xmlhttp.responseText);
           }
        }
     }
  }
  return xmlhttp;
}
/*****/