// -----------------------------------------------------------------------------------

//
//  Configurationl
//
AlertboxOptions = Object.extend({
    myoverlayOpacity: 0.3,
    animate: true,
    borderSize: 0,
    defaultWidth: 300,
    defaultHeight: 146,
    defaultType: 0,
    closeButtonSrc: "/images/but_close.gif",
    yesButtonSrc: "/images/but_yes.gif",
    noButtonSrc: "/images/but_cancel.gif"
}, window.AlertboxOptioTextContainerns || {});

// -----------------------------------------------------------------------------------

var Alertbox = Class.create();
var alertbox;

Alertbox.prototype = {
    initialize: function() {    

        if (AlertboxOptions.resizeSpeed > 10) AlertboxOptions.resizeSpeed = 10;
        if (AlertboxOptions.resizeSpeed < 1)  AlertboxOptions.resizeSpeed = 1;

	this.myoverlayDuration = AlertboxOptions.animate ? 0.2 : 0;  // shadow fade in/out duration

        var objBody = $$('body')[0];

	objBody.appendChild(Builder.node('div',{id:'myoverlay'}));
	
        objBody.appendChild(Builder.node('div',{id:'alertbox'}, [
            Builder.node('div',{id:'outerTextContainer'})
        ]));

	$('outerTextContainer').innerHTML='<table id="tableTextContainer" cellpadding="0" cellspacing="0">\
<tr><td valign="top" width="13" height="13"><img src="/images/ug1.png" alt=""></td><td class="top-center"><img src="/images/0.gif" alt="" width="1" height="1"></td><td valign="top" width="13"><img src="/images/ug2.png" alt=""></td></tr>\
<tr><td class="bordL tdTextContainer" valign="top">&nbsp;</td><td id="tdTextContainer" class="tdTextContainer"><div id="textContainer"></div></td><td class="bordR tdTextContainer" valign="top">&nbsp;</td></tr>\
<tr><td class="bordL tdbuttonContainer" valign="top">&nbsp;</td><td id="buttonContainer" class="tdbuttonContainer"><input id="closeButton" name="Закрыть" value="Закрыть" type="button"></td><td class="bordR tdbuttonContainer" valign="top">&nbsp;</td></tr>\
<tr><td valign="top" width="13" height="13"><img src="/images/ug4.png" alt=""></td><td class="bordB bottom-center"><img src="/images/0.gif" alt="" width="1" height="1"></td><td valign="top" width="13"><img src="/images/ug3.png" alt=""></td></tr>\
</table>';

this.buttons=[];
this.buttons[0]='<input src="/images/but_close.gif" id="closeButton" name="Закрыть" value="Закрыть" type="image">';
this.buttons[1]='<input src="/images/but_yes.gif" id="yesButton" name="Ok" value="Ok" type="image"> <input src="/images/but_cancel.gif" id="noButton" name="Отмена" value="Отмена" type="image">';
//type 2 allows use of custom buttons
this.buttons[2]='';

		$('myoverlay').hide().observe('click', (function() { this.end(); }).bind(this));
		$('alertbox').hide().observe('click', (function(event) { if (event.element().id == 'alertbox') this.end(); }).bind(this));
		$('outerTextContainer').setStyle({ width: AlertboxOptions.defaultWidth+'px', height: AlertboxOptions.defaultHeight+'px' });
		$('tableTextContainer').setStyle({ width: AlertboxOptions.defaultWidth+'px', height: AlertboxOptions.defaultHeight+'px' });

        var th = this;
        (function(){
            var ids = 
                'myoverlay alertbox outerTextContainer textContainer tableTextContainer ';   
            $w(ids).each(function(id){ th[id] = $(id); });
        }).defer();

    },

    //  Display myoverlay and alertbox
    start: function(text, options) {
        options=options||{};
        width=options.width||AlertboxOptions.defaultWidth;
        height=options.height||AlertboxOptions.defaultHeight;
        type=options.type||AlertboxOptions.defaultType;

        closeButtonSrc=options.closeButtonSrc||AlertboxOptions.closeButtonSrc;
        yesButtonSrc=options.yesButtonSrc||AlertboxOptions.yesButtonSrc;
        noButtonSrc=options.noButtonSrc||AlertboxOptions.noButtonSrc;

        if(type>2) type=0;
        this.buttons[2]=options.buttons||'';

        if(this.buttons[type]!=''){
            $('buttonContainer').parentNode.style.display="";
            $('buttonContainer').innerHTML=this.buttons[type];
        }
        else {
            $('buttonContainer').parentNode.style.display="none";
        }
//        else if($('buttonContainer')) $('buttonContainer').parentNode.parentNode.removeChild($('buttonContainer').parentNode);

        if(type==0){
            this.onClose=options.onClose||(function(){});
            $('closeButton').observe('click', (function(){ this.onClose(); this.end(); }).bind(this));
            $('closeButton').src=closeButtonSrc;
        }
        else if(type==1){
            this.onYes=options.onYes||(function(){});
            this.onNo=options.onNo||(function(){});
//onYes must return TRUE if alertbox shouldn't close 
            $('yesButton').observe('click', (function() { if(!this.onYes()) this.end(); }).bind(this));
            $('noButton').observe('click', (function() { this.onNo(); this.end(); }).bind(this));
            $('yesButton').src=yesButtonSrc;
            $('noButton').src=noButtonSrc;
        }
        $$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'hidden' });
        // stretch myoverlay to fill page and fade in
        var arrayPageSize = this.getPageSize();
        $('myoverlay').setStyle({ width: arrayPageSize[0] + 'px', height: arrayPageSize[1] + 'px' });
        new Effect.Appear(this.myoverlay, { duration: this.myoverlayDuration, from: 0.0, to: AlertboxOptions.myoverlayOpacity });

        // calculate top and left offset for the alertbox 
        var arrayPageScroll = document.viewport.getScrollOffsets();
        var alertboxTop = options.top||arrayPageScroll[1] + (document.viewport.getHeight() / 2) - (height / 2);
        var alertboxLeft = options.left||arrayPageScroll[0];
        this.alertbox.setStyle({ top: alertboxTop + 'px', left: alertboxLeft + 'px' }).show();
        
        this.changeText(text, width, height);
    },

    //
    //  restart()
    //  show last alertbox again
    //
    restart: function() {
        $$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'hidden' });
        var arrayPageSize = this.getPageSize();
        $('myoverlay').setStyle({ width: arrayPageSize[0] + 'px', height: arrayPageSize[1] + 'px' });
        new Effect.Appear(this.myoverlay, { duration: this.myoverlayDuration, from: 0.0, to: AlertboxOptions.myoverlayOpacity });
