var a1 = function() {
	var	DOMReady = function() {	
		$("a[rel=external]").live("click", function(e) {
			e.preventDefault();
			window.open(this);
		}).attr("title", "Öppnas i ett nytt fönster.");
		if(this.isIE6) {document.execCommand("BackgroundImageCache", false, true);}
		a1.map.DOMReady();
		a1.slideshow.DOMReady();
		a1.carousel.init();
		a1.morelink.DOMReady();
	};
	return {	
		init: function() {
			
			if(window.XMLHttpRequest) {
				this.isIE6 = true;
			}
			
			$(document).ready(DOMReady);
		},
		getClassNameValue: function(el, prefix) {
			var ret = new RegExp(".*" + prefix + "-(.*?)(?:\\s|$).*").exec(el.className);
			if (ret) {
				return ret[1];
			}
			return null;
		}
	};
}();


a1.carousel = function() {
	var carouselElms;
	var carouselContainer;
	var carouselContainerWidth = 0;
	var carouselContainerHeight = 0;
	var currentElm;
	var elmWidth;
	var elmHeight;
	var inTransition = false;

	/**
	 * @private
	 */
	var createCarousel = function() {
		carouselContainer = $("#view-point ul");
		carouselContainerWidth = carouselContainer.width();
		carouselElms = $("#view-point ul li");
		
		for(var i = 0, l = carouselElms.length; i < l; i++) {
			if(typeof(elmWidth) === "undefined") {
				elmWidth = $(carouselElms[i]).outerWidth()+21;	
			}
			var elm = $(carouselElms[i]);
			elmHeight = $(carouselElms[i]).innerHeight();
			carouselContainerHeight = Math.max(elmHeight, carouselContainerHeight);
			elm.css({
				"height": elmHeight + "px"
			});
			elm.data("position", elmWidth*i);
			carouselContainerWidth += elmWidth;
		}
		carouselContainer.css({
			"width": carouselContainerWidth + "px",
			"height": carouselContainerHeight + "px",
			"overflow": "hidden",
			"position": "relative"
		});
	};
	
	var moveCarousel = function(forward) {
		var node;
		var anims;
		var speed;
		var easing;
		toPos = elmWidth;
		
		if(forward) {
			anims = { "left": "-" + toPos + "px" };
			node = carouselContainer.children(":first");
			carouselContainer.append(node.clone());
			speed = "fast";
			easing = "easeInQuad";
		} else {
			anims = { "left": 0 + "px" };
			node = carouselContainer.children(":last");
			carouselContainer.css("left", -toPos);
			speed = "normal";
			easing = "easeOutExpo";
			carouselContainer.prepend(node.clone());
		}
		
		carouselContainer.animate(anims, speed, easing, function() {
			if(forward) {
				carouselContainer.css("left", 0);
			}
			node.remove();
			inTransition = false;
		});
		return false;
	};
	
	return {
		/**
		 * Initialises all carousel tabs, call on init.
		 */
		init: function() {
			createCarousel();
			$("#show-previous").live("click", function(e) {
				if(!inTransition) {
					inTransition = true;
					e.preventDefault();
					this.blur();
					moveCarousel(false);	
				}
			});
			$("#show-next").live("click", function(e) {
				if(!inTransition) {
					inTransition = true;
					e.preventDefault();
					this.blur();
					moveCarousel(true);					
				}
			});
		}
	};
}();


a1.map = function() {
    var myOptions = {
		zoom: 15,
		center: new google.maps.LatLng(-34.397, 150.644),
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		scaleControl: true,
		navigationControl: true,
		mapTypeControlOptions: {
			style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
		},
		navigationControlOptions: {
			style: google.maps.NavigationControlStyle.SMALL
		}
    };
	var map;
	var more;
	var wrapper;
	var geocoder = new google.maps.Geocoder();
	var infoWindow = new google.maps.InfoWindow({
		content: "",
		maxWidth: 250
	});
	var points;

	var createMarker = function(point, position) {
		point.marker = new google.maps.Marker({
			map: map, 
			position: position,
			icon: "http://maps.gstatic.com/intl/sv_se/mapfiles/ms/micons/ylw-pushpin.png"
		});
		map.setCenter(position);
		map.panBy(210, 0);
		google.maps.event.addListener(point.marker, "click", function() {
			showInfoWindow(point);
		});
	};
	
	var showInfoWindow = function(point) {
		var content = "";
		content += "<div class=\"info-window-content\">";
		content += point.title;
		content += "</div>";
		infoWindow.setContent(content);
		infoWindow.open(map, point.marker);
	};
	
	var init = function() {
		map = new google.maps.Map(document.getElementById("map"), myOptions);
		more = $("#map-wrap .more");
	};
	
	var addPoints = function(pointsArray) {
		points = pointsArray;
	    for (var i = 0, l = points.length; i < l; i++) {
	    	var point = points[i];
	    	point.id = i;
	    	if((point.lat === "" || point.lng === "") && point.address !== "") {
		    	geocoder.geocode({
			    		address: point.address,
			    		country: "se"
			    	}, 
					function(results, status) {
			    		if (status == google.maps.GeocoderStatus.OK) {
							createMarker(point, results[0].geometry.location);
						} else {
							//alert("Geocode was not successful for the following reason: " + status);
						}
					}
				);
	    	} else if(point.lat !== "" && point.lng !== "") {
	    		createMarker(point, new google.maps.LatLng(point.lat, point.lng));
	    	}
	    }
	};
	
	return {
		DOMReady: function() {
			if(document.getElementById("map")) {
				init();
				addPoints([
					{
						address: $("#content-meta .street").html() + " " + $("#content-meta .city").html(),
						title: "<h3>Kontoret</h3><p>Armémuseum<br/>Riddargatan 13A, 114 51 Stockholm</p>",
						lng: "",
						lat: ""
					}
				]);	
			}
			
			if (more !== null && typeof(map) !== "undefined") {
				var open = false;
				var description = $("#description");
				wrapper = $("#map-wrap");
				more.live("click", function(e){
					e.preventDefault();
					if(open) {
						description.fadeIn();
						wrapper.animate({
							"marginLeft": "0px",
							"width": "420px"
						}, function() {
							wrapper.css({
								"borderLeft": "solid 1px #dcaa63",
								"paddingLeft": "12px"
							});
						});
						more.text("< Större karta?");
						open = false;
					} else {
						description.fadeOut();
						wrapper.animate({
							"marginLeft": "-400px",
							"width": "820px"
						}, function() {
							wrapper.css({
								"borderLeft": "none",
								"paddingLeft": "0px"
							})
						});
						more.text("Mindre karta >");
						open = true;
					}
				});
			}
		}
	};
}();

