jQuery(function() {
	
		
	if (jQuery.browser.msie) {
		/* IE has problems with overflow:visible... */
		var a = jQuery('.lso-en a');
		var en = jQuery("<div></div>").attr("class","lso-buttons lso-buttons-en");
		en.append(jQuery('<div class="'+a.parent().attr('class')+'"><\/div>').append(a.css('color',a.css('color'))));
		jQuery("#navigation-container").append(en);
	}
		
		
	
				
	jQuery('.lso-event-filter').lsoEvents();
	
	jQuery('.lso-order-tickets').lsoTicketOrderForm();
	jQuery('.lso-order-shop').lsoShopOrderForm();
	jQuery('.lso-abo-order-buttons .lso-button').lsoAboOrderForm();
	
	jQuery('.lso-form').lsoForm();
	
	
	
	/**
	 * Footer positioning
	 */
	
	(function($) {
		var $window = $(window);
		var $document 	= $(document);
		var $html = $(document.documentElement);
		var _footer = function() {
			if ($window.height() < $document.height()) {
				$html.addClass('scroll-y');
			}
			else {
				$html.removeClass('scroll-y');
			}
		}
		
		var _footerTimer = 0;
		
		$(window)
			.scroll(function() {
				_footer();
			})
			.resize(function() {
				_footer();
			});
			
		setInterval(function() {
			_footer();
		},200)
			
		// setup
		_footer();
	})(jQuery);
	
	
	
	
	/**
	 * Main navigation decoration
	 */ 
		
	jQuery('#main-navigation, .sub-navigation').each(function() {
		
		var dotw = 10;
		var doth = 5;
		
		var $this 	= jQuery(this);
		var $ul		= $this.children();
		
		if ($ul.length==0) return;
		
		$ul = $ul.eq(0);
		                                     
		
		var w = $ul.width();
		var l = $ul.position().left;
		var max = parseInt(w/dotw);
		var mod	= parseInt((w%dotw)/2);
		
		var left = (l+5+mod)
						
		var $dottedTop = jQuery("<div/>")
			.addClass('lso-dotted-navigation')
			.css({
				left: left+'px',
				width: dotw*max+'px',
				top: '5px'
			});

		var $dottedBottom = jQuery("<div/>")
			.addClass('lso-dotted-navigation')
			.css({
				left: left+'px',
				width: dotw*max+'px',
				bottom: '5px'
			});
			
		$this.prepend($dottedTop,$dottedBottom);
		
		
	});



	/**
	 * Sub navigation decoration
	 */ 
		
	jQuery('#lso-calendar').each(function() {
		var $this 	= jQuery(this);
			
		$this.prepend(
			'<div class="lso-dotted-calendar" style="top:10px;"><\/div>'+
			'<div class="lso-dotted-calendar" style="bottom:10px;"><\/div>'+
			'<div class="lso-dotted-calendar-vertical"><\/div>'
		);
		
	});
	

	jQuery('.lso-event-item, .lso-standard-content').append('<div class="lso-dotted-top"><\/div><div class="lso-dotted-bottom"><\/div>');
	jQuery('.lso-event-item .lso-event-detail').append('<div class="lso-dotted-top"><\/div>');
	
	jQuery('.web-form-portlet .portlet-content-container').append('<div class="lso-dotted-top"><\/div><div class="lso-dotted-bottom"><\/div>');
	
	
	/**
	 * Mouseover thumbnails
	 */
	setTimeout(function() {

		
		jQuery('.lso-thumbnail-left, .col-1 .lso-thumbnail, .lso-person-display-2 .lso-thumbnail').each(function() {

			var $img 	= jQuery(this);
			var $a 		= $img.parents('.lso-thumbnail-container').eq(0);
						
			$a.css({
				cursor: 'default'
			});

			var $body	= jQuery(document.body);
		
			var $btn = $a.find('.lso-thumbnail-hover');
			if ($btn.length == 0) {
				$btn = $a;
			}

		
			$btn
				.hover(
					function() {
						var pos = $a.offset();
						$img.css({
							position: 'absolute',
							right: ($body.width() - pos.left + 25)+'px',
							top: pos.top+'px'
						}).appendTo($body).show();
					},
					function() {
						$img.hide();
					}
				)
				.click(function() {
					return false;
				});
		
		});
		
		jQuery('.col-2 .lso-thumbnail').each(function() {
			var $img 	= jQuery(this);
			var $a 		= $img.parents('.lso-thumbnail-container').eq(0);
						
			$a.css({
				cursor: 'default'
			});

			var $body	= jQuery(document.body);
		
			var $btn = $a.find('.lso-thumbnail-hover');
			if ($btn.length == 0) {
				$btn = $a;
			}
		
			$btn
				.hover(
					function() {
						var pos = $a.offset();
						$img.css({
							position: 'absolute',
							left: (pos.left + $a.outerWidth() + 26)+'px',
							top: pos.top+'px'
						}).appendTo($body).show();
					},
					function() {
						$img.hide();
					}
				)
				.click(function() {
					return false;
				});
				
		
		});
		
		
		
		jQuery('.lso-person-display-2 .row .overview').each(function() {
						
			var $a = jQuery(this);
			
			
			$a.find('a').click(function() {
				window.location.href = jQuery(this).attr('href');
				return false;
			});
			
			if ($a.parent().find('.detail').length==0) return;
			
			$a.css({
				cursor: 'pointer'
			});
			
			
			var collapsedHeight = 0;
			var expandedHeight = 0;
			var $container = null;
			var $details = null;
			var expanded = false;

			
			
			$a.click(function() {
				
				if (expandedHeight == 0) {
					$container 		= $a.parent();
					$details		= $container.find('.detail');

					$details.show();
					collapsedHeight	= $a.outerHeight();
					expandedHeight	= collapsedHeight + $details.outerHeight();
					
					$container.css({height: $container.height()+'px', overflow: 'hidden'});
					$details.hide().css({position: 'static',visibility:'visible'});
				}
				
				if (expanded) {
					$container.animate({height:collapsedHeight+'px'});
					$details.fadeOut();
				}
				else {
					$container.animate({height:expandedHeight+'px'});
					$details.fadeIn();
				}
				
				expanded = !expanded;

				return false;
				
			});
			
			
		});
		
		
		
	},300);
	
	
	jQuery('.lso-person-display-2 .row .overview').append('<div class="lso-dotted-m-top"><\/div><div class="lso-dotted-m-bottom"><\/div>');
	jQuery('.lso-person-display-3 h3').append('<div class="lso-dotted-m-bottom"><\/div>');
	jQuery('.lso-person-display .outro, .lso-shop-article').append('<div class="lso-dotted-m-top"><\/div>');
	
	jQuery('#lso-calendar .lso-calendar-event .buttons a').append('<div class="top-dots"><\/div><div class="bottom-dots"><\/div>');
	jQuery('#column-home .lso-banner-content').append('<div class="top-dots png"><\/div><div class="bottom-dots png"><\/div>');
	
	/**
	 * Saalplan
	 */
	setTimeout(function() {
		(function($) {
			
			$('.lso-saalplan-tab').livequery('click', function() {
				
				var $this = $(this);
				var idx = $this.parent().children('.lso-saalplan-tab').index($this);
				
				$this.parent().children().removeClass('selected');
				$this.addClass('selected');

				var $images = $this.parent().next('.lso-saalplan-content');
				$images.children('.lso-saalplan-content-item').hide().eq(idx).show();
				
				return false;
			});
			
			$('.sinfonieorchester-saalplan .lso-buttons a').livequery('click',function() {
				
				var $this 		= $(this);
				var $portlet	= $this.parents('.sinfonieorchester-saalplan');
				var $wrapper 	= $(this).parent().next();
				
				var expanded = !!$(this).data('lso-expanded');
				$(this).data('lso-expanded', !expanded);
								
				if (expanded) {
					$wrapper.animate({height:0,opacity:0},function() {
						if (!$this.data('expanded')) {
							$portlet.css({height:0});
						}
					});
				}
				else {
					$wrapper.css({opacity:0});
					$portlet.css({height:'auto'});
					var h = $wrapper.children().eq(0).outerHeight();
					$wrapper.animate({height: h+'px',opacity:1});
				}
				
			});
					
			

		})(jQuery);
	},400);
	
	
	/**
	 * Order Tickets from Calendar or Panel
	 */
	(function($) {
		
		var _load_order = function(t) {
			$('.lso-event-filter li.order a').attr('href','#'+t).click();
			var $item = $('.lso-'+t);
			if ($item.length == 0) {
				window.location.href = '/veranstaltungskalender#'+t;
			}
			
			var $a = $item.find('.lso-event-item-overview>a');
			if (!$a.data('expanded')) {
				$a.click();
			}
			$item.find('.lso-order-tickets').click();
			
		}
		
		var _load_ref = function(t) {
			//alert(t);
		}
		
		var h = window.location.hash.split('#')[1];
		if (h && h.substring(0,5) == 'order') {
			setTimeout(function() {
				_load_order(h.replace('#',''));
			},300);
		}
		else if (h && h.substring(0,3) == 'ref') {
			setTimeout(function() {
				_load_ref(h.replace('#',''));
			});
		}
		
		$('.preview .lso-order-tickets, .lso-panel .lso-order-tickets').livequery('click',function() {
			$(this).parents('.lso-panel-container').find('.lso-panel-close').click();
			var t = $(this).attr('name');
			_load_order(t);
			return false;
		});
		
		
		// Saalplan panel
		$('.lso-panel-saalplan').livequery('click',function() {
			var $this = $(this);
			$.get('/repository/proxy/content/service/sites/sinfonieorchester/saalplan?site=sinfonieorchester&locale=de_CH&panel=true',function(html) {
				$.fn.lsoPanel.open($this, html);				
			})
		});
		
	})(jQuery);
	
	
	
	/**
	 * Search
	 */
	
	(function($) {
		
		var $panel = null;
		var $search = null;
		var $results = null;
		
		$('#lso-search .search-nav').click(function() {
			
			var $container = $('#lso-search');
						
			
			/**
			 * Panel: input field and result list.
			 */
			
			if (!$panel) {
				
				$search = $("<input type=\"text\"/>").attr('name','q');				
				
				$results = $("<div/>").attr('id','lso-search-results').hide();
				
				$panel = $("<div/>").addClass('search-panel').append($search,$results);
				$panel.appendTo($container);
				
				$panel.hover(
					function() {
						$(document).unbind('click.lso-search');
					},
					function() {
						$(document).bind('click.lso-search', function() {
							$panel.fadeOut(200);
							$(document).unbind('click.lso-search');
						});
					}
				);
				
				// Livesearch
				$search
					.oiLivesearch('/repository/proxy/content/service/oi/content/search/sinfonieorchester/de_CH/text',{
						container: $results,
						animate: false,
						willblur: function() {
							return false;
						},
						didshow: function() {
							$('#navigation-container').css('z-index','999')
						},
						didhide: function() {
							$('#navigation-container').css('z-index','0')
						}
					});
				
			}
			
			$panel.show();
			$search.focus();

			
		});
		
	})(jQuery);
	
});



