﻿// JScript File
//Uses yahoo-min.js and event-min for Event Handling
//Uses AJAXMessage.js for Message Handling
//Uses prototype.js for AJAX Asyn Loading Data
//Uses DOMUtils.js for DOM manipulation (TipandBlink and FadeMessage)
// JScript File

function AJAXTopWindow(id , highlight, msghighlight , msghighlightfunc , content, footer , msg , url, scroll)
{   
    this.id = id;
    this.highlight = highlight;
    this.msghighlight = msghighlight;
    this.msghighlightfunc = msghighlightfunc;
    this.content = content;
    this.footer = footer;
    this.msg = msg;
    this.url = url;
    this.inic = 0;
    this.end = 0;
    this.scroll = scroll;
    this.boxHeight = 0;
    this.repeatHeight = 0;
    this.x = 0;
    this.Scroll = 0;
    this.ResponseBack = new YAHOO.util.CustomEvent("ResponseBack", this); 
    this.addLoadEvent(this.InitMsgWait, this)	
    this.addLoadEvent(this.GetTopAlertas , this)	
    tt_tShow = new Number(0);
}

AJAXTopWindow.prototype.addLoadEvent = function (func, outer ) 
{
    var oldonload = window.onload;
	if (typeof window.onload != 'function') 
	{
	    window.onload = function() 
	    {
		    func(outer);
        }
    }
    else 
        {
            window.onload = function() {
		    oldonload();
		    func(outer);
        }
    }
}

AJAXTopWindow.prototype.InitMsgWait = function (outer ) 
{
    MyAJAXMessage = outer.SetMessage('AJAXMessage' + outer.id);
    outer.AJAXMessage = 'AJAXMessage' + outer.id;
    document.getElementById(outer.msg).appendChild(document.getElementById('AJAXMessage' + outer.id));
    MyAJAXMessage.InitEvents(outer);
}

AJAXTopWindow.prototype.GetTopAlertas = function (outer) 
{
    this.returnedCallback = 
    {
        success: function(o) 
        {
            outer.complete(o);
        },
        failure: function(o) 
        {
            outer.ShowCallBackError(o);
        }
    }
    //YAHOO.util.Connect.setDefaultPostHeader(false);
    //YAHOO.util.Connect.initHeader("Content-Type", "text/xml; charset=utf-8"); 
    this.getXML = YAHOO.util.Connect.asyncRequest("GET",outer.url,this.returnedCallback); 
}

AJAXTopWindow.prototype.complete = function (response)	
{   
    this.ResponseBack.fire(MyAJAXMessage.MessageOk,MyAJAXMessage.CSSImgOk);  
    document.getElementById(this.content).innerHTML = response.responseText;
    //FadeMessage(this.AJAXMessage, "True", 5);
    //Diminuido o tempo da mensagem de carregamento com sucesso
    FadeMessage(this.AJAXMessage, "True", 1);
    if (this.scroll == "True")
        {
            this.inic = findPosY(this.content);
            this.end = findPosY(this.footer)
            this.boxHeight = document.getElementById(this.content).style.height.replace('px','')
            this.repeatHeight = document.getElementById(this.content).scrollHeight //get the current height so we know when to wrap
            if (this.boxHeight < this.repeatHeight)
                {
                    TipandBlink(this.highlight , this.msghighlight);
                }
                else
                {
                    document.getElementById(this.highlight).onclick = (function() 
	                    {
                            return function()
                                { 
                                    void(0);
                                }
                            })();
                }
        }
    if ((this.scroll == "True") && (this.boxHeight < this.repeatHeight))
    {
            tt_tShow.content = this.content;
            tt_tShow.id = this.id;
            tt_tShow.x = 0;
            tt_tShow.Scroll = 0;
            tt_tShow.repeatHeight = this.repeatHeight ;
            var append = document.getElementById(this.content).innerHTML ;
            document.getElementById(this.content).innerHTML = document.getElementById(this.content).innerHTML + '<br><br><br>';
            tt_tShow.repeatHeight = document.getElementById(this.content).scrollHeight;
            document.getElementById(this.content).innerHTML = document.getElementById(this.content).innerHTML + append;  //add a second copy so we can scroll down to the wrap point
            document.getElementById(this.content).onmouseover = tt_tShow.stopScroll;
            document.getElementById(this.content).onmouseout = tt_tShow.inicScroll;        
            this.x = setTimeout("scrollMe('" + this.id + "','" + this.content + "')",1000)
    }
    else
    {
        document.getElementById(this.content).scrollTop = 0;
    }
   
}

AJAXTopWindow.prototype.ShowCallBackError = function (t)	
{   
    document.getElementById(this.content).innerHTML = "";
    this.ResponseBack.fire(MyAJAXMessage.MessageNOk + ': ' + t.status + ',' + t.statusText, MyAJAXMessage.CSSImgNok);          
    FadeMessage(this.AJAXMessage,"True", 5);
}

AJAXTopWindow.prototype.SetMessage = function (id) 
{
    var MyMessageProps = new AJAXMessageProps();   
    var MyAJAXMessage = new AJAXMessage(id, MyMessageProps.ImgWait ,MyMessageProps.ImgOk, MyMessageProps.ImgNok, MyMessageProps.CSSImgWait , MyMessageProps.CSSImgOk , MyMessageProps.CSSImgNok , MyMessageProps.AJAXMessagePosImg, MyMessageProps.AJAXMessagePosMsg, MyMessageProps.Message, MyMessageProps.MessageOk ,MyMessageProps.MessageNOk , "False" , this, "False");
    MyAJAXMessage.AJAXMessageinitOut();
    return MyAJAXMessage;
}

scrollMe = function (id , content) 
{
    clearTimeout(tt_tShow.x)
	if(tt_tShow.Scroll == 1) 
	{
		return
	}
	document.getElementById(content).scrollTop=document.getElementById(content).scrollTop+1
	if(document.getElementById(content).scrollTop<= tt_tShow.repeatHeight) 
	    {
		// keep on scrolin' 
            tt_tShow.x = window.setTimeout("scrollMe('" + id + "','" + content + "')",40)
	    }
	else 
	    { //we have hit the wrap point
		    document.getElementById(content).scrollTop=0
            tt_tShow.x = window.setTimeout("scrollMe('" + id + "','" + content + "')",40)
	    }
}

Number.prototype.inicScroll = function ()
{
   tt_tShow.Scroll = 0;
   scrollMe(tt_tShow.id,tt_tShow.content);
}

Number.prototype.stopScroll = function ()
{
   tt_tShow.Scroll = 1;
}

