// Modified by AP 

$(document).ready(function() {
	$("#hero").css("display", "block");
	$("#ephemera").css("display", "block");
});

$(function()
{
	Drinkaware.doNav($('#main-nav>ul'));
	$('.box-accordion ul.accordion').each(function() { Drinkaware.doAccordion($(this)); });
	Drinkaware.doEphemera($('#ephemera'));
	$('.box-sliding-list').each(function() { new Drinkaware.SlidingList($(this)); });
});

function Drinkaware() {
	this.ACCORDION_SPEED       = 300;
}

Drinkaware.doNav = function(oEl)
{
	var iL   = (arguments.length == 1) ? 0 : arguments[1];
	
	// iterate through children
	oEl.children('li').addClass('l'+iL).each(function()
	{
		var oT = $(this);
		var iL = parseInt(oT.attr('class').match(/\d+/));
		var oU = oT.children('ul');
		
		if (oU.length > 0)
		{			
			// continue right-expand flag if required
			var iLeft = 0;
			if (oT.hasClass('er'))
			{
				iLeft -= oU.outerWidth();
				oU.children('li').addClass('er');
			}
			else
			{
				iLeft += oT.outerWidth();
			}
			
			// position submenu
			oU.css(
			{ 
					top:  ((iL == 0) ? oT.outerHeight() : -2    )+'px',
					left: ((iL == 0) ? 0                : iLeft )+'px'
			});
			
			// hook
			oT.addClass('hs').mouseenter(function()
			{
				$(this).addClass('active').children('ul').show();
			}).mouseleave(function()
			{
				$(this).removeClass('active').children('ul').hide();
			});
			
			// recurse
			oU.show(); 
			Drinkaware.doNav(oU, iL+1);
			oU.hide();
		}
	});
	oEl.find("a").each(function(){
		if($(this).html().indexOf('&gt;&gt;') > 0 || $(this).html().indexOf('>>') > 0){
			$(this).click(function(){return false;});
		}
	});
}

Drinkaware.doAccordion = function(oEl)
{
	// apply styles
	oEl.addClass('js');

	// clicks
	oEl.find('h3').click(function()
	{
		var oLi = $(this).parent();

		// if it's already open, go to the location the link points at		
		if (oLi.hasClass('open'))
		{
            var oTmp = oLi.find('a');
            if (oTmp.length == 0)
            	return false;
            
            document.location.href = oTmp.attr('href');
            return false;
		}
	
		oLi.parent().children('li.open').removeClass('open').find('div').slideUp(Drinkaware.ACCORDION_SPEED);
		oLi.addClass('open').find('div').slideDown(Drinkaware.ACCORDION_SPEED);
		return false;
	});

	// open first
	oEl.find('li div').hide();
	oEl.children('li').eq(0).addClass('open').find('div').show();
}

