﻿var centerLatitude = 57.607463;
var centerLongitude = 12.991347; 
var startZoom = 6;

// === Some cookie parameters ===
var cookiename = "mapinfo";  // name for this cookie
var expiredays = 7;          // number of days before cookie expiry

// Map specific functions
// filtering checkboxes
function updatemarkers2() {
    for (var i=0; i<gmarkers.length; i++) {
        if (gmarkers[i].plugs & plugsShown) {
            gmarkers[i].show();
        } else {
            gmarkers[i].hide();
        }
    }
    clusterer.forceRecalc = true;
    clusterer.DisplayLater();
}

// filtering checkboxes
function showhide( category, isVisible)
{
    document.getElementById(category+"box").checked = isVisible;
    /*
    if (isVisible)    {        show(category);    } 
    else {        hide(category);    }
    */
}

// == shows all markers of a particular category, and ensures the checkbox is checked ==
function show(category) {
    var categorybit = plugtype[category];
    plugsShown = plugsShown | categorybit; // add flag
    updatemarkers2();

    // == check the checkbox ==
    document.getElementById(category+"box").checked = true;
}

// == hides all markers of a particular category, and ensures the checkbox is cleared ==
function hide(category) {
    var categorybit = plugtype[category];
    plugsShown = plugsShown & ~categorybit; // remove flag
    updatemarkers2();
    // == clear the checkbox ==
    document.getElementById(category+"box").checked = false;
    // == close the info window, in case its open on a marker that we just hid
    map.closeInfoWindow();
}

// == a checkbox has been clicked ==
function boxclick(box,category) {
    //document.getElementById("side_bar").innerHTML = "";
    if (box.checked) {
        show(category);
    } else {
        hide(category);
    }
    // == rebuild the side bar
    makeSidebar();
}


  // == rebuilds the sidebar to match the markers currently displayed ==
  function makeSidebar() {
     var html = "";
    for (var i=0; i<gmarkers.length; i++) {
      if (!gmarkers[i].isHidden()) {
        html += '<a href="javascript:myclick(' + i + ')">' + gmarkers[i].myname + '</a><br>';
      }
    }
    
    document.getElementById("sidebar").innerHTML = html;
  }

function windowHeight() {
	// Standard browsers (Mozilla, Safari, etc.)
	if (self.innerHeight)
		return self.innerHeight;
	// IE 6
	if (document.documentElement && document.documentElement.clientHeight)
		return document.documentElement.clientHeight;
	// IE 5
	if (document.body)
		return document.body.clientHeight;
	// Just in case.
	return 0;
}

function handleResize() {
//	var height = windowHeight();

	var height = windowHeight() - document.getElementById('toolbar').offsetHeight - document.getElementById('header').offsetHeight - document.getElementById('footer').offsetHeight - 40;
	document.getElementById('map').style.height = height + 'px';
  document.getElementById('sidebar').style.height = height + 'px';
}

function changeBodyClass(from, to) {
     document.body.className = document.body.className.replace(from, to);
     return false;
}


function ReadCookie() {

    // === Look for the cookie ===
    if (document.cookie.length>0) {
        cookieStart = document.cookie.indexOf(cookiename + "=");
        if (cookieStart!=-1) {
            cookieStart += cookiename.length+1; 
            cookieEnd=document.cookie.indexOf(";",cookieStart);
            if (cookieEnd==-1) {
                cookieEnd=document.cookie.length;
            }
            cookietext = document.cookie.substring(cookieStart,cookieEnd);
            // == split the cookie text and create the variables ==
            bits = cookietext.split("|");
            centerLatitude = parseFloat(bits[0]);
            centerLongitude = parseFloat(bits[1]);
            startZoom = parseInt(bits[2]);
            mapType = parseInt(bits[3]);
            if (parseInt(bits[4]) != 0)
                plugsShown = parseInt(bits[4]);
        } 
    }
}

// === Set the cookie before exiting ===
function setCookie() {
    maptype = 0;
    for (var i=0;i<map.getMapTypes().length;i++) {
        if (map.getCurrentMapType() == map.getMapTypes()[i]) {
            maptype = i;
        }
    }
    var cookietext = cookiename+"="+map.getCenter().lat()+"|"+map.getCenter().lng()+"|"+map.getZoom()+"|"+maptype+"|"+plugsShown;
    if (expiredays) {
        var exdate=new Date();
        exdate.setDate(exdate.getDate()+expiredays);
        cookietext += ";expires="+exdate.toGMTString();
    }
    // == write the cookie ==
    document.cookie=cookietext;
    // == Call GUnload() on exit ==
    GUnload();
}

function localPreInit()
{
    document.getElementById('button-sidebar-hide').onclick = function() { return changeBodyClass('sidebar-right', 'nosidebar'); };
    document.getElementById('button-sidebar-show').onclick = function() { return changeBodyClass('nosidebar', 'sidebar-right'); };
    handleResize();
    ReadCookie();
}

function localPostInit()
{
    updatemarkers2();
    updatecheckboxes();

    // == create the initial sidebar ==
    makeSidebar();
}

function checkSiteId(siteId, index)
{
}

window.onresize = handleResize;
addLoadEvent(init);
//window.onload = function() { init; };
//window.onload = init;
// JScript File
