/* Venues Domready Event
------------------------------------------------------------------------------------*/
window.addEvent('domready', function(){	
	// create play louder
	playLouder = new PlayLouder('play-louder', 'tab-content', {
		showFirst: false
	});
	
	//create event group
	if($chk($('event-group'))){
		eventGroup = new EventGroup('event-group', 'group-previous', 'group-next', {
			onUpdateComplete: function(){				
				// replace the headings on the events using sifr
				sIFR.replace(univers2, {
					selector: '.event-date .header h4', 
					wmode: 'transparent',
					css: [
					  '.sIFR-root { font-size: 25px; color: #ffffff; font-weight: normal; }'
					],
					filters: {
						DropShadow: {
							knockout: false, distance: 1, color: '#000000', strength: 1
						}
				    },
					preventWrap: false,
					fitExactly: true,
					tuneHeight: -10,
					offsetTop: -5
				});
			}
		});
	}
				
	// create venue list
	scrollBar = new ScrollBar($('venue-list'));
	newList();
	
	// ajax updating of venue list
	var typeFilters = $$('.listing .header a');
	var venueUpdater = new VenueUpdater($('venue-list'), typeFilters[0], typeFilters[1]);
	venueUpdater.addEvent('onUpdate', newList);
	
	// function to use when we pull in a new list of venues
	function newList(){
		/*new VenueMenu('venue-list',{
			onToggle: function(){
				scrollBar.updateThumbs();
			}
		});*/
		// update the scroll thumbs because we changed length of content with venue menue
		scrollBar.updateThumbs();
		
		// look for a selected item
		var selected = $$('#venue-list .selected')[0];
		var top = 0;
		if(selected != null){
			// find how far we need to scroll to show the selected item in view
			top = selected.getCoordinates().top - $('venue-list').getCoordinates().top;
		}
		// scroll to selected item;
		scrollBar.vSnapTo(top);
	}
});

var VenueMenu = new Class({
	options: {
		onToggle: Class.empty
	},
	initialize: function(element, options){
		this.setOptions(options);
		
		this.container = $(element);
		
		this.parentElements = new Array();
		this.container.getElements('ul ul').each(function(item, index){
			var temp = item.getParent();
			
			temp.triggerElement = temp.getElement('a');
			temp.stateElement = temp.getElement('span');
			temp.subElement = item;
			
			if(temp.stateElement.hasClass('more')){
				temp.toggleState = false;
				temp.subElement.setStyle('display', 'none');
			}
			else{
				temp.toggleState = true;
				temp.subElement.setStyle('display', 'block');
			}
			this.parentElements[index] = temp;
		}.bind(this));
		
		this.attach();
	},
	attach: function(){
		var self = this;
		this.parentElements.each(function(item, index){
			item.triggerElement.addEvent('click', function(e){
				new Event(e).preventDefault();
				if(item.toggleState){
					item.subElement.setStyle('display', 'none');
					item.stateElement.removeClass('less').addClass('more');
					item.toggleState = false;
				}
				else{
					item.subElement.setStyle('display', 'block');
					item.stateElement.removeClass('more').addClass('less');
					item.toggleState = true;
				}
				self.fireEvent('onToggle');
			});
		});
	}
});
VenueMenu.implement(new Options, new Events);

var VenueUpdater = new Class({
	options: {
		onUpdate: Class.empty
	},
	initialize: function(container, clubsElement, hotelsElement, options){
		this.setOptions(options);
		
		this.container = $(container);
		
		// test for the current selected venue
		this.selectedId = getParam(window.location, 'venueId');
		if(this.selectedId == ''){
			this.selectedId = getParam(window.location, 'attractionId')
			if(this.selectedId == ''){
				this.selectedId = getParam(window.location, 'hotelId');
			}
		}
		
		// grab filters and store starting state
		this.clubsElement = $(clubsElement);
		this.hotelsElement = $(hotelsElement);
		if(this.clubsElement.hasClass('selected'))
			this.isClubs = true;
		else
			this.isClubs = false;
			
		// keep an ajax object for updating
		this.ajaxUpdate = new Ajax('/vegas/play/music/includes/venue-listing.jsp',{
			update: this.container,
			method: 'get'
		});
		
		// history manager
		this.historyKey = 'venuesList';
		this.history = HistoryManager.register(
			this.historyKey,
			[this.isClubs.toString()],
			function(values) {
				// clubs or hotels
				if(values[0] == 'true')
					this.isClubs = true;
				else
					this.isClubs = false;
				// update
				this.update();
			}.bind(this),
			function(values) {
				return this.historyKey + '(' + values[0] + ')';
			}.bind(this),
			this.historyKey + '\\((true|false)\\)'
		);
		HistoryManager.start();
		
		this.attach();
		this.update();
	},
	attach: function(){
		// click clubs link
		this.clubsElement.addEvent('click', function(e){
			new Event(e).preventDefault();
			// only update if there is a change
			if(!this.isClubs){
				this.isClubs = true;
				this.history.setValues([this.isClubs.toString()]);
				this.update();
			}
		}.bind(this));
		
		// click hotels link
		this.hotelsElement.addEvent('click', function(e){
			new Event(e).preventDefault();
			// only update if there is a change
			if(this.isClubs){
				this.isClubs = false;
				this.history.setValues([this.isClubs.toString()]);
				this.update();
			}
		}.bind(this));
		
		// fire event after successfull update
		this.ajaxUpdate.addEvent('onComplete', function(e){
			this.fireEvent('onUpdate');
		}.bind(this));
	},
	update: function(){
		var filterParent = this.clubsElement.getParent();
		// set appearance state of filters and swap position
		// selected element first on the left
		if(this.isClubs){
			this.clubsElement.addClass('selected').injectTop(filterParent);;
			this.hotelsElement.removeClass('selected').injectInside(filterParent);;
		}
		else{
			this.clubsElement.removeClass('selected').injectInside(filterParent);;
			this.hotelsElement.addClass('selected').injectTop(filterParent);;
		}
		
		// build a data object for ajax to parse in to param string
		var state = {};
		if(this.selectedId != '')
			state.id = this.selectedId
			
		if(this.isClubs)
			state.type = 'venues';
		else
			state.type = 'hotels';
		this.ajaxUpdate.options.data = state;
		
		// make ajax request
		this.ajaxUpdate.request();
	}
});
VenueUpdater.implement(new Options, new Events);