var indeflash , sidebar_tabs, bio_tabs, news_carousel, name_tm, att_search_result;
var par_coords,coords,dragger;

var base_url = document.location.protocol + '//' + document.domain + '/';

var dragger, iscr ;



window.addEvent('domready', function() {



	if ( $('wrapp_attsearch_results') ) {
		// $('wrapp_attsearch_results').mask({style:{color:'#333', 'font-size':'12px'}}).unmask();
		$('wrapp_attsearch_results').get('mask').element.set({styles:{color:'#333', 'font-size':'12px'},'html':'<span style="padding:10px;position:absolute;">To search for an attorney, enter the first few letters of their first or last name. you can filter results by practice and title, or jump right to a practice by leaving the other fields blank.</span>'});
		$('wrapp_attsearch_results').unmask();
	}

if ( !Browser.Engine.trident ) $$('form').set('autocomplete','off');




if ( $('attsearch_results') ) {
	// $('attsearch_results').addEvent('touchstart', function(){
	// 	alert('touch');
	// })
	
	
	

dragger = $('attsearch_results').makeDraggable({
		modifiers:{x:null,y:'margin-top'},
		onBeforeStart:function(){
			par_coords= $('attsearch_results').getParent().getCoordinates();	
			$('attsearch_results').addClass('grabbing');
		}, 
		onStart: function() {
			coords= $('attsearch_results').getCoordinates();	
			
			
				if (  coords.top >= par_coords.top) {
					this.element.setStyle('margin-top',0);
				}
				if (  coords.top+coords.height <= par_coords.top+par_coords.height) {
					this.element.setStyle('margin-top',par_coords.height-coords.height);
				}
			
		},
		onDrag: function() {

			coords= $('attsearch_results').getCoordinates();	

				if (  coords.top >= par_coords.top) {
					this.element.setStyle('margin-top',0);
				}
				if (  coords.top+coords.height <= par_coords.top+par_coords.height) {
					this.element.setStyle('margin-top',par_coords.height-coords.height);
				}

		},
		onComplete: function() {
			$('attsearch_results').removeClass('grabbing');
		}
	});
	
	
	dragger.handles.addEvent('touchstart', function(e){
			e = new Event(e).preventDefault();
			document.body.style.WebkitUserSelect = 'none';
			dragger.fireEvent('beforeStart');
			dragger.fireEvent('start');

	}).addEvent('touchmove', function(e){
			e = new Event(e).preventDefault();
		
			dragger.fireEvent('drag');
	}).addEvent('touchend', function(){
			document.body.style.WebkitUserSelect = '';
			dragger.fireEvent('complete');
	})
	
	
	if(Browser.Engine.trident) dragger.handles.ondragstart = function() 
	{ return false; };
	
	
	
	if ( Browser.Platform.ipod || navigator.userAgent.match("Pre") ) {
		iscr = new  iScroll( dragger.handles, 'y' );
	}
	
}


	if ( $('indeflasharea') ) {
		 indeflash =  new Swiff('88E17A/assets/flash/Home_Banner.swf',{
		 	container: $('indeflasharea'),
		 	width:660,
		 	height:315
		 	});
		
	}



if ( $('locations-list') ) {
	$('locations-list').getElements('a.tabber').each(function(item) {
		var div = item.getParent('li').getNext().getElement('div');
		item.addEvent('click', function(){
			if (!this.hasClass('selected')) {
				this.getParent('ul').getElements('div').removeClass('active');
				this.getParent('ul').getElements('a').removeClass('selected');
				div.addClass('active');
				this.addClass('selected');
				$('location-ad').setStyle('display','none');
				
				return false;
			}
			else {
				div.removeClass('active');
				this.removeClass('selected');
				$('location-ad').setStyle('display','block');
				return false;
			}
		})
	})
}


if ( $("practiceareas-navigation") ) {
	practiceareas_navigation_tabs =  new Tabular($("practiceareas-navigation").getElements('a'));
}



if ( $("sidebar-navigation") ) {
	sidebar_tabs =  new Tabular($("sidebar-navigation").getElements('a'));
	
	
	$("sidebar-navigation").getElements("a").addEvent('click', function() {
		$('locations-list').getElements('div').removeClass('active');
		$('locations-list').getElements('a').removeClass('selected');
		$('location-ad').setStyle('display','block');

		$("attsearch_form").reset();
		$('lpa').value="";
		$('rank').value="";
		$("attsearch_form").getElements('input').addClass('empty_search');
		

		$("attsearch_results").set('html',"");
		$("att-shadow-top").setStyle('display','none');
		$("att-shadow-bottom").setStyle('display','none');
		
		$("attsearch_results").setStyle('margin-top',0);
		
	})


	
}

if ($('newslist')) {
	   news_carousel = new Inficarousel($('newslist'),{ tail:22, go_right:$('leftscroll'), go_left:$('rightscroll') });
}



if ($('name')) {
	$('name').addEvent('keyup', function(){
		
		$clear(name_tm);
		name_tm = ( function() {  do_name_search() }).delay(500);
	}).addEvent('keydown', function(){
		$clear(name_tm);
	}).addEvent('focus', function(){
		if ( this.value == 'Name') {
			this.value="";
		}
		this.removeClass('empty_search');
		
	}).addEvent('blur', function(){
		if ( this.value.clean() == '') {
			this.value="Name";
			this.addClass('empty_search');
			$('wrapp_attsearch_results').unmask();
			$('attsearch_results').empty()
			$('wrapp_attsearch_results').get('mask').element.set('html','<span style="padding:10px;position:absolute;">To search for an attorney, enter the first few letters of their first or last name. you can filter results by practice and title, or jump right to a practice by leaving the other fields blank.</span>');
			$('wrapp_attsearch_results').get('mask').element.setStyles({'background':'transparent'});
			$('wrapp_attsearch_results').mask();

			$("att-shadow-top").setStyle('display','none');
			$("att-shadow-bottom").setStyle('display','none');
			$('drag_msg').setStyle('display','none');


		}
	})
}


if ($('practice')) {
	$('practice').addEvent('keyup', function(e){
		var e =  new Event(e);
		pra = $('practice').value;
		$('auto_practice').empty();
		if ( pra.length >= 1 ) {
			
			practice_arr.each(function(item) {
			if ( item.name.test(pra,'i') ) {
				new Element("a",{text:item.name,href:item.url}).inject($('auto_practice')).addEvent('click', function(e) {

					$('lpa').value = item.LPA;
					
					var e = new Event(e);
					if (att_search_result && att_search_result.trim().length > 0) {
						// console.log($type(att_search_result));
						e.stop();
						$('practice').value = this.get('text');
						$('auto_practice').setStyle('display','none');
						$('practice').removeClass('empty_search');
						do_name_search();
							return false;
						}
						
												
				});
				new Element('br').inject($('auto_practice'));
			}	
			});

			$('auto_practice').setStyle('display','block')
			



			if ( att_search_result) {
				do_name_search()						
			}
			
		}
	else {
		if ( att_search_result) {
			do_name_search();
		}
	}	

	if (e.key == 'esc') $('auto_practice').setStyle('display','none');

	}).addEvent('focus', function(){
		if ( this.value == 'Practice') {
			this.value="";
		}
		else {
			$('auto_practice').setStyle('display','block');
		}
		this.removeClass('empty_search');

	}).addEvent('blur', function(){
		if ( this.value.clean() == '') {
			this.value="Practice";
			this.addClass('empty_search');
			$('lpa').value='';
		}
		(function() { $('auto_practice').setStyle('display','none') }).delay(100);
		
	})
}



if ( $("bio-navigation") ) {
	bio_tabs =  new Tabular($("bio-navigation").getElements('a'));
}
if ( $("extrainfo-navigation") ) {
	sidebar_tabs =  new Tabular($("extrainfo-navigation").getElements('a'));
}




if ( $('auto_title')) {
	
title_arr.each(function(item) {
	
	new Element("a",{text:item.name,href:item.url}).inject($('auto_title')).addEvent('click', function(e) {
		$('rank').value = item.rank;
		
		
		var e = new Event(e);
		if (att_search_result && att_search_result.trim().length > 0) {
			e.stop();
			$('title').value = this.get('text');
			$('auto_title').setStyle('display','none')
			$('title').removeClass('empty_search');
			do_name_search()						
				return false;
			}
	});

	new Element('br').inject($('auto_title'));
})

}


if ( $('title')) {
	$('title').addEvent('focus', function(){
		if ( this.value == 'Title') {
			this.value="";
		}
		this.removeClass('empty_search');
		$('auto_title').setStyle('display','block');
	}).addEvent('blur', function(e){
		(function() {
			if ( this.value.clean() == '') {
				this.value="Title";
				this.addClass('empty_search');
				$('rank').value="";
				$('auto_title').setStyle('display','none');
			}
			}.bind(this)).delay(100);
			
	}).addEvent('keyup', function(e) {
		var e = new Event(e);
		if (e.key == 'esc') $('auto_title').setStyle('display','none');
	})	.addEvent('click', function(){
			$('auto_title').setStyle('display','block');
		})
}




Form.Validator.add('validate-multipleEmails', {
	test: function(element) {
		return element.value.split(',').every( function( item ) {
			return (/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i).test(item.trim());
		})
	}
})


if ( $('send_email_form')) {



		var email = new Fx.Slide($('send_email_form'),{onComplete: function(){
			if ( !this.open ) { $(document.body).unmask()}
		}}).hide();

		$('send_email_form').setStyle('display','block');

		var val = new Form.Validator($('send_email_form'));

		if ($('send_by_email') ) {
			$('send_by_email').addEvent('click', function(e) {
				e.stop();
				$(document.body).mask({style:{background:'#000',opacity:0.7,'z-index':100} })
			 	email.toggle();
				return false;
			})
			
		}

		$('share_cancel').addEvent('click', function(){
			email.toggle();
		})

		$('email_close').addEvent('click', function(){
			email.toggle();
		})

	
}


if ( $('send_email_form_2') ) {
		new Form.Validator($('send_email_form_2'));
}


})


