﻿
var map;
$(document).ready(function () {

    // hack for select first radio button in top map
    if ($("#mapFilter").size() > 0) {
        $("#mapFilter input:first").attr('checked', true);
    }

    // esc - close popup
    $(document).keydown(function (e) {
        if (e.which == 27) {  // escape, close box
            try {
                TINY.box.hide();
            } catch (e) { }
        }
    });

    $("#contentWrapper a img").click(function () {
        var a = $(this).parent();
        if (a.attr('href').indexOf('/upload') > -1) {
            var url = a.attr('href');
            $.imgpreload(url, function () {
                TINY.box.show("<img src='" + url + "' alt='' />", 0, 0, 0, 1);
            });
            return false;
        }
    });

    // input boxes
    $("input[type=text].removeText").each(function (n, val) {
        $(val).one('click', function () {
            $(this).val("");
            $(this).css({ color: '#484848' });
        });
    });

    // pdf click
    if (window.webpageID == undefined) {
        $("a[href$=.pdf]").each(function (n, val) {
            $(this).html($(this).text() + ' <a href="' + $(this).attr("href") + '" class="viewPdf" title="Åpne i webleser"><img src="/App_Themes/tp/images/icon_openpdf.png" alt="^" /></a>');
        });
        $("a.viewPdf").click(function () {
            var width = Math.round($(window).width() * 0.8) - 32;
            var height = Math.round($(window).height() * 0.9) - 32;

            //TINY.box.show("<iframe src=\"/modules/pdfviewer10/flashviewer.aspx?pdf=" + $(this).attr('href') + "&w=" + width + "&h=" + height + "\" width=\"" + width + "\" height=\"" + height + "\" style=\"border: none; overflow: hidden\"></iframe>", 0, width, height, 1);

            var url = $(this).attr('href').indexOf('http:') < 0 ? "http://" + window.location.hostname + $(this).attr('href') : $(this).attr('href');
            TINY.box.show("<iframe src=\"http://docs.google.com/viewer?url=" + url + "&embedded=true\" width=\"" + width + "\" height=\"" + height + "\" style=\"border: none;\"></iframe>", 0, width, height, 1);
            return false;
        });
    }

    //
    // MAP
    //

    if ($("#map").size() > 0) {
        function initialize() {

            map = new GMap2(document.getElementById("map"));
            map.setCenter(new GLatLng(63.4319, 10.3975), 13);
            map.addControl(new GSmallZoomControl3D());
            if ($("h1").text().toLowerCase().indexOf("kart") < 0) {
                map.disableInfoWindow();
                GEvent.addListener(map, "click", function (overlay) {
                    if (overlay != null)
                        window.location = '/kart?map=' + selectedMap;
                });
            }
        }
        initialize();
    }

    var selectedMap = null;
    $("#frontpageMap .phus").click(function () {
        removeAll();
        addPhus();
        selectedMap = $(this).attr('class');
    });

    $("#frontpageMap .boligsoner").click(function () {
        removeAll();
        addBoligsone();
        selectedMap = $(this).attr('class');
    });

    $("#frontpageMap .mcpark").click(function () {
        removeAll();
        addMcpark();
        selectedMap = $(this).attr('class');
    });

    $("#frontpageMap .ladestasjoner").click(function () {
        removeAll();
        addLstasjoner();
        selectedMap = $(this).attr('class');
    });

    $("#frontpageMap .hcpark").click(function () {
        removeAll();
        addHcpark();
        selectedMap = $(this).attr('class');
    });

    $("#frontpageMap .busspark").click(function () {
        removeAll();
        addBusspark();
        selectedMap = $(this).attr('class');
    });

    $("#frontpageMap .bobil").click(function () {
        removeAll();
        addBobil();
        selectedMap = $(this).attr('class');
    });

    $("#frontpageMap .ride").click(function () {
        removeAll();
        addRide();
        selectedMap = $(this).attr('class');
    });

    $("#frontpageMap .reguleringer").click(function () {
        removeAll();
        addReguleringer();
        selectedMap = $(this).attr('class');
    });

    $("#frontpageMap .laste").click(function () {
        removeAll();
        addLaste();
        selectedMap = $(this).attr('class');
    });

    $(".l3 .phus").click(function () {
        removeAll();
        addPhus();
        return false;
    });

    $(".l3 .boligsoner").click(function () {
        removeAll();
        addBoligsone();
        return false;
    });

    $(".l3 .mcpark").click(function () {
        removeAll();
        addMcpark();
        return false;
    });

    $(".l3 .hcpark").click(function () {
        removeAll();
        addHcpark();
        return false;
    });

    $(".l3 .lstasjoner").click(function () {
        removeAll();
        addLstasjoner();
        return false;
    });

    $(".l3 .busspark").click(function () {
        removeAll();
        addBusspark();
        return false;
    });

    $(".l3 .bobil").click(function () {
        removeAll();
        addBobil();
        return false;
    });

    $(".l3 .ride").click(function () {
        removeAll();
        addRide();
        return false;
    });

    $(".l3 .reguleringer").click(function () {
        removeAll();
        addReguleringer();
        return false;
    });

    $(".l3 .laste").click(function () {
        removeAll();
        addLaste();
        return false;
    });

    if ($("#frontpageMap").size() > 0)
        $("#frontpageMap .phus").click();

    if ($("h1").text().toLowerCase().indexOf("mc") > 0) {
        addMcpark();
        selectedMap = "mc";
        $(".mapLink").attr('href', "/kart?map=" + selectedMap);
    }

    if ($("h1").text().toLowerCase().indexOf("hc") > 0) {
        addHcpark();
        selectedMap = "hc";
        $(".mapLink").attr('href', "/kart?map=" + selectedMap);
    }

    if ($("h1").text().toLowerCase().indexOf("hus") > 0) {
        addPhus();
        selectedMap = "phus";
        $(".mapLink").attr('href', "/kart?map=" + selectedMap);
    }

    if ($("h1").text().toLowerCase().indexOf("buss") > 0) {
        addBusspark();
        selectedMap = "buss";
        $(".mapLink").attr('href', "/kart?map=" + selectedMap);
    }

    if ($("h1").text().toLowerCase().indexOf("lades") > 0) {
        addLstasjoner();
        selectedMap = "lstasjoner";
        $(".mapLink").attr('href', "/kart?map=" + selectedMap);
    }

    if ($("h1").text().toLowerCase().indexOf("boligsone") > 0) {
        addBoligsone();
        selectedMap = "boligsone";
        $(".mapLink").attr('href', "/kart?map=" + selectedMap);
    }

    if ($("h1").text().toLowerCase().indexOf("bobil") > 0) {
        addBobil();
        selectedMap = "bobil";
        $(".mapLink").attr('href', "/kart?map=" + selectedMap);
    }

    if ($("h1").text().toLowerCase().indexOf("ride") > 0) {
        addRide();
        selectedMap = "ride";
        $(".mapLink").attr('href', "/kart?map=" + selectedMap);
    }

    if ($("h1").text().toLowerCase().indexOf("reguleringer") > 0) {
        addReguleringer();
        selectedMap = "reguleringer";
        $(".mapLink").attr('href', "/kart?map=" + selectedMap);
    }

    if ($("h1").text().toLowerCase().indexOf("laste") > 0) {
        addLaste();
        selectedMap = "laste";
        $(".mapLink").attr('href', "/kart?map=" + selectedMap);
    }

    // check URL

    if (window.location.toString().indexOf("map=mc") > 0)
        addMcpark();

    if (window.location.toString().indexOf("map=hc") > 0)
        addHcpark();

    if (window.location.toString().indexOf("map=phus") > 0)
        addPhus();

    if (window.location.toString().indexOf("map=buss") > 0)
        addBusspark();

    if (window.location.toString().indexOf("map=lades") > 0 || window.location.toString().indexOf("map=lstasjoner") > 0)
        addLstasjoner();

    if (window.location.toString().indexOf("map=boligsone") > 0)
        addBoligsone();

    if (window.location.toString().indexOf("map=bobil") > 0)
        addBobil();

    if (window.location.toString().indexOf("map=ride") > 0)
        addRide();

    if (window.location.toString().indexOf("map=reguleringer") > 0)
        addReguleringer();

    if (window.location.toString().indexOf("map=laste") > 0)
        addLaste();
});
        
