/* Headliners Domready Event
------------------------------------------------------------------------------------*/
window.addEvent('domready', function(){
	// create play louder
	playLouder = new PlayLouder('play-louder', 'tab-content', {
		showFirst: true
	});
	
	// create gallery
	$$('.gallery').each(function(item){
		this.gallery = new Gallery(item);
	});
	
	// create scroll bar for artist list
	scrollBar = new ScrollBar('artist-list');
	var artistUpdater = new ArtistUpdater($('artist-list'), $$('.listing .filter form')[0]);
	artistUpdater.addEvent('onUpdate', function(){
		// update the scrollbar thumbs for new content length
		scrollBar.updateThumbs();
		
		// look for a selected item
		var selected = $$('#artist-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 - $('artist-list').getCoordinates().top;
		}
		// scroll to selected item;
		scrollBar.vSnapTo(top);
	});
	// fire update event to move to the selected item on page load
	artistUpdater.fireEvent('onUpdate');
	
	// show full artist bio on click
	if($chk($('artist-bio-read'))){
		$('artist-bio-read').addEvent('click', function(e){
			var event = new Event(e);
			event.preventDefault();
			// copy elements from headliner page
			var copiedElements=$('artist-bio').cloneNode(true).getChildren();
			
			// create containers to dump elements in to
			var container = new Element('div').setProperty('id', 'artist-bio-overlay');
			var scrollWrap = new Element('div').setProperty('id', 'bio-scroll').addClass('scroll-wrapper');
			// create and inject header
			var artistName = copiedElements[0].getElement('.sIFR-alternate').getText();
			new Element('h2').setText(artistName).injectInside(container);
			
			// inject image
			var image = new Element('span').addClass('profile-image');
			copiedElements[1].getChildren()[0].injectInside(image);
			image.injectInside(scrollWrap);
			
			// inject bio
			// if the bio does not contain p tags
			// if(copiedElements[3].getChildren().length == 0 || copiedElements[3].getChildren().getText().contains(""))
			/*if(copiedElements[3].getChildren().length == 0){
				var text = new Element('p');
				text.setText(copiedElements[3].getText());
				text.injectInside(scrollWrap);
			}*/
			// if the bio comes nicely split up in p tags
			//else{
				copiedElements[3].getChildren().injectInside(scrollWrap);
			//}
			
			// inject scroll bar in to container
			scrollWrap.injectInside(container);
			
			// now show in modal window
			modal.show(container);
			
			// replace header text with sifr but not on mac firefox
			if(navigator.appVersion.contains("Mac") && window.gecko){
				$$('#artist-bio-overlay h2')[0].setStyle('visibility', 'visible');
			}
			else{
				sIFR.replace(univers, {
					selector: '#artist-bio-overlay h2', 
					wmode: 'opaque',
					css: [
					  '.sIFR-root { font-size: 30px; background-color:#000000; color: #ffffff; font-weight: normal; }'
					],
					filters: {
						DropShadow: {
							knockout: false, distance: 2, color: '#000000', strength: 2
						}
					},
					preventWrap: false,
					fitExactly: true,
					tuneHeight: -12,
					offsetTop: -6
				});
			}
			
			// apply style scroll bar
			(function(){
				new ScrollBar('bio-scroll');
			}).delay(100);
		});
	}
});

var ArtistUpdater = new Class({
	options: {
		onUpdate: Class.empty
	},
	initialize: function(container, sortForm, options){
		this.container = $(container)
				
		// grab the sort form and store starting state
		this.sortForm = $(sortForm);
		this.select = this.sortForm.getElement('select');
		var value = this.select.getChildren()[this.select.selectedIndex].getProperty('value');
		if(value == 'genre')
			this.sortByGenre = true;
		else
			this.sortByGenre = false;
		
		// keep an ajax object for updating
		this.ajaxUpdate = new Ajax('/vegas/play/music/includes/artist-listing.jsp',{
			update: this.container,
			method: 'get'
		});
		
		this.attach();
	},
	attach: function(){
		// click form submit
		this.sortForm.addEvent('submit', function(e){
			new Event(e).preventDefault();
			var select = this.sortForm.getElement('select');
			var value = select.getChildren()[select.selectedIndex].getProperty('value');
			
			var oldValue = this.sortByGenre;
			if(value == 'genre')
				this.sortByGenre = true;
			else
				this.sortByGenre = false;
			
			// only update if there is a change
			if(oldValue != this.sortByGenre)
				this.update();
		}.bind(this));
		
		// fire event after successfull update
		this.ajaxUpdate.addEvent('onComplete', function(e){
			this.fireEvent('onUpdate');
		}.bind(this));
	},
	update: function(){		
		// select the correct filter item
		if(this.sortByGenre)
			this.select.selectedIndex = 1;
		else
			this.select.selectedIndex = 0;
		
		// build a data object for ajax to parse in to param string
		var paramId = getParam(window.location, 'id');
		var state = {};
		if(paramId != '')
			state.id = paramId
		if(this.sortByGenre)
			state.sortBy = 'genre';
		this.ajaxUpdate.options.data = state;
		
		// make ajax request
		this.ajaxUpdate.request();	
	}
});
ArtistUpdater.implement(new Options, new Events);
