/*
#####################################################
# 
# Hevy Music Festival 2009
# JavaScript setup routines
#
# Hand-crafted by Phenotype (phenotype.net)
#
#####################################################
*/

	////////////////////////////////////////////////////////////////////
	//	Initialise setup routines
	////////////////////////////////////////////////////////////////////
	
	// Called when DOM is ready
	$(document).ready(domSetup);
	
	// Called when entire page is loaded
	window.onload = pageSetup;
	
	////////////////////////////////////////////////////////////////////
	//	Define setup routines
	////////////////////////////////////////////////////////////////////
	
	/* 
	domSetup()
	
	All JavaScripts requiring initialisation on DOM LOAD should be called
	from this routine
	-----------------------------------------------------------------------
	*/
	
	function domSetup() {

		// Setup dropdown menus
		$('#navigation ul').superfish({
			dropShadows: false,
			autoArrows: false,
			animation : { height: 'show'},
			speed : 'normal'
		});
		
		////////////////////////////////////////////////////////////////////
		//	Overlays
		////////////////////////////////////////////////////////////////////
		
		// Inject overlay HTML
			$('body').append(
				'<div class=\"overlay\" id="overlay"><div class=\"overlay-title\"><a class=\"close\">Close window<span></span></a></div><div class=\"overlay-content\"></div></div>'
			);
		
		// Setup artist information overlays
		if($('.lineup a')[0]){

			// Not IE
			if(!$.browser.msie) {
				// Add rel="#overlay" attributes
				$('.lineup a').attr({'rel' : '#overlay'});
			} else {
				// Not IE6
				if($.browser.version != '6.0'){
					// Add rel="#overlay" attributes
					$('.lineup a').attr({'rel' : '#overlay'});
				// Is IE6, degrade gracefully
				} else {
					// Add rel="external" attributes
					$('.lineup a').attr({'rel' : 'external'});
				}
			}
			
		}
		
		if($('.viewer.artist a')[0]){

			// Not IE
			if(!$.browser.msie) {
				// Add rel="#overlay" attributes
				$('.viewer.artist a').attr({'rel' : '#overlay'});
			} else {
				// Not IE6
				if($.browser.version != '6.0'){
					// Add rel="#overlay" attributes
					$('.viewer.artist a').attr({'rel' : '#overlay'});
				// Is IE6, degrade gracefully
				} else {
					// Add rel="external" attributes
					$('.viewer.artist a').attr({'rel' : 'external'});
				}
			}
			
		}
			
		// Bind overlays
		$("a[rel='#overlay']").overlay({ 

			// Bind close button
			close: 'a.close',
			
			// Before Load event
			onBeforeLoad: function() { 
				
				// Show expose effect
				this.getBackgroundImage().expose({color: '#111'}); 
				
				// Grab overlay content container
				var container = this.getContent().find("div.overlay-content"); 
				
				// Load in overlay content
				container.load(this.getTrigger().attr("href"), function() {
					
					$('.overlay-content link').remove();
					$('.overlay-content meta').remove();
					$('.overlay-content title').remove();
					$('.overlay-content base').remove();
					
					// Fix IE ClearType
					if($.browser.msie){
						$('#overlay').get(0).style.removeAttribute('filter');
					}
					
					// Rebind external links
					externalLinks();
				});
			},  
			 
			// Close event
			onClose: function() { 
				// Grab overlay content container
				var container = this.getContent().find("div.overlay-content"); 
				
				// Close expose
				$.expose.close(); 
				
				// Unload content
				container.empty();
			}
		});

		// Setup image viewer lightboxes
		if($('.viewer.image a')[0]){

			// Add rel="lightbox" attributes
			$('.viewer.image a').attr({'rel' : 'lightbox'});
			
		}
		
		// Bind lightboxes
		$("a[rel='lightbox']").lightBox({
			overlayBgColor	: '#111',
			imageLoading	: 'assets/templates/hevy/images/template/screen/lightbox-loading.gif',
			imageBtnClose	: 'assets/templates/hevy/images/template/screen/lightbox-closelabel.gif',
			imageBtnPrev	: 'assets/templates/hevy/images/template/screen/lightbox-prev.gif',
			imageBtnNext	: 'assets/templates/hevy/images/template/screen/lightbox-next.gif',
			imageBlank		: 'assets/templates/hevy/images/template/screen/blank.gif',
			containerBorderSize: 12
		});
		
		////////////////////////////////////////////////////////////////////
		//	End overlays
		////////////////////////////////////////////////////////////////////
		
		// Contact form enhancements
		if($('#contact')[0]) {
		
			// Replace list of links with select field
			$('#enquiry-selector ul').replaceWith(
				'<ol><li><label for=\"enquiry-type\">Enquiry type:</label><select id=\"enquiry-type\"><option value=\"general-question\">General question</option><option value=\"press-enquiry\">Press enquiry</option></select></li></ol>'
			);
			
			// Set selected list item
			var enquiry = $.query.get('enquiry');
			if(enquiry != 'null'){
				
				// Select enquiry from list
				$('option[value='+enquiry+']').attr({selected : 'selected'});
				
			} else {
			
				// Select default enquiry
			}
			
			// Bind form loader to change event
			$('#enquiry-selector select').bind('change', function(){
				
				// Get enquiry type
				var enquiry = $('#enquiry-selector select option:selected').attr('value')
				
				// Load new form
				window.location = window.location.protocol + "//" + window.location.host + window.location.pathname + '?enquiry=' + enquiry;
				
			});
			
			// Form expose effect
			$('#contact-form-container').bind('click', function() {
				if(!$.browser.msie){
					
					// Not IE, expose away!
					$(this).expose({
						color: '#111'
					});	
					
				} else {
				
					// Is IE 8, which doesn't have z-indexing issues, so expose will work
					if($.browser.version == '8.0'){
						$(this).expose({
							color: '#111'
						});	
					}
				}
			});
		}
		
		// Setup external links
		externalLinks();
		
	} // End domSetup()
	
	/* 
	pageSetup()
	
	All JavaScripts requiring initialisation on PAGE LOAD should be called
	from this routine (all images and elements should be loaded and ready to
	manipulate by this point)
	-----------------------------------------------------------------------
	*/
	
	function pageSetup() {
	
		// Resize page background
		$('#background img').fullbleed({
			minSize: .5
		});
	
	} // End pageSetup()