function getOverlay(url){
        return new GGeoXml(url);
}

function removeAll(){
    removePhus();
    removeBobil();
    removeBoligsone();
    removeBusspark();
    removeHcpark();
    removeLstasjoner();
    removeMcpark();
    removeReguleringer();
    removeRide();
    //map.clearOverlays();
}

var phus;
function addPhus(){
    if(map == null)
        return;
    if (phus != null)
        phus.show();
    else
        phus = getOverlay("http://maps.google.no/maps/ms?ie=UTF8&hl=no&msa=0&output=kml&msid=100250280014039564154.00047ec6fa4b7d1d9a63a");  
    map.addOverlay(phus);
    $(".largerMap").attr('href', "/kart?map=phus");
    $("a.phus").addClass("selected");
}
function removePhus(){
    if(phus!=null)
        phus.hide();
}

var boligsone;
function addBoligsone(){
    if(map == null)
        return;
    if(boligsone != null)
        boligsone.show();
    else
        boligsone = getOverlay("http://maps.google.no/maps/ms?ie=UTF8&hl=no&msa=0&output=kml&msid=100250280014039564154.00047fcb023896aa02e3b");
    map.addOverlay(boligsone);
    $(".largerMap").attr('href', "/kart?map=boligsone");
    $("a.boligsoner").addClass("selected");
}
function removeBoligsone(){
    if(boligsone!=null)
        boligsone.hide();
}

