// Observe hash changes and load changes via ajax

var url = '';

function dp_check_hash() {
	
	if( url !== location.hash ) {
		url = location.hash;
		dp_loadContent( url.substring(1) );
    }
}

setInterval( dp_check_hash, 100 );

function dp_loadContent( url ) {
	
	if( url.indexOf(':') != -1 ) {
			
		var target_view = url.split(':')[0];
		var target_url = url.split(':')[1];
		
		// Fetch actions passed by hash
		
		if( target_url == 'close' ) {
			
			if( target_view == 'event_extended' )
				dp_toggle_extended_info( $('event_extended'), true );
			else if( target_view == 'fullscreen' )
				dp_toggle_fullscreen( true )
			else
				$(target_view).hide();
				
			return false;
				
		}
		else {
			
			new Ajax.Updater( target_view, target_url, {
			
				evalScripts: true,
				onSuccess: function() {
					
					if( target_view == 'event_extended' )
						dp_toggle_extended_info( target_view );
					else if( target_view == 'fullscreen' )
						dp_toggle_fullscreen();
					else {
						$(target_view).show();
						return false;
					}
					
				},
				onComplete: dp_set_event_listners
				
			});	
			
		}
		
	}
	
}

function dp_toggle_extended_info( target_view, hide ) {
	
	if( hide )
		Effect.BlindUp( target_view, { duration: .3 } );
	else {
		if( $(target_view).getStyle('display') != 'block' )
			Effect.BlindDown( target_view, { duration: .3 } );
			
		$(document.body).scrollTo();
		
	}
}

function dp_toggle_fullscreen( hide ) {
	
	if( !hide ) {
		$('fullscreen').show();
		$('fullscreen_blind').show();
		$(document.body).scrollTo();
		clearInterval( t );
	} else {
		$('fullscreen').hide();
		$('fullscreen_blind').hide();
		dp_next_slide();
	}
	
}

// Slideshow

var slide_dimentions;
var slide_end = 0;
var slide_count = 0;
var slide_element;
var t; // Interval timer

document.observe( 'dom:loaded', function() {
	
	var slide_container = $('event_slides');
	
	if( slide_container ) {
		
		var slide_objects = slide_container.select('.slide');
		
		if( slide_objects.size() > 1 ) {
					
			slide_dimentions = slide_container.getDimensions();
			slide_count = slide_objects.size();
			
			// Create slide div
			
			slide_element = new Element('div');
			slide_element.writeAttribute('id', 'slide_element' );
			slide_element.setStyle({ 
				position: 'absolute',
				zIndex: 0,
				top: '0px',
				left: '0px'
			 });
				 
			slide_container.insert( slide_element );
			
			// Set upp action indicator 
			
			var slide_action_indicator = $('event_slide_action_indicator');
			
			if( slide_element && slide_action_indicator && slide_objects.size() > 1 ) {
				slide_element.observe( 'click', function(){					
					if( t ) {
						clearInterval( t );
						t = null;
						slide_action_indicator.src = '/static/site_mainGraphics/event_banners/top_banner/play.png';
					}
					else {
						dp_next_slide();
						slide_action_indicator.src = '/static/site_mainGraphics/event_banners/top_banner/pause.png';
					}
				});
			}
			
			// Append slide content
			
			slide_objects.each( function( element, index ) {
				element.setStyle( {
					position: 'absolute',
					zIndex: index,
					top: '0px',
					left: (slide_dimentions.width * index) + 'px',
					width: slide_dimentions.width + 'px',
					height: slide_dimentions.height + 'px'
				});
				slide_element.insert( { bottom: element } );
			});
			
			// Set up mouse track indicator
			
			if( slide_action_indicator && slide_objects.size() > 1 ) {
				
				var head_banner = $('head_banner');
				var head_banner_offset = head_banner.viewportOffset();
				
				head_banner.observe( 'mouseover', function() {
					slide_action_indicator.show();
				});
				
				head_banner.observe( 'mousemove', function( event ) {
					slide_action_indicator.setStyle({
				     	left: ( Event.pointerX(event) - ( head_banner_offset.left - 10 ) ) +'px',
				     	top: ( Event.pointerY(event) - ( head_banner_offset.top - 10 ) ) + 'px'
				     });					
				});
				
				head_banner.observe( 'mouseout', function() {
					slide_action_indicator.hide();
				});
				
				// Set up event_info so that the action indicator does not show when mouse is on.
				
				head_banner.select('.event_info').each( function( event_info ) {
					
					event_info.observe( 'mouseover', function() {
						Event.stopObserving( head_banner, 'mouseover' );
						slide_action_indicator.hide();
					});
					
					event_info.observe( 'mouseout', function() {
						head_banner.observe( 'mouseover', function() {
							slide_action_indicator.show();
						});
					});
					
				});
			}
		
			dp_next_slide();
			
		}
	}
	
});

