/**
 * Calendar
 */

(function($) {
	
	$.fn.lsoCalendar = function(options) {


		options = $.extend({url: '/repository/proxy/content/service/sites/sinfonieorchester/calendar'}, options);
	
		return this.each(function() {
			
			var $obj	= $(this);
			var $header	= $obj.find('.lso-calendar-header');
			var $title	= $header.find('span');
			var $table	= $obj.find('.lso-calendar-days');
						
			var year	= options.year;
			var month	= options.month;
						
			var update = function(url) {
				$.get(url,function(html) {
					var $html = $(html);
									
					var table = $html.find('.lso-calendar-days').html();

					$table.html(table);
					$title.html($html.find('.lso-calendar-header span').html());
					
					roundCorners();					
										
				});
			};
			
			
			
			var previewChangeTimerID = 0;
			var setupPreviewSlideshow = function() {
								
				var $eve = $obj.next('.lso-calendar-event');
				$eve.find('.lso-panel-opener').lsoPanel();
				var $previews = $eve.children('.preview');
								
				if ($previews.length > 1) {
					var $current = $previews.eq(0);
					previewChangeTimerID = setInterval(function() {
						
						var $old = $current;
						$current = $old.next('.preview');
						if ($current.length == 0) {
							$current = $previews.eq(0);
						}
						
						$current.css({top:'-200px'}).show();
						
						$current.animate({top:0});
						$old.animate({top:'200px'},function() {
							$(this).hide();
						});
						
					},5000);
				}
			}
			
			var stopPreviewSlideShow = function() {
				clearInterval(previewChangeTimerID);
			}
			
						
			var roundCorners = function() {
				var before	= '<div class="lso-calendar-round-before" style="background:red; position: absolute; left:-1px; top: 4px; width:1px; height: 11px;"><\/div>';
				var after	= '<div class="lso-calendar-round-after" style="background:red; position: absolute; right:-1px; top: 4px; width:1px; height: 11px;"><\/div>';
				$('.lso-calendar-days a.cell').each(function() {
					
					var $first = $(this).children('.event-indicator').eq(0);
					var sb = "position: absolute; width: 1px; background-color:"+$first.css('background-color')+";";
					var before = '<div style="'+sb+' top:5px; left: -1px; height: 11px;"><\/div>';//'<div style="'+sb+' top: 6px; left:-2px; height: 7px;"><\/div>';

					var $last = $(this).children('.event-indicator:last');
					var sa = "position: absolute; width: 1px; background-color:"+$last.css('background-color')+";";
					var left = $last.position().left+$last.outerWidth();
					var after = '<div style="'+sa+' top:5px; left: '+left+'px; height: 11px;"><\/div>';//'<div style="'+sa+' top: 6px; right:-2px; height: 7px;"><\/div>';
					
					var html = before + after;
					
					$(this).append(html);

				});
			}
			
			roundCorners();
						
			var $prev = $("<a/>")
				.attr("href","#prev")
				.addClass("prev")
				.click(function() {
					month--;
					var url = options.url+'/'+year+'/'+month+'?'+options.args;
					update(url);
					return false;
				})
				.css({
					left: 0
				})
				.html("&lt;")
				.prependTo($header);
				
			var $next = $("<a/>")
				.attr("href","#next")
				.addClass("next")
				.click(function() {
					month++;
					var url = options.url+'/'+year+'/'+month+'?'+options.args;
					update(url);
					return false;
				})
				.css({
					right: 0
				})
				.html("&gt;")
				.appendTo($header);
				
				
			$('.lso-calendar-days a.cell').livequery('click',function(e) {

				var $eve = $obj.next('.lso-calendar-event');
				
				$eve.children('.preview').fadeOut();
				
				stopPreviewSlideShow();
				
				var $d = $(this);
				var d = $d.find('.day').text();
				
				var url = options.url+'/'+year+'/'+month+'/'+d+'?'+options.args;

				$.get(url, function(html) {
					var $html = $(html);
					$eve.html($html.html());
					
					setupPreviewSlideshow();
					jQuery('#lso-calendar .lso-calendar-event .buttons a').append('<div class="top-dots"><\/div><div class="bottom-dots"><\/div>');
				});
				
				return false;
				
			});
				
			
			
			
		});
		
	};
		
})(jQuery);



