﻿
function showHideTags(bob) {
    document.getElementById('dotdotdot' + bob).style.display = 'none'; 
    document.getElementById('show' + bob).style.display = 'inline';
}

//var loaded = false;

//function load()
//{
//    alert('load');
//    loaded = true;
//}

function addEvent(obj, evType, fn){ 
    if (obj.addEventListener){ 
        obj.addEventListener(evType, fn, false); 
        return true; 
    } else if (obj.attachEvent){ 
        var r = obj.attachEvent("on"+evType, fn); 
        return r; 
    } else { 
        return false; 
    } 
}

/****************************************************************************************************************/

/* GOOGLE MAP */

var map;
var marker;
var overlayInstance = null;
var client;
var lastMarkerLocation;
var panorama;
var mapT;

var zoomTB;

var circle;
var rad;
var col;
var zoomCircleRadius;

var rememberLat;
var rememberLng;

function getCircleRadius(zoomLevel) {
    switch (zoomLevel) {
        case 5:
            zoomCircleRadius = 400000;
            break;
        case 6:
            zoomCircleRadius = 200000;
            break;
        case 7:
            zoomCircleRadius = 100000;
            break;
        case 8:
            zoomCircleRadius = 50000;
            break;
        case 9:
            zoomCircleRadius = 25000;
            break;
        case 10:
            zoomCircleRadius = 12500;
            break;
        case 11:
            zoomCircleRadius = 6000;
            break;
        case 12:
            zoomCircleRadius = 3000;
            break;
        case 13:
            zoomCircleRadius = 1500;
            break;
        case 14:
            zoomCircleRadius = 750;
            break;
        case 15:
            zoomCircleRadius = 375;
            break;
        default:
            zoomCircleRadius = 0;
            break;
    }
}

function zoomOutToShowPointOnMap(lat, lng) {
    if (map && rememberLat && rememberLng) {
        var point = new GLatLng(lat, lng);
        var bounds = map.getBounds();
        while (!bounds.containsLatLng(point) && map.getZoom() > 3) {
            map.zoomOut();
            bounds = map.getBounds();
            
//            var zoom = map.getZoom();
//            alert('Zoom = ' + zoom);
        }
    }
}

function refreshCenter()
{
    //alert('start refresh center ' + rememberLat + ' ' + rememberLng);
    map.setCenter(new GLatLng(rememberLat, rememberLng), 12);
    //alert('refresh center ' + rememberLat + ' ' + rememberLng);
}

function getIcon1()
{
    var icon1 = new GIcon(G_DEFAULT_ICON, "/Images/number_1.jpg");

    
    return icon1;
}

function getIcon(letter)
{
//    var icon = new GIcon(G_DEFAULT_ICON, "<%= Page.ResolveUrl("~/images/marker")%>" + letter + ".png");
//    return icon;
    return null;
}

function getIconFromUrl(markerUrlX)
{
//    alert(markerUrlX);

    var icon = new GIcon(G_DEFAULT_ICON, markerUrlX);
    return icon;
}

function getIconGreenLetter(letter)
{
//    var icon = new GIcon(G_DEFAULT_ICON, "http://www.google.com/intl/en_au/mapfiles/marker_green" + letter + ".png");
//    return icon;
    return null;
}

function getIconGreen()
{
//    var icon = new GIcon(G_DEFAULT_ICON, "<%= Page.ResolveUrl("~/images/marker")%>_green.png");
    return null;
}

function getArrow()
{
    return new GIcon(G_DEFAULT_ICON, "http://www.google.com/intl/en_us/mapfiles/arrow.png");
}

function basicMap()
{
    map = null;
    if (GBrowserIsCompatible()) 
    {
    //tim review what i have done but note that when run in IE the size 
    // values are 0 and 0 essentially this function is being run too early 
    //and either needs a pause in IE or needs to be attached to the on load event
    // also note the checkResize function is shit
    
        var mapdom = document.getElementById("map");
        if(mapdom && !map){
        if(mapdom.clientHeight!=0){
        
//        mapdom.style.height = "100px";
//        mapdom.style.width = "100px";
        map = new GMap2(mapdom);
        map.setCenter(new GLatLng(-31.955400, 115.858590), 8);
        map.addControl(new GSmallMapControl());
        //map.addControl(new GMenuMapTypeControl());
        //map.addControl(new GLargeMapControl());
        map.enableDoubleClickZoom();
        map.enableContinuousZoom();
        
        GEvent.addListener(map, "zoomend", function(oldLevel, newLevel) {
            resizeCircle(newLevel);
//            if(newLevel > 13) {
//                putGuyOnMap(map.getCenter());
//            } else {
//                hideGuy();
//            }
        });
        GEvent.addListener(map, "dblclick", function(overlay, latlng) {
            if(map.getZoom() > 11) {
                putGuyOnMap(latlng);
            }
        });
        
        while (!map.isLoaded())
        {}
        //var thesize = map.getSize();
        zoomTB = document.getElementById("ctl00_cphBottom_lblZoomInfo");
        
        map.checkResize();
        //bob.innerHTML += " height" + thesize.height + " width" + thesize.width;
        mapdom.style.height = "100%";
        mapdom.style.width = "100%";
            
//        map.size.height = 100;
//        map.size.width = 100;
//        
            }
        }
        //map.x = mapdom
    }
}

