var jbAgentWebsite;
window.addEvent('domready',function(){
	jbAgentWebsite = new jb_agentWebsite();
});

var jb_agentWebsite = new Class({
	
	initialize : function(){
		if($type($('realtor-photo'))=='element'){
			if($type($('main-img'))=='element')
				var color = $('main-img').getStyle('border-bottom-color');
			if(color == '' && $type($('main-img-wrapper'))=='element')
				var color = $('main-img-wrapper').getStyle('border-bottom-color');
			
			$('realtor-photo').setStyle('border-right-color',color);
		}
	},
	
	contact: function(agt_id,evt) {
        if (!this.Modal) {
            this.Modal = new Avi_Modal();
        }
        if (!this.Modal_notif) {
        	this.Modal_notif = new Avi_Modal();
        }
        if($type(evt)=='string'){
        	$('notif-msg').set('html',evt);
        }
        
        if (!this.modal_contact) {
            var html = '';
           // if($type(evt) == 'string') html += '<a style="color:#ff0000;">'+evt+'</a>';
            html += '<a style="color:#ff0000;" id="notif-msg"></a><br/>';
           // html += '<form onsubmit="return false;">';
            html += '<p><label>Your Name: </label><span><input id="s-name" type="text" size="28"/></span><br clear="both"/></p>';
            html += '<p><label>Your Email: </label><span><input id="s-email" type="text" size="28"/></span><br clear="both"/></p>';
            html += '<p><label>Your Phone: </label><span><input id="s-phone" type="text" size="28"/></span><br clear="both"/></p>';
            html += '<p>Message:<br/><textarea id="s-msg" type="text" size="28" style="width:290px;height:100px"></textarea></p>';
            html += '<p style="margin-top:10px;border-top:1px solid #AAA"><strong>Type the code in the image:</strong><br/>';
            html += '<span><img title="Are you a human? Type this code." src="http://v2.estatevue.com/public/images/blank.gif" width="150" height="50" id="captcha-img" align="left" style="margin-right:15px;float:left;"/><input id="s-captcha" type="text" maxlength="5" size="17" autocomplete="off"/><br/><a onclick="jbAgentWebsite.captcha_refresh()" style="cursor:pointer;">try other image</a></span><br clear="both"/></p>';
           // html += '</form>';

            this.my_alert = this.Modal_notif.build({
                title: 'Contact',
                html: '<p>Your message has been sent to the agent.</p>',
                width: 320,
                height: 100,
                ok_label:null,
                cancel_label: 'close'
            });
            this.modal_contact = this.Modal.build({
                title: 'Contact',
                html: html,
                width: 320,
                height: 400,
                ok_fn: function(){
                    var param = {
                        n:($('s-name').value).toString().clean(),
                        e:($('s-email').value).toString().clean(),
                        p:($('s-phone').value).toString().clean(),
                        m:($('s-msg').value).toString().clean(),
                        c: ($('s-captcha').value).toString().clean(),
                        id: AGENT_ID
                        //h_conf: JSON.encode(h_conf)
                    };
                    if(!param.n || !param.e || !param.m || !param.c) {
                        alert('All fields are required');
                        return false;
                    }
                    new Request.JSON({
                        url: site_url + 'corporate/ajaxContactAgent', 
                        onComplete: function(respons) {
                            if (respons.success == true) {
                               // var html = '<p>Your message has been sent to the listing realtor.</p>';
                               // this.my_alert.setHTML(html);
                                this.my_alert.show();
                            } else {
                                this.contact(AGENT_ID,respons.msg);
                            }
                        }.bind(this),
                        onFailure: function() {
                            this.contact("Can't connect to our server, please try again leter.");
                        }.bind(this)
                    }).post(param);
                    return true;
                }.bind(this),
                cancel_fn: null,
                ok_label: 'Send',
                cancel_label: 'close'
            });
            
        } else {
        	if($type($('notif-msg')) == 'element')
        		$('notif-msg').set('html','');
        }
        this.modal_contact.show();
        this.captcha_refresh();
    },
    //------------------------------------------------------
    captcha_refresh: function() {
        var img = $('captcha-img');
        if(!img) return;
        var rand = Math.round(Math.random() * 100000);
        img.src = site_url + 'search/captcha/rand-' + rand + '.jpg';
        $('s-captcha').value = '';
    }
});

var Avi_Modal = new Class({
	modals:[],
	elements: {},
	options: {
		title:null,
		html:'',
		width:300,
		height:300,
		ok_fn: null,
		cancel_fn: null,
		ok_label: 'OK',
		cancel_label:'Cancel'		
	},
	
	initialize: function(opts) {
		this.options = $merge(this.options, opts);
		this.elements.modal = new Element('div', {
			'class': 'avi-modal',
			styles:{
				position:'fixed',
				display:'none'
			}
		}).inject(document.body);
	},
	
	//----------------------------
	build: function(options) {
		options = $merge(this.options, options);
		var wrapper = new Element('form',{
			'class':'avi-modal-wrapper',
			styles: {
				width: options.width
			}
		});
		
		if(options.title) {
			new Element('h3',{'class':'avi-modal-title',html:options.title}).inject(wrapper);
		}
		new Element('div',{
			'class':'avi-modal-content',
			'id':'avi-modal-content',
			html:options.html,
			styles:{
				height:options.height - 70
			}
		}).inject(wrapper);
		var footer = new Element('div',{'class':'avi-modal-footer'}).inject(wrapper);
		if(!this.ok_label && !options.cancel_label) {
			new Element('button',{
				text:'close',
				events:{
					click:function(){
						this.hide();
						return false;
					}.bind(this)
				}
			}).inject(footer);
		} else {
			if (options.ok_label) {
				new Element('button', {
					text: options.ok_label,
					events: {
						click: function(){
							var close = true;
							if ($type(options.ok_fn)=='function') 
								close = options.ok_fn.run();
							if(close !== false)
								this.hide();
							return false;
						}.bind(this)
					}
				}).inject(footer);
			}
			if (options.cancel_label) {
				new Element('button', {
					text: options.cancel_label,
					events: {
						click: function(){
							if ($type(options.cancel_fn)=='function') {
								options.cancel_fn.run();
							}
							this.hide();
							return false;
						}.bind(this)
					}
				}).inject(footer);
			}
		}
			
		var i = this.modals.length;
		var modal = {
			el: wrapper,
			options: options,
			show: function(num) {
				this.show(num);
			}.bind(this, i),
			hide: function() {
				this.hide();
			}.bind(this)
		};
		this.modals.push(modal);
		return modal;
	},
	
	//----------------------------
	show: function(num) {
		var m = this.modals[num];
		if(!m) return false;
		if(!Browser.Engine.trident)
			this.elements.modal.set('html','');
		m.el.inject(this.elements.modal);	
		
		var top_val = ((window.getHeight() - m.options.height)/2).toInt();
		var left_val = ((window.getWidth() - m.options.width)/2).toInt();
		
		if(is_widget==true){
			top_val = 50;
		}
		this.elements.modal.setStyles({
			display:'',
			top: top_val,
			left: left_val
		});
		return true;
	},
	//----------------------------
	hide: function() {
		this.elements.modal.setStyle('display','none');
		return false;
	},
	//----------------------------
	fade: function(num) {
	},
	//----------------------------
	unfade: function(num) {
	}
});
