// Copyright (C) 2009-2010 Ilya S. Lyubinskiy. All rights reserved. See license.txt


var cms_current_zIndex = 9;


// ***** Api *******************************************************************

// ***** HTMLEntities *****

function HTMLEntities(str)
{
  str = str.replace(/&/g, '&amp;');
  str = str.replace(/</g, '&lt;' );
  str = str.replace(/>/g, '&gt;' );
  return str;
}

// ***** XMLHttpRequest *****

if (!XMLHttpRequest) var XMLHttpRequest = function ()
{
  try { return new ActiveXObject(   'MSXML3.XMLHTTP'    ); } catch(e) {}
  try { return new ActiveXObject(   'MSXML2.XMLHTTP.3.0'); } catch(e) {}
  try { return new ActiveXObject(   'MSXML2.XMLHTTP'    ); } catch(e) {}
  try { return new ActiveXObject('Microsoft.XMLHTTP'    ); } catch(e) {}
}


// ***** Misc ******************************************************************

// ***** MiscCalcSubmit *****

function MiscCalcSubmit(sitem_id, fractional, msg_fail, url)
{
  var quantity = Number(document.getElementById('cms_dhtml_id_calculator_'+sitem_id+'_quantity').value);
  if (isNaN(quantity) || !fractional && quantity.toFixed(0) != quantity) { alert(msg_fail); return false; }

  var form = document.getElementById('cms_dhtml_id_calculator_'+sitem_id+'_form');
  form.action = url;

  return true;
}

// ***** MiscCalcXMLHttpRequest *****

function MiscCalcXMLHttpRequest(sitem_id, fractional, msg_fail, price)
{
  var quantity = Number(document.getElementById('cms_dhtml_id_calculator_'+sitem_id+'_quantity').value);
  if (isNaN(quantity) || !fractional && quantity.toFixed(0) != quantity) { alert(msg_fail); return false; }

  var quantity = document.getElementById('cms_dhtml_id_calculator_'+sitem_id+'_quantity') ? document.getElementById('cms_dhtml_id_calculator_'+sitem_id+'_quantity').value : '';
  var ext1     = document.getElementById('cms_dhtml_id_calculator_'+sitem_id+'_ext1'    ) ? document.getElementById('cms_dhtml_id_calculator_'+sitem_id+'_ext1'    ).value : '';
  var ext2     = document.getElementById('cms_dhtml_id_calculator_'+sitem_id+'_ext2'    ) ? document.getElementById('cms_dhtml_id_calculator_'+sitem_id+'_ext2'    ).value : '';
  var ext3     = document.getElementById('cms_dhtml_id_calculator_'+sitem_id+'_ext3'    ) ? document.getElementById('cms_dhtml_id_calculator_'+sitem_id+'_ext3'    ).value : '';

  var req = new XMLHttpRequest();
  req.open('get', site_home+'utils/calculator.php?price='+encodeURIComponent(price)+
                                             '&quantity='+encodeURIComponent(quantity)+
                                                 '&ext1='+encodeURIComponent(ext1)+
                                                 '&ext2='+encodeURIComponent(ext2)+
                                                 '&ext3='+encodeURIComponent(ext3));
  req.send(null);
  req.onreadystatechange = function ()
  {
    if (req.readyState == 4 && req.status == 200)
    {
      var arr = req.responseText.split('\n');
      if (arr.length >= 2 && arr[0] ==   'OK') document.getElementById('cms_dhtml_id_calculator_'+sitem_id+'_price').innerHTML = arr[1];
      if (arr.length >= 1 && arr[0] == 'FAIL') alert(msg_fail);
    }
  }
}

// ***** MiscCategExpandCollapse *****

function MiscCategExpandCollapse(categ_id)
{
  if  (document.getElementById(categ_id+'_data').style.display != 'none')
       document.getElementById(categ_id+'_data').style.display  = 'none';
  else document.getElementById(categ_id+'_data').style.display  = 'block';

  if  (document.getElementById(categ_id+'_data').style.display != 'none')
       document.getElementById(categ_id+'_link').style.backgroundPosition = '0px -20px';
  else document.getElementById(categ_id+'_link').style.backgroundPosition = '0px   0px';

  return false;
}

// ***** MiscSitenavSubmit *****

