// Floating Window Definition declaration
/* Constructor */ function FloatingWindow(vInstanceVariableName, vPageUrl, vFadeInDelay, vFadeOutDelay, vCssClass)
{
    // Initialisation
    this.IsWindowOpened = false;
    this.IsReady = false;
    this.IsPageLoaded = false;

    // Values assignation
    this.InstanceVariableName = vInstanceVariableName;
    this.CssClass = vCssClass;
    this.PageUrl = vPageUrl;
    this.FadeInDelay = vFadeInDelay;
    this.FadeOutDelay = vFadeOutDelay;

    // Window size
    this.WindowHeight = 400;
    this.WindowWidth = 500;
    this.CornerOffset = 4;

    this.PositionTimerID = null;

    $(document).ready( function() { eval(vInstanceVariableName + ".IsReady = true;"); } )
}

/* Public */ FloatingWindow.prototype.PreLoadPageContent = function() 
{
    if (!this.IsPageLoaded)
    {
        this.CreateWindowInstance();
    }
}

/* Private */ FloatingWindow.prototype.CreateWindowInstance = function() 
{ 
    this.IsPageLoaded = true;
    $("body").append("<iframe id='" + this.InstanceVariableName + "win' src='" + this.PageUrl + "' style='display: none; Z-INDEX: 10000; POSITION: absolute;' class='" + this.CssClass + "' AllowTransparency='true' FrameBorder='0' />");
    $("body").append("<iframe id='" + this.InstanceVariableName + "winbugIE1' style='display: none; Z-INDEX: 9999; POSITION: absolute;' FrameBorder='0' />");
    $("body").append("<iframe id='" + this.InstanceVariableName + "winbugIE2' style='display: none; Z-INDEX: 9999; POSITION: absolute;' FrameBorder='0' />");
}

/* Public */ FloatingWindow.prototype.Open = function() 
{ 
    if (!this.IsPageLoaded)
    {
        this.CreateWindowInstance();
    }
    
    this.IsWindowOpened = true;

    this.ResetWindowPosition();

    $("#" + this.InstanceVariableName + "win").fadeIn(this.FadeInDelay);
    $("#" + this.InstanceVariableName + "winbugIE1").fadeIn(this.FadeInDelay);
    $("#" + this.InstanceVariableName + "winbugIE2").fadeIn(this.FadeInDelay);
    
    this.PositionTimerID = setInterval(this.InstanceVariableName + ".ResetWindowPosition();", 1500);
}

/* Public */ FloatingWindow.prototype.Close = function() 
{
    this.IsWindowOpened = false;

    if (this.PositionTimerID != null)
    {
        clearInterval(this.PositionTimerID);
        this.PositionTimerID = null;
    }
        
    $("#" + this.InstanceVariableName + "win").fadeOut(this.FadeOutDelay);
    $("#" + this.InstanceVariableName + "winbugIE1").fadeOut(this.FadeInDelay);
    $("#" + this.InstanceVariableName + "winbugIE2").fadeOut(this.FadeInDelay);
}

/* Private */ FloatingWindow.prototype.ResetWindowPosition = function() 
{
    var viewPortWidth;
    var viewPortHeight;
 
    // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
    if (typeof window.innerWidth != 'undefined')
    {
        viewPortWidth = window.innerWidth,
        viewPortHeight = window.innerHeight
    }
    // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
    else if (typeof document.documentElement != 'undefined' && 
             typeof document.documentElement.clientWidth != 'undefined' && 
             document.documentElement.clientWidth != 0)
    {
        viewPortWidth = document.documentElement.clientWidth,
        viewPortHeight = document.documentElement.clientHeight
    }
    // older versions of IE
    else
    {
        viewPortWidth = document.getElementsByTagName('body')[0].clientWidth,
        viewPortHeight = document.getElementsByTagName('body')[0].clientHeight
    }

    this.SetWindowPosition(viewPortWidth, viewPortHeight);
}

/* Private */ FloatingWindow.prototype.SetWindowPosition = function(vViewPortWidth, vViewPortHeight) 
{
    if (!this.IsWindowOpened)
    {
        return;
    }

    centeredTopPosition = (vViewPortHeight/2) - (this.WindowHeight/2);
    if (centeredTopPosition < 0)
    {
        centeredTopPosition = 0;
    }
    centeredLeftPosition = (vViewPortWidth/2) - (this.WindowWidth/2);
    if (centeredLeftPosition < 0)
    {
        centeredLeftPosition = 0;
    }
    
    var WindowFrame = this.GetControlByID(this.InstanceVariableName + "win");
    WindowFrame.style.top = centeredTopPosition + "px";
    WindowFrame.style.left = centeredLeftPosition + "px";
    WindowFrame.style.width = this.WindowWidth + "px";
    WindowFrame.style.height = this.WindowHeight + "px";

    var WindowFrameBugIE1 = this.GetControlByID(this.InstanceVariableName + "winbugIE1");
    WindowFrameBugIE1.style.top = centeredTopPosition + "px";
    WindowFrameBugIE1.style.left = (centeredLeftPosition + this.CornerOffset) + "px";
    WindowFrameBugIE1.style.width = (this.WindowWidth - (2 * this.CornerOffset)) + "px";
    WindowFrameBugIE1.style.height = this.WindowHeight + "px";

    var WindowFrameBugIE2 = this.GetControlByID(this.InstanceVariableName + "winbugIE2");
    WindowFrameBugIE2.style.top = (centeredTopPosition + this.CornerOffset) + "px";
    WindowFrameBugIE2.style.left = centeredLeftPosition + "px";
    WindowFrameBugIE2.style.width = this.WindowWidth + "px";
    WindowFrameBugIE2.style.height = (this.WindowHeight - (2 * this.CornerOffset)) + "px";
}

/* Private */ FloatingWindow.prototype.GetControlByID = function(vControlID)
{
    var control;
    
    if (typeof(vControlID) == "undefined" || vControlID == null || vControlID == "")
    {
        throw "vControlID must be specified.";
    }

    // Determine la façon dont l'objet doit être recupere
    if (document.getElementById) 
    {
        // this is the way the standards work
        control = document.getElementById(vControlID);
    } 
    else if (document.all) 
    {
        //this is the way old msie versions work
        control = document.all[vControlID];
    } 
    else if (document.layers) 
    {
        // this is the way nn4 works
        control = document.layers[vControlID];
    }

    // Verifie si l'objet a ete trouve
    if (typeof(control) == "undefined" || control == null || control == "")
    {
        throw "No control found (vControlID = " + vControlID + ").";
    }
    
    return control;
}

/* Public */ FloatingWindow.prototype.ResizeWindow = function(vWidth, vHeight)
{
    this.WindowHeight = vHeight;
    this.WindowWidth = vWidth;
}