var slide_index = 1;

function dp_next_slide() {
	
	t = window.setInterval( function() {
				
		var next_position = slide_dimentions.width;
		if( slide_index == slide_count ) {
			next_position = ( next_position * ( slide_count-1 ) );
			slide_index = 0;
		}
		else
			next_position = -next_position;
		
		new Effect.Move( slide_element, { x: next_position, y: 0, mode: 'relative', duration: .5 } )

		slide_index++;

	},6000 );	
}


// Set up search criteria field

var search_crit = $('search');

document.observe( 'dom:loaded', function() {
	
	search_crit.observe( 'focus', function() {
		search_crit.addClassName('active');
		if( search_crit.value == search_crit.defaultValue )
			search_crit.value = '';
	});
	
	var empty_button = null;
	
	search_crit.observe( 'keyup', function() {		
		
		if(! empty_button ) {
	
			empty_button = new Element('img');
			empty_button.writeAttribute( 'src', 'static/site_mainGraphics/global/buttons/close_ccc.png' );
			empty_button.setStyle({
				position: 'absolute',
				top: '18px',
				left: '280px',
				width: '14px',
				height: '14px',
				cursor: 'pointer'
			});
			
			empty_button.observe( 'click', function() {
				search_crit.value = search_crit.defaultValue;
				search_crit.removeClassName('active');
				this.hide();
			});
		}		
		else
			empty_button.show();
	
		$('search_form').insert( { top: empty_button } );
	
	});
	
	search_crit.observe( 'blur', function() {
		if( search_crit.value == '' ) {
			search_crit.removeClassName('active');
			search_crit.value = search_crit.defaultValue;
			if( empty_button ) empty_button.hide();
		}
	});
	
});

// Set up month filter

if( $('event_list_control_bar') ) {

	var dp_event_date_span_status = $('event_list_control_handle_status');
	
	var dp_event_date_span = new Control.Slider('event_list_control_handle', 'event_list_control_bar', {
		range: $R(1, 12),
		sliderValue: 12,
		values: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
	});
	
	dp_event_date_span.options.onSlide = function( value ) {
		dp_event_date_span_status.update( value );
	}
	
	dp_event_date_span.options.onChange = function( value ) {
		
		var date_now = new Date();
			month_now = date_now.getMonth();
			month_new = month_now + value;
			date_now.setMonth( month_new );
			with( date_now ) {
				adjusted_date = new Date( getFullYear(), getMonth(), getDay() );
			}
		
		$$('#event_banners h3[class]').each( function( element ) {
		
			var event_date = new Date( element.readAttribute('class').replace('date_','') );
			
			if( event_date < adjusted_date ) {
				element.nextSiblings().invoke('show');
				element.show();
			}
			else {
				element.nextSiblings().invoke('hide');
				element.hide();
			}
		});
		
	};
}


// Set up event filters and listners

