/// <reference path="jquery-1.3.2-vsdoc2.js" />
/// <reference path="swfobject-vsdoc.js" />
/// <reference path="sifr-vsdoc.js" />

$(document).ready(function() {

	// supersized bg image
	$("#supersize img").attr("oldWidth", $("#supersize img").attr("width"));
	$("#supersize img").attr("oldHeight", $("#supersize img").attr("height"));
	$(window).resize(function() { repos($(this)) });
	$(window).resize();

	// check if images have loaded, and do a window resize again
	var timer = null;
	$(window).load(function() {
		if (timer) {
			clearTimeout(timer);
			timer = null;
		}
		$(window).resize();
	});
	timer = setTimeout(function() {
		if (checkAvailable()) {
			$(window).resize();
		}
	}, 500); // wait before checking

	setTimeout("slideInHeader()", 1000);

	// initial fade
	//hideDoc(0);
	//showDoc();

	/* 
	Slide in and out thing
	*/
	// loop through a tags on page 
	/*
	$('a').each(function(idx, item) {
	// if it is not a mailto tag
	if (item.href.indexOf("mailto") == -1) {
	var url = "javascript:delayedRedirect('" + item.href + "');"
	item.href = url;
	}
	});
	*/

	// add google map if applicable
	if ($("#gmap").size() > 0) {

		$("#gmap").css("height", "100%");
		$(".content-inner").css("height", "100%");

		AttachGMap(gm_address, gm_ll, "gmap");
	}

	if ($("#infobox-button").size() > 0) {
		$("#infobox-button").attr("href", "#");
		$("#infobox-button").click(function() {
			$("#infobox").fadeOut(500);
			return false;
		});
	}
});

function slideInHeader() {
	$(".introtitle").css("left", "0");
	$(".introtitle").css("opacity", "0");
	$(".introtitle").animate({ "opacity": "0.95", "width": "100%" },500);
}

function delayedRedirect(href) {
	hideDoc(200);
	setTimeout(document.location = href, 200);
}

function hideDoc(ms) {
	$(".content-frame").animate({ "opacity": "0" }, ms);
	$(".content-bg").animate({ "opacity": "0" }, ms);
}

function showDoc() {
	$(".content-frame").animate({ "opacity": "1" }, 600);
	$(".content-bg").animate({ "opacity": "1" }, 600);
}

// resize bg image
function repos(w) {
	// Get height of window
	var height = w.height();
	var width = w.width();   //- $(".left").outerWidth();

	// set image size
	var img = $("#supersize img");

	// size frame
	var h = w.height();
	if (h < $(".content").height() + 30) h = $(".content").height() + 30;
	if (h < $(".left").height() + 30) h = $(".left").height() + 30;
	
	$(".frame").height(h);
	$(".left").height(h);
	
	var aspect = img.attr("oldWidth") / img.attr("oldHeight");
	var newWidth = width;
	var newHeight = newWidth / aspect;
	
	if (newHeight < height)
	{
		newHeight = height;
		newWidth = newHeight * aspect;
	}

	$(img).width(newWidth);
	$(img).height(newHeight);

	try {
		if ($.browser.msie && $.browser.version == "6.0") {
			$(".content-bg").height(height);
		}
		else {
			var rw = width - $(".left").width();
			if (scrollbarActive()) rw -= scrollbarWidth();
			
			if (jQuery.browser.safari)
				$(".right").width(rw-17);
			else
				$(".right").width(rw);

			if ($(".content-bg").height() < $(window).height()) $(".content-bg").height($(window).height()-20);
		}
	}
	catch (a) {
	}
	
	$(".introtitle").css("top", (height / 2) - ($(".introtitle").height() / 2));

	// repos left top
	//$("#supersize").css("top", (height / 2) - ($(img).height() / 2));
	//$("#supersize").css("left", (width / 2) - ($(img).width() / 2));

	// content frame size
	var margintop = "";
	if ($.browser.msie && parseInt($.browser.version) < 8) {
		width -= 18;
		height -= 20;
		$("#gmap").css("left", "-10px");
		//$("#gmap").css("width", width - 1);
		//$("#gmap").parent().css("overflow", "hidden");
	}
	else {
		width -= 10;
		height -= 20;
	}
	$("#gmap").height(height);

	$(".infobox-bg").height(height+20);
	$(".content-frame").width(width-$(".left").width());
	//$(".content-frame").height($(".content-inner").height()+20);
		
}


/* Google Maps integration -------------------------------------------------------------------------------------- */
function AttachGMap(address, ll, id) {
	$("#gmap").height($(window).height());
	var geocoder = null;
	var map = null;

	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById(id));

		// Get lat/long from address
		var geocoder = new GClientGeocoder();
		//map.setCenter(new GLatLng(36.158887, -86.782056), 13);
		
		// Add some controls to the map
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.enableScrollWheelZoom();
		
		if (gm_ll == '') {
			var point = geocoder.getLatLng(address, function(point) {

				// center on lat/long
				if (!point) {
					alert(address + " not found");
				} else {
					map.setCenter(point, gm_zoom);
				}

				// Add "My maps" for tussa fiber oversikt
				var kml = new GGeoXml("http://maps.google.no/maps/ms?ie=UTF8&hl=no&msa=0&msid=110229161635575226378.000476472563236be83ae&ll=62.207412,6.119235&spn=0.004802,0.013733&z=16&output=kml");
				//var kml = new GGeoXml("http://maps.google.no/maps/ms?ie=UTF8&hl=no&msa=0&msid=105543277964014208373.0004718df5e926ff6bd78&ll=59.921477,10.752504&spn=0.001511,0.005343&z=16&output=kml");
				map.addOverlay(kml);

				// The infowindow contents can be modified as it opens.
				/*
				GEvent.addListener(map, "infowindowprepareopen", function(iwtabs) {
					iwtabs[0].contentElem.innerHTML = $(iwtabs[0].contentElem.innerHTML).find("a").attr("target","_self").innerXHTML();
				});
				*/

			});
		}
		else {
			//"{100,200}"
			var p = gm_ll.split(",");
			alert(p[0] + " - " + p[1]);
			var point = new GPoint(p[0], p[1]);
			//map.setCenter(point, 13);
			// Add "My maps" for tussa fiber oversikt
			var kml = new GGeoXml("http://maps.google.no/maps/ms?ie=UTF8&hl=no&msa=0&msid=110229161635575226378.000476472563236be83ae&ll=" + gm_ll + "&spn=0.004802,0.013733&z=" & gm_zoom & "&output=kml");
			map.addOverlay(kml);			
		}
		
		
	}

	// hide copyrightinfo on the bottom. I did this because it was too big and went outside designated area.
	//$("#" + id + " .gmnoprint,#" + id + " > div:last").css("display", "none");
}

// check if images are loaded
function checkAvailable() {
	var available = true;
	$('img').each(function() {
		try {
			if (this.height == 0) {
				available = false;
				return false;
			}
		}
		catch (e) {
			available = false;
			return false;
		}
	});

	return available;
}

// get width of scrollbar
function scrollbarWidth() {
	var div = $('<div style="width:50px;height:50px;overflow:hidden;position:absolute;top:-200px;left:-200px;"><div style="height:100px;"></div>');
	// Append our div, do our calculation and then remove it 
	$('body').append(div);
	var w1 = $('div', div).innerWidth();
	div.css('overflow-y', 'scroll');
	var w2 = $('div', div).innerWidth();
	$(div).remove();
	return (w1 - w2);
}

function scrollbarActive(item) {
	return document.body.scrollHeight > document.body.clientHeight;
}