$(document).ready(function(){
    $('#slide-container').cycle({
        timeout: 8000,
        prev:   '#left-slide-control',
        next:   '#right-slide-control'
    });

    //collapsable navigation
    
    $('.wpsc_top_level_categories').children('li').children('a').click(function(){
        $('.wpsc_top_level_categories').children('li').children('ul').slideUp();
        $('.wpsc_top_level_categories').children('li').css('list-style-image','url(http://localhost/www/hanafins/wp-content/themes/hanafins/images/product-category-bullet.jpg)');
        $(this).parent('li').css('list-style-image','url(http://localhost/www/hanafins/wp-content/themes/hanafins/images/product-cat-bullet-expanded.gif)').children('ul').slideDown();
        return false;
    });
    $('.wpsc-current-cat').parent().parent().parent().parent().show();
    $('.wpsc-current-cat').parent().parent().show();

    $('.wpsc-current-cat').parent().parent().parent().parent().parent().css('list-style-image','url(http://localhost/www/hanafins/wp-content/themes/hanafins/images/product-cat-bullet-expanded.gif)');
    $('.wpsc-current-cat').parent().parent().parent().css('list-style-image','url(http://localhost/www/hanafins/wp-content/themes/hanafins/images/product-cat-bullet-expanded.gif)');


    //what to do when a store location is clicked
    $('.location-link').click(function(){
        moveMap($(this).parent().children("input.shop-address").val(),15);
        $('.store-info').hide();
        $('#store-id-'+$(this).parent().children("input.store-id").val()).show();
        return false;
    });

    //What to do when a city link is clicked    
    $('.city-link').click(function(){
        moveMap($(this).html()+', New Zealand',11);
        $('.store-info').hide();
        $("#store-info-desc h2").html("Hanafins stores in "+$(this).html());
        $("#store-info-desc div").html("");
        $("#store-info-desc").show();
        return false;
    });

    $(".wpsc_select_variation").change(function(){        
        $(this).parent().parent().siblings("div.wpsc_product_price").children(".pricedisplay").html($(this).children("option:selected").attr("id"));        
    });

});

var geocoder;
var map;



function moveMap(address,zoom) {
    geocoder.geocode( {'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
          map.setZoom(zoom);
          map.setCenter(results[0].geometry.location);
          return true;
    } else {
        alert("Geocode was not successful for the following reason: " + status);
        return false;
    }
    });
}

// Google Maps For Contact Page (Async Loading).
function initialize() {

    geocoder = new google.maps.Geocoder();

    var maplatlng = new google.maps.LatLng(-39.72, 173.41);

    var mapOptions = {
        zoom: 5,
        center: maplatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

    $("li.city-location").each(function(){
        var shopLocation = $(this).children("input.shop-address").val();
        var thisObj = $(this);
        geocoder.geocode( {'address': shopLocation}, function(results, status) {
            if (status != google.maps.GeocoderStatus.OK) {
                alert("Geocode was not successful for the following reason: " + status);
            } else {
                var opts = {
                    map: map,
                    title: shopLocation,
                    position: results[0].geometry.location,
                    clickable: true
                }
                marker = new google.maps.Marker(opts);
                google.maps.event.addListener(marker, 'click', function() {
                    thisObj.children('a.location-link').click();
                });
            }
        });
    });
    $("#store-info-desc").show();
}

function loadScript() {
  var script = document.createElement("script");
  script.type = "text/javascript";
  script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=initialize";
  document.body.appendChild(script);
}

window.onload = loadScript;


