﻿// JScript File

function GoalBoard(sessionid)
{
    this.index = 0;
    this.items = new Array();
    this.AddItem = AddItem;
    this.isinit = false;
    
    this.sessionid = sessionid;
    
    GoalBoard.sessionid = sessionid;
    
    this.SetPosition = SetPosition;
    this.Hide = Hide;
    this.Show = Show;
    this.Refresh = Refresh;
}

function AddItem(id, x, y, html)
{
    if(this.sessionid.length == 0)
    {
        this.isinit = true;
    }
    
    var goalid = id;
    var e = document.getElementById('chork-area');
    var p = $('div#chork-area').position();

    if(e == null)
    {
        alert('Container not found (1).');
        return;
    }
    
    e = e.childNodes[0];
    if(e == null)
    {
        alert('Container not found (2).');
        return;
    }

    id = 'goal-board-item-' + id.toString()
    var item = document.getElementById(id);
    
    if(item != null)
    {
        //TODO: Alert that it's already there?
        return;
    }
    
    item = document.createElement('div', id);

    item.id = id;
    item.className = 'goal-board-item';
    item.style.left = (p.left + x).toString() + 'px';
    item.style.top = (p.top + y).toString() + 'px';
    item.innerHTML = html;
    item.curx = x;
    item.cury = y;
    
    e.appendChild(item);
    
    this.items[this.index] = item;
    this.index++;
    
    if(this.sessionid.length > 0)
    {
        $('div#' + id).draggable({
            stop:function(e, ui)
           {
                 GoalBoard.GoalID = goalid;
                 SetPosition(ui);
           }
        });
        
        if(!this.isinit)
        {
            $('div#' + e.id.toString()).droppable({
                accept: '.goal-board-item'
            });
            
            this.isinit = true;
        }
    }
}

function Refresh()
{
    var p = $('div#chork-area').position();
    var x = p.left;
    var y = p.top;
    
    for(var i = 0; i < this.items.length; i++)
    {
        try
        {
            this.items[i].style.left = (p.left + this.items[i].curx).toString() + 'px';
            this.items[i].style.top = (p.top + this.items[i].cury).toString() + 'px';
        }
        catch(ex)
        {
            alert(ex.toString());
            break;
        }
    }
}

function SetPosition(ui)
{
    var p = $('div#chork-area').position();
    var x = parseInt(ui.position.left) - parseInt(p.left);
    var y = parseInt(ui.position.top) - parseInt(p.top);
    var vars = new Array();
    
    vars[0] = 'goal-id=' + GoalBoard.GoalID.toString();
    vars[1] = 'x=' + x.toString();
    vars[2] = 'y=' + y.toString();
    
    var a = new AjaxObject('goal.board.saveposition', vars, '');
    a.sessionid = GoalBoard.sessionid;
    
    a.Send(function(txt)
    {
        //alert('Saved [' + GoalBoard.GoalID.toString() + '] : (' + x.toString() + ', ' + y.toString() + ') = ' + txt);
    });
}

function Hide()
{
    for(var i = 0; i < this.items.length; i++)
    {
        $('#' + this.items[i].id).hide();
    }
}

function Show()
{
    for(var i = 0; i < this.items.length; i++)
    {
        $('#' + this.items[i].id).show();
    }
}

GoalBoard.sessionid = '';
GoalBoard.GoalID = 0;
//GoalBoard.AddItem = AppendGoal;

var MyGoalBoard = null;
