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

//
//  Configurationl
//
AlertboxOptions = Object.extend({
    myoverlayOpacity: 0.3,
    animate: true,
    borderSize: 0,
    defaultWidth: 290,
    defaultHeight: 185,
    defaultType: 0
}, 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='<div id="tableTextContainer">\
			<div id="buy_form">\
				<div id="buttonContainer" class="tdbuttonContainer"></div>\
				<div id="tdTextContainer" class="tdTextContainer"><div id="textContainer"></div></div>\
			</div>\
		</div>';

this.buttons=[];

this.buttons[0]='';
this.buttons[1]='<div id="closeButton">Закрыть</div>';
this.buttons[2]='<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">';

		$('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;
        if(type>2) type=0;
        if(this.buttons[type]!=''){
            if(!$('buttonContainer')) 
            $('buttonContainer').innerHTML=this.buttons[type];
        }
        else if($('buttonContainer')){
            $('buttonContainer').parentNode.removeChild($('buttonContainer'));
        }
        if(type==1){
            this.onClose=options.onClose||(function(){});
            $('closeButton').observe('click', (function(){ this.onClose(); this.end(); }).bind(this));
        }
        else if(type==2){
            this.onYes=options.onYes||(function(){});
            this.onNo=options.onNo||(function(){});
            $('yesButton').observe('click', (function() { this.onYes(); this.end(); }).bind(this));
            $('noButton').observe('click', (function() { this.onNo(); this.end(); }).bind(this));
        }
        $$('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(); });


function buy(id, product) {
	alertbox.start('\
	<p>Оставьте вашу контактную информацию. Мы с вами свяжемся по вопросу приобретения этой работы.</p>\
	<form name="adminForm" method="post" action="/netcat/add.php">\
		<input name="cc" value="75" type="hidden">\
		<input name="sub" value="180" type="hidden">\
		<input name="posting" value="1" type="hidden">\
		<input name="f_Checked" value="1" type="hidden">\
		<input name="f_id" value="'+id+'" type="hidden">\
		<input name="f_product" value="'+product+'" type="hidden">\
		<input name="url" value="'+document.location.href+'" type="hidden">\
		<label id="name">Ф.И.О.<br/>\
		<input type="text" name="f_fio" id="fio_f" /></label>\
		<label id="cont_inf">Контактная информация<br/>\
		<textarea name="f_contacts" rows="3" id="info_f" /></textarea></label>\
		<label id="check">\
		<input id="submit" type="submit" name="subm" value="Отправить" onclick="return checkform();" /></label>\
	</form>');
}

function checkform() {
	var fio = document.getElementById('fio_f').value;
	var info = document.getElementById('info_f').value;
	if (fio!=""&&info!="") {
		return true;
	} else {
		if (!document.getElementById('warn')) {
			var warn = document.createElement('span')
			warn.id = 'warn';
			warn.innerHTML = 'Вы должны заполнить оба поля';
			document.getElementById('submit').parentNode.insertBefore(warn,document.getElementById('submit'));
		}
		return false;
	}
}
