/*  */ 
 
 
/*  */ 
$(document).ready(function() {	

	$(".dropdown").each(function(i){
		//create the dropdown
		var $defaultOption = $(this).children('li:first');
		var $activeOption = $(this).find('.active').parent('li');
	
		$(this).children().wrapAll('<li class="inactive"><ul class="options">');				
		$(this).find('.inactive').before('<li class="selected"><a href="#"><span class="value"></span></a></li>');
	
		if($activeOption.html() == null) {	
			$(this).find('.selected .value').html($defaultOption.find('a').html());	
			$defaultOption.remove();

		} else {	
			$(this).find(".selected .value").html($activeOption.find('a').html());
			$activeOption.hide();
		}

		if($(this).find('.options').children('li').length > 6) {
			$(this).addClass('long');	//denote long list for jscrollPane
		}
		
		$(this).parent().show();
		
		//set up the events
	    $(this).find(".selected a").click(function() {
			if($(this).parents('.dropdown').hasClass('long')) {
				var $options = $(this).parents('.dropdown').find(".inactive .options");
				var element =  $options.jScrollPane({
					verticalDragMinHeight: 50,
					verticalDragMaxHeight: 50
				});
				var api = element.data('jsp');
				$options.toggle('fast',function(){
					api.reinitialise();		//scroll only works if reinitialised after dropdown
				});
			} else {
				$(this).parents('.dropdown').find(".inactive .options").toggle('fast');
			}
			
			// Hide redundant items on tabbed content dropdowns
			$(this).parent().siblings('li').find('li:hidden').show();
			$(this).parent().siblings('li').find('a.active').parent().hide();
			
			return false;
	    });
	
		// trigger dropdown on heading click
		$(this).prev('h2').css('cursor', 'pointer').bind({
			mouseover: function(){
				$(this).next('.dropdown').find('.selected a').addClass('hover');
			}, 
			mouseout: function (){
				$(this).next('.dropdown').find('.selected a').removeClass('hover');
			},
			click: function(){
				if($(this).next('.dropdown').hasClass('long')) {
					var $options = $(this).next('.dropdown').find(".inactive .options");
					var element =  $options.jScrollPane({
						verticalDragMinHeight: 50,
						verticalDragMaxHeight: 50
					});
					var api = element.data('jsp');
					$options.toggle('fast',function(){
						api.reinitialise();		//scroll only works if reinitialised after dropdown
					});
				} else {
					$(this).next('.dropdown').find(".inactive .options").toggle('fast');
				}

				// Hide redundant items on tabbed content dropdowns
				$(this).next('.dropdown').children('li').find('li:hidden').show();
				$(this).next('.dropdown').children('li').find('a.active').parent().hide();

				return false;
			}
		});

	    $(this).find(".inactive .options li a").bind('click', function() {
	        var text = $(this).html();
	        $(this).parents('.dropdown').find(".selected .value").html(text);
	        $(this).parents('.dropdown').find(".inactive .options").hide();
	    });

	    $(document).bind('click', function(e) {
	        var $clicked = $(e.target);
	        if (! $clicked.parents().hasClass("dropdown"))
	            $(".dropdown .inactive .options").hide();
	    });

		// Fix z-index for multiple instances
		$(this).find('.options').css('z-index', 5000 - i);
	});
});
/*
 * jQuery (Image) Gallery Plugin
 * (07-FEB-2011)
 * 
 * Converts a list of image files into a thumbnail gallery
 * Requires jquery.cycle.js (http://jquery.malsup.com/cycle/)
 *
 * Implement like so:
 * $(document).ready(function(){
 * 		$('.class').gallery();
 * });
 *
*/

