// SystemMessageViewer - SMV
// SystemMessageTypes - SMTs

var SMV = {};

SMV.Global = {
    MainContainerID:null,
    EMsgsContainerID:null,
    WMsgsContainerID:null,
    SMsgsContainerID:null,
    IMsgsContainerID:null
};

SMV.CssClasses = {
    CssClass:null,
    cssErrorM:null,
    cssWarningM:null,
    cssSuccessM:null,
    cssInfoM:null
};

SMV.SMTs = 
{
    ErrorType:null,
    WarningType:null,
    SuccessType:null,
    InfoType:null
};

SMV.Init = {
    CssClasses:function(cssClass, cssErrorM, cssWarningM, cssSuccessM, cssInfoM,
                            errorType, warningType, successType, infoType,
                                mainContainerID, EMsgsContainerID, WMsgsContainerID, SMsgsContainerID, iMsgsContainerID){

        // Init global valiables
        SMV.Global.MainContainerID = mainContainerID;
        SMV.Global.EMsgsContainerID = EMsgsContainerID;
        SMV.Global.WMsgsContainerID = WMsgsContainerID;
        SMV.Global.SMsgsContainerID = SMsgsContainerID;
        SMV.Global.IMsgsContainerID = iMsgsContainerID;

        // Init message types variales
        SMV.SMTs.ErrorType = errorType;
        SMV.SMTs.WarningType = warningType;
        SMV.SMTs.SuccessType = successType;
        SMV.SMTs.InfoType = infoType;        

        // Init css classes variables
        SMV.CssClasses.CssClass = cssClass;
        SMV.CssClasses.cssErrorM = cssErrorM;
        SMV.CssClasses.cssWarningM = cssWarningM;
        SMV.CssClasses.cssSuccessM = cssSuccessM;
        SMV.CssClasses.cssInfoM = cssInfoM;
    }
};

SMV.Utils = {
    AddNewMessage:function(msgType, msgText){
        SMV.Utils.AddNewMessage(msgType, msgText, null);
    },
    
    AddNewMessage:function(msgType, msgText, ID){
        
        if (ID != null && $("#" + ID).get(0) != undefined) return;
        
        var cssClass = "";
        var msgsContainerId = "";
		var msgTitle = "";
		var id = "id = " + ID;
	
		//---Long messages processing---
        if (msgText.length > 100)
        {
            msgTitle = " title='" + msgText + "'";
            msgText = msgText.substr(0, 100) + ' ...';
        }
		//------------------------------  
        if(msgType == SMV.SMTs.ErrorType) {
            cssClass = SMV.CssClasses.cssErrorM;
            msgsContainerId = SMV.Global.EMsgsContainerID;
        }
        else if(msgType == SMV.SMTs.WarningType) {
            cssClass = SMV.CssClasses.cssWarningM;
            msgsContainerId = SMV.Global.WMsgsContainerID;
        }   
        else if(msgType == SMV.SMTs.SuccessType) {
            cssClass = SMV.CssClasses.cssSuccessM;
            msgsContainerId = SMV.Global.SMsgsContainerID;
        }
        else if(msgType == SMV.SMTs.InfoType) {
            cssClass = SMV.CssClasses.cssInfoM;
            msgsContainerId = SMV.Global.IMsgsContainerID;
        }   
        //debugger;
        if($("#" + SMV.Global.MainContainerID).get(0) != undefined) {
            if($("#" + msgsContainerId).get(0) != undefined) {
                $("<div " + id + " class='" + cssClass + "'" + msgTitle + "><div class='text'>" + msgText 
                    + "</div></div>").prependTo("#" + msgsContainerId);
            }
            else {
                var content = 
                    "<div id='" + msgsContainerId + "'><div " + id + " class='" + cssClass + "'" + msgTitle 
                        + "><div class='text'>" + msgText + "</div></div></div>"
                if(msgType == SMV.SMTs.WarningType) {
                    if($("#" + SMV.Global.EMsgsContainerID).get(0) != undefined) {
                        $("#" + SMV.Global.EMsgsContainerID).after($(content));
                    }
                    else {
                        $(content).prependTo("#" + SMV.Global.MainContainerID);
                    }
                }
                else if(msgType == SMV.SMTs.SuccessType) {
                    if($("#" + SMV.Global.WMsgsContainerID).get(0) != undefined) {
                        $("#" + SMV.Global.WMsgsContainerID).after($(content));
                    }
                    else if($("#" + SMV.Global.EMsgsContainerID).get(0) != undefined) {
                        $("#" + SMV.Global.EMsgsContainerID).after($(content));
                    }                    
                    else {
                        $(content).prependTo("#" + SMV.Global.MainContainerID);
                    }
                }                
                else if(msgType == SMV.SMTs.ErrorType) {
                    $(content).prependTo("#" + SMV.Global.MainContainerID);
                }
                else if(msgType == SMV.SMTs.InfoType) {
                    $(content).appendTo("#" + SMV.Global.MainContainerID);
                }
            }
        }
        else {
        // TODO
            $("<div id='" + SMV.Global.MainContainerID + "' class='" + SMV.CssClasses.CssClass
                + "'><div id='" + msgsContainerId + "'><div " + id + " class='" + cssClass + "'" + msgTitle 
                        + "><div class='text'>" + msgText + "</div></div></div></div>").prependTo("form");
        }
    },
    
    HandleError:function(sender, e){
        if(e.get_error()){
            var error = e.get_error();
            var errorTxt = error.message;
            SMV.Utils.AddNewMessage(SMV.SMTs.ErrorType, errorTxt, "ex_" + hex_md5(errorTxt));
            e.set_errorHandled(true);
        }
    },
    
    ClearViewer:function()
    {
        if($("#" + SMV.Global.MainContainerID).get(0) == undefined) return;
        
        $("#" + SMV.Global.MainContainerID).empty();
    },
    
    DeleteMessage:function(msgID)
    {
        if($("#" + msgID).get(0) == undefined) return;
        
        $("#" + msgID).remove();
    },
    
    GetMessage:function(msgID)
    {
        if($("#" + msgID).get(0) == undefined) return null;
        
        return $("#" + msgID);
    },
    
    ChangeMessage:function(msgID, msgText)
    {
        if($("#" + msgID).get(0) == undefined) return null;
        
        $("#" + msgID).get(0).value = msgText;
    },
    
    ChangeMessage:function(msgID, msgText, msgType)
    {
        // TODO: Implement this functionality
    } 
};

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded(); 

