﻿/// <reference path="/Kooboojs/jquery-1.4.1.js"/>
/// <reference path="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=ABQIAAAAXqdlwIyFc38TnDZCpFrVGRRFAgPNFoPgMWltjHrJgGvbEhsHiBQJeXw_AnUx3PhO-kHCoTRIwMbAow"/>

jQuery.fn.extend({
    disableSelection: function() {
        this.each(function() {
            this.onselectstart = function() { return false; };
            this.unselectable = "on";
            jQuery(this).css('-moz-user-select', 'none');
        });
    }
}); 


var Idonix = {};

Idonix.Initialize_Menu = function() {

    var subMenuUl = $(".sub-menu");

    if (subMenuUl.css("display") == "none") {

        var hoverCount = 0;
        var animationSpeed = 250;
        var infoTextHoldTime = 300;
        var subMenuHoldTime = 700;
        var showDefaultPageInfo = false;
        var hideSubmenu = false;

        var selectedMenuItemId = $("ul.main-menu > li.selected").attr("id");

        var currentPage = ""; 

        if (selectedMenuItemId != undefined) {
            currentPage = selectedMenuItemId.substring(9, selectedMenuItemId.length);
        }

        var activePage = currentPage;

        var menuItem = $(".menu-item");
        var subMenuItems = subMenuUl.children();

        subMenuItems.css("opacity", "0");
        subMenuUl.css("left", "-60px");

        var showPageInfo = function(pageName) {

            // Only show this page if we arent already on it
            if (pageName != activePage) {

                // Does this page have a description?
                var descriptionDiv = $("#introText_" + pageName);
                if (descriptionDiv.length > 0) {

                    // Fade out all other intro text divs
                    $(".intro-text").hide();

                    // Fade in the description for this page
                    descriptionDiv.fadeIn(animationSpeed);

                    // No longer want to go back to the current pages description.
                    showDefaultPageInfo = false;

                    // This is now our active page
                    activePage = pageName;
                }
            }
        };

        var resetPageInfo = function() {

            // We want to show the current pages info text..
            showDefaultPageInfo = true;

            // So we check this is still the case after infoTextHoldTime expires  
            setTimeout(function() {
                if (showDefaultPageInfo && activePage != currentPage) {
                    // Do it
                    showPageInfo(currentPage);
                }
            }, infoTextHoldTime);
        };

        // Enter menu item
        var onEnter = function() {

            hoverCount++;

            var item = $(this);
            var pageName = this.id.substring(9, this.id.length);
            var subMenu = item.children(".sub-menu");
            var subMenuChildren = $(subMenu.children());
            var pageInfoDiv = $("#introText_" + pageName);

            // When re-hovering over a sub menu item,
            // the owning menu-item will also raise a hover event.
            // This is a hack to stop this second event from resetting the info text.
            if (!(item.hasClass("hasSubMenu") && hoverCount > 1)) {
                // Show this pages info text
                showPageInfo(pageName);
            }

            // Show the sub menu for this page
            if (subMenu.length > 0) {

                // We no longer want to hide the submenu
                hideSubmenu = false;

                // Fade in the sub menu
                subMenu.css("display", "block");
                subMenu.animate({ left: "0px" }, 300);
                subMenuChildren.animate({ opacity: "1" }, 300);

            }

        };

        // Leave menu item
        var onLeave = function() {

            hoverCount--;

            var item = $(this);
            var pageName = this.id.substring(9, this.id.length);
            var subMenu = item.children(".sub-menu");
            var subMenuChildren = subMenu.children();

            // Hide the sub menu
            if (subMenu.length > 0) {

                // We want to hide the submenu..
                hideSubmenu = true;

                // So we check this is still the case after subMenuHoldTime expires  
                setTimeout(function() {
                    if (hideSubmenu) {

                        subMenu.animate(
                            { left: "60px" },
                            200,
                            "linear",
                            function() {
                                if (hideSubmenu) {
                                    subMenu.css("left", "-60px");
                                    subMenu.css("display", "none");
                                    hideSubmenu = false;
                                }
                            });

                        subMenuChildren.animate(
                            { opacity: "0" },
                            200,
                            "linear",
                            function() {
                                if (hideSubmenu) {
                                    //subMenuChildren.css("left", "-60px");
                                }
                            });

                    }
                }, subMenuHoldTime);
            }

            // Reset the info text to the current pages description.
            resetPageInfo();

        };

        menuItem.hover(onEnter, onLeave);
        menuItem.click(onEnter);

    }
}

Idonix.Initilize_LoginBox = function() {

    var loginBox = $(".login-box");
    var loginFormContainer = $("#loginContainer");
    var animationSpeed = 200;
    var holdtime = 800;
    var timerElapsed = false;
    var shouldFadeOut = false;
    var inState = null;
    var outState = null;

    if(jQuery.browser.msie) { 
        inState = { height: "145px" };
        outState = { height: "0px" };
    }
    else {
        inState = { height: "145px", opacity: "1" };
        outState = { height: "0px", opacity: "0" };
        loginFormContainer.css("opacity", "0");  
    }

    var fadeIn = function() {

        loginFormContainer.clearQueue();
        if(!jQuery.browser.msie) { loginFormContainer.css("opacity", "0"); }
        loginFormContainer.css("height", "0px");

        loginFormContainer.css("display", "block");
        loginFormContainer.animate(
        inState,
        animationSpeed);
    }

    var fadeOut = function() {

        loginFormContainer.clearQueue();
        if(!jQuery.browser.msie) { loginFormContainer.css("opacity", "1"); }
        loginFormContainer.css("height", "145px");

        loginFormContainer.animate(
        outState,
        animationSpeed,
        "linear",
        function() {
            loginFormContainer.css("display", "none");
        });

    }
        
    loginBox.hover(
        function() {

            if (shouldFadeOut) {
                shouldFadeOut = false;
            }
            else {
                fadeIn();

                shouldFadeOut = false;
                timerElapsed = false;

                setTimeout(function() {
                    timerElapsed = true;
                    if (shouldFadeOut) {
                        fadeOut();
                        shouldFadeOut = false;
                    }
                }, holdtime);
            }

        },
        function() {
            if (timerElapsed) {
                fadeOut();
            }
            else {
                shouldFadeOut = true;
            }
        });

}