(function($){
    jQuery.fn.extend({
        gallery: function(options) {  

            var defaults = { 
				gallery: 'gallery',
				thumbnail_size: '160c',
				image_size: '960n',
				captions: true
            }

            var options = jQuery.extend(defaults, options);

            return this.each(function() { 
				var $this = $(this);
				var gallery = options.gallery;
				var pager = "#pager_" + gallery;
				var scroller = "#scroller_" + gallery;
				var thumbnail_size = options.thumbnail_size;
				var image_size = options.image_size;
				var captions = options.captions;
				
				resize_images();				// Set landscape images to 965px wide
			
				if ($this.children().length > 1)  create_gallery();				// Create cycle gallery
		
				if (captions) toggle_captions();

				if ($this.children('li').length > 6) setup_scroller();
				
				function create_gallery(){
					
					var srcImage = '_' + image_size;
					
					$this.before('<div class="scroll" style="display:none;"><ul id="pager_' + gallery + '" class="grid compact thumbnails"></ul></div><div class="placeholder" />').wrap('<div class="media" />').after('<div id="gallery_nav_' + gallery + '" class="gallery_nav"><a class="prev" title="Previous image" href="#"></a><a class="next" title="Next image" href="#"></a></div>').cycle({
						activePagerClass: 'active',
						after: onAfter,
						prev: '#gallery_nav_' + gallery + ' .prev',
						next: '#gallery_nav_' + gallery + ' .next',
					    pager:  '#pager_' + gallery,
						pagerAnchorBuilder: function(idx, slide) {	
								var $img = jQuery(slide).find('img').attr('src').split(srcImage, 1);
								if (captions){
									var $caption = jQuery(slide).find('.caption1').html();
									if ($caption != '') return '<li><a href="#"><img src="' + $img + '_160c.jpg" width="160" /><span class="caption1">' + $caption + '</span></a></li>';
									if ($caption == '') return '<li><a href="#"><img src="' + $img + '_160c.jpg" width="160" /></a></li>';
								} else {
									return '<li><a href="#"><img src="' + $img + '_160c.jpg" width="160" /></a></li>';
								}
						},
						nowrap: 1,
						timeout: 0
					});
					
					if ($this.children('li').length > 1) {
						$('#pager_' + gallery).parent('.scroll').show();
					}
										
				}
				
				function onAfter(curr, next, opts) {
					var index = opts.currSlide;	
				    if (index == 0) {
						$('#gallery_nav_' + gallery + ' .prev').addClass('disabled').attr('title', '');
					} else {
						$('#gallery_nav_' + gallery + ' .prev').removeClass('disabled').attr('title', 'Previous image');
					}
					if (index == opts.slideCount - 1) {
						$('#gallery_nav_' + gallery + ' .next').addClass('disabled').attr('title', '');
					} else {
						$('#gallery_nav_' + gallery + ' .next').removeClass('disabled').attr('title', 'Next image');
					}
				}
				
				function resize_images(){
					$this.find('img').each(function(){

						var img = $(this); 
						var pic_real_width, pic_real_height;
						$("<img/>") // Make in memory copy of image to avoid css issues
						.attr("src", $(img).attr("src"))
						.load(function() {
						   pic_real_width = this.width;   // Note: $(this).width() will not
						   pic_real_height = this.height; // work for in memory images.
							if(pic_real_height < 700) {
								img.width(965);
							}
						});		        
					});
				}
				
				function toggle_captions(){
					
					$this.children('li').each(function(){
						if (!($(this).find('.caption2').hasClass('empty'))) {
							//add the caption toggle button
							$(this).append('<a class="info show_caption"></a>');
						}
						
						$(this).find('.show_caption').click(function(){
							$(this).parent().find('.caption').slideToggle();
						});
					});
				}
				
				function toggle_thumbs(){
					
					$('.scroll').hide();
					
					$('.content_nav').bind({
						mouseenter: function(event){
							$('.scroll').slideDown();
							//console.log('The mouse cursor is at ('  + event.pageX + ', ' + event.pageY + ')');
						},
						click: function(event){
							$('.scroll').slideToggle();
							//console.log('The mouse cursor is at ('  + event.pageX + ', ' + event.pageY + ')');
						}
					});
				}
				
				function setup_scroller() {

					// Append scroller
					$(pager).parent('.scroll').before('<div id="scroller_' + gallery + '" class="navigate"><h2>Explore Gallery</h2><div class="scroller"><a class="left"><img src="/img/icons/arrow_left_sm.gif" /></a><div class="scroll_wrap"><div class="draggable"><img src="/img/icons/toggler.gif"></div></div><a class="right"><img src="/img/icons/arrow_right_sm.gif" /></a></div></div>');
					
					// Custom carousel functions
					function carousel_initCallback(carousel) {
					    $(scroller).find('.right').bind('click', function() {
					        carousel.next();
					        return false;
					    });

					    $(scroller).find('.left').bind('click', function() {
					        carousel.prev();
					        return false;
					    });
					};

					// Create carousel
					$(pager).jcarousel({
						easing: 'linear',
						scroll: 1,
						visible: 6,
						initCallback: carousel_initCallback,
				        buttonNextHTML: null,
				        buttonPrevHTML: null
				    });
				
					// Fix ul width
					$(pager).width($this.children('li').length * 161);

					//Get the API
					var carousel = $(pager).data('jcarousel');
					var speed = 0;
					
					// Create draggable
					$(scroller).find( ".draggable" ).draggable({ 
						containment: "parent", 
						revert: true,
						revertDuration: 200,
						create: function(event, ui) { 
							$(scroller).find('.draggable').css({'left':'95px'}); // Initial position set for UI bug in Safari/Chrome
						},
						stop: function(){
							stopScroll();
						},
						drag: function(event, ui){
							if (ui.position.left == 95) {
								stopScroll();
							}
							if (ui.position.left > 95) {
								speedChange(true, 80, 1);
							}
							if (ui.position.left > 140) {
								speedChange(true, 30, 5);
							}
							if (ui.position.left < 95) { 
								speedChange(false, 80, 1);
							}
							if (ui.position.left < 65) {
								speedChange(false, 30, 5);
							}						
						}
					});
					
					// Move right
					function imgScrollRight(){
						carousel.next();
					}

					// Move left
					function imgScrollLeft(){
						carousel.prev();
					}

					// Change Speed
					function speedChange(dir,speed,numItems){
						clearInterval(initScroll);
						carousel.options.scroll = numItems;							
						if (dir) {
							initScroll = setInterval(imgScrollRight, speed);	
						} else {
							initScroll = setInterval(imgScrollLeft, speed);
						}
					}

					function startScroll(speed) {
						initScroll = setInterval(imgScrollRight, speed);
					}

					function stopScroll() {
						clearInterval(initScroll);
					}

					startScroll(0);
					stopScroll();
				}
	        });
		}
    });
})(jQuery);