function putGuyOnMap(lat, lng) {
    putGuyOnMap(new GLatLng(lat, lng));
}

function putGuyOnMap(location) {
    if(!client) client = new GStreetviewClient();
    
    if(!marker) {
        var guyIcon = new GIcon(G_DEFAULT_ICON);
        guyIcon.image = "http://maps.google.com/intl/en_us/mapfiles/cb/man_arrow-0.png";
        guyIcon.transparent = "http://maps.google.com/intl/en_us/mapfiles/cb/man-pick.png";
        guyIcon.imageMap = [
            26,13, 30,14, 32,28, 27,28, 28,36, 18,35, 18,27, 16,26,
            16,20, 16,14, 19,13, 22,8
         ];
        guyIcon.iconSize = new GSize(49, 52);
        guyIcon.iconAnchor = new GPoint(25, 35);  // near base of guy's feet
        guyIcon.infoWindowAnchor = new GPoint(25, 5);  // top of guy's head

        marker = new GMarker(location, {icon: guyIcon, draggable: true});
        map.addOverlay(marker);
        lastMarkerLocation = location;
        GEvent.addListener(marker, "dragend", onDragEnd);
        GEvent.addListener(marker, "click", openPanoramaBubble);
//        GEvent.addListener(map, "moveend", function() {
//            putGuyOnMap(map.getCenter());
//        });
    } else {
        marker.show();
        marker.setLatLng(location);
    }
}

function hideGuy() {
    marker.hide();
}
function openPanoramaBubble() {
  var contentNode = document.createElement('div');
  contentNode.style.textAlign = 'center';
  contentNode.style.width = '500px';
  contentNode.style.height = '300px';
  contentNode.innerHTML = 'Loading panorama';
  marker.openInfoWindow("<div id='pano' style='width:200px;height:200px;'></div>", {maxContent: contentNode, maxTitle: "Street View"});

  panorama = new GStreetviewPanorama(document.getElementById("pano"));
  panorama.setLocationAndPOV(marker.getLatLng(), null);
  GEvent.addListener(panorama, "newpano", onNewLocation);
  GEvent.addListener(panorama, "yawchanged", onYawChange); 

  var iw = map.getInfoWindow();
  GEvent.addListener(iw, "maximizeend", function() {
    panorama.setContainer(contentNode);  
    window.setTimeout("panorama.checkResize()", 5);
  });
}

function toggleOverlay() {
  if (!overlayInstance) {
    overlayInstance = new GStreetviewOverlay();
    map.addOverlay(overlayInstance);
  } else {
    map.removeOverlay(overlayInstance);
    overlayInstance = null;
  }
}

function onYawChange(newYaw) {
  var GUY_NUM_ICONS = 16;
  var GUY_ANGULAR_RES = 360/GUY_NUM_ICONS;
  if (newYaw < 0) {
    newYaw += 360;
  }
  guyImageNum = Math.round(newYaw/GUY_ANGULAR_RES) % GUY_NUM_ICONS;
  guyImageUrl = "http://maps.google.com/intl/en_us/mapfiles/cb/man_arrow-" + guyImageNum + ".png";
  marker.setImage(guyImageUrl);
}

function onNewLocation(lat, lng) {
  var latlng = new GLatLng(lat, lng);
  marker.setLatLng(latlng);
}

function onDragEnd() {
  var latlng = marker.getLatLng();
  if (panorama) {
    client.getNearestPanorama(latlng, onResponse);
  }
}

function onResponse(response) {
  if (response.code != 200) {
    marker.setLatLng(lastMarkerLocation);
  } else {
    var latlng = new GLatLng(response.Location.lat, response.Location.lng);
    marker.setLatLng(latlng);
    lastMarkerLocation = latlng;
    openPanoramaBubble();
  }
}

function resizeCircle(zoomLevel) {
    //alert('Zoom = ' + zoomLevel);
    if(circle) {
        map.removeOverlay(circle);
        zoomTB.innerHTML = "";
        if(getCircleRadius(zoomLevel) != 0) {
            drawCircle(rememberLat, rememberLng, zoomCircleRadius, col, 2, 0.8, 200);
        }
    }
}

