$('document').ready(function() {
  
  /* CHROME DETECTING */
  var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;

  /* GOOGLE MAP INITIALIZE */
  if ($("#mapa").attr("id")=='mapa')
    initialize();

  /* HOMEPAGE GRAYSCALE LOGOS */
  if ($('div.opacityClients').attr('class')== 'opacityClients') {
    if (!is_chrome)
    grayscale( $('.opacityClients img') );
    $('.opacityClients img').fadeTo('fast', 0.35);
    $('.opacityClients img').mouseover(function() { grayscale.reset( $(this) ); $(this).fadeTo('fast',1); }).mouseout(function() { grayscale( $(this) ); $(this).fadeTo('fast', 0.35);});
  };

  /* HOMEPAGE BANNER ROTATOR */
  if ($('div.homeBox').attr('class') == 'homeBox') {
    $('.homeBox').cycle({
      fx:         'scrollHorz',
      timeout:     7000,
      pager:      '#scroll',
      pagerAnchorBuilder: pagerFactory
    });
  }

  /* 3 BOX - SERVICES PAGE */
  $(".blueBox").mouseover(function() {
    $(".outsourcingBox").stop().animate({top:140}, {duration: 1000, easing: 'easeOutQuart'});
  }).mouseout(function() {
    $(".outsourcingBox").stop().animate({top:0}, {duration: 1000, easing: 'easeOutQuart'});
  });
  $(".violetBox").mouseover(function() {
    $(".hostingBox").stop().animate({top:140}, {duration: 1000, easing: 'easeOutQuart'});
  }).mouseout(function() {
    $(".hostingBox").stop().animate({top:0}, {duration: 1000, easing: 'easeOutQuart'});
  });
  $(".orangeBox").mouseover(function() {
    $(".webBox").stop().animate({top:140}, {duration: 1000, easing: 'easeOutQuart'});
  }).mouseout(function() {
    $(".webBox").stop().animate({top:0}, {duration: 1000, easing: 'easeOutQuart'});
  });

  if ($('#sliderImageSmall').attr('id')== 'sliderImageSmall' || $('#sliderImageSmall2').attr('id')== 'sliderImageSmall2') {
  $('#sliderImageSmall, #sliderImageSmall2').cycle({
    fx:     'scrollVert',
    speed:  500,
    timeout: 0,
    next:   '#go-prev3',
    prev:   '#go-next3'
  });
}

if ($('#sliderImageBig').attr('id')== 'sliderImageBig') {
$('#sliderImageBig').cycle({
  fx:     'scrollHorz',
  speed:  500,
  timeout: 0,
  next:   '#go-next3',
  prev:   '#go-prev3'
});
}

$('.prevButton img').css({'left':0});
$(".prevButton a").hover(function(){
  $("img", this).stop().animate({left:"-50px"},{queue:false,duration:200});
}, function() {
  $("img", this).stop().animate({left:"0px"},{queue:false,duration:200});
});

$('.nextButton img').css({'right':0});
  $(".nextButton a").hover(function(){
    $("img", this).stop().animate({right:"-50px"},{queue:false,duration:200});
  }, function() {
    $("img", this).stop().animate({right:"0px"},{queue:false,duration:200});
  });

});

function pagerFactory(idx, slide) {
  var s = idx > 2 ? ' style="display:none"' : '';
  return '<a '+s+' href="#"></a>';
};

var map;

function initialize() {
  function kolejnoscTworzenia(marker) {	return 0; }

  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("mapa"));
    var mapControl = new GHierarchicalMapTypeControl();

    map.setCenter(new GLatLng(52.180619,21.055486), 14);
    map.disableScrollWheelZoom();

    mapControl.clearRelationships();
    mapControl.addRelationship(G_SATELLITE_MAP, G_HYBRID_MAP, "Etykiety", true);

    map.addControl(mapControl);
    map.addControl(new GLargeMapControl3D());
        /*
    map.addOverlay(new GMarker(new GLatLng(52.180702,21.072024)));
    */


    var tvnIcon = new GIcon(G_DEFAULT_ICON);
    tvnIcon.image = "/content/images/icon_tvn.png";
    tvnIcon.iconSize = new GSize(30, 30);
    markerOptions = { icon:tvnIcon, zIndexProcess: 1};
    map.addOverlay(new GMarker(new GLatLng(52.178982,21.073463),{ icon:tvnIcon, zIndexProcess: kolejnoscTworzenia}));

    var mIcon = new GIcon(G_DEFAULT_ICON);
    mIcon.image = "/content/images/icon_nv_map.png";
    mIcon.iconSize = new GSize(54, 54);
    markerOptions = { icon:mIcon, zIndexProcess: 2};
    map.addOverlay(new GMarker(new GLatLng(52.1807562,21.072023),{ icon:mIcon, zIndexProcess: kolejnoscTworzenia}));

  }
}

function strpos(haystack, needle, offset) {
  var i = (haystack + '').indexOf(needle, (offset || 0));
  return i === -1 ? false : i;
}

function obliczOdleglosc() {
  var punkt = new GLatLng(52.1807562,21.072023);
  initialize();
  var adres = $("#adres").val();
  if (!strpos(adres,',')) adres = "Warszawa, "+adres;
  var trasa = new GDirections(map);
  GEvent.addListener(trasa, "load", function () {
    var dystans = trasa.getDistance().meters/1000;
    var wynik = document.getElementById('wynik');
    wynik.innerHTML = 'Odległość: ' + Math.round(dystans) + ' km';
  });
  trasa.load("from: "+adres+" to: "+punkt);
}