/*
 * jQuery HTML5 Playlist Plugin
 * (15-FEB-2011)
 * 
 * Converts a list of video files into a thumbnail gallery using the JWPlayer v5.4
 *
 * Implement like so:
 * $(document).ready(function(){
 * 		$('.class').playlist();
 * });
 *
*/

(function($){
    jQuery.fn.extend({
        playlist: function(options) {  

            var defaults = { 
				gallery : 'gallery'
			}

            var options = jQuery.extend(defaults, options);

            return this.each(function() { 
				var $this = $(this);
				var $playlistItem = $this.children('li');
				var gallery = options.gallery;
				var playerId = gallery + '_player';
				var scroller = "#scroller_" + gallery;
								
				// Hide playlist if only one film
				if ($playlistItem.length < 2) {
					single_video();
				} else {
					create_player();
					create_playlist();
				}
				
				// Add scroller if > 6 items
				if ($playlistItem.length > 6) {
					setup_scroller();
				}
				
				function single_video() {
					
					$this.hide();
					var video = $this.children('li').find('a').attr('href');
					var thumb = $this.children('li').find('img').attr('src').split('_160c').join('_960n').split('?');
										
					$this.after('<div class="media" id="' + playerId +'"></media>');
					jwplayer(playerId).setup({
						file: video,
						image: thumb[0],
						width: '960',
						height: '540',
					    flashplayer: 	"/swf/jw_player.swf",
						'skin': 		'/skins/html5/html5.xml',
						"plugins":{
							"gapro":{"accountid":"UA-7210865-1"}
						}
					});
				}
					
				function create_player() {
					$this.after('<div class="media"><div id="' + playerId +'"></div><div id="' + playerId +'_caption" class="caption"></div></media>');
					$this.children('li:nth-child(1)').find('a').addClass('active');
					$this.wrap('<div class="scroll"></div>');
					$this.parent('.scroll').after('<div class="placeholder"></div>');
				}
				
				function create_playlist() {
					var playlist = [];
					
					$this.children('li').each(function(){
						var video = $(this).find('a').attr('href');
						var thumb = $(this).find('img').attr('src').split('_160c').join('_960n').split('?');						
						var caption = "";
						if ($(this).find('.caption2').html().length > 0) {
							caption = $(this).find('.caption2').html();
						}
						playlist.push({file: video, image: thumb[0], description: caption});						
					});
					
					init_player(playlist, playerId);					
				}
				
				function init_player(playlist, playerId) {
					jwplayer(playerId).setup({
					    flashplayer: 	"/swf/jw_player.swf",
					    'playlist': 	playlist,
						width: '960',
						height: '540',
						'skin': 		'/skins/html5/html5.xml',
						events: {
							onPlaylistItem: function(event) {
								$('#' + playerId +'_caption').html(jwplayer(playerId).getPlaylistItem(event.index).description);
							}
				        },
						"plugins":{
							"gapro":{"accountid":"UA-7210865-1"}
						}
					});	
				}
		
				$playlistItem.click(function(event) {
					event.preventDefault();

					var itemIndex = $playlistItem.index($(this));					
					jwplayer(playerId).playlistItem(itemIndex);
					
					$(this).siblings().find('a').removeClass('active');
					$(this).find('a').addClass('active');
					
				});
				
				function toggle_thumbs(){
					$('.content_nav').bind({
						mouseenter: function(event){
							$('.scroll, .placeholder').slideDown();
						},
						click: function(event){
							$('.scroll, .placeholder').slideToggle();
						}
					});
				}
				
				function setup_scroller() {
					
					// Append scroller to explore pane
					$this.parent('.scroll').before('<div  id="scroller_' + gallery + '" class="navigate"><h2>Explore Gallery</h2><div class="scroller"><a class="left"><img src="/img/icons/arrow_left_sm.gif" /></a><div class="scroll_wrap"><div class="draggable"><img src="/img/icons/toggler.gif"></div></div><a class="right"><img src="/img/icons/arrow_right_sm.gif" /></a></div></div>');

					function carousel_initCallback(carousel) {
					    $(scroller).find('.right').bind('click', function() {
					        carousel.next();
					        return false;
					    });

					    $(scroller).find('.left').bind('click', function() {
					        carousel.prev();
					        return false;
					    });
					};

					// Create the carousel
					$this.jcarousel({
						easing: 'linear',
						scroll: 1,
						visible: 1,
						initCallback: carousel_initCallback,
				        buttonNextHTML: null,
				        buttonPrevHTML: null
				    });
				
					// Fix ul width
					$this.width($this.children('li').length * 161);

					//Get the API
					var carousel = $this.data('jcarousel');
					var speed = 0;

					// Create the draggable
					$(scroller).find( ".draggable" ).draggable({ 
						containment: "parent", 
						revert: true,
						revertDuration: 200,
						create: function(event, ui) { 
							$(scroller).find('.draggable').css({'left':'95px'}); // Initial position set for UI bug in Safari/Chrome
						},
						stop: function(){
							stopScroll();
						},
						drag: function(event, ui){
							if (ui.position.left == 95) {
								stopScroll();
							}
							if (ui.position.left > 95) {
								speedChange(true, 80, 1);
							}
							if (ui.position.left > 140) {
								speedChange(true, 30, 5);
							}
							if (ui.position.left < 95) { 
								speedChange(false, 80, 1);
							}
							if (ui.position.left < 65) {
								speedChange(false, 30, 5);
							}						
						}
					});
					
					// Move right
					function imgScrollRight(){
						carousel.next();
					}

					// Move left
					function imgScrollLeft(){
						carousel.prev();
					}

					// Change Speed
					function speedChange(dir,speed,numItems){
						clearInterval(initScroll);
						carousel.options.scroll = numItems;							
						if (dir) {
							initScroll = setInterval(imgScrollRight, speed);	
						} else {
							initScroll = setInterval(imgScrollLeft, speed);
						}
					}

					function startScroll(speed) {
						initScroll = setInterval(imgScrollRight, speed);
					}

					function stopScroll() {
						clearInterval(initScroll);
					}

					startScroll(0);
					stopScroll();
				}

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