Drinkaware.SlidingList = function(oEl)
{
    this.init = function()
    {
        // get width of list
        this.oList = this.oContainer.find('ul');
        this.iWidth = this.oList.width();
        
        // get items in that list and break them into groups
        var oItem = this.oList.children('li').hide();
        var iPx   = Math.ceil(oItem.length / this.iPageSize);
        for (i = 0; i < iPx; i++)
        {
            // slice
            this.aoPage[i] = oItem.slice((i * this.iPageSize), (i * this.iPageSize) + this.iPageSize);
            
            // get height
            this.aoPage[i].show();
            this.aiHeight[i] = this.oList.height();
            this.aoPage[i].hide();
        }
        oItem.show().width(oItem.eq(0).width());
        
        // position individual groups
        this.positionGroups();
                
        // switch stuff around
        this.oContainer.addClass('js');
        this.oList.height(this.aiHeight[0]);
        oItem.removeClass('first').css('left', this.iWidth+'px');
        this.aoPage[0].css('left', 0);
        this.oList.children('li:nth-child('+this.iPageSize+'n+1)').addClass('first');
        
        // add nav
        this.addNav();
        this.updatePageSt();
    }
    
    this.positionGroups = function()
    {
        var iO;
        for (i = 0; i < this.aoPage.length; i++)
        {
            iO = 0;
            for (j = 0; j < this.iPageSize; j++)
                iO += this.aoPage[i].eq(j).css('top', iO+'px').outerHeight(true);
        }
        return true;
    }
    
    this.addNav = function()
    {
        var oNav = $('<div class="nav"></div>');
        this.oPgSt = $('<span class="st"></span>');
        var oRef = this;
        
        oNav.append($('<abbr class="sir p" title="previous page"><span><!-- --></span>‹</abbr>').click(function() { oRef.prev(); return false;}));
        oNav.append(this.oPgSt);
        oNav.append($('<abbr class="sir n" title="next page"><span><!-- --></span>›</abbr>').click(function() { oRef.next(); return false; }));
        
        this.oContainer.append(oNav);
        
        // add padding to header
        this.oContainer.find('h2').css({ paddingRight: (oNav.innerWidth() + 30)+'px' });
                
        return true;
    }
    
    this.next = function()
    {
        if (this.iCurrent == (this.aoPage.length - 1))
            return false;
                
        this.aoPage[  this.iCurrent].animate({ left: '-'+this.iWidth+'px' }, { easing: 'linear' });
        this.aoPage[++this.iCurrent].animate({ left: 0 }, { easing: 'linear' });
        this.oList.animate({ height: this.aiHeight[this.iCurrent]+'px' });
        this.updatePageSt();
        return true;
    }
    
    this.prev = function()
    {
        if (this.iCurrent == 0)
            return false;
        
        this.aoPage[  this.iCurrent].animate({ left: this.iWidth+'px' }, { easing: 'linear' });
        this.aoPage[--this.iCurrent].animate({ left: 0 }, { easing: 'linear' });
        this.oList.animate({ height: this.aiHeight[this.iCurrent]+'px' });
        this.updatePageSt();
        return true;
    }
    
    this.updatePageSt = function()
    {
        this.oPgSt.html((this.iCurrent+1)+' / '+this.aoPage.length);
        this.oPgSt.siblings('.p').toggleClass('d', (this.iCurrent == 0));
        this.oPgSt.siblings('.n').toggleClass('d', (this.iCurrent == (this.aoPage.length - 1)));
        
    }
    
    this.oContainer = oEl;
    this.iPageSize  = 3;
    this.oList      = null;
    this.aoPage     = new Array();
    this.aiHeight   = new Array();
    this.iCurrent   = 0;
    this.iWidth     = 0;
    this.oPgSt      = null;

    this.init();
}

Drinkaware.doEphemera  = function(oEl)
{
	// wrap stuff
	oEl.addClass('js').html('<div class="epC">'+oEl.html()+'</div>');
	
	
	// create hide
	oEl.html(oEl.html()+'<span class="epH_old" id="epHclass"><span>Hide this.</span></a>');
	$("#epHclass").addClass('epH');
	$(".epH").click(function() { oEl.slideUp(); });
	
	// toggle link
	oEl.prepend($('<p class="epP">'+oEl.attr('title')+' <em>Read more</em><span><!-- --></span>').click(function()
	{
		var oT = $(this);
		var oD = oT.parent().children('.epC');
		var oS = oT.children('em');
		
		if (oS.text() == 'Read more')
		{
			oS.text('Close').siblings('span').addClass('o');
			oD.slideDown();
		}
		else
		{
			oS.text('Read more').siblings('span').removeClass('o');
			oD.slideUp();
		}
	}));
	
	// create hide
	//oEl.append($('<span class="epH"><span>Hide this</span></a>').click(function() { oEl.slideUp(); }));
	//oEl.append('<span class="epH_old" id="epHclass"><span>Hide this..</span></a>');

}