function displaylatlng(lat, lng) 
{
    if (!map) 
    {
        map = document.getElementById("map");
        if (GBrowserIsCompatible()) {
            map = new GMap2(document.getElementById("map"));
            map.setCenter(new GLatLng(lat, lng), 12);
        }
    }
}

function displayMarker(lat,lng)
{
    if (map)
    {
        var markerUrl = baseUrl + "Images/marker_blue.png";
        var marker = new GMarker(new GLatLng(lat, lng), getIconFromUrl(markerUrl));
        map.addOverlay(marker);
    }
}

function displayMarker1(lat,lng)
{
    var marker = new GMarker(new GLatLng(lat, lng), getIcon1());
    map.addOverlay(marker);
}

function displayMarkerWithLetter(markerUrl, lat, lng, letter)
{
    map.addOverlay(new GMarker(new GLatLng(lat, lng), getIconFromUrl(markerUrl)));
}

function displayMarkerArrow(lat, lng)
{
    map.addOverlay(new GMarker(new GLatLng(lat, lng), getArrow()));
}

function displayMarkerWithMessage(lat,lng,message)
{
    var marker = new GMarker(new GLatLng(lat, lng));
    map.addOverlay(marker);
    marker.openInfoWindowHtml(message);
    centermap(lat, lng);
}

function centermap(lat, lng)
{
//hack by Tim here
if(map){
    var loc = new GLatLng(lat, lng);
    map.setCenter(loc, 12);
    rememberLat = lat;
    rememberLng = lng;
//    alert('set center complete ' + lat + ' ' + lng);
}
}

function displayMarkerGreen(markerUrl, lat, lng)
{
if(map){
    var marker = new GMarker(new GLatLng(lat, lng), getIconFromUrl(markerUrl));
    map.addOverlay(marker);
    centermap(lat, lng);
    }
}

function displayMarkerGreenMessage(markerUrl, lat, lng, message)
{
if(map){
    var marker = new GMarker(new GLatLng(lat, lng), getIconFromUrl(markerUrl));
    map.addOverlay(marker);
    marker.openInfoWindowHtml(message);
    centermap(lat, lng);
}}

/**
 * radius - meters
 * colour - string of hex, ie '#RRGGBB'
 * weight - width in px
 * opacity - double from 0 to 1
 * numVerticies - the number of verticies to draw the circle out of
 */
function drawCircle(centerLat, centerLng, radius, colour, weight, opacity, numVerticies)
{
    center = new GLatLng(centerLat, centerLng);
    rad = radius / 1000;
    col = colour;
    
    if (zoomTB && rad != 0) {
        if (radius > 1000) {
            zoomTB.innerHTML = "Circle radius = "+ rad +"km";
        } else {
            zoomTB.innerHTML = "Circle radius = "+ radius +"m";
        }
    }
    pts = new Array(numVerticies + 1);
    
    for (i = 0; i < numVerticies; i ++)
    {
        pts[i] = pointAtRangeAndBearing(center, radius, 360 * i / numVerticies);
    }
    pts[numVerticies] = pts[0];
    
    circle = new GPolyline(pts, colour, weight, opacity);
    
    if(map){
        map.addOverlay(circle);
    }
    circle.show();
}

function pointAtRangeAndBearing(point, distanceMeters, azimuthDegrees)
{
     var latr = point.lat() * Math.PI / 180.0;
     var lonr = point.lng() * Math.PI / 180.0;

     var coslat = Math.cos(latr);
     var sinlat = Math.sin(latr);
     var az = azimuthDegrees* Math.PI / 180.0;
     var cosaz = Math.cos(az);
     var sinaz = Math.sin(az);
     var dr = distanceMeters / 6378137.0; // distance in radians using WGS84 Equatorial Radius in metres
     var sind = Math.sin(dr);
     var cosd = Math.cos(dr);

    return new GLatLng(Math.asin((sinlat * cosd) + (coslat *
        sind * cosaz)) * 180.0 / Math.PI,
    (Math.atan2((sind * sinaz), (coslat * cosd) - (sinlat *
        sind * cosaz)) + lonr) * 180.0 / Math.PI);
}

function displayMarkerWithLetterRedirect(markerUrl, lat, lng, letter, redirectUrl)
{
    
//    alert(markerUrl);
    
    var marker = new GMarker(new GLatLng(lat, lng), getIconFromUrl(markerUrl));
   if(map){ map.addOverlay(marker);}
    
    GEvent.addListener(
        marker, 
        "click", 
        function() 
        {
            redirect(redirectUrl);
        });
}

function markerClick(marker)
{
//    alert('click');
}

function redirect(url)
{
    window.open(url);
}



/* END GOOGLE MAP */

/****************************************************************************************************************/

/* MODAL POPUP */


var styleToSelect;
        
var popupX, popupY;

function onOk() {
    $get('Paragraph1').className = styleToSelect;
}