function MiscSitenavSubmit(sender)
{
  if (sender.tagName ==  'INPUT') var href = document.getElementById('cms_dhtml_id_sitenav_'+sender.name+'_'+sender.value).href;
  if (sender.tagName == 'SELECT') var href = document.getElementById('cms_dhtml_id_sitenav_'+sender.name                 ).href;
  if (sender.tagName == 'SELECT')
  if (sender.selectedIndex)
        href = href.replace(sender.name+'='+'0', sender.name+'='+encodeURIComponent(sender.value));
   else href = href.replace(sender.name+'='+'0', '');

  href = href.replace( /&&/g, '&');
  href = href.replace(/\?&/g, '?');
  href = href.replace( /&$/g,  '');
  href = href.replace(/\?$/g,  '');

  document.location = href;
}


// ***** Maps ******************************************************************

// ***** GoogleMapInit *****

function GoogleMapInit(div_id, input_id, zoom, center, markers)
{
  var fn1 = function ()
  {
    if (!input_id) return;
    if ( map.getZoom()         == 0)
    if ( map.getCenter().lat() == 0)
    if ( map.getCenter().lng() == 0) return;
    document.getElementById(input_id+'_map_zoom'     ).value = map.getZoom();
    document.getElementById(input_id+'_map_latitude' ).value = Math.round(map.getCenter().lat()*100000)/100000;
    document.getElementById(input_id+'_map_longitude').value = Math.round(map.getCenter().lng()*100000)/100000;
  }

  var fn2 = function (marker, icon, zIndex)
  {
    var pos = new google.maps.LatLng(marker.latitude, marker.longitude);
    var opt = { 'map' : map, 'position' : pos, 'title' : marker.title, 'icon' : icon, 'zIndex' : zIndex };
    var dot = new google.maps.Marker(opt);
    google.maps.event.addListener(dot, 'click', function () { if (marker.url) document.location = marker.url; });
  }

    var pos = new google.maps.LatLng(center.latitude, center.longitude);
    var opt = { 'zoom' : zoom, 'center' : pos, 'mapTypeId' : google.maps.MapTypeId.ROADMAP };
    var map = new google.maps.Map(document.getElementById(div_id), opt);
    google.maps.event.addListener(map,   'zoom_changed', fn1);
    google.maps.event.addListener(map, 'center_changed', fn1);
                           fn2(center,     site_home+'images/marker_red.png',    1);
    for (var i in markers) fn2(markers[i], site_home+'images/marker_yellow.png', 2);
    fn1();
    return map;
}

// ***** GoogleMapMove *****

function GoogleMapMove(map, address)
{
  var fn1 = function (res, status) { if (status == 'OK') map.setCenter(res[0].geometry.location); }
  new google.maps.Geocoder().geocode({ 'address' : address }, fn1);
  return false;
}


// ***** Input *****************************************************************

// ***** InputClear *****

function InputClear(sender, defaults)
{
  var tags = sender.form.getElementsByTagName('input');
  for (var i = 0; i < tags.length; i++)
  {
    if (tags[i].type ==   'hidden')
    if (document.getElementById('cms_dhtml_id_wysiwyg_'+tags[i].id))
    WysiwygInit('cms_dhtml_id_wysiwyg_'+tags[i].id,
                 defaults ? document.getElementById('cms_dhtml_id_wysiwyg_'+tags[i].id+'_frame_data').value :
                            document.getElementById('cms_dhtml_id_wysiwyg_'+tags[i].id+'_frame_none').value);


    if (tags[i].type !=     'text')
    if (tags[i].type != 'password')
    if (tags[i].type != 'checkbox') continue;
    if (tags[i].type ==     'text') tags[i].value   = defaults ? tags[i].defaultValue   : '';
    if (tags[i].type == 'password') tags[i].value   = defaults ? tags[i].defaultValue   : '';
    if (tags[i].type == 'checkbox') tags[i].checked = defaults ? tags[i].defaultChecked : false;
    if (typeof(tags[i].onchange) == 'function') tags[i].onchange();
    if (typeof(tags[i].onclick ) == 'function') tags[i].onclick ();
  }

  var tags = sender.form.getElementsByTagName('textarea');
  for (var i = 0; i < tags.length; i++)
  {
    tags[i].value = defaults ? tags[i].defaultValue : '';
    if (typeof(tags[i].onchange) == 'function') tags[i].onchange();
    if (typeof(tags[i].onclick ) == 'function') tags[i].onclick ();
  }

  var tags = sender.form.getElementsByTagName('select');
  for (var i = 0; i < tags.length; i++)
  {
         var n = 0;
    for (var j = 0; j < tags[i].options.length; j++)
                    if (tags[i].options[j].defaultSelected) if (defaults) n = j;
                        tags[i].selectedIndex = n;
    if (typeof(tags[i].onchange) == 'function') tags[i].onchange();
    if (typeof(tags[i].onclick ) == 'function') tags[i].onclick ();
  }
}