Drinkaware.Hero = function(sSel)
{
	this.init = function()
	{
		// grab slides
		this.aoSlide = this.oContainer.addClass('sl').children('li');

		// create thumbnails
 		var oTn = $('<ul class="th"></ul>');
		var oRef = this;
		var i    = 0;
		this.aoSlide.each(function()
		{
			var oT = $(this);
			var oH = oT.children('h2');
			var sH = oH.text();
                        var link = oH.children('a'); 
                        var aH = link.attr('href');
			oTn.append($('<li class="i'+(i++)+'"><a href="' + aH + '"><strong>'+sH+'</strong> '+oH.attr('title')+'</a></li>').mouseenter(function()
			{
				oRef.clearInterval();
				oRef.tick(parseInt($(this).attr('class').replace('i', '')));
				return false;
			}).mouseleave(function() { oRef.setInterval(); }).click(function()
			{
                oRef.aoSlide.eq(parseInt(this.className.replace(/[^\d]+/g, ''))).click();
			}));
			
			oT.html('<div>'+oT.html()+'</div>').addClass(sH.toLowerCase().replace(/ /g, '-').replace(/[^a-z0-9\-]/g, ''));
		});
		this.oContainer.parent().append(oTn).addClass('js');
		this.aoThumb = oTn.children('li');
		oTn = null;
		
		// hook up clicking on the item
		this.aoSlide.click(function()
		{
			document.location.href = $(this).find('a').attr('href');
			return false;
		});
		
		// hide everything
		this.aoSlide.hide().eq(0).show();
		this.aoThumb.eq(0).addClass('current');
		this.iCurrent = 0;
		
		this.setInterval();
	}

	this.setInterval = function()
	{
		var oRef = this;
		clearInterval(this.rInterval);
		this.rInterval = setInterval(function() { oRef.tick(); }, this.iTxDelay);
	}

	this.clearInterval = function()
	{
		clearInterval(this.rInterval);
	}

	this.tick = function()
	{
		
		var iCurr = this.iCurrent;
		var iNext = (arguments.length == 0) 
					? (((iCurr + 1) == this.aoSlide.length) ? 0 : iCurr + 1)
					: (((arguments[0] > 0) && (arguments[0] <= this.aoSlide.length)) ? arguments[0] : 0);

		// if we're on the current one...
		if (iNext == this.iCurrent)
			return true;


		// slides
		this.aoSlide.eq(iCurr).fadeOut(this.iTxSpeed);
		this.aoSlide.eq(iNext).fadeIn (this.iTxSpeed);
		
		// thumbnail
		this.aoThumb.removeClass('current').eq(iNext).addClass('current');
		
		// update
		this.iCurrent = iNext;
		return true;
	}

	this.rInterval  = null;
	this.iCurrent	= -1
	this.aoSlide	= null;
	this.aoThumb	= null;
	this.iTxSpeed	= 100;
	this.iTxDelay	= 10000;

	this.oContainer = $(sSel+' ul');	
	if (this.oContainer.length > 0)
		this.init();	
}

Drinkaware.Driver = function() {}

