var commerce = commerce || {};

$(function () {
	commerce.init();
});

commerce = {
	init: function() {
		commerce.search.init();
		commerce.general.init();
		commerce.slider.init();
		commerce.dropdown.init();
	},
	search: {
		init: function() {
			$(".topSearch input[type='text']").focus(function() {
				$(this).val("");
			});
			$(".topSearch input[type='text']").focusout(function() {
				if ($(this).val().length < 1) {
					$(this).val("Sök");
				}
			});
		} // end of search.init
	},
	general: {
		init: function() {
			var exits = $(".navigation li:not(:first-child)").hasClass("active"); //exit = EXam ITemS if item isn't first-child and active
			if (exits == true) {
				$(".navigation li.active").prev().css("border", "none");
			}
		}
	},
	slider: {
		init: function() {
			$('#slides').slides({
					effect: "fade",
					fadeEasing: "easeInOutQuad",
					crossfade: "true",
					fadeSpeed: 1200,
					preload: true,
					preloadImage: '/Assets/Images/loading.gif',
					play: 7000,
					hoverPause: false,
					animationStart: function(current) {
						$('.caption').animate({
								bottom: -35
							}, 100);
					},
					animationComplete: function(current) {
						$('.caption').animate({
								bottom: 0
							}, 200);
					},
					slidesLoaded: function() {
						$('.caption').animate({
								bottom: 0
							}, 200);
					}
				});
		}
	}, dropdown: {
		init: function() {
			var dropdownMenu = $("div.custom");
			dropdownMenu.hide();
			$(".dropMenu").live("click", function(e) {
				e.preventDefault();
				var elm = $(this).next();
				if (elm.hasClass('custom')) {
					elm.fadeIn(300);
				} else {
					var topDropDown = $('.shops.custom');
					var lnkOffset = $(this).offset();
					topDropDown.css("left", lnkOffset.left - 250);
					topDropDown.fadeIn(300);
				}
			});
			dropdownMenu.hover(function() { }, function() { $(this).fadeOut(300); });
		}
	}
};

// GOOGLE MAPS 
//var commerceMap = new google.maps.LatLng(59.3369195, 18.0622704);
//var parliament = new google.maps.LatLng(59.327383, 18.06747);
var marker;
var map;

function initialize() {

	var geocoder = new google.maps.Geocoder();
	var latlng = new google.maps.LatLng(59.332788, 18.064488);

	geocoder.geocode({ 'address': $('#address').val() }, function (results, status) {


		if (status == google.maps.GeocoderStatus.OK) {

			var mapOptions = {
				zoom: 15,
				mapTypeId: google.maps.MapTypeId.ROADMAP,
				center: latlng,
				disableDefaultUI: true
			};

			map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

			var marker = new google.maps.Marker({
				map: map,
				draggable: true,
				position: results[0].geometry.location
			});
			
			map.setCenter(results[0].geometry.location);
			
		} else {
		}
	});
}

function toggleBounce() {

  if (marker.getAnimation() != null) {
    marker.setAnimation(null);
  } else {
    marker.setAnimation(google.maps.Animation.BOUNCE);
  }
}