document.observe( 'dom:loaded', function() {
	
	var content_filters = $$('a.filter');
	
	if( content_filters && content_filters.size() > 0 ) {
	
		$$('a.filter').each( function( element ) {
			element.observe( 'click', function( event ) {
				
				event.stop();
				
				var sibling_is_selected = false;
				element.siblings().each( function( filter ) {
					sibling_is_selected = ( filter.hasClassName('selected') || sibling_is_selected ) ? true : false;
				});
				
				if( sibling_is_selected ) {
					this.toggleClassName('selected');
					
					$$('#event_banners .event_container').each( function( event_container ) {
						if( event_container.hasClassName( element.readAttribute('rel') ) )
							event_container.toggle();
					});
					
					// If a month end up empty after filtering, hide the heading
					
					$$('#event_banners h3').each( function( h3 ) {
						
						var active_siblings = false;
						var heading_date = h3.readAttribute('class').substring(5,12);
						var event_list = $$('#event_banners .event_container a[rel^=' + heading_date + ']')
						
						event_list.each( function( event_object ) {
							active_siblings = ( event_object.up().getStyle( 'display' ) == 'block' || active_siblings ) ? true : false;
						});
						
						if(! active_siblings )
							h3.hide();
						else
							h3.show();
					
					});
				}
				else
					Effect.Pulsate( element, { pulses: 2, duration: .5 }); //alert('Du måste ha minst ett område valt!')
				
			});
		});
	}
	
});

// Ticket dialog

function dp_change_retailer() {

	var trigger = $('ticket_retailer_trigger');
	var trigger_name = $('ticket_retailer_name');

	$('ticket_retailers').observe( 'change', function() {
		if( this.value != '' ) {
			trigger.writeAttribute('href', this.value );
			trigger.writeAttribute('target', '_blank' );
			trigger_name.update( this.options[this.selectedIndex].text );
			trigger.show();
		}
		else
			trigger.hide();
	});

}

// Set class="external" to target="_blank"

document.observe( 'dom:loaded', dp_set_event_listners );

function dp_set_event_listners() {

	$(document.body).select('a[class^=external]').each( function(a) { 
		a.target = '_blank';
	});
	
	$$('a.inactive').each( function( element ) {
		element.observe( 'click', function( event ) {
			event.stop();
		});
		element.setOpacity('.4');			
	});

}

//	Set up drop menus

document.observe( 'dom:loaded', function() {
	
	var top_navigation = $('navigation_wrapper');
	var new_zindex = 1000;
	
	if( top_navigation ) {
		
		top_navigation.select('li').each( function( li, index ) {;
		
			var ul = li.select('ul');
			var top_pos = 0;
			var left_pos = 0;
			
			if( ul.size() > 0 ) {
			
				var first_node = ul[0];
				var node_timer;
			
				first_node.setStyle({
					zIndex: new_zindex++
				})
			
				top_pos = ( li.up().id == 'navigation_wrapper' ) ? 49 : 0;
				left_pos = ( li.up().id == 'navigation_wrapper' ) ? 0 : 200;
				
				li.addClassName('has_children');				
				li.observe( 'mouseover', function() {
					first_node.setStyle( { top: top_pos + 'px', left: left_pos + 'px' } );
					first_node.show();
					clearTimeout( node_timer );
				});

				li.observe( 'mouseout', function() {
					node_timer = setTimeout( function(){ dp_close_menu_node( first_node ) }, 200 ) ;
				});
				
				return true;
	  	    }
	  	});
	  	
		return true;
	  	
	}
	else
		return false;
	  	

});

function dp_close_menu_node( node ) {
	node.hide();
}

// Search support

function dp_load_search_category( category ) {
		
	var search_form = $('search_form');
	var search_category = $('search_category');
	var search_criteria = $('search');
	
	if( category == 'all' )
		search_category.value = $('search_category').defaultValue;
	else
		search_category.value = category;
	
	if( search_criteria.value == $('search').defaultValue )
		search_criteria.value = '';
	
	search_form.submit();
	
	return false;
	
}
