var basePath = "/java/php/";
$(document).ready(mainInit);

function mainInit(){
  makeStartTips();
}
function sendEMail(){
  var email= $("#eMail").val();
  if(!testEMail(email))
    createErrorTip("eMail", "Ââåäèòå êîððåêòíûé E-mail");
  else{
    var file = basePath + "sendemail.php";
    $.post(file,{
      type: "application/x-www-form-urlencoded",
      eMail: email
    },
    function(responseText){
      alert(responseText);
    }
  );}
}

function myGetTop(element){
  var iTop = 0;
  for(;;){
    if(($(element).css("position")) == "absolute"){
      iTop +=element.offsetTop;
      break;
    }
    if(element.tagName == 'BODY'){
      iTop +=element.offsetTop;
      break;
    }
    iTop +=element.offsetTop;
    element = element.offsetParent;
  }
  return iTop;
}
function myGetLeft(element){
  var iLeft = 0;
  for(;;){
    if(($(element).css("position")) == "absolute"){
      iLeft +=element.offsetLeft;
      break;
    }
    if(element.tagName == 'BODY'){
      iLeft += element.offsetLeft;
      break;
    }
    iLeft += element.offsetLeft;
    element = element.offsetParent;
  }
  return iLeft;
}

function popUp(el, elTo){
  this.marginTo = 0;
  this.positionType = 'bottom';
  this.speed=0;
  this.animateType ='height';
  this.el = $(el).get(0);
  this.elTo = $(elTo).get(0);
  this.topP = myGetTop($(elTo).get(0));
  this.leftP = myGetLeft($(elTo).get(0));
  return this;
}
popUp.prototype.setMarginTo = function(marginTo){
  this.marginTo = marginTo;
  return this;
};
popUp.prototype.setSpeed = function(speed){
  this.speed = speed;
  return this;
};
popUp.prototype.setAnimateType = function(animateType){
  switch(animateType){
  case 'height':
    this.animateType = 'height';
    break;
  case 'width':
    this.animateType = 'width';
    break;
  }
  return this;
}
popUp.prototype.setPositionType = function(positionType){
  switch(positionType){
  case 'top':
    this.positionType = 'top';
    break;
  case 'right':
    this.positionType = 'right';
    break;
  case 'bottom':
    this.positionType = 'bottom';
    break;
  case 'left':
    this.positionType = 'left';
    break;
  }
  return this;
}
popUp.prototype.show = function(){
  $(this.el).hide();
  $(this.el).css({
    position: 'absolute',
    top 	: this.topP + 'px',
		left	: this.leftP + 'px'
  });
  switch(this.positionType){
  case 'top':
    this.topP -= this.el.offsetHeight + this.marginTo;
    break;
  case 'right':
    this.leftP += this.elTo.offsetWidth + this.marginTo;
    break;
  case 'bottom':
    this.topP += this.elTo.offsetHeight + this.marginTo;
    break;
  case 'left':
    this.leftP -= this.el.offsetWidth + this.marginTo;
    break;
  }
  $(this.el).css({
    top 	: this.topP + 'px',
		left	: this.leftP + 'px'
  });
  switch (this.animateType){
    case 'height':
      $(this.el).animate({height: "show"}, this.speed);
    break;
    case 'width':
      $(this.el).animate({width: "show"}, this.speed);
    break;
    default: $(this.el).show(this.speed); break;
  }
  return this;
};
popUp.prototype.remove = function(){
  $(this.el).remove();
}
function createErrorTip(elId, text){
  var temp = $('<div class="errorPP" id="error'+elId+'"><div><div><div>îøèáêà:'+text+'</div></div></div></div>').appendTo("body");
  new popUp(temp, $("#"+elId).get(0)).setMarginTo(-7).show();
  $("#"+elId).click(function(){$("#error"+elId).remove();});
}

function testEMail (mail) {
  var re = /[0-9a-z_\.\-]+@[0-9a-z_\.\-]+\.[a-z]{2,4}/;
  return (re.test(mail));
}
function makeStartTips(){
  $("input[@alt]").each(function(i){
    var text = ($(this).attr("alt"));
    myBlurInput(this,text);
    $(this).click(function(){myClearInput(this,text);});
    $(this).blur(function(){myBlurInput(this,text);});
  });
}
function myClearInput(item, text){
  if ($(item).val() == text) $(item).val("");
}
function myBlurInput(item, text){
  if ($(item).val() == "") $(item).val(text);
}
