var realtorViewSwap = Class.create({
    initialize: function(elem){
		this._images = $$('img.get_image');
		this._triggers = $$('.' + elem);
		this._conts = $$('.realtorViews');
		this._triggers.each(function(elem){
			elem.observe('click',this.handleOpenEvent.bind(this));
		}.bind(this));
	},
	handleOpenEvent: function(e){
		Event.stop(e);
		var ele = Event.element(e);
		var _div = $(ele.readAttribute('rel'));
		this._conts.each(function(elem){
			elem.hide();
		});
		this._triggers.each(function(elem){
			elem.removeClassName('activeTrigger');
		});
		ele.addClassName('activeTrigger');
		if(_div.identify() == 'realtorThumbs'){
			this._images.each(function(elem){
				var _new_source = elem.readAttribute('longdesc');
				if(_new_source != elem.src) {
					elem.src = elem.readAttribute('longdesc');
				}
			});
		}		
		if(_div.visible()){
			_div.hide();
		}else{
			_div.show();
		}
	}	
});
var RealtorInfoAjax = Class.create({
    initialize: function(){
        this._ajaxResponsesCache = new Array();
        this.userID = null;
        this.trigger = null;
        this.tooltop = null;
        var realtor_tip = new jo_tooltip('jo_tooltip',{
            onOpen: function(element,element2){this.onOpen(element,element2);}.bind(this),
            useStem:true,
            stemHeight:126,
            stemBuffer:20,
            offset:{x:45,y:0}
        });
    },
    onOpen: function(element,element2){
        this.trigger = element;
        this.tooltip = element2;
        this.userID = this.trigger.id.replace('uid_','');   
        this.tooltip.update('<img src="/images/ajax-loader.gif" alt="loading" />');
        if(this._ajaxResponsesCache[this.userID] == undefined){           
            var ajaxRequest = new Ajax.Request('/realtor/realtor_info/' + this.userID , {
                method:'post',
                onSuccess: function(response,json){this.onOpenAjaxResponse(response,json);}.bind(this)
            });
        }else{
            this.getResponse();
        }
    },
    onOpenAjaxResponse: function(response,json){
        var temp = null;
		var email = null;
		if(json.error){
			temp = '<div>There was an error retreiving this agent\'s information.</div>';
        }else{
            temp = '<div>';
			if(json.pic_url != undefined && json.pic_url.length > 4){
				temp += '<img class="realtor_photo" src="' + json.pic_url + '/crop/100,130/" width="85" height="120" alt="" />';
			}else{
				temp += '<img class="realtor_photo" src="/images/person_avatar.gif" width="85" height="120" alt="" />';
			}
			if(json.name != undefined){
				temp += '<p><span class="rosterName">' + json.name + '</span></p>';
			}
			if(json.parent != undefined && json.parent.name != undefined){
				temp += '<p><strong>' + json.parent.name + ' Office</strong></p>';
			}
			if(json.address.Cell_Phone.value != undefined && json.address.Cell_Phone.value.length > 0){
				temp += '<p><span class="itemLabel">Mobile</span> ' + json.address.Cell_Phone.value + '</p>';
			}
			if(json.address.Office_Phone.value != undefined && json.address.Office_Phone.value.length > 0){
				temp += '<p><span class="itemLabel">Office</span> ' + json.address.Office_Phone.value + '</p>';
			}
			if(json.address.Fax_Phone.value != undefined && json.address.Fax_Phone.value.length > 0){
				temp += '<p><span class="itemLabel">Fax</span> ' + json.address.Fax_Phone.value + '</p>';
			}
			if(json.email != undefined){
				email = json.email.split('@');
				if(email[0].length > 15){
					temp += '<p><span class="itemLabel">Email</span> ' + email[0] + '@<br />' + email[1] + '</p>';
				}else{
					temp += '<p><span class="itemLabel">Email</span> ' + json.email + '</p>';
				}
			}
			if(json.website_url != undefined && json.website_url.length > 6 && json.entity_type == 'entity_team'){
				temp += '<p style="clear:left;"><span class="itemLabel">Our Website</span> ' + json.website_url.truncate(40) + '</p>';
			}else if(json.website_url != undefined && json.website_url.length > 6){
				temp += '<p style="clear:left;"><span class="itemLabel">My Website</span> ' + json.website_url.truncate(40) + '</p>';
			}
				temp += '<p style="clear:left;"><span class="instructions_tip">Click on my name in the agent roster for more info &amp; to contact me.</span></p>';
            temp += '<div class="clearAll">&nbsp;</div></div>';
        }
		this._ajaxResponsesCache[this.userID] = temp;
        this.getResponse();
    },
    getResponse: function(){
        this.tooltip.update(this._ajaxResponsesCache[this.userID]);
    }
});