//Declare variables
var activeItem = 1;
var animationDuration = 900;
var hrefAttr = "";
var titleAttr = "";

//Define default animation easing
jQuery.easing.def = "easeInOutCubic";

//Collapses the initial content section and animates it open
function initLayout(aItem) {
	
    //Check amount of panels
    var panelCount = $("#section-2 .genericB").length;
    if(panelCount == 2){
	$("#section-2 .genericB").css("height", "245px");
    }

    $("#section-2 .item-" + aItem).addClass("opened");
    $("#section-2 .item-" + aItem + " .genericB").css("display", "none").slideDown(animationDuration);
    if ($.browser.msie && /6.0/.test(navigator.userAgent)) {
        $('#section-2').css("display","block");
    };
	//storeAttrs(aItem);
	//replaceATag(aItem);
}

//Closes the currently open content section and opens the clicked content section
function setItem(clickedItem) {
    //If clicked item is not currently open,
    if (clickedItem != activeItem) {
        //Close the open item
        $("#section-2 .opened .genericB").slideUp(animationDuration);
		//Replace the open item's <span> with <a>, making it clickable again
		//replaceSpanTag(activeItem);
		//Replace the open item's href and title attribute
		//$("#section-2 .item-" + activeItem + " h2 span a").attr("href", hrefAttr).attr("title", titleAttr);
		//Remove "opened" class from the open item
        $("#section-2 .opened").removeClass("opened");
        //Open the clicked item
        $("#section-2 .item-" + clickedItem + " .genericB").slideDown(animationDuration);
		//Store and remove the clicked item's attributes
		//storeAttrs(clickedItem);
		//Replace the clicked item's <a> with <span>, removing its clickablility
		//replaceATag(clickedItem);
        //Add "opened" class to the clicked item
        $("#section-2 .item-" + clickedItem).addClass("opened").css("padding-top","2px");
        //Assign the clicked item to the activeItem variable
        activeItem = clickedItem;
    }
}

//Stores and removes the href and title attributes of the specified section's anchor tag
function storeAttrs(actItem) {
	var selector = $("#section-2 .item-" + actItem + " h2 span a");
	hrefAttr = selector.attr("href");
	titleAttr = selector.attr("title");
	selector.removeAttr("href").removeAttr("title");
}

//Replaces <a> with <span>
function replaceATag(actItem) {
	var selector = $("#section-2 .item-" + actItem + " .genericA h2 span");
	selector.replaceWith("" + $(selector).text() + "");
}

//Replaces <span> with <a> and rebinds the itemClick function
function replaceSpanTag(actItem) {
	var selector = $("#section-2 .item-" + actItem + " .genericA h2");
	selector.replaceWith("<a>" + $(selector).text() + "</a>");
	var aSelector = $("#section-2 .item-" + actItem + " .genericA h2 a");
	aSelector.click(itemClick);
	
}

function itemClick() {
	//Get class attribute string from anchor's accordion item container
	var itemClass = $(this).parent().parent().parent().attr("class");
	//Pulls the item number out of the class attribute string
	var itemNumber = itemClass.substr((itemClass.indexOf("item-")+5), 1);
	//Passes the item number to the setItem function
	setItem(itemNumber);
	
	//Keeps the anchor from linking to the URL assigned to its href attribute
	return false;
}

jQuery.elReady('#section-2', function() {
    //Start the initial animation once all page elements are loaded
    //$(window).load(function() {
        initLayout(activeItem);
    //});

    //Section anchor click function
    $("#section-2 .list .genericA h2 span").click(itemClick);
});