function Tooltip(marker, text, padding){
	this.marker_ = marker;
	this.text_ = text;
	this.padding_ = padding;
}
Tooltip.prototype = new GOverlay();
Tooltip.prototype.initialize = function(map){
	var div = document.createElement("div");
	jQuery(div).html(this.text_);
	div.className = 'tooltip';
	div.style.position = 'absolute';
	div.style.visibility = 'hidden';
	map.getPane(G_MAP_FLOAT_PANE).appendChild(div);
	this.map_ = map;
	this.div_ = div;
}
Tooltip.prototype.remove = function(){
	this.div_.parentNode.removeChild(this.div_);
}
Tooltip.prototype.copy = function(){
	return new Tooltip(this.marker_,this.text_,this.padding_);
}
Tooltip.prototype.redraw = function(force){
	if (!force) return;
	var markerPos = this.map_.fromLatLngToDivPixel(this.marker_.getPoint());
	var iconAnchor = this.marker_.getIcon().iconAnchor;
	var xPos = Math.round(markerPos.x - this.div_.clientWidth / 2);
	var yPos = markerPos.y - iconAnchor.y - this.div_.clientHeight - this.padding_;
	this.div_.style.top = yPos + 'px';
	this.div_.style.left = xPos + 'px';
}
Tooltip.prototype.show = function(){
	this.div_.style.visibility = 'visible';
}
Tooltip.prototype.hide = function(){
	this.div_.style.visibility = 'hidden';
}

$(document).ready(function() {
//	+-------------------------------+
//	|	GOOGLE MAPS START.			|
//	+-------------------------------+
	var map = new GMap2(document.getElementById('map'));
	var topRight = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,40));
	
	map.addControl(new GSmallMapControl(), topRight);
	map.setCenter(new GLatLng(67.13397, 20.660984), 7);

	/*
	Kod för att lägga till en ny markör
	*/
	var marker = new GMarker(new GLatLng(67.13397, 20.660984));
	var tooltip = new Tooltip(marker, '<strong>Gellivare Lapland</strong><br />Storgatan 16,<br />SE-982 31 Gellivare,<br />Sweden,<br />Tel: +46 970 166 60', 4);

	map.addOverlay(marker);
	map.addOverlay(tooltip);

	GEvent.addListener(marker, 'mouseover', function() {
	    tooltip.show();
	});
	GEvent.addListener(marker, 'mouseout', function() {
	    tooltip.hide();
	});
	
	$(".TellusProductList li, .TellusProduct").each(function() {
		if($(this).children('.coordinations').length>0) {
			var marker = new GMarker(new GLatLng(
					$(this).children('.coordinations').children('.latitude').html(),
					$(this).children('.coordinations').children('.longitude').html()
				)),
				tooltip = new Tooltip(
					marker,
					$(this).children('.coordinations').children('.description').html(),
					4
				);

			map.addOverlay(marker);
			map.addOverlay(tooltip);

			GEvent.addListener(marker, 'mouseover', function() {
				tooltip.show();
			});
			GEvent.addListener(marker, 'mouseout', function() {
				tooltip.hide();
			});
		}
	});

	hide_map();
	$("#map-handle").click(function(e) {
		if ($("#map-ct").hasClass('active')) {
			$("#map-ct").animate({width:"22px"}, {duration: 500, complete: hide_map});
		 	$("#map-handle-img").attr('src', $("#map-handle-img").attr('src').replace("_right","_left"));								
		} else {
			show_map();
			$("#map-ct").animate({width:"940px"}, {duration:500, complete:map.checkResize()});
			$("#map-handle-img").attr('src',  $("#map-handle-img").attr('src').replace("_left","_right"));
		}

		$("#map-ct").toggleClass("active");
		e.preventDefault();
		return false;
	});
//	+-------------------------------+
//	|	GOOGLE MAPS ENDS.			|
//	+-------------------------------+

	$(".box h2 a").click(function(e) {
		$(this).toggleClass('ui_collapsed');
		$(e.target).parent().next(".box_collapsable").slideToggle("fast");
		$(this).parent().parent().toggleClass("inactive");
		e.preventDefault();

		return false;
	});
});

// Helper functions
function hide_map() {
	$("#map").fadeOut("fast");
}
function show_map() {
	$("#map").show();
}
