var iTimerID = 0;
var bApplicationStarted = 0;
var bApplicationVisible = 0;
function Start()
{
    System.Debug.outputString("in setContentText avant Open");
    System.Gadget.Flyout.file = "Flyout.htm";
    var consoleStartCommand = Console.Open(consoleName);
    System.Debug.outputString("in setContentText après Open");
    if (iTimerID != 0)
    {
        window.clearTimeout(iTimerID);
        iTimerID = 0;
    }
    if (consoleStartCommand == null || consoleStartCommand.Length < 5)
    {
        waitingContent.style.visibility = "visible";
        System.Debug.outputString("consoleStartCommand == null || consoleStartCommand.Length < 5");
        window.setTimeout("Start();", 10000);
        if (!bApplicationStarted)
        {
            var currentApplicationName = System.Gadget.Settings.readString("applicationName");
            System.Debug.outputString("currentApplicationName :");
            System.Debug.outputString(currentApplicationName);
            
            if (currentApplicationName != "")
            {
                System.Shell.execute(currentApplicationName);
                bApplicationStarted = 1;
            }
        }
    }
    else
    {
        System.Debug.outputString("saving : applicationName= " + consoleStartCommand);
        System.Gadget.Settings.writeString("applicationName", consoleStartCommand);
        waitingContent.style.visibility = "hidden";
    }
}

function Closing()
{
    System.Debug.outputString("Closing");
    Console.ClosedFromGadget();
    System.Debug.outputString("Closing après Closed");
}
function GoDblClick()
{
    System.Debug.outputString("GoDblClick avant closed");
    Console.MainDoubleClickFromGadget();
    System.Debug.outputString("GoDblClick après Closed");
}

function ChangeVisible(newValue)
{
    System.Debug.outputString("ChangeVisible(newValue)");
    System.Debug.outputString(newValue);
    System.Debug.outputString(backgroundid.src);
    System.Gadget.beginTransition()
    if (newValue)
    {
        backgroundid.src="url(background_gadget.png)";
//        backgroundid.style.height="130px";
//        window.document.body.style.height="130px";
        liveContent.style.visibility="visible";
        Player.controls.play();
        bApplicationVisible = 1;
    }
    else
    {
        backgroundid.src="url(background_transparent.png)";
//        backgroundid.style.height="0px";
//        window.document.body.style.height="0px";
        liveContent.style.visibility="hidden";
        Player.controls.stop();
        bApplicationVisible = 0;
    }
    System.Gadget.endTransition(0, 2)
}

function Console::Visible(newValue)
{
    System.Debug.outputString("Console::Visible(newValue)");
    ChangeVisible(newValue);
}

var iTimerAlertID = 0;
var sTextHtml = "";
function FlyerClick()
{
    System.Debug.outputString("FlyerClick");
    Console.AlertClickFromGadget();
    System.Debug.outputString("FlyerClick après");
}

function FlyerReady()
{
   System.Debug.outputString("FlyerReady");
   System.Debug.outputString(System.Gadget.Flyout.document.readyState);
   if (System.Gadget.Flyout.document.readyState=="complete")
   {
       System.Gadget.Flyout.document.body.innerHTML = sTextHtml;
   }
   System.Debug.outputString("FlyerReady après");
}

function FlyerTimeout()
{
   System.Debug.outputString("FlyerTimeout après");
   System.Gadget.Flyout.show = false;
   window.clearTimeout(iTimerAlertID);
   iTimerAlertID = 0;
   System.Debug.outputString("FlyerTimeout après");
}

function Console::Alert(textHtml, durationInMs)
{
    System.Debug.outputString("Console::Alert ");
    System.Debug.outputString(durationInMs);
    System.Debug.outputString(textHtml);
    sTextHtml = textHtml;
//    System.Gadget.beginTransition()
    System.Gadget.Flyout.show = true;
    System.Gadget.Flyout.document.onreadystatechange= FlyerReady;
    System.Gadget.Flyout.document.onclick = FlyerClick;
    if (iTimerID != 0)
    {
        window.clearTimeout(iTimerAlertID);
        iTimerID = 0;
    }
    window.setTimeout("FlyerTimeout();", durationInMs);
//    System.Gadget.endTransition(0, 2)

}

function Console::Url(newUrl)
{
    System.Debug.outputString("Console::Url ");
    System.Debug.outputString(newUrl);
    System.Debug.outputString("player status :");
    System.Debug.outputString(Player.status);
    
    Player.URL=newUrl;
    ChangeVisible(true);
//    System.Debug.outputString("before Player.controls.play()");
//    Player.controls.play();
//    System.Debug.outputString("after Player.controls.play()");
}

function Console::PositionX()
{
    System.Debug.outputString("Console::PositionX ");
    return window.screenLeft;
}

function Console::PositionY()
{
    System.Debug.outputString("Console::PositionY ");
    return window.screenTop;
}

function Console::ConsoleClosed()
{
    System.Debug.outputString("Console::ConsoleClosed()");
    ChangeVisible(false);
    Player.URL="";
    window.setTimeout("Start();", 10000);
}

function Player::DoubleClick(nButton, nShiftState, fX, fY)
{
    System.Debug.outputString("Player::DoubleClick");
    System.Debug.outputString(fX);
    System.Debug.outputString(fY);
    Console.MainDoubleClickFromGadget();
}