// map
var gmap;
function initMap() {
  if ($('#map').length == 0) return;
  
  if (GBrowserIsCompatible()) {
    gmap = new GMap2(document.getElementById('map'));
    var LatLng = new GLatLng( 52.34664,-0.186231 ); 
    gmap.setCenter(LatLng, 14);
    if ($('li.map').length == 0) {
//      gmap.addOverlay(new GMarker(LatLng));
  		gmap.addControl(new GLargeMapControl3D());
  		gmap.addControl(new GHierarchicalMapTypeControl());
    }
  }
}

function initSearchMap() {
  if ($('#search-map').length == 0) return;
  
  if (GBrowserIsCompatible()) {
    gmap = new GMap2(document.getElementById('search-map'));
    var LatLng = new GLatLng( 52.34664,-0.186231 );
    gmap.setCenter(LatLng, 14); 
//    gmap.addOverlay(new GMarker(LatLng));
		gmap.addControl(new GLargeMapControl3D());
		gmap.addControl(new GHierarchicalMapTypeControl());
  }
}

// products catalogue
function initCatalogue() {
  $('#steps li').each(function (idx) {
    $(this).css('z-index', 100-idx);
  });

  $('.products li a').click(function() {
    var addr = $(this).attr('href');
    if ($(addr).length == 0) {
      return true;
    }

    var parentStep = $(this).closest('#steps>li');
    var nextStep = parentStep.next('li');
    var parentLi = $(this).parent('li');

    parentStep.nextAll('li.active').removeClass('active');
    nextStep.addClass('active');
    parentStep.nextAll('li').find('ul').removeClass('active');
    parentStep.nextAll('li').find('li').removeClass('active');

    parentLi.addClass('active');
    parentLi.siblings('li').removeClass('active');

    parentLi.find('')
    $(addr).addClass('active');
    return false;
  });
}

//image rotator
var imageCount;
var activeImage = 1;
var imageWidth = 702;
var rotateTime = 5000;
var rotateTimeout;
var autoRotate = true;

function showSlide(idx) {
  if (idx==activeImage) return false;
  
  $('#rotator .nav li').removeClass('active');
  $('#rotator .nav li.nav-'+idx).addClass('active');
  activeImage = idx;
  var offset = -(activeImage-1)*imageWidth;
  $('#rotator .slides').stop().animate({'left': offset+'px'}, 500);
  
  if (autoRotate)
    rotateTimeout = setTimeout('nextSlide()', rotateTime);
}

function nextSlide() {
  if (activeImage < imageCount)
    showSlide(activeImage+1);
  else
    showSlide(1);
}

function initRotator() {
  if ($('#rotator').length==0) return false;
  
  imageCount = $('#rotator .slides img').length;
  
  $('#rotator .slides img').each(function(idx) { $(this).attr('id', 'rotator-'+idx); });
  $('#rotator').append('<ul class="nav"></ul>');
  for (i=1; i<=imageCount; i++) {
    $('#rotator .nav').append('<li class="nav-'+i+'"><a rel="rotator-'+i+'"><span>'+i+'</span></a></li>')
  }
  $('#rotator .nav li:first-child').addClass('active');
  $('#rotator .nav li a').click(function() {
    autoRotate = false;
    clearTimeout(rotateTimeout);
    showSlide(parseInt($(this).attr('rel').replace('rotator-', '')));
    return false;
  });
  
  if (autoRotate)
    rotateTimeout = setTimeout('nextSlide()', rotateTime);
}

// image slider
var slideDistance = 658;

function slide(offset) {
  var pos = $('#slider .slides').scrollLeft();
  $('#slider .slides').animate({'scrollLeft': pos-offset}, 500);
}

function initSlider() {
  if ($('#slider').length==0) return false;
  
  slideDistance = $('#slider .slides').width();
  var nextLeft = 5;
  $('#slider .slides ul li').each(function() {
    $(this).css('left', nextLeft+'px');
    nextLeft += $(this).width()+10;
  });
//  $('#slider .slides ul li a').fancybox();
  $('#slider .left').click(function() { slide(slideDistance); return false; });
  $('#slider .right').click(function() { slide(-slideDistance); return false; });
}

// sliding addresses
var scrollDistance;
var scrollTops = [];
var activeScroll = 0;

function checkNav() {
  if (activeScroll==0) $('#addresses li.prev').addClass('inactive');
                  else $('#addresses li.prev').removeClass('inactive');
  
  if (activeScroll>=scrollTops.length-1) $('#addresses li.next').addClass('inactive');
                                    else $('#addresses li.next').removeClass('inactive');
}
function scrollV(offset) {
  if (activeScroll+offset < 0 || activeScroll+offset >= scrollTops.length) {
    return false;
  }
  
  activeScroll += offset;
  $('#addresses .results').stop().animate({'scrollTop': scrollTops[activeScroll]}, 1000, function() {
    checkNav();
  });
}
function initSlidingAddresses() {
  if ($('#addresses').length==0) return false;
  scrollDistance = $('#addresses .results').height();
  
  $('#addresses .results li:nth-child(3n+1)').each(function() { scrollTops.push($(this).position().top); });
  
  checkNav();
  
  $('#addresses li.prev a').click(function() { scrollV(-1); return false; });
  $('#addresses li.next a').click(function() { scrollV(1); return false; });
}

// faq
function initFAQ() {
  if ($('#faq').length==0) return false;
  
  $('#faq dt').each(function() {
    $(this).next('dd').css('margin-top', -$(this).height()-22);
  });
  
  $('#faq dt').hover(function() {
    if ($(this).next('dd').css('display') == 'none') {
      $('#faq dd').hide('fast');
      $(this).next('dd').show('fast');
    }
  }, function() {});
}

// default input values
function initInputs() {
  if ($('#frm-login').length > 0)
    $('#frm-login input.username').focus();
    
  if ($('#frm-search').length > 0) {
    $('#frm-search input.text').focus(function() { if ($(this).val()=='Search') $(this).val(''); });
    $('#frm-search input.text').blur(function() { if ($(this).val()=='') $(this).val('Search'); });
  }

  if ($('#frm-stockist').length > 0) {
    $('#frm-stockist input.text').focus(function() { if ($(this).val()=='Postcode') $(this).val(''); });
    $('#frm-stockist input.text').blur(function() { if ($(this).val()=='') $(this).val('Postcode'); });
  }
}

$(document).ready(function() {
  initInputs();
  initMap();
  initSearchMap();
  initCatalogue();
  initRotator();
  initSlider();
  initSlidingAddresses();
  initFAQ();
});
