﻿var map_canvas;
var map;
var marker = null;

$(document).ready(function() {
    $(".autoClear").focus(function() {
        if (!$(this).attr('originalVal')) {
            $(this).attr('originalVal', $(this).val());
            $(this).val('');
        }
    }).blur(function() {
        if ($.trim($(this).val()).length == 0) {
            $(this).val($(this).attr('originalVal'));
            $(this).removeAttr('originalVal');
        }
    });

    if (typeof $(".carousel").children("ul").jcarousel == 'function') {
        $('.carousel').children("ul").each(function() {
            var liCount = $(this).children('li').length;

            if (liCount == 0)
                $(this).parent().parent().remove();
            else {
                if (liCount <= $(this).parent().attr('visibleItems')) {
                    $(this).parent().parent().children('.nextBut').remove();
                    $(this).parent().parent().children('.prevBut').remove();
                    $(this).css({
                        'list-style-type': 'none'
                    });
                    $(this).children('li').css({
                        'float': 'left',
                        'marginRight': '-4px'
                    });
                }
                else {
                    $(this).jcarousel({
                        wrap: 'circular',
                        scroll: 3,
                        initCallback: mycarousel_initCallback,
                        buttonNextHTML: null,
                        buttonPrevHTML: null,
                        animation: 1000,
                        easing: 'easeInOutCubic'
                    });
                }
            }
        });
    }

    if (typeof $('a.zoomBut').lightbox == 'function') {
        $('a.zoomBut').lightbox();
    }

    $('.visiblePage:last').addClass('last');

    $('.zoomMe').jqzoom({
        zoomWidth: 335,
        zoomHeight: 350,
        zoomType: 'reverse',
        title: false,
        xOffset: 15,
        yOffset: 80
    });
});

function mycarousel_initCallback(carousel) {
    var container = $(carousel.container).parent().parent();
    $(container).children('.nextBut').bind('click', function() {
        carousel.next();
        return false;
    });

    $(container).children('.prevBut').bind('click', function() {
        carousel.prev();
        return false;
    });
};

function setupMap(map_id, store_id, store_name, lat, lng) {
    var options = {
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map_canvas = document.getElementById(map_id);
    map = new google.maps.Map(map_canvas, options);
    
	setMapLocation(store_id, store_name, lat, lng);
}

function setMapLocation(store_id, store_name, lat, lng) {
	if(marker != null)
		marker.setMap(null);
	
	marker = new google.maps.Marker({
		title       : store_name,
		position    : new google.maps.LatLng(lat, lng)
    });

    $('ul.stockistList li').removeClass('selected');
    $('li#' + store_id).addClass('selected');
	
	marker.setMap(map);
	
	var bounds = new google.maps.LatLngBounds();
	
	bounds.extend(marker.getPosition());
	map.fitBounds(bounds);
	google.maps.event.addListener(map, 'bounds_changed', function(event) {
		if (map.getZoom() > 16)
			map.setZoom(16);
		google.maps.event.clearListeners(map, 'bounds_changed');
	});
    map.setCenter(bounds.getCenter());
}