// Add click handlers for buttons to show and hide modal popup on pageLoad
//function pageLoad() {
//    $addHandler($get("showModalPopupClientButton"), 'click', showModalPopupViaClient);
//    $addHandler($get("hideModalPopupViaClientButton"), 'click', hideModalPopupViaClient);        
//}

function showModalPopupViaClient(ev) {
    ev.preventDefault();
    var modalPopupBehavior = $find('programmaticModalPopupBehavior');
    modalPopupBehavior.show();
}

function hideModalPopupViaClient(ev) {
    ev.preventDefault();        
    var modalPopupBehavior = $find('programmaticModalPopupBehavior');
    modalPopupBehavior.hide();
}

function popupOk()
{
    alert("ok");
}

function mousedown(mEvent)
{
    popupX = mEvent.clientX;
    popupY = mEvent.clientY;
}

function hideSpan(spanid)
{
    var span = document.getElementById(spanid);
    if (!span) 
    {
        alert("no span :( " + spanid);
        return;
    }
    span.className="hide";
}

function showSpan(spanid)
{
    var span = document.getElementById(spanid);
    if (!span) 
    {
        alert("no span :( " + spanid);
        return;
    }
    span.className="show";
}


/* END MODAL POPUP */

/****************************************************************************************************************/

/* DISPLAY ADDRESS CONTROL */


function getControlAddress(ctlAddressElementBase)
{
    
    var controlAddress;
    controlAddress = document.getElementById(ctlAddressElementBase + "tbStreetAddress").value;
    controlAddress += " " + document.getElementById(ctlAddressElementBase + "tbSuburbTown").value;
    controlAddress += " " + document.getElementById(ctlAddressElementBase + "ddlState").value;
    controlAddress += " " + document.getElementById(ctlAddressElementBase + "tbPostcode").value;
    controlAddress += " " + document.getElementById(ctlAddressElementBase + "tbCountry").value;
    controlAddress += " " + document.getElementById(ctlAddressElementBase + "tbPhoneNumber").value;
    
    return controlAddress;
}


/* END DISPLAY ADDRESS CONTROL */

/****************************************************************************************************************/

/* TINY MCE */


function initialiseTinyMCE_AllFeatures()
{
    tinyMCE_GZ.init({
        plugins : 'style,layer,table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras',
        themes : 'simple,advanced',
        languages : 'en',
        disk_cache : true,
        debug : false
    });
    
    tinyMCE.init({
        mode : "textareas",
        theme : "advanced",
        plugins : "style,layer,table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,zoom,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras",
        theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
        theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
        theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
        theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,|,visualchars,nonbreaking",
        theme_advanced_toolbar_location : "top",
        theme_advanced_toolbar_align : "left",
        theme_advanced_statusbar_location : "bottom",
        extended_valid_elements : "a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]"
    });
}

function initialiseTinyMCE()
{
    tinyMCE_GZ.init({
        plugins : 'style,layer,table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras',
        themes : 'simple,advanced',
        languages : 'en',
        disk_cache : true,
        debug : false
    });
    
    tinyMCE.init({
        mode : "textareas",
        theme : "advanced",
        plugins : "style,layer,table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,zoom,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras",
        theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,fontselect,fontsizeselect,|,sub,sup",
        theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,undo,redo,|,image,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
        theme_advanced_buttons3 : "",
        theme_advanced_buttons4 : "",
        theme_advanced_toolbar_location : "top",
        theme_advanced_toolbar_align : "left",
        theme_advanced_statusbar_location : "bottom",
        extended_valid_elements : "a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]"
    });
}

        /* Text Boxes to be Re-Initialised should be surrounded with the to prevent errors */

function initialiseATinyMCE(texboxID, spanID) {
    if (document.getElementById(texboxID) != null) {
        var span = document.getElementById(spanID);
        if(span.innerHTML.match("mce_editor") == null) {
            tinyMCE.execCommand('mceAddControl', false, texboxID );
        }
    }
}


/* END TINY MCE */
        
/****************************************************************************************************************/

/* TAG DROP DOWN */


function checkDropDownListTag(ddlTagID)
{
//    var val = document.getElementById(ddlTagID).value;
//    if (val == "0") 
//    {
//        return false;
//    }
    return true;
}

/* END TAG DROP DOWN */


/****************************************************************************************************************/



/****************************************************************************************************************/
/****************************************************************************************************************/
/****************************************************************************************************************/
/****************************************************************************************************************/
/****************************************************************************************************************/
/****************************************************************************************************************/
/****************************************************************************************************************/
/****************************************************************************************************************/
/****************************************************************************************************************/
/****************************************************************************************************************/
/****************************************************************************************************************/
/****************************************************************************************************************/
/****************************************************************************************************************/
/****************************************************************************************************************/
/****************************************************************************************************************/