
/* Copyright (c) 2011 der|Design */

(function($) {

	/* Scroller Widget Class */

	Der.classes.ScrollerWidget = function(id, type, options, widget) {

		var self = this;

		var ul = widget.find('ul.scroll');

		var busy = false;

		self.id = id;

		self.type = type;

		self.options = options;

		self.children = ul.find('> li:not(.clear)');

		self.page = 1;

		self.total_pages = Math.ceil(self.children.length / parseFloat(self.options.posts_per_view) );

		if ( self.total_pages == 1 ) { widget.find('ul.controls').remove(); }

		var t = 300;

		this.set_page = function(page) {

			lock();

			ul.stop().animate({
				opacity: 0
			}, t, 'easeOutQuad');

			var data = get_page_data(page);

			var elems = [];

			for ( var i=0; i < self.children.length; i++ ) {

				if ( i >= data.start && i <= data.end ) {
					continue;
				} else {
					elems.push(self.children[i]);
				}

			}

			setTimeout(function() {

				self.page = page;

				self.children.show();

				$(elems).hide();

				ul.stop().animate({
					opacity: 1
				}, t, 'easeOutQuad');

				setTimeout(unlock, t);

			}, t);

		}

		function get_page_data(page) {

			var start = ( page - 1 ) * self.options.posts_per_view;

			var end = start + ( self.options.posts_per_view - 1 );

			return {
				start: start,
				end: end
			};

		}

		function show_initial() {

			var elems = [];

			var start = 0;

			var end = self.options.posts_per_view - 1;

			for ( var i=start; i < self.children.length; i++ ) {

				if ( i >= start && i <= end ) {
					continue;
				} else {
					elems.push(self.children[i]);
				}

			}

			$(elems).hide();

		}

		function lock() {
			busy = true;
		}

		function unlock() {
			busy = false;
		}

		// Show elements from page 1

		show_initial();

		// Setup click events

		if ( self.total_pages > 1 ) {

			var next = widget.find('ul.controls li.next').data('inc', 1);

			var prev = widget.find('ul.controls li.previous').data('inc', -1);

			next.add(prev).click(function() {

				if ( busy ) {return;}

				/* @current should start from zero, so it nees to be substracted 1
				 * cycle() returns a zero-based index, so the result needs to be incremented by 1 */

				var page = cycle( $(this).data('inc'), self.page - 1, self.total_pages ) + 1;

				self.set_page(page);

			});

		}

	}
	
	/* Gallery Widget (manually inherits Der.classes.ScrollerWidget) */
	
	Der.classes.GalleryWidget = function(id, type, options, widget)	{
		
		var self = this;
		
		self.type = type;
		
		self.scroller = new Der.classes.ScrollerWidget(id, type, options, widget); // Manual inheritance
		
		widget.find("a[rel='lightbox[gallery-widget]']").attr('rel', 'lightbox[gallery-widget-' + id + ']'); // Make galleries unique with gallery id
		
	}
	
	/* Slideshow Widget */

	Der.classes.SlideshowWidget = function(id, type, options, widget) {

		var self = this;

		self.id = id;

		self.content = null;

		self.type = type;

		self.options = options;
		
		self.container = widget.find('.frame .container');

		var paused = ! options.autoplay;

		var anchor = widget.find('.frame a.hover');

		if ( ! options.autoplay ) {widget.find('ul.controls li.pause').remove();}

		var li_next, li_previous, li_pause, images_array;

		this.set_content = function(data) {

			self.content = data; // contains: Array(thumb, hires);

			if ( data.length == 0 ) { widget.find('ul.controls').remove(); }

			images_array = Array( [self.container.find('img').attr('src'), self.container.siblings('.hover').attr('href')] ).concat(data);
			
			var images = '';

			for (var i=0; i < data.length; i++) {
				images += ('\n<img src="' + data[i][0] + '" width="243" height="' + options.height + '" />');
			}

			// Add images and setup nivoSlider

			self.container.append(images).nivoSlider({
				pauseTime: options.timeout * 1000,
				manualAdvance: ! options.autoplay,
				animSpeed: options.speed,
				effect: 'fade',
				controlNav: false,
				beforeChange: function() {
					var i = self.vars.currentSlide + 1;
					if (i==self.vars.totalSlides) {i=0;}
					if (i<0) {i=self.vars.totalSlides-1;}
					anchor.attr('href', images_array[i][1]);
				}
			});

			self.container.css('height', options.height + 'px');

			// Add self.nivoslider to control the slideshow, and self.vars to retrieve nivo runtime vars

			self.nivoslider = self.container.data('nivoslider');

			self.vars = self.container.data('nivo:vars');

			// Configure directional navigation with controls

			var nivo_nextNav = self.container.find('.nivo-nextNav');

			var nivo_prevNav = self.container.find('.nivo-prevNav');

			li_previous = widget.find('ul.controls li.previous');

			li_next = widget.find('ul.controls li.next');

			li_pause = widget.find('ul.controls li.pause');

			li_previous.click(function() {nivo_prevNav.click();});

			li_next.click(function() { nivo_nextNav.click();});

			li_pause.click(self.toggle);

		}

		this.next = function() {li_next.click();}

		this.previous = function() {li_previous.click();}

		this.toggle = function() {

			if ( paused ) {li_pause.removeClass('paused');self.nivoslider.start();paused = false;}

			else {li_pause.addClass('paused');self.nivoslider.stop();paused = true;}

		}

	}

	/* Sidebar Class */

	Der.classes.Sidebar = function(selector, proxy) {

		var self = this;

		var object = $(selector);

		var busy = false;

		var vbutton = $('#right-bg span.vbutton');

		self.visible = true;

		proxy = $(proxy);

		// Animation parameters

		var ease = {
			expand: 'easeInQuad',
			collapse: 'easeInQuad'
		};

		var duration = 200;

		this.expand = function() {

			if ( busy || self.visible ) {
				return;
			} else {
				lock();
			}

			Der.set_cookie('milkySidebarStatus', 'expanded');

			object.stop().animate({
				left: '0'
			}, duration, ease.expand);

			proxy.stop().animate({
				right: '0'
			}, duration, ease.expand);

			setTimeout(function() {
				vbutton.css('border-width', '1px 0 1px 1px').removeClass('expand').addClass('collapse').css('marginRight', '0');
			}, duration*0.5);

			self.visible = true;

			setTimeout(unlock, duration);

		}

		this.collapse = function() {

			if ( busy || ! self.visible ) {
				return;
			} else {
				lock();
			}

			Der.set_cookie('milkySidebarStatus', 'collapsed');

			Der.hide_visible_popups('posts_widget_popup');

			object.stop().animate({
				left: '267px'
			}, duration, ease.collapse);

			proxy.stop().animate({
				right: '-267px'
			}, duration, ease.collapse);

			setTimeout(function() {
				vbutton.css('border-width', '1px 1px 1px 0').removeClass('collapse').addClass('expand').css('marginRight', '6px');
			}, duration*0.5);

			self.visible = false;

			setTimeout(unlock, duration);

		}

		this.show = function() {

			if ( busy || self.visible ) {
				return;
			} else {
				lock();
			}

			object.css('left', '0');

			proxy.css('right', '0');

			vbutton.css('border-width', '1px 0 1px 1px').removeClass('expand').addClass('collapse').css('marginRight', '0');

			self.visible = true;

			unlock();

		}

		this.hide = function() {

			if ( busy || ! self.visible ) {
				return;
			} else {
				lock();
			}

			Der.hide_visible_popups('posts_widget_popup');

			object.css('left', '267px');

			proxy.css('right', '-267px');

			vbutton.css('border-width', '1px 1px 1px 0').removeClass('collapse').addClass('expand').css('marginRight', '6px');

			self.visible = false;

			unlock();

		}

		this.auto_display = function() {

			switch ( Der.get_cookie('milkySidebarStatus') ) {

				case 'expanded':

					self.show();

					break;

				case 'collapsed':

					self.hide();

					break;

			}

		}

		function lock() {
			busy = true;
		}

		function unlock() {
			busy = false;
		}

		/* Event Handlers */

		vbutton.click(function() {

			if ( self.visible ) {
				self.collapse();
			}

			else {
				self.expand();
			}

		});

	}


	/* Initialization Code */

	queue_ready_fn(function() {

		Der.create_objects('slideshow_widget', Der.classes.SlideshowWidget, '#right-sidebar .widget.slideshow_widget', true);

		Der.create_objects('posts_widget', Der.classes.ScrollerWidget, '#right-sidebar .widget.posts_widget', true);

		Der.create_objects('gallery_widget', Der.classes.GalleryWidget, '#right-sidebar .widget:not(.slideshow_widget).gallery_widget', true);

		Der.sidebar = new Der.classes.Sidebar('#right-sidebar', '#right-bg');

		Der.sidebar.auto_display();

		// Add content for the various PostsWidgetPopup objects

		for (var i=0; i < Der.objects.posts_widget.length; i++) {

			var object = Der.objects.posts_widget[i];

			object.children.find('a.frame img').each(function() {
				Der.set_object_content('posts_widget_popup', [$(this).attr('alt')]);
			});

		}

	});

})(jQuery);
