$(function(){
	
	function parseTitle(title) {
		html = "";
		parts = title.split("\n\n");
		//console.log(parts);
		if(parts[0]){ // we have a title
			html = $('<div/>').text(parts[0]).html(); // use jquery's html entity conversion
			html = '<h2>' + html + '</h2>';
		}
		if(parts[1]){ // we have a body
			parts.shift();
			parts = parts.join("\n\n"); // just in case there was more than one separator to begin with
			//parts = $('<div/>').text(parts).html();
			parts = parts.replace(/\n\n/g, '</p><p>');
			parts = parts.replace(/\n/g, '<br/>');
			html += '<p>' + parts + '</p>';
		}
		return html;
	}
	
	//fancybox - add h2 & p tags and split with |
	function formatTitle(title, currentArray, currentIndex, currentOpts) {
		if(title){
			if(html = parseTitle(title)){
				return '<div id="fancybox-title-'+ currentOpts.titlePosition +'">' + html + '</div>';
			}
			return false;
		}
		return false;
	}
	
	$('.lightbox, .gallery a').fancybox({
		'titlePosition': 'over'
		, 'titleFormat': formatTitle
	});
	
	$('.lightbox.title-inside, .gallery.title-inside a').fancybox({
		'titlePosition': 'inside'
		, 'titleFormat': formatTitle
		, 'onComplete': function(){  // Fix for overlay
			$('#fancybox-overlay').css('height', $(document).height()+'px');
		}
	});
	
	$('.lightbox.title-outside, .gallery.title-outside a').fancybox({
		'titlePosition': 'outside'
		, 'titleFormat': formatTitle
	});
	
	$('.slides').each(function(){
		i = $('.slides').index(this);
		selector = '.slides:eq(' + i + ') + .slide-controller .slide-numbers';
		//console.log($(selector));
		$(this).cycle({
			timeout: 0
			, fx: 'fade' // name of transition effect (or comma separated names, ex: 'fade,scrollUp,shuffle') 
	        , easeIn: 'easeInExpo'
	        , easeOut: 'easeOutExpo'//, pagerEvent: 'click.cycle'
			, speed: 'fast'
			, pager: selector
			, pagerAnchorBuilder: function(idx, slide) {
				return '<a href="#" mce_href="#"></a>';
			}
	        , prev: '.slide-prev'
	        , next: '.slide-next'
	        //, cleartypeNoBg: true
			//, timeoutFn:     null  // callback for determining per-slide timeout value:  function(currSlideElement, nextSlideElement, options, forwardFlag) 
			}
		);
	});
    
    $('#tertiary-nav a').click(function(){
    	//console.log(123);
    	target = $(this).parent().index();
    	$('.page').hide().eq(target).fadeIn();
    	$('.page .page').first().show();
    	$(this).parent().parent().children('li').removeClass('active');
    	$(this).parent().addClass('active');
    });
    
	//
    $('.subsection-pager a').click(function(){
		target = $(this).parent().index();
		//console.log(target);
    	$(this).closest('.col-left').siblings('.col-right').children('.page').hide().eq(target).fadeIn().children('.slides').cycle(0);
    	$(this).closest('ul').children('li').removeClass('active');
    	$(this).parent().addClass('active');
    });
    
    // show first pages and hide the rest on load
    $('.page').hide().eq(0).show().end().children('.thumbs').hide().eq(0).show();
    $('.thumbs.firstchild').show();
    
    /*
     * Tooltips: uses the alt from the image, parses it according to the lightbox formatting, hides the title attribute on the parent A tag (native browser tooltips) and shows it again when lightbox calls it.
     */
    function initTooltips(){
		$('body').remove('#tooltips').append('<div id="tooltips"/>');
		$('.thumbs img[alt]').each(
			function(){
				if(text = parseTitle($(this).attr('alt'))){
					i = $('.thumbs img[alt]').index(this);
					$('#tooltips').append('<div class="tooltip" id="tooltip' + i + '">' + text + '</div>');
					$(this).attr('title',''); 
				}
			}
		)
		.hover(
			function(){
				i = $('.thumbs img[alt]').index(this);
				$(this).parent().attr({title2: $(this).parent().attr('title'), title:''}); // hide title from browser tooltip but keep for lightbox
				tip = $('#tooltip' + i);
				x = ($(this).offset().left + $(this).outerWidth() / 2) - (tip.outerWidth()/2);
				if(x < 20){
					x = 20;
				}
				y = $(this).offset().top - tip.height() - 10;
				if(y < 20){
					y = $(this).offset().top + $(this).height();
				}
				//console.log(y);
				tip.stop().css({
					left: x,
					top: y,
					opacity: 0
				}).show();
				tip.animate({
					top: y - 10,
					opacity: 1
				}, 300);
					
			},
			function(){
				i = $('.thumbs img[alt]').index(this);
				tip = $('#tooltip' + i);
				$(this).parent().attr({title: $(this).parent().attr('title2')}); // show title for lightbox
				//console.log(tip);
				tip.stop().animate({
					top: parseInt(tip.css('top'),10) + 10,
					opacity: 0
				}, 200, (function(e){e.hide();})(tip)
				)
			}
		)
		.click(function(){
			$(this).parent().attr({title: $(this).parent().attr('title2')});
		});
	}
	initTooltips();
	
	/* Forms */
		$('[required]').each(function(){
		//console.log(this);
		$(this).prev('label').append(' <span class="label-required">*</span>');
	});
	
});
