function animator()
// Make one step in animation - deselect one item, select another
{
  var el=document.animated.pop();
  if (el)
    el.removeClassName('preview_link_image_animated');
  if (document.toAnimate.length)
  {
    el=document.toAnimate.pop();
    document.animated.push(el);
    el.addClassName('preview_link_image_animated');
    setTimeout(animator, 3000);
  }
}

function animateButtons()
// Prepare blink items list and start timer
{
  document.toAnimate=$$('.animateMe').reverse();
  document.animated=[];

  if (document.toAnimate.length)
    animator();
}


function bindAndFire(fieldId, func)
// bind onchange and onclick function and run it
{
  var f=$(fieldId);
  f.onchange=f.onclick=function(event)
  {
    if (! event) {event = window.event; } // For IE
    func(Event.element(event));
  };
  func(f);
}


function pagesFields(fieldId, data)
// Serve pageForm
{
  bindAndFire(fieldId, function (element)
  {
    var current=data[element.value];
    data.All.each(function(f)
    {
      if (current.indexOf(f) == -1)
        Element.hide($(f).parentNode.parentNode);
      else
        Element.show($(f).parentNode.parentNode);
    } );
  } );
}

function imagesFields(fieldId, pictureId, data)
// Serve imageForm
{
  var picture=$(pictureId);
  bindAndFire(fieldId, function (element)
  {
    picture.src=data[element.value];
  } );
}

function mouseover(divID, elements, getMO)
// divID - DOM ID for Mouseover element
// elements - list of elements, which will have mouseovers
// getMO - callback function (element, mouseover)
{
  this.timeout=700;
  this.div=$(divID);
  this.getMO=getMO;

  this.launch=0;
  this.nexttext='';
  this.nextwidth=0;
  this.nextx=0;
  this.nexty=0;
  this.immediate=false;

  // callbacks
  var self=this;
  var cbMouseoverFuncDelay=function() { self.immediate=false; self.mouseoverFunc(this); };
  var cbMouseoverFuncNow=function() { self.immediate=true; self.mouseoverFunc(this); return false; };
  var cbMouseOut=function() { self.mouseOut(); };
  var delay=function(showAction, launch) { setTimeout(function() { self.showHide(showAction, launch); }, self.timeout); };

  // set callbacks
  this.div.onmouseover=function() { self.launch++; };
  this.div.onmouseout=cbMouseOut;
  $A(elements).each(function(a) {
    a.onmouseover=cbMouseoverFuncDelay;
    a.onmouseout=cbMouseOut;
    if (a.hasClassName('onlymouseover'))
      a.onclick=cbMouseoverFuncNow;
  });

  this.mouseoverFunc=function(element, delayed)
  {
    this.launch++;

    var o=element.cumulativeOffset();    var d=element.getDimensions();
    this.nextx=o[0] + (d.width / 2);
    this.nexty=o[1];

    if (this.getMO && (this.getMO(element, this) === false))
      return;
    this.show();
  };

  this.mouseOut=function()
  {
    this.launch++;
    delay(false, this.launch);
  };

  this.show=function()
  {
    if (this.immediate)
      self.showHide(true, this.launch);
    else
      delay(true, this.launch);
  };

  this.showHide=function(showAction, launchNo)
  // Effects is provided by script.aculo.us
  {
    if (launchNo != this.launch)
      return;

    if (showAction)
    {
      // Set text
      this.div.innerHTML=this.nexttext;
      this.nextwidth=(this.nextwidth ? this.nextwidth : 250);

      // Correct and set coordinates
      var d=this.div.getDimensions();
      this.div.style.left=((this.nextx + d.width) < document.viewport.getWidth() ? this.nextx : (document.viewport.getWidth() - d.width - 10)) +'px';
      this.div.style.top=((this.nexty - d.height) > 0 ? (this.nexty - d.height) : 10) + 'px';

      Effect.Appear(this.div);
    }
    else
      Effect.Fade(this.div);
  };
}

function listMouseover(divID, list)
{
  var elements=$H(list).keys().map(function(v) {return $(v);});

  return new mouseover(divID, elements, function(element, mouseover) {
    if (typeof(list[element.id]) != 'undefined')
      mouseover.nexttext=list[element.id];
    else
      return false;
  });
}

