// JavaScript Document

// Slide Map

slidemap.Registry = [];

slidemap.speed = 1220;

slidemap.hideDelay = 325;

slidemap.minCPUResolution = 10;

// constructor

function slidemap(id)

{

    this.dom = document.getElementById ? 1 : 0

    if (this.dom) {

        this.id             = id

        this.aniTimer     = false

        this.open         = false

        this.startTime     = -0;

        

        this.xtarget = 0;

        this.ytarget = 0;

        

        this.gRef = "slidemap_"+id

        eval(this.gRef+"=this")

        slidemap.Registry[id] = this

        this.load()

    }

}

slidemap.prototype.load = function() {

    var d = document

    var c1 = this.id + "Wrapper"

    var c2 = this.id + "Container"

    var obj1 = d.getElementById(c1);

    if (obj1) var obj2 = d.getElementById(c2)

    var temp

    if (!obj1 || !obj2) window.setTimeout(this.gRef + ".load()", 100)

    else {

        this.Wrapper    = obj1

        this.menu        = obj2

        this.style        = this.menu.style

        

        var temp = "style properties\n";

        this.endSlide()

    }

}

    

slidemap.showMap = function(id,dir,top,left)

{

    var reg = slidemap.Registry

    var obj = slidemap.Registry[id]

     reg[id].startSlide(dir,top,left)

}

slidemap.prototype.startSlide = function(dir,top,left)

{

    this.changeCoordinates( dir, top, left )

    this.startTime = (new Date()).getTime()    

    this.aniTimer = window.setInterval(this.gRef + ".slide()", slidemap.minCPUResolution)

    this.endSlide;

}

slidemap.prototype.slide = function() {

    var elapsed = (new Date()).getTime() - this.startTime;

    

    if (elapsed > slidemap.speed) {

        this.endSlide(); 

    } else {

                

        var x = parseInt( this.style[ "left" ] ); //this.left;

        var y = parseInt( this.style[ "top" ] ); //this.top;

        

        if( isNaN( x ) || isNaN( y ) )

        {

            this.moveTo( -170, -240 );

            x = -240;

            y = -170;

        }

        

        var dx = this.xtarget - x;

        var dy = this.ytarget - y;

        x += Math.round( dx * .2);

        y += Math.round( dy * .2);

        

        this.moveTo(x,y);

        

        x = parseInt( this.style[ "left" ] ); //this.left;

        y = parseInt( this.style[ "top" ] ); //this.top;

    }

}

slidemap.prototype.endSlide = function() {

    this.aniTimer = window.clearTimeout(this.aniTimer);

    var elasped=0;

    

}

slidemap.prototype.changeCoordinates = function(dir, top, left) {

    this.dirType = dir;

    

    if( typeof this.style[ "top" ] == "undefined" || this.style[ "top" ] == "" )

    {

        this.top = -170; //parseInt( this.style[ "top" ], 10 );

        this.left = -240; //parseInt( this.style[ "left" ], 10 );

    }

    else

    {

        this.top = parseInt( this.style[ "top" ], 10 );

        this.left = parseInt( this.style[ "left" ], 10 );

        

    }

    this.ytarget = top;

    this.xtarget = left;

}

slidemap.prototype.moveTo = function(left,top)

{ 

    this.style["left"] = Math.round( left ) + "px";

    this.style["top"] = Math.round( top ) + "px";

}

slidemap.prototype.getPos = function(c) {

    return parseInt(this.style[c])

}

 





// Traffic Cookies

var cookieName = 'camera'; 

var myarray = init_array();

var html;

var dbug = 0; 

var w_cam='http://www.mto.gov.on.ca/english/traveller/compass/camera/pictures/BurlCamera/loc';

var c_cam='http://www.toronto.ca/trafficimages/loc';

var n_cam='http://www.mto.gov.on.ca/english/traveller/compass/camera/pictures/loc';

function d_a(ary) {

    var beg = next_entry(ary) - 1; 

    for (var i = beg ; i > -1; i--) 

    {

        ary[i] = null;

    }

} 

function init_array() {

    if (dbug) alert('init_cookie');  

    var ary = new Array(null); 

    return ary;

} 

    

function set_cookie(name,value,expires) {

    if (dbug) alert('set_cookie'); 

    if (!expires) expires = new Date();

    document.cookie = name + '=' + escape(value) + '; expires=' + expires.toGMTString() + '; path=/';

} 

function get_cookie(name) {

    if (dbug) alert('get_cookie'); 

    var dcookie = document.cookie; 

    var cname = name + "="; 

    var clen = dcookie.length; 

    var cbegin = 0; 

    while (cbegin < clen) {

        var vbegin = cbegin + cname.length;

        if (dcookie.substring(cbegin, vbegin) == cname) {

            var vend = dcookie.indexOf (";", vbegin); 

            if (vend == -1) vend = clen; 

                return unescape(dcookie.substring(vbegin, vend));

        } 

    

        cbegin = dcookie.indexOf(" ", cbegin) + 1; 

        if (cbegin == 0) break;

    } 

    return null;

} 

function del_cookie(name) {

    if (dbug) alert('del_cookie');

document.cookie = name + '=' + '; expires=Thu, 01-Jan-70 00:00:01 GMT; path=/';

} 

function get_array(name, ary) {

    if (dbug) alert('get_array'); d_a(ary); 

    var ent = get_cookie(name); 

    if (ent) {

        i = 1;

        while (ent.indexOf('^') != '-1') {

            ary[i] = ent.substring(0,ent.indexOf('^')); 

            i++;

ent = ent.substring(ent.indexOf('^')+1, ent.length);

        }

    }

} 

