var activeDialog = null;
var miscOrderPhone = null;

function show_order_webdev_dialog(doc_id) {
        if(activeDialog) {
            activeDialog.destroy();
        }
	activeDialog = new Dialog({
		dialog_type : 'order_webdev',
		request_data : {
                    doc_id : (typeof doc_id == 'undefined' ? 0 : doc_id)
		},
		onload : initMiscOrderPhone
	});
	activeDialog.open();
}

function show_misc_order_dialog(doc_id, need_qty, price) {
        if(activeDialog) {
            activeDialog.destroy();
        }
	activeDialog = new Dialog({
		dialog_type : 'order',
		request_data : {
			doc_id : (doc_id == undefined ? 0 : doc_id),
			need_qty : (need_qty == undefined ? 0 : need_qty),
			price : price
		},
		onload : initMiscOrderPhone
	});
	activeDialog.open();
}
function show_tarif_order_dialog(doc_id, need_qty, price) {
        if(activeDialog) {
            activeDialog.destroy();
        }
	var opt = {
		dialog_type : 'order',
		request_data : {
			tarif_id : (doc_id == undefined ? 0 : doc_id),
			need_qty : (need_qty == undefined ? 0 : need_qty),
			price : price
			
		},
		onload : initMiscOrderPhone
	};
	activeDialog = new Dialog(opt);
	activeDialog.open();
}
function show_main_order_dialog(doc_id, need_qty, price) {
        if(activeDialog) {
            activeDialog.destroy();
        }
	activeDialog = new Dialog({
		dialog_type : 'order',
		request_data : {
			doc_id : (doc_id == undefined ? 0 : doc_id),
			need_qty : (need_qty == undefined ? 0 : need_qty),
			price : price
		},
		onload : initMiscOrderPhone
	});
	activeDialog.open();

}
function show_main_phone_dialog(doc_id, need_qty, price) {
        if(activeDialog) {
            activeDialog.destroy();
        }
	activeDialog = new Dialog({
		dialog_type : 'phone',
		request_data : {
			doc_id : (doc_id == undefined ? 0 : doc_id),
			need_qty : (need_qty == undefined ? 0 : need_qty),
			price : price
		},
		onload : initMiscOrderPhone2
	});
	activeDialog.open();
}

function initMiscOrderPhone2()
{
	var el = $('creq_contact_phone_container');
	if (!el)
		return;
	el.empty();
	var inp = new PhoneInput({
		city_codes : city_codes_array,
		input_name : 'add[cphone]',
		input_id : 'creq_contact_phone',
		phone : true
	});
	el.grab(inp.wrapper);
}
function initMiscOrderPhone() {
        initPhone('misc_order_phone_container');
        initPhone('misc_order_fax_container');
}

function initPhone(id) {
	var el = $(id);
	if (!el)
		return;
        var el_name = el.getElement('input').get('name');
	el.empty();
	var inp = new PhoneInput({
		city_codes : city_codes_array,
		input_name : el_name,
		input_id : id.replace(/_container/i, '')
	});
	el.grab(inp.wrapper);
}

var Dialog = new Class({
	Extends: LightFace,
	
	options: {
		url: '/Dialog',
		request: {
			url: '/Dialog',
                        method : 'post'
		},
                width: 'auto',
                height: 'auto',
                draggable: true,
                dialog_type: '',
                request_data: {},
                onOpen: function() {this.outerOverlay.show();},
                onClose: function() {this.outerOverlay.hide();},
               errorMessage: 'Ошибка загрузки',
               onload: function() {}
	},
        
        req: null,
	
	initialize: function(options) {
		this.parent(options);
                var data_str = "";
		for (var p in options.request_data)
			data_str += '&' + encodeURIComponent(p) + '=' + encodeURIComponent(options.request_data[p]);
                this.options.request.data = 'ajax=1&doit=get_dialog&type=' + encodeURIComponent(options.dialog_type) + data_str;
                this.load();
	},
        
	load: function(url,title) {
                var self = this;
                this.outerOverlay = new Overlay(document.body, {onClick: function() { self.close();  this.close(); }});
		var props = (Object.append || $extend)({
			onRequest: function() {
				this.fade();
				this.fireEvent('request');
			}.bind(this),
			onSuccess: function(response) {
				this.messageBox.set('html',response);
				this.fireEvent('success');
				this.options.onload();
                                this._position();
			}.bind(this),
			onFailure: function() {
				this.messageBox.set('html',this.options.errorMessage);
				this.fireEvent('failure');
			}.bind(this),
			onComplete: function() {
				this._resize();
				this._ie6Size();
				this.messageBox.setStyle('opacity',1);
				this.unfade();
				this.fireEvent('complete');
			}.bind(this)
		},this.options.request);

		if(title && this.title) this.title.set('html',title);
		if(!props.url) props.url = url || this.options.url;
                
                this.req = new Request(props);
                this.req.send();
		return this;
	}
	
});

var Overlay = new Class({
  
    Implements: [Options,Events],
  
    options:  {
        id: 'overlay',
        color: '#000',
        duration: 500,
        opacity: 0.5,
        zIndex: 5000/*,
    onClick: $empty,
    onClose: $empty,
    onHide: $empty,
    onOpen: $empty,
    onShow: $empty
    */
    },
  
    initialize: function(container,options) {
        this.setOptions(options);
        this.container = document.id(container);
        this.overlay = new Element('div',{
            id: this.options.id,
            opacity: 0,
            styles: {
                position: 'absolute',
                background: this.options.color,
                left: 0,
                top: 0,
                'z-index': this.options.zIndex
            },
            events: {
                click: function() {
                    this.fireEvent('click');
                }.bind(this)
            }
        }).inject(this.container);
        this.tween = new Fx.Tween(this.overlay,{ 
            duration: this.options.duration,
            link: 'cancel',
            property: 'opacity',
            onStart: function() {
                var size = this.container.getScrollSize();
                this.overlay.setStyles({
                    width: size.x,
                    height: size.y
                });
            }.bind(this),
            onComplete: function() {
                this.fireEvent(this.overlay.get('opacity') == this.options.opacity ? 'show' : 'hide');
            }.bind(this)
        });
    },
    open: function() {
        this.fireEvent('open');
        this.tween.start(this.options.opacity);
        var x = $$('select').combine($$('div[class=flashbox]'));
        this.objects = x.filter(function(el)
        {
                return el.style.visibility=='hidden' ? false : !!(el.style.visibility = 'hidden');
        });
    },
    close: function() {
        this.fireEvent('close');
        this.tween.start(0);
        $$('select').combine($$('div[class=flashbox]')).each( function(el) { el.setStyle('visibility', 'visible'); } );
    },
    show: function() {
        this.open();
    },
    hide: function() {
        this.close();
    }
});
