$(function () {
  $('a.show-all').click(function(){
    $(this).siblings('ul.hide').show();
    $(this).remove();
    return false;
  });


  $('#works-gallery').elastislide({
    imageW:366,
    onClick:function ($item) {
      window.location.href = $item.find('a').attr('href');
    }
  });

  $('section.about').find('a').bind('click', function () {
    $(this).siblings('p.hidden').toggle();
    return false;
  });

  $('div.agency h2 a').click(function () {
    $(this).closest('h2').next('div').toggle();
    return false;
  });

  //точки на карте "Агентство - коллектив - по месту рождения
  $('#russia-map').find('img').hover(
      function () {
        $('span', this.parentNode).stop().show().fadeTo(300, .75)
      },
      function () {
        $('span', this.parentNode).stop().fadeTo(300, 0, function () {
          $(this).hide()
        })
      }
  );

  //точки на карте "Агентство - коллектив - по месту обитания
  $('#moscow-map').find('img.dot').hover(
      function () {
        $('div:first', this.parentNode).stop().show().fadeTo(300, 0.75);
        $('img:first', this.parentNode).show();
      },
      function () {
        $('div:first', this.parentNode).stop().fadeTo(300, 0.0, function () {
          $(this).hide()
        });
        $('img:first', this.parentNode).hide();
      }
  );

  var office = $('#moscow-map').find('div.office').position();
  $('#moscow-map').find('img.path').each(
      function () {
        var place = $(this).parent().position();

        //вычисляем конечные координаты пути до офиса относительно точки начала пути (дома)
        var x = (place.left + 8) - (office.left + 10);
        var y = (place.top + 8) - (office.top + 10);

        //смещения линии относительно нулевых координат иконки офиса, чтобы путь не приходил в эти самые координаты
        var offsetLeft = 0;
        var offsetTop = 0;

        var text = $(this).siblings('div');

        if (x < 0 && y <= 0)
          text.css('top', 5 - text.height() + 'px')

        if (x < 0 && y > 0 && y <= 10)
          text.css('top', '5px')

        //вычисление смещения
        if (x > 0)
          offsetLeft = -x;
        if (y > 0)
          offsetTop = -y;

        //рисование линии
        this.src = '/site/img/line.php?x=' + x + '&y=' + y;

        $(this).css({top:offsetTop + 8 + 'px', left:offsetLeft + 8 + 'px'});
        //$(this).hide();
      }
  );


/*  if (!!document.getElementById('g-map') && GBrowserIsCompatible()) {
    var gmap = new GMap2(document.getElementById('g-map'), {size:new GSize(720, 500)});
    var garinPoint = new GLatLng(55.807799, 37.51179);
    var garinIcon = new GIcon({image:'/images/gmap-garin.png', shadow:'/images/gmap-garin-shadow.png'});
    var garinMarker = new GMarker(garinPoint, {title:'Garin Studio', icon:garinIcon});

    gmap.setUIToDefault();
    gmap.setCenter(garinPoint, 15);
    gmap.addOverlay(garinMarker);


    GEvent.addListener(garinMarker, 'click', function () {
      this.openInfoWindowHtml('<h1>Garin Studio</h1><p>Балтийская ул., д. 5, 4-й этаж</p><p>(495) 223-32-57</p>');
    });

    document.onunload = GUnload;
  } */

  // Map
  if($('#garin-map').length) {
    var gmOptions = {
      center: new google.maps.LatLng(55.807799, 37.51179),
      zoom: 15,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    var map = new google.maps.Map(document.getElementById("garin-map"),gmOptions);
    var contentString = '<h1>Garin Studio</h1><p>Балтийская ул., д. 5, 4-й этаж</p><p>(495) 223-32-57</p>';
  
    var infowindow = new google.maps.InfoWindow({
      content: contentString
    });

    var marker = new google.maps.Marker({
      position: new google.maps.LatLng(55.807799, 37.51179),
      map: map,
      title:"Garin Studio"
    });
 
    google.maps.event.addListener(marker, 'click', function() {
      infowindow.open(map,marker);
    });
  }

  try {
    $('.works-roller').workspinner();
  } catch (e) {
  }

});