a1.slideshow = function() {
	var slideshow = {
		container: null,
		images: null,
		loader: "<img src=\"/img/loader.gif\" alt=\"\" />",
		
		changeImage: function() {
			var elm;
			var image;
			var index;			
			// Find out the index of the currently displayed image
			for(index = 0; index < slideshow.images.length; index++) {
				elm = slideshow.images.eq(index);
				if(elm.hasClass("active")) {
					elm.removeClass("active");
					break;
				}
			}

			if (index === 0) { index = slideshow.images.length; }
			index = index - 1;
			
			
			// Animate the new image
			image = slideshow.images.eq(index);
			image.attr("class", "active");
			image.css({
				"opacity": ".0",
				"filter": "alpha(opacity=0)"
			});
			image.fadeTo(1000, 1);
		}
	};
	return {
		DOMReady: function() {		
			slideshow.container = $("#slideshow").get(0);
			if(slideshow.container) {
				slideshow.images = $(slideshow.container).find("li");
				$(slideshow.images[0]).addClass("active");
				$(document).everyTime(6000, "changeImage", function() {
					slideshow.changeImage();
				}, 0);
			}
		}
	};
}();


a1.morelink = function() {
	return {
		DOMReady: function() {
			$(".more-link").bind("click", function(e) {
				e.preventDefault();
				var moreContent = $(".continue-reading");
				moreContent.hide();
				moreContent.fadeIn();
				$(this).hide();
			});
		}
	};
}();


/* Easing */
// t: current time, b: begInnIng value, c: change In value, d: duration
jQuery.easing['jswing'] = jQuery.easing['swing'];
jQuery.extend( jQuery.easing,
{
	def: 'easeOutQuad',
	swing: function (x, t, b, c, d) {
		//alert(jQuery.easing.default);
		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
	},
	easeInQuad: function (x, t, b, c, d) {
		return c*(t/=d)*t + b;
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	easeInOutQuad: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t + b;
		return -c/2 * ((--t)*(t-2) - 1) + b;
	},
	easeInCubic: function (x, t, b, c, d) {
		return c*(t/=d)*t*t + b;
	},
	easeOutCubic: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t + 1) + b;
	},
	easeInOutCubic: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t + b;
		return c/2*((t-=2)*t*t + 2) + b;
	},
	easeInQuart: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t + b;
	},
	easeOutQuart: function (x, t, b, c, d) {
		return -c * ((t=t/d-1)*t*t*t - 1) + b;
	},
	easeInOutQuart: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
		return -c/2 * ((t-=2)*t*t*t - 2) + b;
	},
	easeInQuint: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t*t + b;
	},
	easeOutQuint: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t*t*t + 1) + b;
	},
	easeInOutQuint: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
		return c/2*((t-=2)*t*t*t*t + 2) + b;
	},
	easeInSine: function (x, t, b, c, d) {
		return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
	},
	easeOutSine: function (x, t, b, c, d) {
		return c * Math.sin(t/d * (Math.PI/2)) + b;
	},
	easeInOutSine: function (x, t, b, c, d) {
		return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
	},
	easeInExpo: function (x, t, b, c, d) {
		return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
	},
	easeOutExpo: function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	},
	easeInOutExpo: function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	},
	easeInCirc: function (x, t, b, c, d) {
		return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
	},
	easeOutCirc: function (x, t, b, c, d) {
		return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
	},
	easeInOutCirc: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
		return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
	},
	easeInElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
	},
	easeOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	},
	easeInOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
		return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
	},
	easeInBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*(t/=d)*t*((s+1)*t - s) + b;
	},
	easeOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
	},
	easeInOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158; 
		if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
		return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
	},
	easeInBounce: function (x, t, b, c, d) {
		return c - jQuery.easing.easeOutBounce (x, d-t, 0, c, d) + b;
	},
	easeOutBounce: function (x, t, b, c, d) {
		if ((t/=d) < (1/2.75)) {
			return c*(7.5625*t*t) + b;
		} else if (t < (2/2.75)) {
			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
		} else if (t < (2.5/2.75)) {
			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
		} else {
			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
		}
	},
	easeInOutBounce: function (x, t, b, c, d) {
		if (t < d/2) return jQuery.easing.easeInBounce (x, t*2, 0, c, d) * .5 + b;
		return jQuery.easing.easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b;
	}
});


a1.init();