var hcpark;
function addHcpark(){
    if(map == null)
        return;
    if(hcpark != null)
        hcpark.show();
    else
        hcpark = getOverlay("http://maps.google.no/maps/ms?ie=UTF8&hl=no&msa=0&output=kml&msid=100250280014039564154.00047fcb0b216d59e6307");
    map.addOverlay(hcpark);
    $(".largerMap").attr('href', "/kart?map=hcpark");
    $("a.hcpark").addClass("selected");
}
function removeHcpark(){
    if(hcpark!=null)
        hcpark.hide();
}

var busspark;
function addBusspark(){
    if(map == null)
        return;
    if(busspark != null)
        busspark.show();
    else
        busspark = getOverlay("http://maps.google.no/maps/ms?ie=UTF8&hl=no&msa=0&output=kml&msid=100250280014039564154.00047fcb12a6ed771049a");
    map.addOverlay(busspark);
    $(".largerMap").attr('href', "/kart?map=busspark");
    $("a.busspark").addClass("selected");
}
function removeBusspark(){
    if(busspark!=null)
        busspark.hide();
}

var lstasjoner;
function addLstasjoner(){
    if(map == null)
        return;
    if(lstasjoner != null)
        lstasjoner.show();
    else
        lstasjoner = getOverlay("http://maps.google.no/maps/ms?ie=UTF8&hl=no&msa=0&output=kml&msid=100250280014039564154.00047fcb10c10c5faef96");
    map.addOverlay(lstasjoner);
    $(".largerMap").attr('href', "/kart?map=lstasjoner");
    $("a.lstasjoner").addClass("selected");
}
function removeLstasjoner(){
    if(lstasjoner!=null)
        lstasjoner.hide();
}

var mcpark;
function addMcpark(){
    if(map == null)
        return;
    if(mcpark != null)
        mcpark.show();
    else
        mcpark = getOverlay("http://maps.google.no/maps/ms?ie=UTF8&hl=no&msa=0&output=kml&msid=100250280014039564154.00047fcb0e9424ebcb3b8");
    map.addOverlay(mcpark);
    $(".largerMap").attr('href', "/kart?map=mcpark");
    $("a.mcpark").addClass("selected");
}
function removeMcpark(){
    if(mcpark!=null)
        mcpark.hide();
}

