var Login_ValidateForm = function(login, password)
{
  if (login.length < 1) {
    alert('Введите имя пользователя!');
    return false;
  }
  if (password.length < 1) {
    alert('Введите пароль');
    return false;
  }
  if (!login.match(/^[a-z][a-z0-9-]+$/)) {
    alert('Неправильное имя пользователя');
    return false;
  }
  return true;
}

var Login_Submit = function()
{
  var login = $('_flogin').value;
  
  if (native_password) {
    var password = native_password; 
  } else {
    var password = $('_fpassword').value;  
  }
  if (!Login_ValidateForm(login, password)) {
    return false;
  }
  
  var remember = $('_fremember').value; 
  
  var url = '/login';
  var req = new Ajax.Request(url, {
    method: 'post',
    parameters: {_login: login, _password: password, _ajax: 1, _remember: remember},
    onCreate: function (t) {      
      var b = $('_floginbutton');
      b.style.background = 'gray';
      b.onclick = function() {
        alert('Подождите, запрос обрабатывается');
        return false;
      }
    },
    onComplete: function (t) {      
      $('_floginbutton').onclick = null;
    },
    onSuccess: function (t) { 
      // process result
      var result = t.responseJSON.result;
      if (result == "+logged") {
        var b = $('_floginbutton');
        b.style.background = 'white';
        b.disabled = true;
        //document.location.href="index.php";
        window.location.reload();
        document.location.reload();
      } else if (result == "-login") {
        alert('Неправильное имя пользователя!');
      } else if (result == "-password") {
        alert('Неправильный пароль!');
      } else if (result == '-banned') {
        alert('Ваш аккаунт заблокирован!');
      } else {
        alert('Вход не возможен!');
        alert(result);
      }
    }
  });
  return false;
}

var main_flash_show = 0;

function showMainFlash() {
  var ver_ch = DetectFlashVer(6, 0, 0);
  if (!ver_ch) {
    if(main_flash_show == 0) {
      flashStep(1);
      main_flash_show = 1;
      document.getElementById('main-banner').style.background = 'none';
      document.getElementById('botomLine').style.bottom = '-1px';
      document.getElementById('botomLine').style.bottom = '0';
    }
    else{
      main_flash_show = 0;
      var elements  = $$('div[id*="kadr_"]');
      for(var i=0;i<elements.length;i++) {
        elements[i].hide();
      }
      document.getElementById('main-banner').style.background = 'url(/themes/site/images/air-bubbles.jpg) no-repeat 515px bottom';
      document.getElementById('botomLine').style.bottom = '-1px';
      document.getElementById('botomLine').style.bottom = '0';
    }
  }
  else{
    var flash_code = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="982" height="270" title="Продажа бизнеса по шагам"><param name="movie" value="/themes/site/flash/biz.swf" /><param name="quality" value="high" /><param name="wmode" value="transparent" /><embed src="/themes/site/flash/biz.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="982" height="270"></embed></object>';
    if(main_flash_show == 0) {
      $('flash-footer').toggle();
      $('flash-footer').innerHTML = flash_code;
      main_flash_show = 1;
      document.getElementById('main-banner').style.background = 'none';
      document.getElementById('botomLine').style.bottom = '-1px';
      document.getElementById('botomLine').style.bottom = '0';
    }
    else {
      $('flash-footer').toggle();
      $('flash-footer').innerHTML = '';
      main_flash_show = 0;
      document.getElementById('main-banner').style.background = 'url(/themes/site/images/air-bubbles.jpg) no-repeat 515px bottom';
      document.getElementById('botomLine').style.bottom = '-1px';
      document.getElementById('botomLine').style.bottom = '0';
    }
  }
  return false;
}

function flashStep(step){
  var elements  = $$('div[id*="kadr_"]');
  for(var i=0;i<elements.length;i++) {
    elements[i].hide();
  }
  $('kadr_' + step).style.display = '';
}

function hideLayer(id) {
  $(id).hide();
}



function showLayer(id) {



  $(id).toggle();

}



var now_movepx = 250;

var bisy = 0;

