﻿function showcompanies() {
    if (GBrowserIsCompatible()) {
        // Show the map
        PageMethods.Get_All_Locations(addcompanies, ajaxfailure);
    }

}

function addcompanies(value, ctx, methodName) {

    var geocoder = new GClientGeocoder();
    var bounds = new GLatLngBounds();
    var map = new GMap2(document.getElementById("homemap"));

    map.setUIToDefault();

    // Create icons
    var companyIcon = new GIcon(G_DEFAULT_ICON);
    companyIcon.iconSize = new GSize(11, 18);
    companyIcon.image = "/_resources/files/icons/maps/company.png";
    companyIcon.printImage = "/_resources/files/icons/maps/print/company.gif";
    companyIcon.shadow = "/_resources/files/icons/maps/shadow.png";
    companyIcon.shadowSize = new GSize(22, 18);
    companyIcon.iconAnchor = new GPoint(5, 17);
    companyIcon.infoWindowAnchor = new GPoint(6, 9);
    var projectIcon = new GIcon(G_DEFAULT_ICON);
    projectIcon.iconSize = new GSize(11, 18);
    projectIcon.image = "/_resources/files/icons/maps/project.png";
    projectIcon.printImage = "/_resources/files/icons/maps/print/project.gif";
    projectIcon.shadow = "/_resources/files/icons/maps/shadow.png";
    projectIcon.shadowSize = new GSize(22, 18);
    projectIcon.iconAnchor = new GPoint(5, 17);
    projectIcon.infoWindowAnchor = new GPoint(6, 9);

    var markerOptions;

    for (var i = 0; i < value.length; i++) {

        var ppoint = new GLatLng(value[i].latitude, value[i].longitude);
        var pmarkerhtml;
        if (value[i].type == "c") {
            pmarkerhtml = "<p><strong>" + value[i].name + "</strong><br />No. projects: " + value[i].trips;
            markerOptions = { icon: companyIcon };
        }
        else {
            pmarkerhtml = "<p><strong>" + value[i].name + "</strong><br />" + value[i].address;
            markerOptions = { icon: projectIcon };
        }
        map.addOverlay(createmarker(ppoint, pmarkerhtml, markerOptions));
        // add point to array
        bounds.extend(ppoint);

    }

    //map.setCenter(point);
    map.setCenter(bounds.getCenter());
    var zoom = map.getBoundsZoomLevel(bounds) - 1;
    if (zoom > 15) {
        zoom = 5;
    }
    map.setZoom(zoom);
}

function createmarker(point, markerhtml, markerOptions) {

    var marker
    if (markerOptions) {
        marker = new GMarker(point, markerOptions);
    }
    else {
        marker = new GMarker(point);
    }
    GEvent.addListener(marker, "click", function () {
        marker.openInfoWindowHtml(markerhtml);
    });

    return marker;

}

onloadAdd('showcompanies()');

/* Quotes */

function initquotes() {
    if (document.getElementById("quotes")) {
        var quotes = new quotescroller(document.getElementById("quotes"));
    }
}

onloadAdd("initquotes()");

function quotescroller(o) {
    this.list = o;
    this.quotes = o.getElementsByTagName("LI");
    this.current = 0;

    var self = this;
    this.changequote = function (e) {
        self.timeout = null;

        self.quotes[self.current].className = "hidden";
        if (self.current == self.quotes.length - 1) {
            self.current = 0;
        }
        else {
            self.current += 1;
        }

        self.quotes[self.current].className = "";

        self.timeout = window.setTimeout(self.changequote, 15000);
    }

    this.timeout = window.setTimeout(this.changequote, 15000)

}

/*
    News
*/

function news() {

    this.itemArray = new Array();
    // Get outer banner
    this.container = document.getElementById("news");
    this.news = this.container.getElementsByTagName("UL")[0];
    this.containerheight = this.container.offsetHeight;
    this.height = this.news.offsetHeight + 50;
    this.offset = 0;
    this.timer = null;

    // Set the status
    this.interval = 40;

    var self = this;

    // Animation functions
    this.animateSlide = function (v, src) {
        // Move the li
        if (self.offset < (self.height * -1)) {
            self.offset = self.containerheight;
        }
        else {
            self.offset -= 1;
        }

        self.news.style.marginTop = self.offset + "px";
    }

    // Slide
    this.slidepause = function () {
        //animate the slide
        window.clearInterval(self.timer);
        self.timer = null;
    }

    this.slideup = function () {
        //animate the slide
       self.timer = window.setInterval(self.animateSlide, self.interval);
    }

    // Add handlers
    if (isIE) {
        this.container.attachEvent("onmouseover", self.slidepause);
        this.container.attachEvent("onmouseout", self.slideup);
    }
    else {
        this.container.addEventListener("mouseover", self.slidepause, false);
        this.container.addEventListener("mouseout", self.slideup, false);
    }

    // Animate timer
    this.slideup();
}

onloadAdd('new news()');