var bobil;
function addBobil(){
    if(map == null)
        return;
    if(bobil != null)
        bobil.show();
    else
        bobil = getOverlay("http://maps.google.no/maps/ms?ie=UTF8&hl=no&msa=0&output=kml&msid=100250280014039564154.00047fcb1552289185da6");
    map.addOverlay(bobil);
    $(".largerMap").attr('href', "/kart?map=bobil");
    $("a.bobil").addClass("selected");
}
function removeBobil(){
    if(bobil!=null)
        bobil.hide();
}

var ride;
function addRide(){
    if(map == null)
        return;
    if(ride != null)
        ride.show();
    else
        ride = getOverlay("http://maps.google.no/maps/ms?ie=UTF8&hl=no&msa=0&output=kml&msid=100250280014039564154.00047fcbeecd709da6329");
    map.addOverlay(ride);
    $(".largerMap").attr('href', "/kart?map=ride");
    $("a.ride").addClass("selected");
}
function removeRide(){
    if(ride!=null)
        ride.hide();
}

var reguleringer;
function addReguleringer(){
    if(map == null)
        return;
    if(reguleringer != null)
        reguleringer.show();
    else
        reguleringer = getOverlay("http://maps.google.no/maps/ms?ie=UTF8&hl=no&msa=0&output=kml&msid=100250280014039564154.00047fcb2756718e8b466");
    map.addOverlay(reguleringer);
    $(".largerMap").attr('href', "/kart?map=reguleringer");
    $("a.reguleringer").addClass("selected");
}
function removeReguleringer(){
    if(reguleringer!=null)
        reguleringer.hide();
}

var laste;
function addLaste(){
    if(map == null)
        return;
    if(laste != null)
        laste.show();
    else
        laste = getOverlay("http://maps.google.no/maps/ms?ie=UTF8&hl=no&msa=0&output=kml&msid=100250280014039564154.0004815dbc7acaf479977");
    map.addOverlay(laste);
    $(".largerMap").attr('href', "/kart?map=laste");
    $("a.laste").addClass("selected");
}
function removeLaste(){
    if(laste!=null)
        laste.hide();
}

$(document).ready(function() {

    // Add pdf icons to pdf links
    $(".attachments a[class!='viewPdf']").addClass("download"); //default icon, might be overridden later
    $(".attachments a[href$='.pdf'][class!='viewPdf']").addClass("pdf");
    $(".attachments a[href$='.mp3'], .attachments a[href$='.wav']").addClass("audio");
    $(".attachments a[href$='.doc'], .attachments a[href$='.rtf'], .attachments a[href$='.docx']").addClass("word");
    $(".attachments a[href$='.xls'], .attachments a[href$='.xlsx'], .attachments a[href$='.csv']").addClass("excel");
    $(".attachments a[href$='.jpg'], .attachments a[href$='.png'], .attachments a[href$='.gif'], .attachments a[href$='.tif'], .attachments a[href$='.tiff'], .attachments a[href$='.bmp']").addClass("picture");
    $(".attachments a[href$='.avi'], .attachments a[href$='.flv'], .attachments a[href$='.wmv'], .attachments a[href$='.mov'], .attachments a[href$='.divx'], .attachments a[href$='.mpeg']").addClass("video");
    $(".attachments a[href$='.zip'], .attachments a[href$='.rar']").addClass("zip");


    /*
    // Add email icons to email links
    $("a[href^='mailto:']").addClass("email");

    //Add external link icon to external links - 
    $('a').filter(function() {
    //Compare the anchor tag's host name with location's host name
    return this.hostname && this.hostname !== location.hostname;
    }).addClass("external");
    */

    //You might also want to set the _target attribute to blank
    /*
    $('a').filter(function() {
    //Compare the anchor tag's host name with location's host name
    return this.hostname && this.hostname !== location.hostname;
    }).addClass("external").attr("target", "_blank");
    */
});
function fillTextBox(place) {
    switch(place){
        case 'name':
            $(".delName").val($(".newName").val());
            if ($(".invoName").length) {
                $(".invoName").val($(".newName").val());
            }
            break;
        case 'surname':
            $(".delSurname").val($(".newSurname").val());
            if ($(".invoSurname").length) {
                $(".invoSurname").val($(".newSurname").val());
            }
            break;
        default:
            break;
        }   
}