function openMenu(id, vto) {
  var ie = (navigator.appName.indexOf ("Microsoft") != -1) ? 1 : 0;
  if(ie){
    var ie = (navigator.appVersion.indexOf ("MSIE 6.0") != -1) ? 1 : 0;
  }
  if (bisy) {
    //alert('занят');
    return false;
  }
  //alert('свободен');
  bisy = 1;
  //скроем меню

  var elements  = $$('ul[id*="submenu_id_"]');
  var elements1 = $$('li[id*="top_acttitle_"]');
  var elements2 = $$('li[id*="top_title_"]');
  for(var i=0;i<elements.length;i++) {
    elements[i].hide();
    elements1[i].hide();
    elements2[i].show();
  }
  if(0){  //вырубим катание  !ie
    //уменьшим  белую плашку до шарика и подвинем под старый пункт
    var attributes = {
      width: {  to: 6 },
      marginLeft:  { to: now_movepx }
    };
    var minSize = new YAHOO.util.Anim('top-navbar', attributes);
    //сейчас нужно подвинуть шарик на наш новый пункт меню.
    var attributes = {
      marginLeft: { to: vto }
    };
    var moveToNew = new YAHOO.util.Anim('top-navbar', attributes);
    //типа движение на новый пункт это следущая анимация
    minSize.onComplete.subscribe(function() {
      $('top-navbar').show();
      moveToNew.animate();
    });
    //сейчас нужно расширить плашку и показать новое меню
    var attributes = {
      width: { to: 500 },
      marginLeft:  { to: 0 }
    };
    var maxSize = new YAHOO.util.Anim('top-navbar', attributes);
    moveToNew.onComplete.subscribe(function() {
      maxSize.animate();
    });
    maxSize.onComplete.subscribe(function() {
      $('submenu_id_' + id).show();
      //покажем новый активный пункт
      $('top_title_' + id).hide();
      $('top_acttitle_' + id).show();
      now_movepx = vto;
      //разбизимся
      bisy = 0;
    });
    //типа движение на новый пункт это следущая анимация
    minSize.onComplete.subscribe(function() {
      moveToNew.animate();
    });
    //гоу
    minSize.animate();
  }
  else{
    $('submenu_id_' + id).show();
    //покажем новый активный пункт
    $('top_title_' + id).hide();
    $('top_acttitle_' + id).show();
    bisy = 0;
  }
}









//  Форма в фак

function faqForm() {

  $('_bnt_faq').value = "Передача...";
  $('_bnt_faq').disabled = "disabled";


  var force = true;
  var req = new Subsys_JsHttpRequest_Js();

  req.onreadystatechange = function() {
    if (req.readyState == 4) {
      if (req.responseJS) {
        $('faq-form').innerHTML = req.responseJS.q;
      }
      //$('debug').innerHTML = req.responseText;

    }
  }

  req.caching = false;
  req.open('POST', '/consultation/faq/ajax?manager=' + $('_manager').value, true);
  req.send({ fio : $('_fio').value, text : $('_text').value,  email : $('_email').value, phone: $('_phone').value, firm:$('_firm').value });

}



function faqItem(tag) {

  //var elements1 = $$('li[id*="_item_"]');

  //var elements2 = $$('div[id*="_act_item_"]');

  /*for(var i=0;i<elements1.length;i++)

  elements1[i].show();*/

  /*for(var i=0;i<elements2.length;i++)

  elements2[i].hide();*/

  $('_act_item_' + tag).toggle();

  //$('_item_' + tag).toggle();

}





function check_form() {



  if ($('_fio').value != "" && $('_text').value != "" && $('_email').value != "") {

    $('_bnt_faq').disabled = '';

  } else {

    $('_bnt_faq').disabled = 'disabled';

  }



}





/*

function switchProfile(alias)

{

var req = new Ajax.Request('/profile/' + alias, {

method: 'post',

parameters: {ajax: 1},

onSuccess: function (t) {

//alert(t.responseJSON.map);

document.getElementById("profile-content").innerHTML=t.responseJSON.result;

}

});

}

*/





