var ViewController = new function(){
	this.headerIsMinimized = false;
	this.mainIsMinimized = false;

	this.minimizeHeader = function(){
		if(this.headerIsMinimized)
			return false;
		this.headerIsMinimized = true;
			
		$("#header").animate({
			top: "-100px"
		}, "normal", "linear", function() {
			PageController.updateCoordsInAddress();
		});
		
		if(!this.mainIsMinimized){
			$("#main").animate({
				top: "50px"
			}, "normal");
		}
		$('#minimize').attr('src', '/themes/images/arrow-down.png');
		
		return false;
	}

	this.restoreHeader = function(){
		if(!this.headerIsMinimized)
			return false;
		this.headerIsMinimized = false;
			
		$("#header").animate({
			top: "20px"
		}, "normal", function() {
			PageController.updateCoordsInAddress();
		});
		
		if(!this.mainIsMinimized){
			$("#main").animate({
				top: "170px"
			}, "normal", "linear");
			ViewController.hideRestoreBtn();
		}
		$('#minimize').attr('src', '/themes/images/arrow.png');
		
		return false;
	}

	this.closeMain = function(){
		$("#main").slideUp(300);
		PageController.activePage = null;
		return false;
	}
	
	this.minimizeMain = function(){
		if(this.mainIsMinimized)
			return false;
		this.mainIsMinimized = true;

		$("#main").animate({
			top: ($(document).height()-40) + "px",
			bottom: "-5000px"
		}, "normal", "linear", function() {
			PageController.updateCoordsInAddress();
		});
		
		$('#mainMinimize').attr("src", "/themes/images/arrow_white.png");
		return false;
	}
	
	this.restoreMain = function(headerIsMax){
		if(!this.mainIsMinimized)
			return false;
		this.mainIsMinimized = false;

		if(!this.headerIsMinimized || headerIsMax){
			$("#main").animate({
				top: "170px",
				bottom: "20px"
			}, "normal", "linear", function() {
				PageController.updateCoordsInAddress();
			});
			ViewController.hideRestoreBtn();
		} else {
			$("#main").animate({
				top: "50px",
				bottom: "20px"
			}, "normal", "linear", function() {
				PageController.updateCoordsInAddress();
			});					
		}
		
		$('#mainMinimize').attr("src", "/themes/images/arrow_white-down.png");
		return false;
	}
	
	this.restoreHeaderVar = false;
	this.restore = function(){
		//headerIsMax = false;
		if(this.restoreHeaderVar){	
			this.restoreHeader();
			//headerIsMax = true;
		}
		this.hideRestoreBtn();
		this.restoreMain(); //headerIsMax);
	}

	this.hideRestoreBtn = function(){
		$('#googleMapRestore').animate({
			right: "-54px"
		}, "normal");
	}

	this.showRestoreBtn = function(){
		$('#googleMapRestore').animate({
			right: "-8px"
		}, "normal");
		
		// if the header is maximized or not
		this.restoreHeaderVar = false;
		if(!this.headerIsMinimized)
			this.restoreHeaderVar = true;
	}
	
	this.popupImage = function(url, pos, title, description, date, author){
		$.imgpreload(url,function()
		{
			var sizes = Util.calcNewDimensions(this[0].width, this[0].height, Math.round($(window).width()*0.8), Math.round($(window).height()*0.8));
			TINY.box.show(
				'<div class="close"><a href="#" onclick="TINY.box.hide(); return false;"><img src="/themes/images/close2.png" alt="x" /></a></div> ' +
				(title != undefined ? '<h2>' + title + '</h2>' : '') +
				'<img src="' + url + '" alt="' + (title != undefined ? title : '') + '" style="width: ' + sizes[0] + 'px; height: ' + sizes[1] + 'px" />' +
				'<div class="desc">' + 
					(pos != undefined ? '<div class="right"><a href="#" onclick="TINY.box.hide(); PageController.showOnMap(' + pos.lat() + ',' + pos.lng() +', 15, true); return false;" title="Vis i kart"><img src="/themes/images/icon_showonmap_32.png" alt="Vis i kart" class="iconShowOnMap" /></a></div>' : '') + (description != null ? description + '<br />' : '' ) + '<span class="infoline">' + (author ? "<b>Fotograf:</b> " + author : "") + ((date && date>0) ? " <b>Dato tatt:</b> " + Util.unixTimeToString(date) : "") + 
				'</span></div>'
			,0,sizes[0],sizes[1],1);
		});

	}
	
	this.showTooltip = function(text, x, y){
		$("body").append("<p id='tooltip'>"+ text +"</p>");
		$("#tooltip")
			.css("left",(x + 9) + "px")
			.css("top",(y - 25) + "px")
			.fadeIn("fast");	
	}
	
	this.hideTooltip = function(){
		$("#tooltip").remove();
	}
	
	var loaderIsShowing = false;
	this.showLoader = function(){
		if(!loaderIsShowing)
			$("#loader").fadeIn('fast');
		loaderIsShowing = true;
	}
	
	this.hideLoader = function(){
		$("#loader").fadeOut('fast');
		loaderIsShowing = false;
	}
	
	// startup loader
	this.activateStartupLoader = function(){
		$("#startupLoader").show();
		setTimeout(function(){
			$("#startupLoader").hide();
		}, 10000);
	}
	
	this.moveHeader = function(){
		if($("#mainContent h1").size()>0 && $("#mainContent h1").text().length>0){
			$("#mainTop h1").text($("#mainContent h1").text());
			$("#mainContent h1").remove();
		}
	}
	
	this.updateStyles = function(){
		var url = document.location.href;
		
		var color = "#477896";
		
		if(url.indexOf('/tur')>=0){
			color = "#e2373e";
		} else if(url.indexOf('/natur')>=0){
			color = "#1c9a50";	
		} else if(url.indexOf('/kultur')>=0){
			color = "#f7921d";		
		} else if(url.indexOf('/reiseliv')>=0){
			color = "#723982";
		} else if(url.indexOf('/vatn')>=0){
			color = "#00b6dc";
		} else if(url.indexOf('/bilder')>=0){
			color = "#00a9a1";	
		}
		
		//
		// 522e1f
		// 1c9a50
		
		$("#mainTop").css('background-color', color);
	}
}

