window.addEvent('domready',function(){
	// remove scroll bars from the map
	$('map-wrapper').setStyle('overflow','hidden');

	// add scroll functions to map and pub list
	var scroll = new Fx.Scroll('map-wrapper', {
		wait: false,
		duration: 500,
		/*offset: {'x': -200, 'y': -100},*/
		transition: Fx.Transitions.Quad.easeInOut
	});
	var scroll2 = new Fx.Scroll('nav-wrapper', {
		wait: false,
		duration: $('nav-inner').getSize().y * 4.5,
		transition: Fx.Transitions.Quad.easeInOut
	});
	
	// set up dropdown fade in, out and remove functions
	var ddTimer = new Array();
	var ddfadein = function(){
		var id = this.id.split('-');
		id = id[1];
		var hd = $('hover-'+id);
		$clear(ddTimer[hd.id]);
		var styles = new Fx.Styles(hd, {
			wait: false,
			duration: 100
		});
		styles.start({
			opacity: '.85'
		});
		$('loc-'+id).addClass('hover');
		$('det-'+id).addClass('hover');
	}

	var ddfadeout = function(){
		var id = this.id.split('-');
		id = id[1];
		var hd = $('hover-'+id);
		var styles = new Fx.Styles(this, {
			wait: false,
			duration: 500
		});
		styles.start({
			opacity: '.25'
		});
		ddTimer[hd.id] = ddremove.delay(510,hd);
		$('loc-'+id).removeClass('hover');
		$('det-'+id).removeClass('hover');
	}

	var ddremove = function(){
		var id = this.id.split('-');
		id = id[1];
		var hd = $('hover-'+id);
		$clear(ddTimer[hd.id]);
		if (!$defined($('dropdown-'+id))) {
			var clone = hd.clone();
			clone.set({'id':'dropdown-'+id});
			clone.setStyles('');
			clone.removeClass('hovermap');
			clone.inject($('loc-'+id));
		}
		if ($defined(hd)) {	hd.remove();}
	}


	// get map container object
	var pubmap = $('pubmap');

	// move map to region 1 (north west)
	scroll.toElement('mapreg-1');

	// add hover events for each map location
	$$('#map-wrapper .maploc').each(function(item){
		var id = item.id.split('-');
		id = id[1];

		item.addEvents({
			'mouseenter': function(event) {
				event = new Event(event).stop();
				$$('div.hovermap').each(function(item){
					var rem = ddremove.bind(item);
					rem();
				});
				$$('div.maploc').each(function(item){
					var id = item.id.split('-');
					id = id[1];
					item.removeClass('hover');
					$('det-'+id).removeClass('hover');
				});
				if($defined($('link-'+id))) { 
					$('link-'+id).addClass('hover');
					scroll2.toElement('link-'+id);
				}
				$('det-'+id).addClass('hover');
				item.addClass('hover');
				if($defined($('dropdown-'+id))){
					var dd = $('dropdown-'+id);
					var clone = dd.clone();
					dd.remove();
					clone.set({
						'id':'hover-'+id
					});
					clone.setStyles({
						top: this.getTop([$('map-wrapper')]) - pubmap.getTop(),
						left: this.getLeft([$('map-wrapper')]) - pubmap.getLeft()+20,
						opacity: '.25'
					});
					clone.addClass('hovermap');
					clone.inject('pubmap');
					var fade = ddfadein.bind($('hover-'+id));
					fade();
				}
			},
			'mouseleave': function(event) {
				event = new Event(event).stop();
				if ($defined($('link-'+id))) { $('link-'+id).removeClass('hover'); }
				$('det-'+id).removeClass('hover');
				item.removeClass('hover');
				var injectID = this.id;
				if ($defined($('hover-'+id))) {
					var fade = ddfadeout.bind($('hover-'+id));
					fade();
				}
			}
		});
	});

	// add hover events for pub links list
	$$('.publink').each(function(item){
		var id = item.id.split('-');
		id = id[1];
		item.addEvents({
			'mouseenter': function(event) {
				event = new Event(event).stop();
				$('loc-'+id).addClass('hover');
				$('det-'+id).addClass('hover');
			},
			'mouseleave': function(event) {
				event = new Event(event).stop();
				$('loc-'+id).removeClass('hover');
				$('det-'+id).removeClass('hover');
			}
		});
	});

	// scroll map and list using buttons on map
	$$('.mapreg').each(function(item){
		var id = item.id.split('-');
		id = id[1];
		item.addEvent('mouseover',function(event){
			event = new Event(event).stop();
			scroll.toElement('mapreg-'+id);
			if ($defined($('reg-'+id))) { scroll2.toElement('reg-'+id); }
		});
	});

	// scroll map using sections of list
	$$('.pubregion .publink').each(function(item){
		var id = item.getParent().getParent().getParent().id.split('-');
		id = id[1];
		item.addEvent('mouseover',function(event){
			event = new Event(event).stop();
			scroll.toElement('mapreg-'+id);
		});
	});

	// scroll list up
	if ($defined($('navup'))) {
		$('navup').addEvents({
			'mouseover':function(event){
				event = new Event(event).stop();
				scroll2.toTop();
			},
			'mouseleave':function(event){
				event = new Event(event).stop();
				scroll2.cancel();
			}
		});
	}

	// scroll list down
	if ($defined($('navdown'))) {
		$('navdown').addEvents({
			'mouseover':function(event){
				event = new Event(event).stop();
				scroll2.toBottom();
			},
			'mouseleave':function(event){
				event = new Event(event).stop();
				scroll2.cancel();
			}
		});
	}

	var pubs = [];
	var pubLinks = [];
	$$('#publistscroller a.publink').each(function(a,b) {
		pubs.push( a.firstChild.nodeValue );
		pubLinks.push( a.getAttribute( 'href' ) );
	});
	new Autocompleter.Local('pub-search-query', pubs );
	var lytelink = document.createElement('a');
	lytelink.setAttribute( 'rel', 'lyteframe' );
	lytelink.setAttribute( 'rev', 'scrolling:no;height:500;width:600' );
	$$('#publistsearch form').addEvent( 'submit', function() {
		var value = $$('#pub-search-query')[0].value;
		for( var i = 0, l = pubs.length; i < l; ++i ) {
			if( pubs[i] == value ) {
				window.location = pubLinks[i];
				return false;
			}
		}
		lytelink.setAttribute( 'href', '/contentonly/pubs/?qs='+encodeURIComponent(value) );
		myLytebox.start(lytelink,false,true);
		return false;
	} );
	//new Autocompleter.Request.JSON('pub-search-query', [ 'First', 'Second', 'Third' ] );
});

function clear_textbox()
{
	if(document.text_form.qs.value == "Search postcode/address ")
	document.text_form.qs.value = "";
}