// ***** InputSubmitAsCopy *****

function InputSubmitAsCopy(sender, idref_id, idref_value)
{
  document.getElementById(idref_id).value = sender.checked ? '' : idref_value;
}

// ***** InputSendFieldsDisabled *****

function InputSendFieldsDisabled(sender)
{
  var fn1 = function (name, value)
  {
    if (!name || !value) return;
    var input = document.createElement('input');
        input.type  = 'hidden';
        input.name  =  name;
        input.value =  value;
       sender.appendChild(input);
  }

  var tags = sender.getElementsByTagName('input');
  for (var i = 0; i < tags.length; i++) if (tags[i].disabled)
  fn1(tags[i].name, tags[i].type != 'checkbox' ||
                    tags[i].type == 'checkbox' && tags[i].checked ? tags[i].value : '');

  var tags = sender.getElementsByTagName('textarea');
  for (var i = 0; i < tags.length; i++) if (tags[i].disabled)
  fn1(tags[i].name, tags[i].value);

  var tags = sender.getElementsByTagName('select');
  for (var i = 0; i < tags.length; i++) if (tags[i].disabled)
  fn1(tags[i].name, tags[i].value);
}

// ***** InputSendFieldsQueryStr *****

function InputSendFieldsQueryStr(sender)
{
  var fn1 = function (name, value)
  {
    if (!name || !value) return;
    for (var i = 0; i < sender.elements.length; i++) if (sender.elements[i].name == name) return;
    var input = document.createElement('input');
        input.type  = 'hidden';
        input.name  =  name;
        input.value =  value;
       sender.insertBefore(input, sender.firstChild);
  }

                             if (sender.action.indexOf('?') == -1) return;
  var arr = sender.action.substr(sender.action.indexOf('?')+1).split('&');
  for (var i = arr.length-1; i >= 0; i--)
  {
        arr[i] = arr[i].split('=');
    if (arr[i].length >= 2) fn1(arr[i][0], decodeURIComponent(arr[i][1]));
  }
}

// ***** InputSendFieldsWysiwygs *****

function InputSendFieldsWysiwygs(sender)
{
  var tags = sender.getElementsByTagName('input');
  for (var i = 0; i < tags.length; i++)
   if (document.getElementById('cms_dhtml_id_wysiwyg_'+tags[i].id))
    tags[i].value = WysiwygDoc('cms_dhtml_id_wysiwyg_'+tags[i].id).body.innerHTML;
}

// ***** InputSetEnabledDisabled *****

function InputSetEnabledDisabled(sender, child_ids, enabled)
{
  var fn1 = function (tag_id, selected)
  {
    var tag = document.getElementById(tag_id);
    if (!tag) return;
    if ( tag.tagName != 'INPUT' ||
         tag.tagName == 'INPUT' && tag.type != 'checkbox')
         tag.style.backgroundColor = selected != enabled ? '#E0E0E0' : '#FFFFFF';
         tag.style.color           = selected != enabled ? '#808080' : '#000000';
         tag.disabled              = selected != enabled;
  }

  var postfix = new Array('', '_hhh', '_min', '_day', '_mon', '_yyy', '_exp', '_min', '_max');

  if  (sender.tagName ==  'INPUT')
  for (var i in child_ids)
  for (var k in postfix)
  fn1(child_ids[i]   +postfix[k], sender.checked);

  if  (sender.tagName == 'SELECT')
  for (var i in child_ids)
  for (var j in child_ids[i])
  for (var k in postfix)
  fn1(child_ids[i][j]+postfix[k], sender.value == i);
}

// ***** InputUploaderIFResponse *****

function InputUploaderIFResponse(sender)
{
  if (navigator.appName == 'Microsoft Internet Explorer')
       var data = document.frames[sender.id].document.body.innerHTML;
  else var data = document.getElementById(sender.id).contentWindow.document.body.innerHTML;

  data = data.split('::');
  for (var i = 1; i < data.length-1; i += 2)
  {
    document.getElementById('cms_dhtml_id_input_'+data[0]+'_'+data[i]+'_checkbox').style.display = 'inline';
    document.getElementById('cms_dhtml_id_input_'+data[0]+'_'+data[i]+'_filename').innerHTML = data[i+1];
  }
}

