﻿/* Header Fader */
var images = new Array();
images[0]="Images/h2.jpg";
images[1]="Images/h1.jpg";

var preloadedImages = new Array();
var currentImage = 0;
var refreshRate = 3500.0;
var timeToFade = 800.0;
var irTimer = null;

/* Google Maps */
var ukZoom = 6;
var northNorfolkZoom = 10;
var nnrZoom = 12;
var stationZoom = 15;
var map = null;
var sheringhamStation = null;
var weyborneStation = null;
var holtStation = null;

function Initialise()
{
    var imageRotatorDIV = document.getElementById("imageRotator");
    imageRotatorDIV.style.background = "black url(" + images[currentImage] + ") no-repeat top center";

    if (document.images) 
    {
        // Preload images
        for (var i = 0; i < images.length; i++)
        {
            preloadedImages[i] = new Image();
            preloadedImages[i].src = images[i];
        }
    }
    
    StartTimer();
}

function initGoogleMaps() 
{
    if (GBrowserIsCompatible())
    {
        sheringhamStation = new GLatLng(52.9418, 1.208);
        weyborneStation = new GLatLng(52.9328, 1.1499);
        holtStation = new GLatLng(52.912305, 1.113546);

        map = new GMap2(document.getElementById("map_canvas"));

        map.setCenter(holtStation, northNorfolkZoom);
        
        map.addOverlay(createInfoMarker(sheringhamStation, "Sheringham Station"));
        map.addOverlay(createInfoMarker(weyborneStation, "Weybourne Station"));
        map.addOverlay(createInfoMarker(holtStation, "Holt Station / The Railway Institute"));

        map.setUIToDefault();

        map.addControl(new TextualZoomControl());
    }
}

function createInfoMarker(point, info)
{
    var marker = new GMarker(point);

    GEvent.addListener(marker, "click",
        function()
        {
          marker.openInfoWindowHtml(info);
        }
   );

    return marker;
}

function showUK()
{
    map.setCenter(holtStation, ukZoom);
}

function showNorthNorfolk()
{
    map.setCenter(holtStation, northNorfolkZoom);
}

function showNNR()
{
    map.setCenter(weyborneStation, nnrZoom);
}

function showSheringham() 
{
    map.setCenter(sheringhamStation, stationZoom);
}

function showWeybourne() 
{
    map.setCenter(weyborneStation, stationZoom);
}

function showHolt() 
{
    map.setCenter(holtStation, stationZoom);
}

function IsImageLoaded(img) 
{
    if (!img.complete) 
    {
        return false;
    }

    if (typeof img.naturalWidth != 'undefined' && img.naturalWidth == 0) 
    {
        return false;
    }

    return true;
}

function Cleanup()
{
    StopTimer();
}

function StartTimer()
{
    if (irTimer != null)
    {
        StopTimer();
    }

    irTimer = setInterval('OnTick()', refreshRate);
}

function StopTimer()
{
    if (irTimer != null)
    {
        clearInterval(irTimer);
    }
    
    irTimer = null;
}

function OnTick()
{
    StopTimer();

    var nextImage = currentImage + 1;
    if (nextImage >= images.length) 
    {
        nextImage = 0;
    }
    
    if (document.images) 
    {
        if (!IsImageLoaded(preloadedImages[nextImage]))
        {
            StartTimer();
            return;
        }
    }

    var fadeImageRotatorDIV = document.getElementById("fadeImageRotator");
    var imageRotatorDIV = document.getElementById("imageRotator");

    fadeImageRotatorDIV.style.background = "black url(" + images[currentImage] + ") no-repeat top center";
    $("#fadeImageRotator").show();
    
    // Set the current image div to the next image which we want to fade to
    imageRotatorDIV.style.background = "black url(" + images[nextImage] + ") no-repeat top center";

    // Next image
    currentImage = nextImage;

    // Fade out the old image
    $("#fadeImageRotator").fadeOut(2000);

    StartTimer();
}

function CreateAttribute(element, attributeName, attributeValue)
{
    var attribute = document.createAttribute(attributeName);
    attribute.value = attributeValue;      
    element.attributes.setNamedItem(attribute);   
}

function showModalPopupViaClient(alt, src, ev)
{
    var fullImageIMG = document.getElementById("fullImage");
    CreateAttribute(fullImageIMG, "alt", alt);
    CreateAttribute(fullImageIMG, "src", src);

    var modalPopupBehavior = $find('programmaticModalPopupBehavior');
    modalPopupBehavior.show();
}

function hideModalPopupViaClient(ev)
{
    ev.preventDefault();        
    var modalPopupBehavior = $find('programmaticModalPopupBehavior');
    modalPopupBehavior.hide();
    
    var fullImageIMG = document.getElementById("fullImage");
    CreateAttribute(fullImageIMG, "alt", "");
    CreateAttribute(fullImageIMG, "src", "");
}

function initShadowBox()
{
    var options = 
        {
            overlayColor: '#939393',
            overlayOpacity: '0.8',
            onClose:shadowBoxClosed
        }; 
    Shadowbox.init(options); 
}

function showShadowBox(elementID, title)
{
    StopTimer();

    var element = document.getElementById(elementID);
    if (element != null)
    {
        Shadowbox.open
        (
            {
                player: 'html', 
                title:title, 
                content:element.innerHTML, 
                width:800, 
                height:120, 
                overlayOpacity:0, 
                resizeDuration:0.2,
                onClose:shadowBoxClosed
            }
        );
    }
}

function shadowBoxClosed(gallery)
{
    StartTimer();
}