function hideAll() {



  var elements = $$('div[id*="_act_item_"]');

  for(var i=0;i<elements.length;i++){

    elements[i].hide();
  }
  $('reveal-all').innerHTML = '<img src="/themes/site/images/open.png" alt="Раскрыть все" /> <a href="#javascript;" onclick="showAll(); return false;">Раскрыть все</a>';

}



function showAll() {



  var elements = $$('div[id*="_act_item_"]');

  for(var i=0;i<elements.length;i++){
    elements[i].show();
  }
  $('reveal-all').innerHTML = '<img src="/themes/site/images/collapse.png" alt="Свернуть все" /> <a href="#javascript;" onclick="hideAll(); return false;">Свернуть все</a>';

}



function newsItem(tag) {

  var elements1 = $$('li[id*="_item_"]');

  var elements2 = $$('li[id*="_actitem_"]');

  var elements3 = $$('ul[id*="_subnews_"]');


  var i=0
  for(i = 0;i<elements1.length;i++) {

    elements1[i].show();

    elements2[i].hide();

    elements3[i].hide();

  }



  $('_actitem_' + tag).toggle();

  $('_item_'     + tag).toggle();

  $('_subnews_'  + tag).toggle();

      document.getElementById('botomLine').style.bottom = '-1px';
      document.getElementById('botomLine').style.bottom = '0px';

}





function delDemand(id)

{

  var req = new Ajax.Request('/profile/deldemand', {

    method: 'post',

    parameters: {id: id},

    onSuccess: function (t) {

      //alert(t.responseJSON.map);

      document.getElementById("demand_" + id).style.display='none';

    }

  });

}




//document.documentElement.scrollTop
function mapAction(alias) {

  var elements1 = $$('a[id*="map_"]');
  var elements2 = $$('div[id*="info_"]');
  var elements3 = $$('div[id*="flasht_"]');
  var elements4 = $$('div[id*="mng_"]');

  for(var i=0;i<elements1.length;i++) {
    elements1[i].removeClassName('active-city');   
  }
  
  for(var i=0;i<elements2.length;i++) {
    elements2[i].hide();
  }

  for(var i=0;i<elements3.length;i++) {
    elements3[i].removeClassName('red');
    elements3[i].removeClassName('gray');
    elements3[i].addClassName('gray');
  }


  for(var i=0;i<elements4.length;i++) {
    elements4[i].style.display='none';
  }
  
  if ($('mng_' + alias)) {
    $('mng_' + alias).style.display='';
  }
  
  if ($('map_' + alias)) {
    $('map_' + alias).addClassName('active-city');    
  }  
 
  if ($('info_' + alias)) {
    $('info_' + alias).show();
  }

  if (alias == 'ekb' || alias == 'moskva' || alias == 'krasnoyarsk') {
    $('flasht_'  + alias).removeClassName('gray');
    $('flasht_'  + alias).addClassName('red');
  }
}

/*Event.observe(document.body, 'click', function(event) {
  var element = $(Event.element(event));
});*/

/*$('area6').onclick = function(e) {actionReg('reg6', e); }
$('area5').onclick = function(e) {actionReg('reg5', e); }
$('area4').onclick = function(e) {actionReg('reg4', e); }
$('area3').onclick = function(e) {actionReg('reg3', e); }
$('area2').onclick = function(e) {actionReg('reg2', e); }
$('area1').onclick = function(e) {actionReg('reg1', e); }

Event.observe($('area6'),'onclick', actionReg, false);*/

/*Event.observe($('area6'), 'click', function(event) {
  actionReg('reg6', event)
});
Event.observe($('area5'), 'click', function(event) {
  actionReg('reg5', event)
});
Event.observe($('area4'), 'click', function(event) {
  actionReg('reg4', event)
});
Event.observe($('area3'), 'click', function(event) {
  actionReg('reg3', event)
});
Event.observe($('area2'), 'click', function(event) {
  actionReg('reg2', event)
});
Event.observe($('area1'), 'click', function(event) {
  actionReg('reg1', event)
});*/

/*var mCur;
document.onmousemove = function(e){ mCur = mousePageXY(e);};*/