// ***** InputUploaderSubmitFile *****

function InputUploaderSubmitFile(sender)
{
  sender.form.target = sender.id+'_frame';
  sender.form.action = site_home+'utils/uploader.php';
  sender.form.submit();
}

// ***** InputUploaderSubmitForm *****

function InputUploaderSubmitForm(sender, action)
{
  sender.form.target = '';
  sender.form.action = action;
}

// ***** InputXMLHttpRequestSelect *****

function InputXMLHttpRequestSelect(array, parent_id, child_id)
{
  var parent = document.getElementById(parent_id);
  var  child = document.getElementById( child_id);

  while (child.options.length > 1) child.remove(1);
  if (typeof(child.onchange) == 'function') child.onchange();
  if (typeof(child.onclick ) == 'function') child.onclick ();
  if (parent.value == '') return;

  var req = new XMLHttpRequest();
  req.open('get', site_home+'utils/data.php?array='+encodeURIComponent(array)+
                                          '&index='+encodeURIComponent(parent.value));
  req.send(null);
  req.onreadystatechange = function ()
  {
    if (req.readyState == 4 && req.status == 200)
    {
      var arr = req.responseText.split('\n');
      for (var i in arr) if (arr[i])
      {
        var pos = arr[i].indexOf('::');
        var txt = arr[i].substr(2+pos);
        var val = arr[i].substr(0,pos);
        if (pos == -1) continue;
        if (txt == '') continue;
        if (val == '') continue;
        child.options[child.options.length] = new Option(txt, val, false, false);
      }
    }
  }
}


// ***** Multi *****************************************************************

// ***** MultiSelectAll *****

function MultiSelectAll(input_id, name, checked)
{
	var tags = document.getElementById(input_id).getElementsByTagName('input');
	for (var i = 0; i < tags.length; i++) if (tags[i].name == name) tags[i].checked = checked;
	return false;
}

// ***** MultiIsChecked *****

function MultiIsChecked(input_id, name, msg_not_checked)
{
	var tags = document.getElementById(input_id).getElementsByTagName('input');
	for (var i = 0; i < tags.length; i++) if (tags[i].name == name) if (tags[i].checked) return true;
  alert(msg_not_checked);
	return false;
}

// ***** MultiDoChecked *****

function MultiDoChecked(input_id, name, action_id, action_value, msg_not_checked, msg_confirmation)
{
  if   (!MultiIsChecked(input_id, name, msg_not_checked)) return false;
  if   (  msg_confirmation && !confirm(msg_confirmation)) return false;
  document.getElementById(action_id).value = action_value;
  document.getElementById( input_id).submit();
  return false;
}


// ***** Wysiwyg ***************************************************************

// ***** WysiwygWnd *****

function WysiwygWnd(wysiwyg)
{
  if (navigator.appName == 'Microsoft Internet Explorer')
       return document.frames[wysiwyg];
  else return document.getElementById(wysiwyg).contentWindow;
}

// ***** WysiwygDoc *****

function WysiwygDoc(wysiwyg)
{
  if (navigator.appName == 'Microsoft Internet Explorer')
       return document.frames[wysiwyg].document;
  else return document.getElementById(wysiwyg).contentWindow.document;
}

// ***** WysiwygRng *****

     var WysiwygRngList = new Array();
function WysiwygRng(wysiwyg)
{
  if (navigator.appName == 'Microsoft Internet Explorer')
    if (document.activeElement.id == wysiwyg)
      WysiwygRngList[wysiwyg] = WysiwygDoc(wysiwyg).selection.createRange();
  setTimeout('WysiwygRng("'+wysiwyg+'")', 100);
}

// ***** WysiwygInit *****

function WysiwygInit(wysiwyg, html)
{
  WysiwygDoc(wysiwyg).write(html);
  WysiwygDoc(wysiwyg).close();
  WysiwygDoc(wysiwyg).body.contentEditable =  'true';
  WysiwygRng(wysiwyg);
}

// ***** WysiwygDone *****

function WysiwygDone(wysiwyg)
{
  WysiwygDoc(wysiwyg).body.contentEditable = 'false';
}

