﻿function clsTimeCtrl()
{   
    this.resizeTimeout = null;
    this.divTime =  null;
    this.svlTimeCtrl = null;
    this.height = 22;
    this.startBound = "";
    this.endBound = "";
    this.start = "";
    this.end = "";
    
    //this.timeObjectTag = "<div id='silverlightControlHost><object id='slvTime' data='data:application/x-silverlight,' type='application/x-silverlight-2' width='100%' height='56px'><param name='source' value='LuisObjects/Timebox.xap'/><param name='onerror' value='onSilverlightError' /><param name='minRuntimeVersion' value='2.0.31005.0' /><param name='autoUpgrade' value='true' /><a href='http://go.microsoft.com/fwlink/?LinkID=124807' style='text-decoration: none;'><img src='http://go.microsoft.com/fwlink/?LinkId=108181' alt='Get Microsoft Silverlight' style='border-style: none'/></a></object><iframe style='visibility:hidden;height:0;width:0;border:0px'></iframe></div>";
    //this.timeObjectTag_winless = "<div id='silverlightControlHost><object id='slvTime' data='data:application/x-silverlight,' type='application/x-silverlight-2' width='100%' height='56px'><param name='source' value='LuisObjects/Timebox.xap'/><param id='timeWinless' value='true' name='windowless'/><param name='onerror' value='onSilverlightError' /><param name='minRuntimeVersion' value='2.0.31005.0' /><param name='autoUpgrade' value='true' /><a href='http://go.microsoft.com/fwlink/?LinkID=124807' style='text-decoration: none;'><img src='http://go.microsoft.com/fwlink/?LinkId=108181' alt='Get Microsoft Silverlight' style='border-style: none'/></a></object><iframe style='visibility:hidden;height:0;width:0;border:0px'></iframe></div>";
    
    this.timeObjectTag = "<object id='slvTime' data='data:application/x-silverlight,' type='application/x-silverlight-2' width='100%' height='56px'><param name='source' value='LuisObjects/Timebox.xap'/><param name='onerror' value='onSilverlightError' /><param name='minRuntimeVersion' value='2.0.31005.0' /><param name='autoUpgrade' value='true' /><a href='http://go.microsoft.com/fwlink/?LinkID=124807' style='text-decoration: none;'><img src='http://go.microsoft.com/fwlink/?LinkId=108181' alt='Get Microsoft Silverlight' style='border-style: none'/></a></object>";
    this.timeObjectTag_winless = "<object id='slvTime' data='data:application/x-silverlight,' type='application/x-silverlight-2' width='100%' height='56px'><param name='source' value='LuisObjects/Timebox.xap'/><param value='true' name='windowless'/><param name='onerror' value='onSilverlightError' /><param name='minRuntimeVersion' value='2.0.31005.0' /><param name='autoUpgrade' value='true' /><a href='http://go.microsoft.com/fwlink/?LinkID=124807' style='text-decoration: none;'><img src='http://go.microsoft.com/fwlink/?LinkId=108181' alt='Get Microsoft Silverlight' style='border-style: none'/></a></object>";
}

var ctrlTime = new clsTimeCtrl();   //The temporary time control

clsTimeCtrl.prototype.initTime = function(start_, end_)
{
    this.divTime =  document.getElementById("divTime");
}

clsTimeCtrl.prototype.InputDates = function(startBound_, endBound_, std_, edd_)
{
    this.startBound = startBound_;
    this.endBound = endBound_;
    this.start = std_;
    this.end = edd_;
    this.divTime.innerHTML = this.timeObjectTag;
    
    var svlTime = document.getElementById('slvTime');
    
}

clsTimeCtrl.prototype.SetDates = function(stt_, edt_)
{
   var svlTime = document.getElementById('slvTime');
   svlTime.content.me.Select(stt_, edt_);
}

clsTimeCtrl.prototype.Resize = function()
{
    if(ctrlTime.startBound != "")
	{
	    ctrlTime.divTime.innerHTML = this.timeObjectTag;
	}
}

clsTimeCtrl.prototype.SetWindowless = function()
{
    if(ctrlTime.startBound != "")
	{
	    ctrlTime.divTime.innerHTML = this.timeObjectTag_winless;
	}
}


clsTimeCtrl.prototype.SetObject = function()
{
    var svlTime = document.getElementById('slvTime')
    if(this.layoutMode != "graph")
    {
        try
        {
           //alert(this.startBound,svlTime);
            //svlTime.content.me.Load(this.startBound,svlTime.offsetWidth);
            svlTime.content.me.Load(svlTime.offsetWidth);
             
            svlTime.content.me.Select(this.start, this.end);
        }
        catch(e)
        {
            setTimeout("ctrlTime.Time_Loaded()",300)
        }
    }
}


function time(start,end)
{
    var arrStart = start.split(' ');
    var arrEnd = end.split(' ');
    
    arrTime_start = arrStart[0].split('-');
    arrTime_end = arrEnd[0].split('-');
    
    if(arrTime_start.length > 2)
    {
        arrStart[0] = arrTime_start[1] + '/';
        arrStart[0] += arrTime_start[2] + '/';
        arrStart[0] += arrTime_start[0];
    }
    if(arrTime_end.length > 2)
    {
        arrEnd[0] = arrTime_end[1] + '/';
        arrEnd[0] += arrTime_end[2] + '/';
        arrEnd[0] += arrTime_end[0];
    }
    
    ctrlTime.start = arrStart[0];
    ctrlTime.end  = arrEnd[0];
    
    //var param = "startDate=" + start + "&endDate=" + end;
    //The data in the client side clean up for getting new data
    //If it is not here, possibly memory leaking posibly occur
    
    //Call Ajax function 
    ctrlAjax.CallAjaxFunction("GetDatasetByDates","startDate=" + arrStart[0] + "&endDate=" + arrEnd[0]);
}

function Time_Loaded(){
    setTimeout("ctrlTime.SetObject()",700);
}

function onSilverlightError(sender, args) 
{        
    var appSource = "";
    if (sender != null && sender != 0) {
        appSource = sender.getHost().Source;
    } 
    var errorType = args.ErrorType;
    var iErrorCode = args.ErrorCode;
    
    var errMsg = "Unhandled Error in Silverlight 2 Application " +  appSource + "\n" ;

    errMsg += "Code: "+ iErrorCode + "    \n";
    errMsg += "Category: " + errorType + "       \n";
    errMsg += "Message: " + args.ErrorMessage + "     \n";

    if (errorType == "ParserError")
    {
        errMsg += "File: " + args.xamlFile + "     \n";
        errMsg += "Line: " + args.lineNumber + "     \n";
        errMsg += "Position: " + args.charPosition + "     \n";
    }
    else if (errorType == "RuntimeError")
    {           
        if (args.lineNumber != 0)
        {
            errMsg += "Line: " + args.lineNumber + "     \n";
            errMsg += "Position: " +  args.charPosition + "     \n";
        }
        errMsg += "MethodName: " + args.methodName + "     \n";
    }

    throw new Error(errMsg);
}
        