﻿//javascript for processing general jave script
//Author: Ben Hooper
//Date: 25 July 2009
var TwitterDelayLong = 25000;
var TwitterDelayShort = 5000;
var LastTwitterID = -1;
var mapTwitter = null;


function masterpage_onload()
{  
     /*jQuery:*/
     $(document).ready(function() 
     {
         $('#nw_hmenu_bar li.headlink').hover(
            function() { $('ul', this).css('display', 'block'); },
            function() { $('ul', this).css('display', 'none'); });
     });
    
    if (window.content_onload != null)
        window.content_onload(); //this condition check whether the rendered page has function are not.
}

function masterpage_onunload() {
  if (window.content_onunload != null)
      window.content_onunload(); //this condition check whether the rendered page has function are not.
}


//-------------------------general functions------------------------
function mixuppictures() {   
    var divMiddleImage = document.getElementById('MiddleTitleStamp');
    if (divMiddleImage != null) {       
        divMiddleImage.style.visibility = "visible";
    }

    $('#MiddleTitleStamp').ready(function() {
    $('#MiddleTitleStamp').animate({ opacity: 0}, 0, function() {
    $('#MiddleTitleStamp').animate({ opacity: 1.0 }, 2000);
        });
    });
    
          
    var i = 0;
    var ran = 0;
    for (i = 1; i <= 3; i++) {
        var picture = "pic" + i;
        
        var dv = document.getElementById(picture);
        var randomnumber = Math.floor(Math.random() * 10);
        randomnumber = randomnumber + 1;

        if (dv != null) {
            dv.style.zIndex = randomnumber;
            dv.style.visibility = "visible";

            var postcardID = "#" + picture;    
            $(postcardID).ready(function() {    
                $(postcardID).animate({ opacity: 0.0 }, 0, function() {
                    $(postcardID).animate({ opacity: 1.0 }, 3000);
                });         
            });             
        }
    }    
}

//-----------------------------read latest twitters from the database----------------
//Set a timer to read the system twitters on
function ReadTwitters() {

    var dv = document.getElementById("tweetdisplay");
    if (dv != null) {        
        dv.style.visibility = "visible";
    }
    
    //slowley faid up the opacity of the twitter
    $('.tweet').ready(function() {
        $('.tweet').animate({ opacity: 0.0 }, 0, function() {
            $('.tweet').animate({ opacity: 1.0 }, 1000);
        });       

        //start a timer to read the next twitter   
        setTimeout(function() { funcReadSystemTwitter(); }, TwitterDelayShort);
    });
  
   
}

//load the xml for the system twitter
function funcReadSystemTwitter() {   
    var status;
    var date;
    var latitude, longitude;
    $(document).ready(function() {
        $.ajax({
            type: "GET",
            url: "twitter.ashx?id=" + LastTwitterID,
            dataType: "xml",
            success: function(xml) {
                $(xml).find('twitter').each(function() {
                    status = $(this).find('twitterstatus').text();
                    $(xml).find('twitterdetails').each(function() {
                        date = $(this).attr('date');
                        LastTwitterID = $(this).attr('id');
                        latitude = $(this).attr('y');
                        longitude = $(this).attr('x');
                    });
              
                    //make the move seamlessly
                    $('.tweet').animate({ opacity: 0.0 }, 1000, function() {
                        $('#twitter_text').html(status);
                        $('#twitter_date').text(date);
                        $('.tweet').animate({ opacity: 1.0 }, 1000, function() { movetwittermap(latitude, longitude); }); //move the twitter map to the item pulled from the XML
                    });
                })
            },
            error: function(xml, ajaxOptions, thrownError) {
                //alert(xml.status);
            }
        });
    });

    //reset the delay to read the twitter
    setTimeout(function() { funcReadSystemTwitter(); }, TwitterDelayLong);
}

//javascript for processing google maps
//Author: Ben Hooper
//Date: 25 July 2009
function initialize(latitude, longitude, maptype) 
{
    if (GBrowserIsCompatible()) {
        var mapNews = new GMap2(document.getElementById("map_canvas"));
        switch (maptype) {
            default:
                mapNews.setMapType(G_NORMAL_MAP);
                break;
            case 1:
                mapNews.setMapType(G_SATELLITE_MAP);
                break;
            case 2:
                mapNews.setMapType(G_HYBRID_MAP);
                break;
            case 3:
                mapNews.setMapType(G_PHYSICAL_MAP);
                break;
        };

        //set up the UI for the tweet map
        var ui = mapNews.getDefaultUI();
        ui.zoom.scrollwheel = false;
        mapNews.setUI(ui);

        var latlng = new GLatLng(latitude, longitude);
        mapNews.setCenter(latlng, 13);
        mapNews.addOverlay(new GMarker(latlng));
    }
}

///display map for a tweet
function initializetweet(latitude, longitude) {
    if (GBrowserIsCompatible()) {
        mapTwitter = new GMap2(document.getElementById("map_tweet"));
        mapTwitter.setMapType(G_PHYSICAL_MAP);
        //set up the UI for the tweet map
        var ui = mapTwitter.getDefaultUI();
        ui.controls.scalecontrol = false;
        ui.controls.smallzoomcontrol3d = true;
        ui.zoom.scrollwheel = false;
        ui.zoom.doubleclick = false;
        ui.controls.maptypecontrol = false;
        mapTwitter.setUI(ui);

        var latlng = new GLatLng(latitude, longitude);
        mapTwitter.setCenter(latlng, 8);
    }
}

//move the twitter map when required, new tweet incoming
function movetwittermap(latitude, longitude) {
    if (mapTwitter == null)
        return;
    var latlng = new GLatLng(latitude, longitude);
    mapTwitter.panTo(latlng);        
}

//display the map for the site map page
function initializemapsitemap(latitude, longitude) {
    if (GBrowserIsCompatible()) {
        var mapSitemap = new GMap2(document.getElementById("map_sitemap"));
        mapSitemap.setMapType(G_PHYSICAL_MAP);
        //set up the UI for the tweet map
        var ui = mapSitemap.getDefaultUI();
        ui.controls.scalecontrol = false;
        ui.controls.smallzoomcontrol3d = false;
        ui.zoom.scrollwheel = false;
        ui.zoom.doubleclick = false;
        ui.controls.maptypecontrol = false;
        mapSitemap.setUI(ui);

        var latlng = new GLatLng(latitude, longitude);
        mapSitemap.setCenter(latlng, 5);
    }
}