// ***** WysiwygButtonState *****

function WysiwygButtonState(sender, image)
{
  sender.style.backgroundImage = 'url("'+site_home+'images/wysiwyg/'+image+'")';
}

// ***** WysiwygCmd *****

function WysiwygCmd(wysiwyg, command, value)
{
  WysiwygDoc(wysiwyg).body.focus();
  WysiwygDoc(wysiwyg).execCommand(command, false, value);
}

// ***** WysiwygCmdColor *****

function WysiwygCmdColorBack(wysiwyg, color) { WysiwygCmd(wysiwyg, 'backcolor', color); return false; }
function WysiwygCmdColorFore(wysiwyg, color) { WysiwygCmd(wysiwyg, 'forecolor', color); return false; }

// ***** WysiwygIns *****

function WysiwygIns(wysiwyg, tag)
{
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var tmp = document.createElement('span');
        tmp.appendChild(tag);

    WysiwygDoc(wysiwyg).body.focus();
    if (!WysiwygRngList[wysiwyg])
         WysiwygRngList[wysiwyg] = WysiwygDoc(wysiwyg).selection.createRange();
         WysiwygRngList[wysiwyg].select();
         WysiwygRngList[wysiwyg].pasteHTML(tmp.innerHTML);
  }
  else
  {
    WysiwygDoc(wysiwyg).body.focus();
    var range     = WysiwygDoc(wysiwyg).createRange();
    var selection = WysiwygWnd(wysiwyg).getSelection();

    for(var i = selection.rangeCount-1; i >= 0; i--)
                selection.getRangeAt(i).deleteContents();
                selection.getRangeAt(0).insertNode(tag);

    range.setStartAfter(tag);
    range.setEndAfter(tag);
    selection.removeAllRanges();
    selection.addRange(range);
	}
}

// ***** WysiwygInsSmiley *****

function WysiwygInsSmiley(wysiwyg, filename)
{
                  var tag = WysiwygDoc(wysiwyg).createElement('img');
                      tag.src       = site_home+'images/smileys/'+filename;
                      tag.alt       = '';
                      tag.width     = 20;
                      tag.height    = 20;
  WysiwygIns(wysiwyg, tag);
}

// ***** WysiwygInsTable *****

function WysiwygInsTable(wysiwyg)
{
  if     ('' == document.getElementById(wysiwyg+'_popup_window_table_col'        ).value) return;
  if     ('' == document.getElementById(wysiwyg+'_popup_window_table_row'        ).value) return;

  var row     = document.getElementById(wysiwyg+'_popup_window_table_row'        ).value;
  var col     = document.getElementById(wysiwyg+'_popup_window_table_col'        ).value;

                document.getElementById(wysiwyg+'_popup_window_table'            ).style.display = 'none';

                  var tag = WysiwygDoc(wysiwyg).createElement('table');
                  for (var i = 0; i < row; i++)
                  {
                  var trN = WysiwygDoc(wysiwyg).createElement('tr');
                      tag.appendChild(trN);
                  for (var j = 0; j < row; j++)
                  {
                  var tdN = WysiwygDoc(wysiwyg).createElement('td');
                      tdN.innerHTML    = 'Item'+' '+i+':'+j;
                      trN.appendChild(tdN);
                  }
                  }
  WysiwygIns(wysiwyg, tag);
}

// ***** WysiwygInsPicture *****

function WysiwygInsPicture(wysiwyg)
{
  if     ('' == document.getElementById(wysiwyg+'_popup_window_picture_url'      ).value) return;

  var url     = document.getElementById(wysiwyg+'_popup_window_picture_url'      ).value;
  var alt     = document.getElementById(wysiwyg+'_popup_window_picture_alt'      ).value;
  var width   = document.getElementById(wysiwyg+'_popup_window_picture_width'    ).value;
  var height  = document.getElementById(wysiwyg+'_popup_window_picture_height'   ).value;

                document.getElementById(wysiwyg+'_popup_window_picture'          ).style.display = 'none';
                document.getElementById(wysiwyg+'_popup_window_picture_url'      ).value = '';
                document.getElementById(wysiwyg+'_popup_window_picture_alt'      ).value = '';
                document.getElementById(wysiwyg+'_popup_window_picture_width'    ).value = '';
                document.getElementById(wysiwyg+'_popup_window_picture_height'   ).value = '';

                  var tag = WysiwygDoc(wysiwyg).createElement('img');
                      tag.src       = url;
                      tag.alt       = alt;
          if (width)  tag.width     = width;
          if (height) tag.height    = height;
  WysiwygIns(wysiwyg, tag);
}