function ajaxMouseover(divID, AJAXurl, elements)
{
  if (typeof(elements) == 'undefined')
    elements=$$('A');
  elements=elements.findAll(function(el) { return (el.rel != 'nomouseover'); });

  return new mouseover(divID, elements, function(element, mouseover) {
    if (typeof(element.data) == 'undefined')
    {
      new Ajax.Request(AJAXurl, { method: 'get',
        parameters: {
          'id': element.href,
          'rel': element.rel
        },
        onSuccess: function(transport) {
          element.data=transport.responseJSON;
          if (element.data)
          {
            mouseover.nextwidth=element.data[0];
            mouseover.nexttext=element.data[1];
            mouseover.show();
          }
        }
      });

      return false;
    }

    if (! element.data)   // Don't show empty bars
      return false;

    mouseover.nextwidth=element.data[0];
    mouseover.nexttext=element.data[1];
  });
}


function AOcalculator(ID, IA, PD, PA, AO)
{
  var self=this;
  self.ID=$(ID);  self.IA=$(IA);  self.PD=$(PD);  self.PA=$(PA);  self.AO=$(AO);

  self.calc=function()
  {
    if (self.ID.value > self.PD.value)
      self.AO.innerHTML=self.IA.value;
    else
      self.AO.innerHTML=self.IA.value - self.PA.value;
    if (self.AO.innerHTML < 0)
      self.AO.innerHTML = 0;
  };

  self.ID.onchange=self.IA.onkeyup=self.PD.onchange=self.PA.onkeyup=self.calc;
  self.calc();
}


function lockConflicts(data)
{
  data.keys().each(function(key)
  {
    w=$('wizard_'+key);
    if (w)
    w.onchange=w.onclick=function()
    {
      var checked=this.checked;
      data.get(key).each(function(id)
      {
        var el=$('wizard_'+id);
        if (el)
          el.disabled=checked;
      });
    };
    if (w && w.checked)
      w.onchange();
  });
}

function selectProvince(country_dd, province_dd, province, url, level)
{
  var self=this;
  self.country_dd=$(country_dd);  self.province_dd=$(province_dd);  self.province=$(province);
  self.url=url;  self.level=level;

  self.getContainer=function(el)
  {
    for (var i=0; i<self.level; i++)
      el=el.parentNode;
    return el;
  };

  self.getProvincies=function()
  {
    new Ajax.Updater(self.province_dd, self.url, {
      parameters: { id: self.country_dd.value },
      onComplete: self.updateProvincies
    });
  };

  self.updateProvincies=function()
  {
    if (self.province_dd.options.length)
    {
      var selected=parseInt(self.province.value, 10);
      if (selected)
        self.province_dd.value=selected;
      Element.show(self.getContainer(self.province_dd));
    }
    else
      Element.hide(self.getContainer(self.province_dd));
    self.setProvince();
  };

  self.setProvince=function()
  {
    if (self.province_dd.value === 0)
    {
      self.province.value='';
      Element.show(self.getContainer(self.province));
    }
    else
    {
      Element.hide(self.getContainer(self.province));
      self.province.value=self.province_dd.value;
    }
  };

  self.country_dd.onchange=self.country_dd.onkeyup=self.getProvincies;
  self.province_dd.onchange=self.province_dd.onkeyup=self.setProvince;
  self.getProvincies();
}

function doAjaxVnRequest(data)
{
  if (typeof(data) == 'undefined')
    data={};
  data.id=document.vnconfig.id;
  new Ajax.Updater(document.vnconfig.divID, document.vnconfig.url, {
    method: 'post',
    parameters: data
  });
}

function changeLabelTxt(on)
{
	var helpLabels = document.getElementsByTagName('label');
	
	var lab = null;
	for (var i = 0; i < helpLabels.length; i++)
		if (helpLabels[i].getAttribute('for') == 'wizard_serviceLongtermNumber_conf_addvn')
			lab = helpLabels[i];

	if (lab)
		if (on) 
			lab.innerHTML = 'Select a Direct Virtual Phone Number.<br/>Choose the Area Code, Region, or City';
		else
			lab.innerHTML = 'Select a Direct Virtual Phone Number.<br />Start by clicking on your country of choice or "others", to see more available countries.';	

}

function onLoad()
{
  // For menu
  Prototype.Browser.IE6=Prototype.Browser.IE &&
    parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE") + 5), 10) == 6;
  Prototype.Browser.IE7=Prototype.Browser.IE && !Prototype.Browser.IE6;

  if (Prototype.Browser.IE7)
  {
    var menu = $$('.li_panel', '.li_panel2');
    for(i=0; i<menu.length; i++)
    {
      menu[i].observe('mouseover', function() { this.addClassName('hover'); });
      menu[i].observe('mouseout', function() { this.removeClassName('hover'); });
    }
  }

  if (window.colorSelectors)
  {
    window.colorSelectors.each(function(id)
    {
      new Control.ColorPicker(id, {IMAGE_BASE : "/images/colorpicker/"});
    });
  }

  // Run other functions if need
  if (window.afterOnLoad)
    window.afterOnLoad();
}

window.onload=onLoad;
