// JavaScript Document

	$(document).ready(function() {
			

		// CAROUSEL 
		// --------------------------------------------------------------------------------------------------------
			
		// Let's define some variables first
		
		var nav_items = $('#infinite_carousel_nav li'); // The different elements, this is an array
		var autoscrolling = true;
				
		$.fn.infiniteCarousel = function () {
	
			function repeat(str, num) {
				return new Array( num + 1 ).join( str );
			}
		  
			return this.each(function () {
				var $wrapper = $('#wrap', this).css('overflow', 'hidden'),
					$slider = $wrapper.find('> ul'),
					$items = $slider.find('> li'),
					$single = $items.filter(':first'),
					
					singleWidth = $single.outerWidth(), 
					visible = Math.ceil($wrapper.innerWidth() / singleWidth), // note: doesnt include padding or border
					currentPage = 1,
					pages = Math.ceil($items.length / visible);            
		
		
				// 1. Pad so that 'visible' number will always be seen, otherwise create empty items
				if (($items.length % visible) != 0) {
					$slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
					$items = $slider.find('> li');
				}
		
				// 2. Top and tail the list with 'visible' number of items, top has the last section, and tail has the first
				$items.filter(':first').before($items.slice(- visible).clone().addClass('cloned'));
				$items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
				$items = $slider.find('> li'); // reselect
				
				// 3. Set the left position to the first 'real' item
				$wrapper.scrollLeft(singleWidth * visible);
				
				// 4. paging function
				function gotoPage(page) {
  					var dir = page < currentPage ? -1 : 1,
    				n = Math.abs(currentPage - page),
    				left = singleWidth * dir * visible * n;
  
  					$wrapper.filter(':not(:animated)').animate({
    					scrollLeft : '+=' + left  					}, 1500, 'easeInOutSine',function () {
						if (page == 0) {
						  $wrapper.scrollLeft(singleWidth * visible * pages);
						  page = pages;
						} else if (page > pages) {
						  $wrapper.scrollLeft(singleWidth * visible);
						  page = 1;
						} 
					
						currentPage = page;
						
						$('#this_item').removeAttr('id');
						$('#infinite_carousel_nav li').eq(currentPage-1).attr('id', 'this_item');
						
						$('#carouseltext').fadeOut( function() {
							$('#carouseltext').html($('#carousel .description').eq(currentPage).html());					
						});
						
						$('#carouseltext').fadeIn();  
					});                         
					
					return false;
				}
				
				//$wrapper.after('<a class="arrow back">&lt;</a><a class="arrow forward">&gt;</a>');
				
				// 5. Bind to the forward and back buttons
				$('a.back', this).click(function () {
					return gotoPage(currentPage - 1);                
				});
				
				$('a.forward', this).click(function () {
					return gotoPage(currentPage + 1);
				});
				
				// create a public interface to move to a specific page
				$(this).bind('goto', function (event, page) {
					gotoPage(page);
				});
				
				 $(this).bind('next', function () {
						gotoPage(currentPage + 1);
				});
			
			
				$('#infinite_carousel_nav li.available').each(function(index) {
					$(this).bind('click', function(){
						var item_number = jQuery.inArray(this, $('#infinite_carousel_nav li')) + 1;
						$('#this_item').removeAttr('id');
						$(this).attr('id', 'this_item');
						if (item_number) { 
							gotoPage(item_number);
						}
						return false;
					});
				});
				
				setInterval(function () {
					if (autoscrolling) gotoPage(currentPage + 1);
				}, 10000);
			
			});  
		};
		
		 
    
		$('#infinite_carousel_window').infiniteCarousel().mouseover(function () {
			autoscrolling = false;
		}).mouseout(function () {
			autoscrolling = true;
		});
		
		/*
		setInterval(function () {
			$('body.home #infinite_carousel_window').trigger('next');
		}, 10000);
		*/
		
				
		$('#infinite_carousel_nav li:first').attr('id', 'this_item');
		
		// Let's define some variables first
		var wrapper = $('#wrap'); // The outer wrapper
		var carousel = $('#carousel'); // The inner wrapper
		var items = $('#carousel li'); // The different elements, this is an array
		var nav_items = $('#carousel_nav li'); // The different elements, this is an array
				
		if (carousel && wrapper) {
			var itemwidth = parseInt(wrapper.width()); // The full width of a single item
			var maxmargin = items.length * itemwidth - itemwidth;
			
			// The function to browse forward
			function next_item(pos){
				if(pos == -maxmargin - itemwidth){
					// Alert (end)
					carousel.animate({
						left: '0'
					  }, 1000);
				} else { 
					var newposition = pos - itemwidth;
					carousel.animate({
						left: newposition
					  }, 1000);
				}
			}
			
			// The function to browse backward
			function previous_item(pos){
				if(pos == 0){
					carousel.animate({
						left: -maxmargin
					  }, 1000);
				} else {
					var newposition = pos + itemwidth;
					carousel.animate({
						left: newposition
					  }, 1000);
				}
			}
		
			// The function to browse to any
			function select_item(pos, itemno){
				var newposition = (itemno*itemwidth-itemwidth)+pos;
				carousel.animate({
						left: pos-newposition
					  }, 1000);
			}
			
			$('#carousel_nav li').each(function(index) {
				$(this).bind('click', function(event) {											
					return false;
				});		
			  });
					
			$('#carousel_nav li.available').each(function(index) {
				$(this).bind('click', function(){
					var item_number = jQuery.inArray(this, $('#carousel_nav li')) + 1;
					$('#this_item').removeAttr('id');
					$(this).attr('id', 'this_item');
					$('#carouseltext').fadeOut( function() {
						$('#carouseltext').html($('#carousel .description').eq(item_number-1).html());					
					});
					$('#carouseltext').fadeIn();
					if ($(this).find('a').attr('rel').length) {
							var playlist = $(this).find('a').attr('rel');
							//embedFlash(playlist);
					}
					if (item_number) { 
						var position = parseInt(item_number)*itemwidth-itemwidth;
						select_item(position, item_number);
					}
					return false;
				});
			});
			
			$('#carousel_nav li:first').attr('id', 'this_item');
			
		}
				
		
		// SORTABLE LISTS 
		// --------------------------------------------------------------------------------------------------------
		
		
		(function($) {
			$.fn.sorted = function(customOptions) {
				var options = {
					reversed: false,
					by: function(a) {
						return a.text();
					}
				};
				$.extend(options, customOptions);
			
				$data = $(this);
				arr = $data.get();
				arr.sort(function(a, b) {
					
					var valA = options.by($(a));
					var valB = options.by($(b));
					if (options.reversed) {
						return (valA < valB) ? 1 : (valA > valB) ? -1 : 0;				
					} else {		
						return (valA < valB) ? -1 : (valA > valB) ? 1 : 0;	
					}
				});
				return $(arr);
			};
		
		})(jQuery);
	
		$(function() {
	  
			  var read_button = function(class_names) {
			var r = {
			  selected: false,
			  type: 0
			};
			for (var i=0; i < class_names.length; i++) {
			  if (class_names[i].indexOf('selected-') == 0) {
				r.selected = true;
			  }
			  if (class_names[i].indexOf('segment-') == 0) {
				r.segment = class_names[i].split('-')[1];
			  }
			};
			return r;
		  };
		  
		  var determine_sort = function($buttons) {
			var $selected = $buttons.parent().filter('[class*="selected-"]');
			return $selected.find('a').attr('data-value');
		  };
		  
		  var determine_kind = function($buttons) {
			var $selected = $buttons.parent().filter('[class*="selected-"]');
			return $selected.find('a').attr('data-value');
		  };
		  
		  var $preferences = {
			duration: 1000,
			easing: 'easeInOutQuad',
			adjustHeight: 'auto',
			useScaling: false
		
		  };
		  
		  var $list = $('.allrecords');
		  var $data = $list.clone();
		  
		  var $controls = $('div#content_categories ul#categories');
		  
		  $controls.each(function(i) {
			
			var $control = $(this);
			var $buttons = $control.find('a');
			
			$buttons.bind('click', function(e) {
			  
			  var $button = $(this);
			  var $button_container = $button.parent();
			  var button_properties = read_button($button_container.attr('class').split(' '));      
			  var selected = button_properties.selected;
			  var button_segment = button_properties.segment;
		
			  if (!selected) {
		
				$buttons.parent().removeClass('selected-0').removeClass('selected-1').removeClass('selected-2').removeClass('selected-3').removeClass('selected-4').removeClass('selected-5').removeClass('selected-6').removeClass('selected-7').removeClass('selected-8').removeClass('selected-9').removeClass('selected-10').removeClass('selected-11').removeClass('selected-12').removeClass('selected-13').removeClass('selected-14').removeClass('selected-15').removeClass('selected-16').removeClass('selected-17').removeClass('selected-18').removeClass('selected-19').removeClass('selected-20').removeClass('selected-21').removeClass('selected-22').removeClass('selected-23').removeClass('selected-24').removeClass('selected-25').removeClass('selected-26');
				$button_container.addClass('selected-' + button_segment);
				
				var sorting_type = determine_sort($controls.eq(1).find('a'));
				var sorting_kind = determine_kind($controls.eq(0).find('a'));
				
				if (sorting_kind == 'all') {
				  var $filtered_data = $data.find('li');
				} else {
				  var $filtered_data = $data.find('li.' + sorting_kind);
				}
				
				if (sorting_type == 'date') {
				  var $sorted_data = $filtered_data.sorted({
					by: function(v) {
					  return parseFloat($(v).find('strong').text().toLowerCase());
					}
				  });
				} else {
				  var $sorted_data = $filtered_data.sorted({
					reversed: true,									   
					by: function(v) {
					  return $(v).find('span').text().toLowerCase();
					}
				  });
				}
				
				$list.quicksand($sorted_data, $preferences);
				
			  }
			  
			  e.preventDefault();
			  
			});
			
		  }); 
		
		});
		
// FANCYBOX 
// --------------------------------------------------------------------------------------------------------
		
		$('a[rel="gallery"]').fancybox({
				'titlePosition'	: 'inside',
				'transitionIn'	: 'elastic',
				'transitionOut'	: 'elastic'
		});

	}); 
