/**
 * @title   PopupT.js
 * @author  Subrata Bhattacharjee, (from CP's original in classTA)
 * 10-23-11: bugfix and documentation in alertApplet
 * 10-19-11: alertFloat? try catch
 *  10-18-11: showApplet fix
 * v 1.6 alertInElement does not work?
 * v 1.5 introduce alertSoft same as alert with soft bg
 * v 1.4: suggest box like alertbox
 * v 1.3 alert refurbished with comments and imagepath
 * v 1.2: make alert more robust with sDvi passed through constructor v 1.1 : 6-3-10
 *
 */
PopupT = Class.create({
    CLASSDEF : {
        name:  'PopupT'
    }
});

PopupT.prototype = {
    initialize: function(sID, bIE) {
        this.sID = sID;  //identify the element 'alertbox' for instance
        this.bIE = true;

    },


    // alert message in a new div ('popup') hooked to a parent div supplied by caller
    alert: function() {
        //0. get arguments
        var args = $A(arguments);
        var sErrorMsg = args.shift();
        var sImgPath  = args.shift();
        if (!sImgPath) {
            sImgPath = '';
        }
        //5. get image names
        var sAlertPng = sImgPath+"ajaxcodes/img/alert.png";
        var sXPng = sImgPath + "ajaxcodes/img/tabClose.png";

        //10. message in a table
        var sHTML;;
        sHTML = '<table width = "99%" cellpadding = "2" cellspacing = "0" border = "0">';
        sHTML += '<tr class = "alertHeader confirmHeader"><td colspan=2 width = "90%" align = "center" class = "alertHeader"><i>thermofluids.net</i> Alert Box</td>  <td width="10%" align="right"><img src="' + sXPng +'" ></td></tr>';
        sHTML += '<tr><td width="10%" align="center"><img src="' + sAlertPng + '" ></td><td colspan = "2" class = "alertMsg">' + sErrorMsg + '</td></tr>';
        sHTML += '<tr><td colspan = "3" align = "center"><input type="button" class="okbutton"  value="OK"/></td></tr></table>';

        // 15. Check for existence of popup box and remove
        //if($(this.sID)) {
        if($('popup')) {
            var oeP = $('popup').parentNode;
            oeP .removeChild($('popup')); //hardcoded name 'popup' given to child node, which will be deleted..
        }

        // 20. Create the new div named by this.id and attach it to popup
        var oDiv = document.createElement("div");
        oDiv.id='popup';
        xAddClass(oDiv, 'alertHome');
        $(this.sID).appendChild(oDiv);
        oDiv.innerHTML = sHTML;

        //25. Attach events
        oDiv.onclick = this.handleEvent.bind(this,'popup');
        Rico.Corner.round(oDiv, {
            border: '#0088bb',
            bgColor: '#fff',
            blend:true
        });
    },


    // same as alert above except a soft background and message box
    alertSoft: function() {
        //0. get arguments
        var args = $A(arguments);
        var sErrorMsg = args.shift();
        var sImgPath  = args.shift();
        if (!sImgPath) {
            sImgPath = '';
        }
        //5. get image names
        var sAlertPng = sImgPath+"ajaxcodes/img/info.png";
        var sXPng = sImgPath + "ajaxcodes/img/tabClose.png";

        //10. message in a table
        var sHTML;;
        sHTML = '<table width = "99%" cellpadding = "2" cellspacing = "0" border = "0">';
        sHTML += '<tr class = "alertHeader confirmHeader"><td colspan=2 width = "90%" align = "center" class = "alertHeader"><i>thermofluids.net</i> Message Box </td>  <td width="10%" align="right"><img src="' + sXPng +'" ></td></tr>';
        sHTML += '<tr><td width="10%" align="center"><img src="' + sAlertPng + '" ></td><td colspan = "2" class = "alertMsg">' + sErrorMsg + '</td></tr>';
        sHTML += '<tr><td colspan = "3" align = "center"><input type="button" class="okbutton"  value="OK"/></td></tr></table>';

        // 15. Check for existence of popup box and remove
        //if($(this.sID)) {
        if($('popup')) {
            var oeP = $('popup').parentNode;
            oeP .removeChild($('popup')); //hardcoded name 'popup' given to child node, which will be deleted..
        }

        // 20. Create the new div named by this.id and attach it to popup
        var oDiv = document.createElement("div");
        oDiv.id='popup';
        xAddClass(oDiv, 'alertHome');
        xAddClass(oDiv, 'cyanbg');
        $(this.sID).appendChild(oDiv);
        oDiv.innerHTML = sHTML;

        //25. Attach events
        oDiv.onclick = this.handleEvent.bind(this,'popup');
        Rico.Corner.round(oDiv, {
            border: '#0088bb',
            bgColor: '#fff',
            blend:true
        });
    },

    // suggest message in a new div ('popup') hooked to a parent div supplied by caller (name and suggestHome class are different
    suggest: function() {
        //0. get arguments
        var args = $A(arguments);
        var sErrorMsg = args.shift();
        var sImgPath  = args.shift();
        if (!sImgPath) {
            sImgPath = '';
        }
        //5. get image names
        var sAlertPng = sImgPath+"ajaxcodes/img/alert.png";
        var sXPng = sImgPath + "ajaxcodes/img/tabClose.png";

        //10. message in a table
        var sHTML;;
        sHTML = '<table width = "99%" cellpadding = "2" cellspacing = "0" border = "0">';
        sHTML += '<tr class = "alertHeader confirmHeader"><td colspan=2 width = "90%" align = "center" class = "alertHeader"><i>thermofluids.net</i> Suggests:</td>  <td width="10%" align="right"><img src="' + sXPng +'" ></td></tr>';
        sHTML += '<tr><td width="10%" align="center"><img src="' + sAlertPng + '" ></td><td colspan = "2" class = "alertMsg">' + sErrorMsg + '</td></tr>';
        sHTML += '<tr><td colspan = "3" align = "center"><input type="button" class="okbutton"  value="OK"/></td></tr></table>';

        // 15. Check for existence of popup box and remove
        //if($(this.sID)) {
        if($('popup')) {
            var oeP = $('popup').parentNode;
            oeP .removeChild($('popup')); //hardcoded name 'popup' given to child node, which will be deleted..
        }

        // 20. Create the new div named by this.id and attach it to popup
        var oDiv = document.createElement("div");
        oDiv.id='popup';
        xAddClass(oDiv, 'suggestHome');
        $(this.sID).appendChild(oDiv);
        oDiv.innerHTML = sHTML;

        //25. Attach events
        oDiv.onclick = this.handleEvent.bind(this,'popup');
        Rico.Corner.round(oDiv, {
            border: '#0088bb',
            bgColor: '#fff',
            blend:true
        });
    },


    // floating (transparant) box just for the parent of a frame (called from bottom.js usually)
    alertFloat: function() {
        var args = $A(arguments);
        var sErrorMsg = args.shift();
        //alert('error: ' +oTop.main.document.body.innerHTML);
        var sHTML;
 
        sHTML = '<table width = "100%" cellpadding = "2" cellspacing = "0" border = "0">';
        sHTML += '<tr class = "floatHeader"><td colspan=2 width = "90%" align = "center" class = "alertHeader"><i>thermofluids.net</i> Alert Box</td>  <td width="10%" align="right"></td></tr>';
        sHTML += '<tr><td width="10%" align="center"> &nbsp;</td><td colspan = "2" class = "alertMsg">' + sErrorMsg + '</td></tr>';
        sHTML += '<tr><td colspan = "3" align = "center"><input type="button" class="okbutton"  value="OK"/></td></tr></table>';

        var oDiv = document.createElement("div");
        oDiv.id='this.sID';


        //if(this.bIE)  oDiv.style.position = 'absolute';
        oDiv.style.position = 'absolute';

        try {
            var oTop = parent;
            while (oTop.frames[0].name !='main') oTop = oTop.parent;

            var oRoot = oTop.main.document.body;
            if($(this.sID)) {
                var oeP = $(this.sID).parentNode;
                oeP .removeChild($(this.sID));
            }

            oRoot.appendChild(oDiv);
            xAddClass(oDiv, 'alertFloat');
            oDiv.innerHTML = sHTML;

            oDiv.onclick = this.handleEventVanish.bind(this,oRoot, oDiv);
            Rico.Corner.round(oDiv, {
                border: '#0088bb',
                bgColor: '#fff',
                blend:true
            });
        }
        catch (err){
            alert(sErrorMsg);
            return;
        }
    },

    handleEventVanish: function(oRoot, oDiv, event) {
        oRoot.removeChild(oDiv);
    },

    handleEvent: function(element, event) {
        Effect.Fade(element);
    },


    // floating (transparant) box just for applets, and animations, called from bottom.js: new way with try catch
    alertApplet: function() {
        //5. ssErrorMsg is the only argument, this.id (window name 'floatWindw') is in constructor
        var args = $A(arguments);
        var sErrorMsg = args.shift();
//alert('we are in popup');
        //10. innerHTML is a table w/o any relative refernce

        var sHTML = '<table width = "100%" cellpadding = "3" cellspacing = "0" border = "0">';
        sHTML += '<tr style = "background: #ffcc55; font-size: 13px; font-weight: bold; color: #0000ff;" ><td  width = "90%" align = "center" class = "alertHeader"><i>thermofluids.net</i> Alert Box</td>  <td width="10%" align="right"> </td></tr>';
        sHTML += '<tr><td  class = "alertAppletMsg">' + sErrorMsg  + '</td><td  align = "center"><input type="button" class="okbutton"  value="OK"/></td></tr>';
        sHTML += '</table>';


        //15. try to use popup without any class only inline style
        try{
            var oDiv = document.createElement("div");
            oDiv.id=this.sID;
            oDiv.style.position = 'absolute';
            oDiv.style.backgroundColor = '#ffeecc';
            oDiv.style.left = '50px';
            oDiv.style.top = '0px';
            oDiv.style.filter = 'alpha(opacity=90)';
            oDiv.style.fontSize = '10px';
            oDiv.style.fontFamily = 'Verdana, Arial, Helvetica, sans-serif';
            oDiv.style.opacity = 0.9;
            oDiv.style.marginRight = '50px';
            oDiv.style.zIndex = 10000;

            //15.5 find the topmost frame
            var oTop = parent;
            while (oTop.frames[0].name !='main') oTop = oTop.parent;

            //15.10 if the alert window already open, close it
            var oMainBody = oTop.main.document.body;
            var oE = oTop.main.document.getElementById(this.sID);           
            if (oE) {
                oMainBody.removeChild(oE);               
            }

            //15.15 Add the new div to main frame
            oMainBody.appendChild(oDiv);
            
            //xAddClass(oDiv, 'alertApplet');
            //15.20 attach html and events
            oDiv.innerHTML = sHTML;
            oDiv.onclick = this.handleEventVanish.bind(this,oMainBody,oDiv);

            //15.25 make it look cool
            Rico.Corner.round(oDiv, {
                border: '#0000ff',
                bgColor: '#fff',
                blend:true
            });
            return;
        }
        //20. simple alert just in case...
        catch(err){
            alert(sErrorMsg);
            return;
        }
    },


    // floating (transparant) box when an element id is supplied
    alertInElement: function(sElement, sErrorMsg) {
        var sHTML;
        sHTML = '<table width = "100%" cellpadding = "3" cellspacing = "0" border = "0">';
        sHTML += '<tr class = "floatHeader"><td  width = "90%" align = "center" class = "alertHeader"><i>thermofluids.net</i> Alert Box</td>  <td width="10%" align="right"> </td></tr>';
        sHTML += '<tr><td  class = "alertAppletMsg">' + sErrorMsg  + '</td><td  align = "center"><input type="button" class="okbutton"  value="OK"/></td></tr>';
        sHTML += '</table>';

        var oDiv = document.createElement("div");
        oDiv.id=this.sID;

        var oRoot = $(sElement);
        oRoot.appendChild(oDiv);

        xAddClass(oDiv, 'alertElement');
        oDiv.innerHTML = sHTML;
        oDiv.onclick = this.handleEventVanish.bind(this,oRoot, oDiv);


        Rico.Corner.round(oDiv, {
            border: '#0000ff',
            bgColor: '#fff',
            blend:true
        });
    },
    // floating (transparant) box when an element id is supplied
    alertInElementCenter: function(sElement, sErrorMsg) {
        this.alertInElement(sElement, sErrorMsg);
        var oDiv = $(this.sID);
        xRemoveClass(oDiv, 'alertElement');
        xAddClass(oDiv, 'alertElementCenter');
        //alert('modify now');
    },

    render: function(innerHTML) {


        // background blurring. remove element in eventlistener method at the bottom
        var s = "<div id='blockUI' class='translucent' style=' background-color: gray;"+
        "width: 100%; height: 1000px; position: absolute; left: 0px; top: 0px; z-index: 9000;'"+
        "onclick='return false' onmousedown='return false' onmousemove='return false'"+
        "onmouseup='return false' ondblclick='return false'> &nbsp;</div>";

        var oBgdiv =  document.createElement("div");
        oBgdiv.innerHTML = s;

        oBgdiv.id = 'blockUItop';
        document.body.appendChild(oBgdiv);
        // innerHTML = "Hello Chrome!";


        //alert("In popup: 61" + this.id);
        if($(this.id)) {
            document.body.removeChild($(this.id));
        }
        // Check for existence and create popup oDiv if necessary


        if(!$(this.id)) {
            var oDiv = document.createElement("div");
            oDiv.id=this.id;
            if(bIE) oDiv.style.position = 'absolute';
            document.body.appendChild(oDiv);
            xAddClass(oDiv, 'standardPopup');
            var popupCloseButton = document.createElement("img");
            popupCloseButton.id = "popupCloseButton";
            popupCloseButton.src = "img/tabClose.png";
            popupCloseButton.align = "right";
            xAddClass(popupCloseButton, 'popupCloseButton');
            oDiv.appendChild(popupCloseButton);
            if(bIE) oDiv.innerHTML = oDiv.innerHTML + '<br><br>' + innerHTML;
            else oDiv.innerHTML = oDiv.innerHTML  + innerHTML;
            //alert("done creating table at 82");
            oDiv.onclick = this.eventListener.bindAsEventListener(this,this.id);
            Rico.Corner.round(oDiv, {
                border: '#1275A7',
                bgColor: '#b7b7b7',
                blend:true
            });
            if(!bIE)
                Effect.Appear(this.id);
        } 
    }
};

