﻿function initResourceBrands()
{
	var resourceBrands = $('.resourceContainer .resourceOuter');

	// add the expand anchors and set their click state
	var expandAnchor = $('<a href="#" class="brandExCol expandCollapse collapsed"><span></span>expand</a>');
	resourceBrands.find('.resourceBrand')
		.append(expandAnchor)
		.find('a.brandExCol').click(function()
		{
			toggleBrand(this);
			return false;
		});

	// hide the categories
	resourceBrands.find('.resourceCategory').hide();

	// bind toggling to the brand name and logo
	resourceBrands.find('.logo').css('cursor', 'pointer').click(function() { toggleBrand(this); });
	resourceBrands.find('.brandHead h2').css('cursor', 'pointer').click(function() { toggleBrand(this); });

	initResourceCategories();
}

function toggleBrand(source)
{
	// required variables
	var outterBrand = $(source).closest('.resourceOuter'),
		anchor = outterBrand.find('a.brandExCol'),
		brand = outterBrand.find('.brandMain'),
		resources = outterBrand.find('.resourceCategory');		
	
	// cancel if an animation is in process for the category
	if (resources.is(':animated')) return;

	// show the resource and toggle the brands class state
	resources.slideToggle(300);
	brand.toggleClass('collapsed').toggleClass('expanded');

	// update the toggle anchor
	anchor.toggleClass('expanded').toggleClass('collapsed');
	anchor.html('<span></span>' + (anchor.hasClass('expanded') ? 'collapse' : 'expand'));
}

function initResourceCategories()
{
	var resourceCategories = $('.resourceCategory');
	
	// set the collapsed state and hide the content
	resourceCategories.addClass('collapsed').find('div.resourceContent').hide();

	resourceCategories.find('.resource .head h3').removeClass('noJs').click(function()
	{
		// select the resource content and ensure it isnt animated
		var resourceContent = $(this).parent().siblings('.resourceContent');
		if (resourceContent.is(':animated')) return;

		// toggle the content
		resourceContent.slideToggle(300).parent()
			.toggleClass('collapsed').toggleClass('expanded');
	});
	
}

function InitResources(resourceTitles, heading) {
    var initTitles = (resourceTitles.length > 0);
    if (initTitles) {
    	resourceTitles.each(function()
    	{
    		var title = $(this);
    		var resourceHead = title.parent();
    		var resource = resourceHead.parent();
    		var resourceContent = resource.find('div.resourceContent');

    		resource.addClass('collapsed');
    		resourceContent.hide();

    		//Create the expand/collapse button
    		title.click(function()
    		{
    			if (!resourceContent.is(':animated'))
    			{
    				resourceContent.slideToggle(300);
    				resource.toggleClass('expanded');
    				resource.toggleClass('collapsed');

    				$(this).toggleClass('expanded');
    				$(this).toggleClass('collapsed');
    			}
    			return false;
    		}).removeClass('noJs');
    		;
    	});
    }
}

$(document).ready(function() 
{
    initResourceBrands();
});