function set_array(name, ary, expires) {

    if (dbug) alert('set_array'); 

    var value = ''; 

    for (var i = 1; ary[i]; i++) {

        value += ary[i] + '^';

    } 

    set_cookie(name, value, expires);

} 

function del_entry(name, ary, pos, expires) {

    if (dbug) alert('del_entry');

    var value = ''; get_array(name, ary); 

    

    for (var i = 1; i < pos; i++) {

            value += ary[i] + '^';

    } 

    for (var j = pos + 1; ary[j]; j++) {

        value += ary[j] + '^';

    } 

    set_cookie(name, value, expires);

} 

function next_entry(ary) {

    if (dbug) alert('next_entry'); 

    var j = 0; 

    for (var i = 1; ary[i]; i++) {

        j = i

    } 

    return j + 1;

}

function debug_on() {dbug = 1;} 

function debug_off() {dbug = 0;} 

function dump_cookies() {

    if (document.cookie == '') 

        document.write('No Cookies Found'); 

    else {

        thisCookie = document.cookie.split('; '); 

        for (i=0; i<thisCookie.length; i++) {

            document.write(thisCookie[i] + '<br \/>');

        }

    }

}

var timeToKeep = 1000 * 60 * 60 * 24 * 365;

function AddToFavorites () {

    var id=document.getElementById('CameraViewer').name;

    if (!id) id='loc_c_17';

    

    var expires = new Date();

    expires.setTime(expires.getTime() + 1000 * 60 * 60 * 24 * 365);

    var num = next_entry(myarray);

    myarray[num] = id;

    set_array(cookieName, myarray, expires);

    

    GetFavorites();

}

function DeleteFromFavorites(id) {

    var timeToKeep = 6000000; // one minute

    var expires = new Date();

    expires.setTime(expires.getTime() + timeToKeep);

    del_entry(cookieName, myarray, id, expires);

    GetFavorites();

}

var now = new Date();

var one_year= new Date(now.getTime() + 1000 * 60 * 60 * 24 * 365);



function AddToFavoritesCookie (id) 

{

    var expires = new Date();

    expires.setTime(expires.getTime() + 1000 * 60 * 60 * 24 * 365);

    var num = next_entry(myarray);

    myarray[num] = id;

    set_array(cookieName, myarray, expires);

}



function getCookie (cookieName) {

  var cookieValue = null;

  var posName = document.cookie.indexOf(escape(cookieName) + '=');

  if (posName != -1) {

    var posValue = posName + (escape(cookieName) + '=').length;

    var endPos = document.cookie.indexOf(';', posValue);

    if (endPos != -1) {

      cookieValue = unescape(document.cookie.substring(posValue,endPos));

    } else {

      cookieValue = unescape(document.cookie.substring(posValue));

    }

  }

  return cookieValue;

}





function GetFavorites() {

    var html="";

    get_array(cookieName,myarray);

    var j=2;

    html = "<table><tr>"

    var traffCook = "";

    for (var i=1; i<next_entry(myarray); i++) {

        //j=(j==1)?2:1; 

        if ((i-1) % 3 == 0)

        {

        html=html+"</tr><tr>";

        }

        traffCook = traffCook + myarray[i] + ",";

            

        url=eval(myarray[i].substring(4,5)+'_cam');

        

//Cameraj

        html+='<td ><h2>'+document.getElementById(myarray[i]).alt+'</h2><img src='+url+myarray[i].substring(6)+'.jpg" width="258" height="194" class="Camera" alt="'+document.getElementById(myarray[i]).alt+'" title="'+document.getElementById(myarray[i]).alt+'"><br clear="all"/><a href="javascript:DeleteFromFavorites('+[i] +');">Delete from Favorites</a></td>';

    }

    setCookie("trafficBC",traffCook, one_year,"/");

    html=html+"</tr></table>";

    document.getElementById('Favorites').innerHTML=html;

}

function GetTrafficHome() {

    var html="";

    get_array(cookieName,myarray);

    var j=2;

    

    for (var i=1; i<3; i++) {

         url=(myarray[i]) ?  eval(myarray[i].substring(4,5)+'_cam'):c_cam;

        cam=(myarray[i]) ?  myarray[i].substring(6):14-i;

         

        var camera=document.getElementById('TrafficCamera'+i);

        camera.src=url+cam+".jpg";

    }

}

 function showCamera(n,d)

 {

    url=eval(d+'_cam');

    var imgPath = url+n+".jpg";

    //var imgPath = "http://ctv1.ctv.ca/etracker/scraperTrafficBC.asp?camID=" + n;

    //var imgPath = "http://www.toronto.ca/trafficimages/loc"+n+".jpg";

    //Load Image    

    var imgSrc = new Image();

    var imgLoad=document.getElementById('LoadingGraphic');

    imgLoad.style.display='block';

     imgSrc.onload= function(){swapCamera(imgPath,imgLoad)};

    imgSrc.onerror = imgSrc.onload 

    imgSrc.src = imgPath;

}

 function swapCamera(imgPath,imgLoad)

 {

    

    var ShowCamera=document.getElementById('LiveCamera');

    ShowCamera.src=imgPath;

    imgLoad.style.display='none';

}                        

    





// OnPreLoad

new slidemap("TrafficMap")

                            








