/*  */ 
 
 
/*  */ 
$(document).ready(function(){
	
	// Set multiple images in a gallery
	$('#image_gallery').gallery({captions: false});
	
	//Tab content
	$(".dropdown").tabs("#main > section", {effect: 'fade', history: true, current: 'active'});
	
	//Fit the subtitle
	$('.hgroup .subtitle').fontfit();
	
	// Scroll to purchase
	$("#go_purchase").click(function(event){
		event.preventDefault();
		
		$("#purchase").slideto({
			highlight: false,
			slide_duration: 1000
		});
	});
	
});
$(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);
	});
});
 
 