// ***** WysiwygInsHyperlink *****

function WysiwygInsHyperlink(wysiwyg)
{
  if     ('' == document.getElementById(wysiwyg+'_popup_window_hyperlink_caption').value) return;
  if     ('' == document.getElementById(wysiwyg+'_popup_window_hyperlink_url'    ).value) return;

  var caption = document.getElementById(wysiwyg+'_popup_window_hyperlink_caption').value;
  var url     = document.getElementById(wysiwyg+'_popup_window_hyperlink_url'    ).value;
  var title   = document.getElementById(wysiwyg+'_popup_window_hyperlink_title'  ).value;

                document.getElementById(wysiwyg+'_popup_window_hyperlink'        ).style.display = 'none';
                document.getElementById(wysiwyg+'_popup_window_hyperlink_caption').value = '';
                document.getElementById(wysiwyg+'_popup_window_hyperlink_url'    ).value = '';
                document.getElementById(wysiwyg+'_popup_window_hyperlink_title'  ).value = '';

                  var tag = WysiwygDoc(wysiwyg).createElement('a');
                      tag.innerHTML = caption;
                      tag.href      = url;
                      tag.title     = title;
  WysiwygIns(wysiwyg, tag);
}

// ***** WysiwygUploadAsPicture *****

function WysiwygUploadAsPicture(wysiwyg, url)
{
  document.getElementById(wysiwyg+'_popup_window_picture_url'      ).value = url;
  document.getElementById(wysiwyg+'_popup_window_picture_alt'      ).value = '';
  document.getElementById(wysiwyg+'_popup_window_picture_width'    ).value = '';
  document.getElementById(wysiwyg+'_popup_window_picture_height'   ).value = '';

               PopupShow(wysiwyg+'_popup_window_picture',
                          wysiwyg+'_popup_window_picture_drag',
                          wysiwyg+'_popup_window_picture_exit',
  'tag-bottom', 0, 5, wysiwyg+'_picture');
}

// ***** WysiwygUploadAsHyperlink *****

function WysiwygUploadAsHyperlink(wysiwyg, url)
{
  document.getElementById(wysiwyg+'_popup_window_hyperlink_caption').value = '';
  document.getElementById(wysiwyg+'_popup_window_hyperlink_url'    ).value = url;
  document.getElementById(wysiwyg+'_popup_window_hyperlink_title'  ).value = '';

               PopupShow(wysiwyg+'_popup_window_hyperlink',
                          wysiwyg+'_popup_window_hyperlink_drag',
                          wysiwyg+'_popup_window_hyperlink_exit',
  'tag-bottom', 0, 5, wysiwyg+'_hyperlink');
}


// ***** Popup *****************************************************************

var popup_dragging = false;
var popup_target;
var popup_mouseX;
var popup_mouseY;
var popup_pointX;
var popup_pointY;
var popup_oldfunction;

// ***** PopupMousedown *****

function PopupMousedown(e)
{
  var ie      = navigator.appName == 'Microsoft Internet Explorer';
  var mouseX  = ie ? window.event.clientX : e.clientX;
  var mouseY  = ie ? window.event.clientY : e.clientY;
  var element = document.getElementById(popup_target);

  popup_pointX   = mouseX;
  popup_pointY   = mouseY;
}

// ***** PopupMousedownDragElement *****

function PopupMousedownDragElement(e)
{
  if ( popup_dragging) return;

  var ie      = navigator.appName == 'Microsoft Internet Explorer';
  var mouseX  = ie ? window.event.clientX : e.clientX;
  var mouseY  = ie ? window.event.clientY : e.clientY;
  var element = document.getElementById(popup_target);

  if ( ie) if (window.event.button != 1) return;
  if (!ie) if (           e.button != 0) return;

  popup_dragging = true;
  popup_target   = this['target'];
  popup_mouseX   = mouseX;
  popup_mouseY   = mouseY;

  if ( ie) popup_oldfunction = document.onselectstart;
  if (!ie) popup_oldfunction = document.onmousedown;

  if ( ie) document.onselectstart = function () { return false; };
  if (!ie) document.onmousedown   = function () { return false; };
}

// ***** PopupMousemove *****