/**
 * Event list
 */

(function($) {
	
	$.fn.lsoEvents = function() {
		
		var animate = false;

		
		return this.each(function() {
			
			var $this 	= $(this);
			var $list	= $('.lso-event-list');
			
			var listClass = $list.attr('class');
			

			$list.children().each(function() {
				var h = $(this).height()
				$(this).data('lso-height',h).css({height:h+'px'});
				
			});
			
			
			var $last = $list.find('.lso-event-item:last');
			$last.css({marginBottom:0}).parent().css({height:($last.parent().data('lso-height')+8)+'px'});
			
			
			
			var _lso_filter = function(id) {
				//var id 	= $a.attr('href').split('#')[1];
				var ref = '';
				if (id=='all') {
					ref = '*:not(.lso-event-category-description)'
				}
				else {
					ref	= '.lso-'+id;
				}
				
				$list.attr('class', listClass+' lso-filter-'+id);

				$list.children('.lso-event-item-wrapper').removeClass('visible').addClass('hidden');
				$list.children(ref).removeClass('hidden').addClass('visible');


				$list.find('.lso-event-item-wrapper').removeClass('last');
				$list.find('.visible.lso-event-item-wrapper:last').addClass('last');
									
				animate = false;
				if (animate) {
					var dur = 200;
					$list.children('.hidden').animate({height:0},dur);
					$list.children('.visible').each(function() {
						var diff = 0;
						if ($(this).hasClass('last')) diff += 8;
						$(this).animate({height:($(this).data('lso-height')+diff)+'px'},dur);
					});
				}
				else {
					$list.children('.hidden').css({height:0});
					$list.children('.visible').each(function() {
						var diff = 0;
						if ($(this).hasClass('last')) diff += 8;
						$(this).css({height:($(this).data('lso-height')+diff)+'px'});
					});
				}
			}
			
			
			if ($list.hasClass('initial-display-true')) {
				_lso_filter('all');
			}
			else {
				_lso_filter('empty');
			}
			
			$list.css({visibility:'visible'});//.hide().fadeIn();
			
			
			$this.find('a').each(function() {
								
				var $a = $(this);
				

				$a.click(function() {
					var id 	= $a.attr('href').split('#')[1];				
					_lso_filter(id);
					
				});
				
				
								
			});
			
			if (window.location.hash) {
				
				var h = window.location.hash.split('#')[1];
				if (h.substring(0,5)=='order') {

				}
				var ref = '.ref-'+ h;
				$('#sub-navigation '+ref).click();
			}
			animate = true;
			
			
			setTimeout(function() {
				
				$list.find('.lso-event-detail').each(function() {
					var $item = $(this);

					var $btn = $item.parent();//('.lso-event-item');
					
					
					var height = 0;
					var fullheight = 0;
					var expanded = false;
					
					$btn.find('.lso-event-item-overview')
						.append('<a href="#" style="position:absolute;top:0;left:0;width:100%;height:100%; background: #fff;"><\/a>')
						.children('a').css({opacity:0})
						.click(function() {
						
						if (height == 0) {
							height = $btn.parent().data('lso-height');
							$item.show();
							fullheight = height + $item.outerHeight();							
							$item.hide();
						}

						var to = expanded ? height : fullheight;
						to += ( $btn.parent().hasClass('last') ? 8 : 0 );
						expanded = !expanded;
						
						$(this).data('expanded',expanded);
						
						if ($.browser.msie) {
							expanded ? $item.fadeIn() : $item.fadeOut();
						}
						else {
							expanded ? $item.fadeIn() : $item.fadeOut();
						}
						$btn.parent().animate({height: to+'px'}, function() {
							if (!expanded) {
								$btn.parent().find('.lso-event-data').show();
								$btn.parent().find('.lso-event-form').remove();
							}
						}).queue(false);
						
						
						return false;
					});
				});
				
			}, 200);
			
			
		})
	}
	
})(jQuery);