function do_name_search() {

		var name = ($('name').value == 'Name') ? "" : $('name').value;
		
		dragger.detach();
		dragger.element.setStyle('margin-top',0);
	
	
		
		
		
		var req = new Request({
				url:base_url,
				onSuccess: function(res) {
					
					$('wrapp_attsearch_results').unmask();
					
					att_search_result = res.trim();
					res =  res.trim();
					
					if ( res && res.test('^<li>','i')  ) {
						$("attsearch_results").set('html', "<li style='height:5px;border:0;background:transparent;border-bottom:1px solid #000;'></li>"+res.trim()+"<li style='height:25px;border:0;background:transparent;'></li>");
						$("att-shadow-top").setStyle('display','block');
						$("att-shadow-bottom").setStyle('display','block');
						
						
						coords= $('attsearch_results').getCoordinates();	
						par_coords= $('attsearch_results').getParent().getCoordinates();	
						
						if (  coords.height > par_coords.height )  {
							dragger.attach()
							$('drag_msg').setStyle('display','block');
						}
						else {
							$('drag_msg').setStyle('display','none');
						}
						
						
					}
					else {
						$("attsearch_results").empty();
						$("att-shadow-top").setStyle('display','none');
						$("att-shadow-bottom").setStyle('display','none');
						
						$('wrapp_attsearch_results').get('mask').element.empty().setStyle('background','').set('html','<span style="padding:10px;position:absolute;">No results.</span>');

						$('wrapp_attsearch_results').mask();
						$('drag_msg').setStyle('display','none');
						
						
						
						
					} 	
					
					
					if ( Browser.Platform.ipod ) {
						iscr.refresh();
					}
									
					
				}
		})
		
		if ( name.length < 3) {
			$("attsearch_results").empty();
			$('wrapp_attsearch_results').get('mask').element.empty().setStyle('background','');
			$('wrapp_attsearch_results').get('mask').element.set('html','<span style="padding:10px;position:absolute;">To search for an attorney, enter the first few letters of their first or last name. you can filter results by practice and title, or jump right to a practice by leaving the other fields blank.</span>');
			$('wrapp_attsearch_results').mask();
			$('drag_msg').setStyle('display','none');

			
		}
	
		else {
			if ( $('wrapp_attsearch_results').getElements('li').length )
				$('wrapp_attsearch_results').get('mask').element.empty().setStyles({'background':'url(88E17A/img/ajax-loader-5.gif) no-repeat center center #ddd','opacity':0.8});
			else
				$('wrapp_attsearch_results').get('mask').element.empty().setStyle('background','url(88E17A/img/ajax-loader-5.gif) no-repeat center center');

			$('wrapp_attsearch_results').mask();

			req.get({'t':'47','format':'xml','stylesheet':'widget_atty_results','directive':'0','Name':name,'rank':$('rank').value,'lpa':$('lpa').value })
		}
		
		
	
		
}