/* try no recalc
        // calculate top and left offset for the alertbox 
        var arrayPageScroll = document.viewport.getScrollOffsets();
        var alertboxTop = arrayPageScroll[1] + (document.viewport.getHeight() / 2) - (height / 2);
        var alertboxLeft = arrayPageScroll[0];
        this.alertbox.setStyle({ top: alertboxTop + 'px', left: alertboxLeft + 'px' }).show();
*/
	this.alertbox.show();
    },
    //
    //  changeText()
    //  Hide most elements and preload image in preparation for resizing image container.
    //
    changeText: function(text, width, height) { 
        var widthNew  = (width  + AlertboxOptions.borderSize * 2);
        var heightNew = (height + AlertboxOptions.borderSize * 2);  
        this.outerTextContainer.setStyle({ width: widthNew+'px', height: heightNew+'px'});
        this.tableTextContainer.setStyle({ width: (width-2)+'px', height: (height-2)+'px'});
        this.textContainer.innerHTML = text;
        var arrayPageSize = this.getPageSize();
        this.myoverlay.setStyle({ height: arrayPageSize[1] + 'px' });
    },

    //
    //  end()
    //
    end: function() {
        this.alertbox.hide();
        new Effect.Fade(this.myoverlay, { duration: this.myoverlayDuration });
        $$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'visible' });
    },

    //
    //  getPageSize()
    //
    getPageSize: function() {
	        
	     var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}

		return [pageWidth,pageHeight];
	}
}

document.observe('dom:loaded', function () { alertbox=new Alertbox(); });