function PopupMousemove(e)
{
  if (!popup_dragging) return;

  var ie      = navigator.appName == 'Microsoft Internet Explorer';
  var mouseX  = ie ? window.event.clientX : e.clientX;
  var mouseY  = ie ? window.event.clientY : e.clientY;
  var element = document.getElementById(popup_target);

  element.style.left = (element.offsetLeft+mouseX-popup_mouseX)+'px';
  element.style.top  = (element.offsetTop +mouseY-popup_mouseY)+'px';
  popup_mouseX       =  mouseX;
  popup_mouseY       =  mouseY;
}

// ***** PopupMouseup *****

function PopupMouseup(e)
{
  if (!popup_dragging) return;

  var ie      = navigator.appName == 'Microsoft Internet Explorer';
  var mouseX  = ie ? window.event.clientX : e.clientX;
  var mouseY  = ie ? window.event.clientY : e.clientY;
  var element = document.getElementById(popup_target);

  popup_dragging = false;

  if ( ie) document.onselectstart = popup_oldfunction;
  if (!ie) document.onmousedown   = popup_oldfunction;
}

// ***** PopupExit *****

function PopupExit(e)
{
  var ie      = navigator.appName == 'Microsoft Internet Explorer';
  var mouseX  = ie ? window.event.clientX : e.clientX;
  var mouseY  = ie ? window.event.clientY : e.clientY;
  var element = document.getElementById(popup_target);

  PopupMouseup(e);
  element.style.display = 'none';
}

// ***** PopupShow ******

function PopupShow(id, drag_id, exit_id, position, x, y, position_id)
{
  var      element = document.getElementById(     id);
  var drag_element = document.getElementById(drag_id);
  var exit_element = document.getElementById(exit_id);
  var width        = window.innerWidth  ? window.innerWidth  : document.documentElement.clientWidth;
  var height       = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight;

  if (position == 'tag' || position == 'tag-right' || position == 'tag-bottom')
  {


/*
    var p0 = document.getElementById(position_id);
    if (position == 'tag-right' ) x += p0.clientWidth;
    if (position == 'tag-bottom') y += p0.clientHeight;
    for (var p = p0; p; p = p.offsetParent) if (p.style.position != 'absolute') x += p.offsetLeft;
    for (var p = p0; p; p = p.offsetParent) if (p.style.position != 'absolute') y += p.offsetTop;
*/

    var p0 = document.getElementById(position_id);
    if (position == 'tag-right' ) x += p0.clientWidth;
    if (position == 'tag-bottom') y += p0.clientHeight;

  for (var p = p0; p; p = p.offsetParent)
  {
    var style = p.currentStyle ? p.currentStyle : document.defaultView.getComputedStyle(p, null);

    if (style.position !== 'static') break;
    x += p.offsetLeft;
    y += p.offsetTop;
  }


    element.style.zIndex   =  cms_current_zIndex++;
    element.style.position = 'absolute';
    element.style.display  = 'block';
    element.style.left     =  x+'px';
    element.style.top      =  y+'px';
  }

  if (position == 'mouse')
  {
    element.style.position = 'absolute';
    element.style.display  = 'block';
    element.style.left     = (document.documentElement.scrollLeft+popup_pointX+x)+'px';
    element.style.top      = (document.documentElement.scrollTop +popup_pointY+y)+'px';
  }

  if (position == 'screen-top-left')
  {
    element.style.position = 'absolute';
    element.style.display  = 'block';
    element.style.left     = (document.documentElement.scrollLeft+(width -element.clientWidth )*0/2+x)+'px';
    element.style.top      = (document.documentElement.scrollTop +(height-element.clientHeight)*0/2+y)+'px';
  }

  if (position == 'screen-center')
  {
    element.style.position = 'absolute';
    element.style.display  = 'block';
    element.style.left     = (document.documentElement.scrollLeft+(width -element.clientWidth )*1/2+x)+'px';
    element.style.top      = (document.documentElement.scrollTop +(height-element.clientHeight)*1/2+y)+'px';
  }

  if (position == 'screen-bottom-right')
  {
    element.style.position = 'absolute';
    element.style.display  = 'block';
    element.style.left     = (document.documentElement.scrollLeft+(width -element.clientWidth )*2/2+x)+'px';
    element.style.top      = (document.documentElement.scrollTop +(height-element.clientHeight)*2/2+y)+'px';
  }

  drag_element['target']   = id;
  drag_element.onmousedown = PopupMousedownDragElement;
  exit_element.onclick     = PopupExit;
}

