var is_ie = (/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent));
function GID(id) { return (is_ie) ? document.all[id] : document.getElementById(id); }
var BrUserAgent = navigator.userAgent.toLowerCase();
var BrUserVersion = parseInt(navigator.appVersion);
var browser = '';
if(BrUserAgent.indexOf('opera')!=-1){browser = 'Opera';}else if(BrUserAgent.indexOf('msie')!=-1){browser = 'Explorer';}else if(BrUserAgent.indexOf('mozilla')!=-1){browser = 'Mozilla';}else{browser = 'Unknown';}

$(document).ready(function(){
  $('.mtItem').each(function(){
    var pos = $(this).offset();
    $(this).find('.mtSubMenu').css({left: pos.left + 20, top: pos.top + 18});
  }).mouseover(function(){
    $(this).find('.mtSubMenu').show();
  }).mouseleave(function(){
    $(this).find('.mtSubMenu').hide();
  });
  $('.mtSubItem').css({cursor: 'pointer'}).click(function(){
    location.href = $(this).find('A').attr('href');
  });
  var tompContentHeight = $('.tompContent TABLE').height();
  //alert(tompContentHeight);
  $('.tomp').css({cursor: 'pointer'}).click(function(){
    if($('.tompContent').height() > 1)
    {
      $('.tompContent').stop().animate({height: 1, opacity: 0}, 400, function(){$(this).hide();});
      $('.tomp').css({backgroundImage: 'url(image/des3/i-razvernut.gif)'}).html('Развернуть описание');
    }
    else
    {
      $('.tompContent').show().stop().animate({height: tompContentHeight, opacity: 1}, 400);
      $('.tomp').css({backgroundImage: 'url(image/des3/i-svernut.gif)'}).html('Свернуть описание');
    }
  });
  //$('.box').hide();
  
  if($.browser.msie)
  {
    $('.box').css({position: 'absolute', right: '0px', bottom: '0px'});
  }
  else
  {
    $('html, body').css({height: '100%', overflow: 'auto', 'vertical-align': 'top'});
    $('.wrapper').css({position: 'relative', width: '100%', height: '100%', overflow: 'auto'});
    $('.box').css({position: 'fixed', right: '0px', bottom: 0});
    //$('* html .box').css({position: 'absolute'});
  }
  
});

function commitFlashObject(_obj, _container) {
   var _output = "";
   var _paramoutput = "";
   for (_cO in _obj) {
      if (_cO != "movie" && _cO != "FlashVars" && _cO != "wmode" && _cO != "quality") _output += _cO+"=\""+_obj[_cO]+"\" ";
      if (_cO != "data" && _cO != "width" && _cO != "height") _paramoutput += "<param name=\""+_cO+"\" value=\""+_obj[_cO]+"\" />\n";
   }
   var ihtm = "<object type=\"application/x-shockwave-flash\" "+_output+">\n";
   ihtm += _paramoutput;
   ihtm += "</object>\n";
   GID(_container).innerHTML = ihtm;
}

function showFlash(path, width, height, div, vars, wmode) {
   var myFlashObject = new Object;
   myFlashObject.movie = path;
   myFlashObject.data = path;
   myFlashObject.quality = "high";
   if (width) myFlashObject.width = width;
   if (height) myFlashObject.height = height;
   if (vars != "") myFlashObject.FlashVars = vars;
   if (wmode != "") myFlashObject.wmode = wmode;
   commitFlashObject(myFlashObject, div);
}

function checkFlash() {
   var flash_versions = 20; var flash_installed = 0; var flash_version = '0.0'; var java_installed = 0;
   if (navigator.plugins && navigator.plugins.length) {  // Netscape style plugin detection
      for (x = 0; x < navigator.plugins.length; x++) {
         if (navigator.plugins[x].name.indexOf('Shockwave Flash') != -1) {
            flash_version = navigator.plugins[x].description.split('Shockwave Flash ')[1];
            flash_installed = 1;
            break;
         }
      }
      for (x = 0; x < navigator.plugins.length; x++) {
         if (navigator.plugins[x].name.indexOf('Java(TM)') != -1) {
            java_installed = 1;
            break;
         }
      }
   }
   else if (window.ActiveXObject) {  // ActiveX style plugin detection
      for (x = 2; x <= flash_versions; x++) {
         try {
            oFlash = eval("new ActiveXObject('ShockwaveFlash.ShockwaveFlash." + x + "');");
            if (oFlash) {
               flash_installed = 1;
               flash_version = x + '.0';
            }
         }
         catch(e) { }
      }
   }
   return flash_installed;
}

var isFlash = checkFlash();