Drinkaware.Driver.UnderstandingUnitGuidelines = function(sSel)
{
	this.init = function()
	{
		// find all the panes
		var oTmp, aoTmp = this.oContainer.find('.i');
		for (i = 0; i < aoTmp.length; i++)
		{
			oTmp = aoTmp.eq(i);
			this.oPane[oTmp.attr('id')] = oTmp;
		}
		
		// add content
		this.oContainer.find('.icl').append($('<p>Click on a person to find out more</p>'));
		this.oContainer.find('.icg').append($('<a href="#dr-uug-l" class="back">‹ <span>Back</span></a>'));
		
		// hook up the links
		var oRef = this;
		this.oContainer.find('a').click(function()
		{
			var oT = $(this);
			if (oT.attr('href').substring(0, 1) != '#')
				return true;
			
			oRef.gotoPane(oT.attr('href').substring(1));
			return false;
		});
		
		// add class
		this.oContainer.addClass('js');
		this.setHeight();
		
		// hide all the panes and only show the one we need
		aoTmp.hide();
		this.sCurrentPane = aoTmp.eq(0).show().attr('id');
	}

	this.setHeight = function()
	{
		// for each of the panes, which one has the greatest height...
		var iMax = 0;
		for (sRef in this.oPane)
		{
			iMax = Math.max(iMax, parseInt(this.oPane[sRef].outerHeight(true)));
		}

		// also pick up the height of the H3...
		iMax += parseInt(this.oContainer.find('h3').outerHeight(true));
		
		// compare it to any height on the container (so we respect any min-height declaration)
		iMax = Math.max(iMax, parseInt(this.oContainer.height()));
		
		// and feed back
		this.oContainer.height(iMax);
	}

	this.gotoPane = function(sPane)
	{
		this.oPane[this.sCurrentPane].fadeOut();
		this.oPane[sPane].fadeIn();
		this.sCurrentPane = sPane;
		
		// hook up any rotational stuff we have going on...
		if (this.oPane[sPane].find('ul.d').length > 0)
			this.initRotation(this.oPane[sPane]);
		
		return true;
	}
	
	this.initRotation = function(oEl)
	{
		if (this.rAnim != null)
			clearInterval(this.rAnim);
		
		// hide all items
		this.aoAnim = oEl.find('li').hide();
		this.iCurr = 0;
		var oRef = this;
		this.rAnim = setInterval(function()
		{
			oRef.aoAnim.eq(oRef.iCurr).fadeOut();
			if (++oRef.iCurr >= oRef.aoAnim.length)
				oRef.iCurr = 0;
			oRef.aoAnim.eq(oRef.iCurr).fadeIn();
		}, 3000);
		this.aoAnim.eq(0).show();
		
		return true;
		
	}
	
	this.oPane = new Object();
	this.sCurrentPane = null;
	this.rAnim = null;
	this.iCurr = 0;
	this.aoAnim = null;
	
	this.oContainer = $(sSel);
	if (this.oContainer.length > 0)
		this.init();
}

Drinkaware.Driver.QuickQuiz = function(sSel)
{
	this.init = function()
	{
		// add class to container and wrap
		this.oContainer.addClass('js').find('.i').addClass('iq');
				
		// hook up clicks
		var oRef = this;
		this.oContainer.find('li').click(function()
		{
			oRef.reveal($(this));
		});
		
		/* because otherwise Saf completely ignores random style rules… */
		if ((navigator.appVersion.indexOf('Safari') != -1) && (navigator.appVersion.indexOf('Chrome') == -1))
		{
		    oRef.oContainer.find('.i').hide();
            setTimeout(function() { oRef.oContainer.find('.i').show(); }, 10);
        }
	}
	
	this.reveal = function(oAns)
	{
		// fade out the old content
		var oOld = this.oContainer.find('.i').fadeOut(400);
	
		// create new content...
		var oRef = this;
		var oTmp = oAns.find('span').clone();
		var oNew = $('<div class="i ia"></div>');
		oNew.append('<h3>'+oTmp.find('em').remove().text()+'</h3>');
		oNew.append('<p>'+oTmp.html()+'</p>');
		oNew.append(this.oContainer.find('p.dr').clone());
		oNew.append($('<p class="back">‹ <span>back</span></p>').click(function() { oRef.revert(); return false; }));
		
		// append it to the container
		this.oContainer.append(oNew);		
		
		// height stuff
		var iH = oNew.outerHeight(true);
		oNew.hide();
				
        // animate the resize and add a class
		this.oContainer.animate({ height: iH+'px'}, 200);
		oNew.fadeIn(400);
		return true;
	}
	
	this.revert = function()
	{	    
        // fade out the old content
        this.oContainer.find('.ia').fadeOut(400, function() { $(this).remove(); });
        
        // get the height of the new content
        var oNew = this.oContainer.find('.iq').show();
        this.oContainer.animate({ height: oNew.outerHeight(true)+'px' }, 200);
        oNew.hide().fadeIn(400);
        
        return true;
	}
	
	this.oContainer = $(sSel);
	if (this.oContainer.length > 0)
		this.init();
}