// ***** Attach Events *****

if (navigator.appName == 'Microsoft Internet Explorer')
     document.attachEvent   ('onmousedown', PopupMousedown);
else document.addEventListener('mousedown', PopupMousedown, false);

if (navigator.appName == 'Microsoft Internet Explorer')
     document.attachEvent   ('onmousemove', PopupMousemove);
else document.addEventListener('mousemove', PopupMousemove, false);

if (navigator.appName == 'Microsoft Internet Explorer')
     document.attachEvent   ('onmouseup', PopupMouseup);
else document.addEventListener('mouseup', PopupMouseup, false);


// ***** Dropdown **************************************************************

// ***** DropdownAux *****

function DropdownAux(parent, child)
{
  var p0  = document.getElementById(parent);
  var c0  = document.getElementById(child );

//  document.body.appendChild(c0);
  c0.style.position = 'absolute';
  c0.style.display  =    'block';

//  var x = (c0['dropdown_position'] == 'x') ? p0.offsetWidth +2 : 0;
//  var y = (c0['dropdown_position'] == 'y') ? p0.offsetHeight+2 : 0;

  var x = 0;
  var y = 0;
  var p = c0['dropdown_position'].split('-');

  if (p[0] === 'b') y +=  2+p0.offsetHeight;
  if (p[0] === 't') y += -2-c0.offsetHeight;
  if (p[0] === 'r') x +=  2+p0.offsetWidth;
  if (p[0] === 'l') x += -2-c0.offsetWidth;

  if (p[1] === 't') y +=  2+p0.offsetHeight;
  if (p[1] === 't') y += -2-c0.offsetHeight;
  if (p[1] === 'l') x +=  2+p0.offsetWidth;
  if (p[1] === 'l') x += -2-c0.offsetWidth;

  var p = document.getElementById('header2');


  for (var p = p0; p; p = p.offsetParent)
  {
    var style = p.currentStyle ? p.currentStyle : document.defaultView.getComputedStyle(p, null);

    if (style.position !== 'static') break;
    x += p.offsetLeft;
    y += p.offsetTop;
  }

  if ((p ? p.offsetLeft : 0)+x+c0.offsetWidth+50 > document.documentElement.scrollWidth)
        x += p0.offsetWidth-c0.offsetWidth;

  c0.style.zIndex   =  cms_current_zIndex++;
  c0.style.left     =     x+'px';
  c0.style.top      =     y+'px';
}

// ***** DropdownShow *****

function DropdownShow()
{
  var p = document.getElementById(this['dropdown_parent']);
  var c = document.getElementById(this['dropdown_child' ]);

  DropdownAux(p.id, c.id);
  clearTimeout(c['dropdown_timeout']);
}

// ***** DropdownHide *****

function DropdownHide()
{
  var p = document.getElementById(this['dropdown_parent']);
  var c = document.getElementById(this['dropdown_child' ]);

  c['dropdown_timeout'] = setTimeout('document.getElementById("'+c.id+'").style.display = "none"', 100);
}

// ***** DropdownClick *****

function DropdownClick()
{
  var p = document.getElementById(this['dropdown_parent']);
  var c = document.getElementById(this['dropdown_child' ]);

  if (c.style.display != 'none') DropdownAux(p.id, c.id); else c.style.display = 'none';
  return false;
}

// ***** DropdownAttach *****

function DropdownAttach(parent, child, showtype, position, cursor)
{
  var p = document.getElementById(parent);
  var c = document.getElementById(child);

  p['dropdown_parent']     = p.id;
  c['dropdown_parent']     = p.id;
  p['dropdown_child']      = c.id;
  c['dropdown_child']      = c.id;
  p['dropdown_position']   = position;
  c['dropdown_position']   = position;

  c.style.position = 'absolute';
  c.style.display  = 'none';

  switch (showtype)
  {
    case 'click':
      p.onclick     = DropdownClick;
      p.onmouseout  = DropdownHide;
      c.onmouseover = DropdownShow;
      c.onmouseout  = DropdownHide;
      break;
    case 'hover':
      p.onmouseover = DropdownShow;
      p.onmouseout  = DropdownHide;
      c.onmouseover = DropdownShow;
      c.onmouseout  = DropdownHide;
      break;
  }
}
