$(document).ready(function(){
	Util.log("Startup");
	var localUrl = "http://" + window.location.hostname;

	// create & setup Google Map
	PageController.initialize();
	
	$("#lnkCloseMain").click(function () {
		ViewController.closeMain();
		PageController.addBeforeCoordsInAddress("");
		return false;
	});

	$(".lnkShowMain").live('click', function () {
		PageController.showPage($(this).attr("href"));
		return false;
	});

	$("#lnkBack").click(function () {				
		PageController.historyGoBack();
		return false;
	});

	$("#lnkForward").click(function () {				
		PageController.historyGoForward();
		return false;
	});


	$("#lnkMinimizeMain").click(function () {				
		if(ViewController.mainIsMinimized)
			return ViewController.restoreMain();
		else
			return ViewController.minimizeMain();
	});
	
	$("#lnkMinimizeHeader").click(function () {	
		if(ViewController.headerIsMinimized)
			return ViewController.restoreHeader();
		else
			return ViewController.minimizeHeader(true);	
	});	
	
	$("#lnkGmapZoomIn").click(function () {	
		MapController.map.zoomIn();	
		return false;
	});			
	
	$("#lnkGmapZoomIn").mouseover(function () {	
		$("#lnkGmapZoomIn").attr("src", "/themes/images/zoomin_active.png");	
	}).mouseout(function () {	
		$("#lnkGmapZoomIn").attr("src", "/themes/images/zoomin.png");	
	});	
	
	$("#lnkGmapHome").click(function () {	
		MapController.map.goHome();	
		return false;
	});		
	
	$("#lnkGmapHome").mouseover(function () {	
		$("#lnkGmapHome").attr("src", "/themes/images/home_active.png");	
	}).mouseout(function () {	
		$("#lnkGmapHome").attr("src", "/themes/images/home.png");	
	});	
	
	$("#lnkGmapZoomOut").click(function () {	
		MapController.map.zoomOut();	
		return false;
	});				
	
	$("#lnkGmapZoomOut").mouseover(function () {	
		$("#lnkGmapZoomOut").attr("src", "/themes/images/zoomout_active.png");	
	}).mouseout(function () {	
		$("#lnkGmapZoomOut").attr("src", "/themes/images/zoomout.png");	
	});	
	
	$("#lnkGmapRestore").click(function () {	
		return ViewController.restore();	
	});				
	
	$("#lnkGmapRestore").mouseover(function () {	
		$("#lnkGmapRestore").attr("src", "/themes/images/restore_active.png");	
	}).mouseout(function () {	
		$("#lnkGmapRestore").attr("src", "/themes/images/restore.png");	
	});	
	
	$("#mainContent a, .desc a").live('click', function(){
		var href = $(this).attr('href');
		
		// IE7 fix: strip out domain
		if(href.substr(0,localUrl.length)==localUrl)
			href = href.substr(localUrl.length+1);
		if(href.substr(0,4)=="map:"){
			var coords = href.substr(4).split(";");
			PageController.showOnMap(coords[0], coords[1], null, true);
			return false;
		} if($(this).attr('target').length>0)
			return true;
		if(href.substr(0,11)=="javascript:")
			return true;
		if(href=="#" || href=="/#")
			return true;
		if(href.substr(0,1)!="/"){
			if(href.substr(0,4) == "http"){
				window.open(href);
				return false;
			}
		}
		PageController.showPage(href);
		return false;
	});
	
	$("#searchVal").keypress(function(e){
		var code = (e.keyCode ? e.keyCode : e.which);
		if(code == 13) { //Enter keycode
			$("#search").click();
		}

	});
	$("#search").click(function(){
		$("#search").attr('href', '/sok?q=' + $("#searchVal").val());
		_gaq.push(['_trackEvent', 'Internsøk', $("#searchVal").val()])
	});

	// map arrow click
	$(document).keypress(function(e){
		var code = (e.keyCode ? e.keyCode : e.which);

		if(code == 37) { // left
			MapController.map.left();
			e.preventDefault();
		} else if(code == 38) { // up
			MapController.map.up();
			e.preventDefault();
		} else if(code == 39) { // right
			MapController.map.right();
			e.preventDefault();
		} else if(code == 40) { // down
			MapController.map.down();
			e.preventDefault();
		}
	});
	
	// img click
	$(".articleImagesmall").live("click", function(){
		$(".articleImage").attr("src", $(this).attr("src"));
		$(".articleImage").parent().attr("href", $(this).parent().attr("href"));
		return false;
	});
	
	/*var ar = new Array(33,34,35,36,37,38,39,40);
	$(document).keypress(function(e){
		 var key=e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
		  console.log(key);
		  //if(key==35 || key == 36 || key == 37 || key == 39)
		  if($.inArray(key,ar))
		  {
			  e.preventDefault();
			  return false;
		  }
		  return true;
	});*/

});


function onDocumentLoaded(){

	// settings page
	if($("#settings-map").size()>0){
	
		var selectedMap = MapController.map.selectedMap;
		$("." + selectedMap).attr('checked', 'checked');
	
	}
	if($("#settings-elements").size()>0){
		$("#settings-elements input").each(function(n,val){
			var manager = $(val).attr('class');
			if(manager!="" && MapController.map.getManager(manager).isVisible)
				$(val).attr('checked', 'checked');
			else
				$(val).removeAttr('checked');
		});
	}

}
