var rootUrl = 'http://aspholmen.strategportalen.com/';
var TRIGGER_FOOTER = null;
$(function() {
	$('input[type="checkbox"]').uniform();
	
	if ($('#project-list').length) {
		$('#project-list .project').css('cursor', 'pointer');
		$('#project-list .project').click(function(e) {
			console.log(e.target.tagName);
			if (e.target.tagName != 'A')
				window.location.href = $(this).find('a').attr('href');
		});
	}
	
	var orig = $('.campaign_anchor');
	var bottom = $('.campaign_anchor').clone();
	bottom.attr('id', 'campaign_absolute');
	bottom.insertAfter($('.campaign_anchor'));
	//$('.campaign_anchor').append(bottom);
	
	$('.campaign_anchor').css('z-index', 50);
	bottom.css({
		position: 'absolute',
		bottom: $($(bottom.parent()).next()).outerHeight(),
		zIndex: 1,
		left: 0
	});
	
	var bh = bottom.height();
	var me = this;
	if ($.browser.msie)
		me = window;
		
	$(me).scroll(function() {
		var o = $(this).scrollTop();
		var t = bottom.offset().top;
		var d = t - o;
		//var dh = getDocHeight();
		var wh = getViewPortHeight();
		var wht = wh - bh;
		
		// console.log(o, t, t - o, dh, );
		if (d < wht) {
			orig.hide();
		} else {
			orig.show();
		}
	});
	
	TRIGGER_FOOTER = function() {
		$(me).scroll();
	};
	
	TRIGGER_FOOTER();
	
	$(window).resize(function() {
		TRIGGER_FOOTER();
	});
	
	fixExpandables();
});

function getViewPortHeight() {
	if (typeof window.innerWidth != 'undefined') {
      return window.innerHeight;
	} else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
       return document.documentElement.clientHeight;
	} else {
		return document.getElementsByTagName('body')[0].clientHeight
	}
}

function getDocHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}

var exIcons = {
	plus: '/webdav/images/Mallbilder/icon-plus.png',
	minus: '/webdav/images/Mallbilder/icon-minus.png'
};
function fixExpandables() {
	$('.expandable:not(.expandable-fixed)').each(function() {
		var ex = $(this);
		ex.addClass('exandable-fixed');
		
		if (ex.css('position') == 'static')
			ex.css('position', 'relative');
			
		var imgPlus = $('<img src="' + exIcons.plus + '" class="expandable-plus open" alt="" />');
		var imgMinus = $('<img src="' + exIcons.minus + '" class="expandable-minus close" alt="" />');
		
		var current = imgPlus,
			other = imgMinus;
		
		ex.append(imgPlus).append(imgMinus);
		ex.hover(function() {
			current.fadeIn('normal');
		}, function() {
			current.fadeOut('normal');
		});
		
		ex.data('switchexpando', function() {
			var tmp = current;
			current = other;
			other = tmp;
			
			if (other.css('display') != 'none') {
				other.fadeOut('normal');
				current.fadeIn('normal');
			}
		});
	});
}

function init_RealEstate_Map(lat, lng) {
	var startPos = new google.maps.LatLng(lat, lng);
    var myOptions = {
		zoom: 12,
		center: startPos,
		mapTypeControl: false,
		streetViewControl: false,
		navigationControl: false,
		scaleControl: true,
		scaleControlOptions: {
			position: google.maps.ControlPosition.TOP_RIGHT
		},
		scrollwheel: false,
		
		zoomControl: true,
		zoomControlOptions: {
			style: google.maps.ZoomControlStyle.LARGE,
			position: google.maps.ControlPosition.RIGHT_TOP
		},
		
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	
	var aspMapStyle = [{
			featureType: "all",
			elementType: "all",
			stylers: [{
				visibility: "off"
			}]
		}, {
			featureType: "road",
			elementType: "geometry",
			stylers: [{
				hue: "#11ff00"
			}, {
				visibility: "on"
			}, {
				invert_lightness: true
			}, {
				gamma: 0.63
			}, {
				lightness: 12
			}, {
				saturation: 0
			}]
		}, {
			featureType: "landscape.man_made",
			elementType: "all",
			stylers: [{
				saturation: 49
			}, {
				gamma: 1.29
			}, {
				visibility: "on"
			}, {
				lightness: 44
			}]
		}, {
			featureType: "landscape.natural",
			elementType: "all",
			stylers: [{
				lightness: 99
			}, {
				gamma: 9.99
			}, {
				saturation: -100
			}, {
				visibility: "off"
			}]
		}, {
			featureType: "road.highway",
			elementType: "all",
			stylers: [{
				visibility: "on"
			}, {
				hue: "#11ff00"
			}, {
				gamma: 0.63
			}]
		}, {
			featureType: "road.highway",
			elementType: "labels",
			stylers: [{
				visibility: "off"
			}]
		}, {
			featureType: "road.arterial",
			elementType: "geometry",
			stylers: [{
				visibility: "on"
			}, {
				hue: "#11ff00"
			}, {
				lightness: -21
			}, {
				gamma: 1.08
			}]
		}, {
			featureType: "water",
			elementType: "geometry",
			stylers: [{
				visibility: "on"
			}, {
				hue: "#ffc300"
			}, {
				lightness: 6
			}, {
				saturation: 74
			}, {
				gamma: 0.6
			}]
		}];
	
	map = new google.maps.Map($('#map_container').get(0), myOptions);
	var aspMap = new google.maps.StyledMapType(aspMapStyle, {name: 'aspMap'});
	
	map.mapTypes.set('aspmap', aspMap);
	map.setMapTypeId('aspmap');
	
	var markerUrl = rootUrl + 'webdav/images/marker.png';
	var markerIcon = new google.maps.MarkerImage(markerUrl);
	var gMarker = new google.maps.Marker({
		position: new google.maps.LatLng(lat, lng),
		map: map,
		icon: markerIcon,
		title: '',
		animation: google.maps.Animation.DROP,
		draggable: false
	});
}