function fnCheckSearchForm(f) {
   if (f.search.value.length < 1) {
      alert("Укажите минимальную стоимость!");
      f.price_min.focus();
      return false;
   }
   return true;
}
function fnCheckSearch2Form(f) {
   if (f.search.value.length < 1) {
      alert("Укажите поисковый запрос!");
      f.search.focus();
      return false;
   }
   return true;
}
var map;
var mapCoords = new Array();
var marker = new Array();
var gmStartPoint = [0, [55.7086478, 37.6309946, 10], [59.9390390, 30.3157850, 11], [56.838056, 60.597259, 11]];
function onLoad(domen)
{
  if(!(q = GID('mapgoogle')))
    return false;
  if (GBrowserIsCompatible())
  {
    var iconEvo = new GIcon(G_DEFAULT_ICON);
    iconEvo.image = "image/des3/map-i-firm.png";
    iconEvo.shadow = "image/des3/map-i-firm-shadow.png";
    iconEvo.iconSize = new GSize(37, 44);
    iconEvo.shadowSize = new GSize(37, 44);
    markerEvoOptions = { icon:iconEvo};
    var iconDefault = new GIcon(G_DEFAULT_ICON);
    iconDefault.image = "image/des3/map-i-other.png";
    iconDefault.shadow = "image/des3/map-i-other-shadow.png";
    iconDefault.iconSize = new GSize(24, 29);
    iconDefault.shadowSize = new GSize(24, 29);
    markerOtherOptions = { icon:iconDefault};
    /* Создание объекта карты, googlemap - имя, по которому находится объект карты, описываемый ниже*/
    map = new GMap2(document.getElementById("mapgoogle"));
    map.enableScrollWheelZoom();

    map.addControl(new GLargeMapControl3D());
    map.addControl(new GMapTypeControl());
    //map.addControl(new GScaleControl());

    /* Центрировать карту по созданной точке, установить масштаб и сделать начальным видом - космическую съемку, а не карту*/
    map.setCenter(new GLatLng(gmStartPoint[domen][0], gmStartPoint[domen][1]), gmStartPoint[domen][2]);
    //map.addOverlay(new GMarker(new GLatLng(55.7672433, 37.6509946), markerOptions));
    coordsNum = 0;
    for(q = 0; q < sellPointsEvoCount; ++q)
    {
      mapCoords[coordsNum] = new GLatLng(sellPointsEvo[q][0], sellPointsEvo[q][1]);
      map.addOverlay(marker[coordsNum] = new GMarker(mapCoords[coordsNum], markerEvoOptions));
      content = getTooltipContent(coordsNum, sellPointsEvo[q][2]);
      marker[coordsNum].bindInfoWindowHtml(content);
      ++coordsNum;
      //if(q == 0) w.click = function(){ this.showMapBlowup(); }
    }
    coordsNum = sellPointsEvoCount;
    for(q = 0; q < sellPointsOtherCount; ++q)
    {
      mapCoords[coordsNum] = new GLatLng(sellPointsOther[q][0], sellPointsOther[q][1]);
      map.addOverlay(marker[coordsNum] = new GMarker(mapCoords[coordsNum], markerOtherOptions));
      content = getTooltipContent(coordsNum, sellPointsOther[2]);
      marker[coordsNum].bindInfoWindowHtml(content);
      ++coordsNum;
    }
  }
}//onLoad
function getTooltipContent(num)
{
  return GID('google_marker_'+num).innerHTML.replace(/\<a /im, '<span style="color: #3287C1;" ').replace(/\<\/a\>/im, '</span>').replace('<!-- [name]', '').replace('[name] -->', '').replace(/\<\!\-\-/im, '').replace(/\-\-\>/im, '');
}//getTooltipContent
function zoomIn(id1, id2, size, sizeB)
{
  $('#'+id1).css('z-index', '40').animate({width: sizeB[0], height: sizeB[1], opacity: 0}, 300);
  $('#'+id2).css('z-index', '41').css('visibility', 'visible').animate({width: sizeB[0], height: sizeB[1], opacity:1}, 300);
}//zoomIn
function zoomOut(id1, id2, size, sizeB)
{
  $('#'+id1).css('z-index', '30').animate({width: size[0], height: size[1], opacity:1}, 300);
  $('#'+id2).css('z-index', '31').animate({width: size[0], height: size[1], opacity: 0}, 300, false, function(){$('#'+id2).css("visibility", "hidden")});
}//zoomOut

function updateCalendar()
{
  var month = GID('month').value;
  var year = GID('year').value;
  var req = new JSHttpRequest();
  req.onreadystatechange = function() {
    if (req.readyState == 4)
    {
      //var e = GID('div_calendar');
      var e = GID('div_calendar');
      if(is_ie)
      {
        var child;
        while (child = e.firstChild)
          e.removeChild(child);
        e.innerHTML = '';
        var newdiv = document.createElement("div");
        newdiv.style.display = 'inline';
        newdiv.innerHTML = req.responseJS.res;
        e.appendChild(newdiv);
      }
      else
        e.innerHTML = req.responseJS.res;
      GID('id_calendar').style.display = 'block';
    }
  }
  req.caching = false;
  req.open('GET', 'calendar.htm', true);
  req.send({ m: month, y: year });
}

function clickMapEvoi(link, div, col)
{
  if($('#'+div).css('display') == 'none')
  {
    $('#'+div).show();
    $('#'+link).css({color: col, 'text-decoration': 'none'});
  }
  else
  {
    $('#'+div).hide();
    $('#'+link).css({color: '#3783BF', 'text-decoration': 'underline'});
  }
  $('#'+link).blur();
  return false;
}