Idonix.Initialize_GoogleMap = function() {

    if (typeof GBrowserIsCompatible != 'undefined') {
        if (GBrowserIsCompatible()) {

            var map = new GMap2(document.getElementById("map_canvas"));

            if (map != null) {
                map.addControl(new GLargeMapControl());
                map.addControl(new GMapTypeControl());

                map.setCenter(new GLatLng(52.59724, -2.558114), 14);

                var point = new GLatLng(52.59724, -2.558114);
                var marker = new GMarker(point);

                var html = '<p style="color:black;"><em>Idonix</em><br />7 Sheinton Street<br />Much Wenlock<br />Shropshire<br />TF13 6HT</p>';

                GEvent.addListener(marker, "click", function() {
                    marker.openInfoWindowHtml(html);
                });

                map.addOverlay(marker);
                map.openInfoWindow(map.getCenter(), html);
            }
        }
    }
}

var EDay = {};

EDay.InitializeWidget = function(widgetType, width, elementId) {

    var showFlash = true;
    var altImgURL = "";
    var siteName = "";
    var meterType = "";
    var height = width * 0.3792;
    var title = "E-Day";
    var bgColour = "#FFFFFF"
    var titleURL = "http://www.e-day.org.uk";
    var scaleMode = "showAll";
    var link = "http://www.e-day.org.uk";

    switch (widgetType) {
        case "OfficeDial":
            siteName = "family";
            meterType = "Dial";
            bgColour = "#000000";
            title = "Idonix Office"
            showFlash = true;
            break;
        case "NationalDial":
            siteName = "national";
            meterType = "Dial";
            bgColour = "#00BEF2";
            title = "National electricity usage"
            showFlash = true;
            break;
        case "NationalCost":
            siteName = "national";
            meterType = "CostMeter";
            bgColour = "#00BEF2";
            title = "National electricity Bill"
            showFlash = true;
            break;
        case "NationalDemandCurve":
            siteName = "national";
            meterType = "Graph";
            bgColour = "#00BEF2";
            title = ""
            showFlash = true;
            break;
        case "NationalC02":
            siteName = "national";
            meterType = "EmmisionsMeter";
            bgColour = "#00BEF2";
            title = "National electricity C02 Emissions"
            showFlash = true;
            break;
        case "Summary":
            siteName = "national,island,school,family historic";
            meterType = "Summary";
            showFlash = (new Date() >= Date.parse("Oct 6, 2009"));
            altImgURL = "http://www.e-day.org.uk/Portals/0/Images/staticSummaryImage_1.png";
            break;
        case "ScillyOMeter":
            siteName = "island";
            meterType = "CircleMeter";
            bgColour = "#00709E";
            showFlash = true;
            break;
        case "IslandDemandCurve":
            siteName = "island";
            meterType = "Graph";
            bgColour = "#00709E";
            showFlash = true;
            break;
        default:
            siteName = "national,island,school,family";
            meterType = "Summary";
            showFlash = (new Date() >= Date.parse("Oct 6, 2009"));
            altImgURL = "http://www.e-day.org.uk/Portals/0/Images/staticSummaryImage_1.png";
            break;
    }

    var html = "";

    if (showFlash) {
        html = "<div style='background-color:" + bgColour + "; width:" + width + "px; height:" + height + "px;'><embed src='http://www.e-day.org.uk/Portals/0/widgets/Widgets_8.swf' class='flash' scaleMode='" + scaleMode + "' FlashVars='_MeterType=" + meterType + "&_SiteName=" + siteName + "&_Title=" + title + "&_TitleURL=" + titleURL + "&_Link=" + link + "&_WebServiceHost=http://www.e-day.org.uk/dataservice/' quality='high' wmode='transparent' bgcolor='" + bgColour + "' width='" + width + "' height='" + height + "' name='Widgets' align='top' allowScriptAccess='sameDomain' allowFullScreen='false' type='application/x-shockwave-flash' pluginspage='http://www.adobe.com/go/getflashplayer' /></div>";
    }
    else {
        html = "<a href='" + link + "' target='_blank'><img width='" + width + "' height='" + height + "' border='0' alt='" + title + "' src='" + altImgURL + "'></img></a>";
    }

    if (elementId != undefined) {
        var element = document.getElementById(elementId);
        if (element != undefined) {
            element.innerHTML = html;
        }
    }

    return html;
}

$(document).ready(function() {

    Idonix.Initialize_Menu();
    Idonix.Initilize_LoginBox();
    Idonix.Initialize_GoogleMap();

});