function lightReg(reg) {

  for(var i=1;i<7;i++) {  
    if ('reg'+i != active_region) {
      $('reg'+i).hide();
    }
  }
  $(reg).show(); 
}

var active_region = "";
function actionReg(reg) {
  
  
  var elements  = $$('#map-layer div[id*="reg"]');
  var elements2 = $$('#map-layer div[id*="layer_cities_"]');
  
  for(var i=0;i<elements.length;i++) {
    elements[i].hide();   
  }
  
  for(var i=0;i<elements2.length;i++) {
    elements2[i].hide();   
  }
    
  if (reg) {
  
    /*var y = mCur.y - $('map-layer').style.top;
    var x = mCur.x - $('map-layer').style.left;*/
  
    /*$('layer_cities_' + reg).top = y + 'px'; 
    $('layer_cities_' + reg).left = x + 'px';*/
    $('layer_cities_' + reg).show(); 
    $(reg).show();  
    
    active_region = reg;
  }
}

function closeCitiesList(district_num) {  
 
  if ($('layer_cities_' + district_num)) {
    $('layer_cities_' + district_num).hide(); 
  }
  active_region = '';
}

function mousePageXY(e)
{
  
  var x = 0, y = 0;

  if (!e) e = window.event;

  if (e.pageX || e.pageY)
  {
    x = e.pageX;
    y = e.pageY;
  }
  else if (e.clientX || e.clientY)
  {
    x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
    y = e.clientY + (document.documentElement.scrollTop || document.body.scrollTop) - document.documentElement.clientTop;
  }

  return {"x":x, "y":y};
}


function animateEnter(vfrom, vto, open) {

  if(open == 1){
    $('user_panel').className = 'container-open';
  }

  var attributes = {

    right: { from: vfrom, to: vto }

  };

  var anim = new YAHOO.util.Anim('right-green', attributes);

  anim.animate();

  if(open == 0){
    setTimeout(closeContainer, 1000);
  }
}

function closeContainer(){
  $('user_panel').className = 'container-close';
}



function ch_search2() {
  if (document.getElementById('search_field').value == 'Поиск по сайту') {
    document.getElementById('search_field').value = '';
    //document.getElementById('search_field').style.color = '#000000';
  }
}

function drop_search2() {
  if (document.getElementById('search_field').value == '') {
    //document.getElementById('search_field').style.color = '#666666';
    document.getElementById('search_field').value = 'Поиск по сайту';
  }
}

function smaj(id,title)
{
  var req = new Ajax.Request('/sitemap/category', {
    method: 'post',
    parameters: {id: id},
    onSuccess: function (t) {
      //alert(t.responseJSON.map);
      document.getElementById("categs").innerHTML = t.responseJSON.result;
      document.getElementById("citycat").innerHTML = title;
      $('city-layer').hide();
    }
  });
}

function mngpr(type,id)
{
  switch(type){
    case 'cur' :
	    document.getElementById(id + "_prcur").className='active-item';
	    document.getElementById(id + "_prarch").className='menu-item';
	    document.getElementById(id + "_prcurlist").style.display='';
	    document.getElementById(id + "_prarchlist").style.display='none';
    break;
    case 'arch':
	    document.getElementById(id + "_prcur").className='menu-item';
	    document.getElementById(id + "_prarch").className='active-item';
	    document.getElementById(id + "_prcurlist").style.display='none';
	    document.getElementById(id + "_prarchlist").style.display='';
  }
}




function delHistory(id)
{
  var req = new Ajax.Request('/profile/delhistory', {
    method: 'post',
    parameters: {id: id},
    onSuccess: function (t) {
      //alert(t.responseJSON.map);
      document.getElementById("hist_" + id).style.display='none';
    }
  });
}

function getBodyScrollTop()
{
	return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}


function openMap() {
  
  if ($('map-layer').style.display == 'none') {
    //alert(document.documentElement.scrollTo);
    var top = getBodyScrollTop() - 50;
    if (top < 0) {
      top = 0
    }
    $('map-layer').style.top = top + 'px';
  }
  $('map-layer').toggle();
}

