﻿var map = null;
var SportsDoChart = new TempChart();

function TempChart()
{
  this.style = null;
}

TempChart.prototype.SetReturnValue = function(x)
{
}

    
function getMap()
{
  if(map == null)
  {
    if(document.getElementById("type").value=="live" )
    {
      map = new SMap('map', 'live');
    }
    else
    {
      map = new SMap('map','google');
    }
  }
  return map;
}

function moveDoer(x, y)
{
  getMap().showDoer(x, y);
}

function highlight(min, max)
{
  getMap().draw(min, max);
}

function loadLap(n, x, y, t, s)
{
  getMap().loadLap(n, x, y, t, s);
}


function loadPhotos(p)
{
  if(p!=null)
  {
    for(i=0;i<p.list.source.length;i++)
    {
      getMap().photos.push(p.list.source[i]);
      //getMap().drawPhoto(getMap().photos[i]);
    }
  }
}

function loadPois(p)
{
  if(p!=null)
  {
    for(i=0;i<p.list.source.length;i++)
    {
      getMap().pois.push(p.list.source[i]);
    }
  }

  if(document.getElementById("resortId") != null)
  {
    if(document.getElementById("resortId").value != "0")
    {
      getMap().centrePois();
      getMap().draw();
    }
  }
}

function setStart(x, y)
{
  getMap().setStart(x, y);
}

function setEnd(x, y)
{
  getMap().setEnd(x, y);
}

function centre()
{
  getMap().resetCentre();
}

function clearLaps()
{
    getMap().clear();
}

function initChart(p)
{
  var prms = "";
  if(document.getElementById("resortId") != null && document.getElementById("resortId").value!="0")
  {
    prms = 'rid=' + document.getElementById("resortId").value;
  }
  else if(document.getElementById("sessionId") != null)
  {
    prms = 'id=' + document.getElementById("sessionId").value;
  }
  else
  {
    if(document.getElementById("prms") != null)
    {
      prms = document.getElementById("prms").value;
    }
    else
    {
      prms = 'userId=' + document.getElementById("userId").value + '&datespec=4h&ambient=1';
    }
  }
  CheckFlashInstall(
			"src", "/SportsDoChart",
			"width", "100%",
			"height", "100%",
			"align", "middle",
			"id", "SportsDoChart",
			"quality", "high",
			"bgcolor", "#869ca7",
			"id", "SportsDoChart",
			"name", "SportsDoChart",
			"flashvars", prms,
			"allowScriptAccess","sameDomain",
			"type", "application/x-shockwave-flash",
			"pluginspage", "http://www.adobe.com/go/getflashplayer");
			
  if(document.getElementById('resizer') != null)
  {
    if(document.getElementById("sessionId") != null)
    {
      setChartHeight(150);
    }
    else
    {
      setChartHeight(54);
    }
    var r = new resizer(document.getElementById('resizer'));
  }
  SportsDoChart = document.getElementById("SportsDoChart");
}

function RefreshMapDate(activity, minDate, maxDate)
{
  SportsDoChart.Activity(activity);
  SportsDoChart.MinDate(minDate);
  SportsDoChart.MaxDate(maxDate);
  SportsDoChart.Fetch();
}

function RefreshMapRecent(activity, dateSpec)
{
  SportsDoChart.Activity(activity);
  SportsDoChart.DateSpec(dateSpec);
  SportsDoChart.Fetch();
}

function RefreshMapToday(activity)
{
  SportsDoChart.Activity(activity);
  SportsDoChart.Today();
  SportsDoChart.Fetch();
}


function setChartHeight(h)
{
    var mainHeight = parseInt(document.getElementById("main").height);

    if(!isNaN(mainHeight))
    {
        setMapHeight(mainHeight - h);
    }
}

function setMapHeight(h1)
{
  var mainHeight = parseInt(document.getElementById("main").height);
  if(!isNaN(mainHeight))
  {
    var h2 = mainHeight - h1;
    if(h2<54)
    {
      h2 = 54; h1 = mainHeight - h2;
    }
    if(h1>=54 && h1>=0)
    {
      document.getElementById('map').style.height = h1 + "px";
      document.getElementById('chart').style.height = h2 + "px";
      if(document.getElementById('SportsDoChart') != null)
        document.getElementById('SportsDoChart').height = document.getElementById('chart').style.height;
      //getMap().checkResize();
      getMap().resize(document.getElementById('chart').offsetWidth, h1);
    }
  }
}

addListener('load', initChart, 2);

function resizer(div)
{
    this.divResizeBar = div;

    var instance = this; 
    if (document.attachEvent) 
    { 
        div.attachEvent("onmousedown", function(evnt) { instance.mouseDown(evnt) });
    } 
    else
    {
        div.addEventListener("mousedown", function(evnt) { instance.mouseDown(evnt) } , false);
    }
    this.tracking = false;
    this.startY = 0;
    this.x = 0;
    this.height1 = 0;
}

resizer.prototype.mouseDown = function(evnt)
{
    var evnt = (!evnt) ? window.event : evnt; 
    this.startY = parseInt(this.divResizeBar.style.top);
    this.clickY = evnt.clientY;
    this.height1 = parseInt(document.getElementById('map').style.height);
    this.tracking = true;
    var instance = this; 
    if (document.attachEvent) 
    { 
        document.attachEvent("onmousemove", function(evnt) { instance.mouseMove(evnt) });
        document.attachEvent("onmouseup", function() { instance.mouseUp() });
    } 
    else
    {
        document.addEventListener("mousemove", function(evnt) { instance.mouseMove(evnt) } , false);
        document.addEventListener("mouseup", function() { instance.mouseUp() } , false);
    }
}
resizer.prototype.mouseMove = function(evnt)
{
    var evnt = (!evnt) ? window.event : evnt; // The mousemove event
    if (this.tracking)
    {
        setMapHeight(this.height1 + evnt.clientY - this.clickY);
        return false
    }
}
resizer.prototype.mouseUp = function()
{
    this.tracking = false;
    if (document.detachEvent) 
    { 
        document.detachEvent("onmousemove", function(evnt) { instance.mouseMove(evnt) });
        document.detachEvent("onmouseup", function() { instance.mouseUp() });
    } 
    else
    {
        document.removeEventListener("mousemove", function(evnt) { instance.mouseMove(evnt) } , false);
        document.removeEventListener("mouseup", function() { instance.mouseUp() } , false);
    }
}    
