﻿function setIconsWrapper(postID) {
    $('div.bnnIconsHeader').each(function() {
        if (postID == null) // Ran on PageLoad; set all images off except 1st in each set
        {
            setIcons(this);
        }
        else { // Ran when image is selected
            if ($(this).find('a#img_' + postID).length > 0) {
                setIcons(this, postID);
                return (false); // break .each() loop
            }
        }
    });
}

// Do not call this function directly; call setIconsWrapper()
function setIcons(container, postID) {
    var index = 0;

    $(container).find('a').each(function() {
        var excerpt = $(this).attr("rel").replace('"', '');
        var off = excerpt.split(';')[0];
        var on = excerpt.split(';')[1];

        if (((postID == null) && (index == 0)) ||
           ($(this).attr("id") == ("img_" + postID))) {
            $(this).html('<img src="' + skinPath + 'images/' + on + '" />');
        }
        else {
            $(this).html('<img src="' + skinPath + 'images/' + off + '" onmouseover="javascript:this.src=\'' + skinPath + 'images/' + on + '\';" onmouseout="javascript:this.src=\'' + skinPath + 'images/' + off + '\';" />');
        }
        index++;
    });
}

$(function() { setIconsWrapper(); });

function flipFeatures(postID) {
    setIconsWrapper(postID);

    $('a#img_' + postID).parents('.bnnIconsPostList').find('.bnnPostsListPost').each(function () {
        if ($(this).attr("id") == postID)
            $(this).show();
        else
            $(this).hide();
    });
}

// Javascript array object that will hold ID's of siblings of accordian objects (like an associative array)
var accordianSiblings = [];

// <params>
//   governor: control responsible for calling function
//   elementID: element ID to check for 
//   expandedInnerText: governor's innerText when expanded (i.e. '-')
//   collapsedInnerText: other items' text in accordian when collapsed (i.e. '+')
//   commonClass: the common class of all accordian items that contains the information to show/hide 
//   commonParent: the "wrapper" div that's common to all elements in accordian - basically, the outer bounds
// </params>
function accordian(governor, elementID, expandedInnerText, collapsedInnerText, commonClass, commonParent) {

    // First, check if already expanded, if so collapse
    /*if ($('div#' + elementID).is(":visible")) {
        $('div#' + elementID).hide();
        if (accordianSiblings[elementID] != null) {
            for (var i = 0; i < accordianSiblings[elementID].length; i++) {
                $('div#' + accordianSiblings[elementID][i]).hide();
            }
        }
        $('span#span_' + elementID).html(collapsedInnerText);
    }
    else {*/
        $('#' + governor.id).parents('.' + commonParent).find('.' + commonClass).each(function () {

            if ($(this).attr("id") == elementID) {
				if ($('div#' + elementID).is(":visible")) {
					$(this).hide();

					// Set siblings hidden
					if (accordianSiblings[$(this).attr("id")] != null) {
						for (var i = 0; i < accordianSiblings[$(this).attr("id")].length; i++) {
							$('div#' + accordianSiblings[$(this).attr("id")][i]).hide();
						}
					}
					
					// Change +'s to -'s and vice versa
					$('#' + governor.id).find('.accControl').each(function () {
							$(this).html(collapsedInnerText);
					});
				} else {
					$(this).show();

					// Set siblings visible
					if (accordianSiblings[$(this).attr("id")] != null) {
						for (var i = 0; i < accordianSiblings[$(this).attr("id")].length; i++) {
							$('div#' + accordianSiblings[$(this).attr("id")][i]).show();
						}
					}
					
					// Change +'s to -'s and vice versa
					$('#' + governor.id).find('.accControl').each(function () {
							$(this).html(expandedInnerText);
					});
				}
            }
            /*else {
                $(this).hide();

                // Set siblings hidden
                if (accordianSiblings[$(this).attr("id")] != null) {
                    for (var i = 0; i < accordianSiblings[$(this).attr("id")].length; i++) {
                        $('div#' + accordianSiblings[$(this).attr("id")][i]).hide();
                    }
                }
            }*/
        });

        // Change +'s to -'s and vice versa
        //$('#' + governor.id).parents('.' + commonParent).find('.accControl').each(function () {
        //    //if ($(this).attr("id") == "span_" + elementID)
		//	if ($('div#' + elementID).is(":visible"))
        //        $(this).html(expandedInnerText);
        //    else
        //        $(this).html(collapsedInnerText);
        //});
    //}
}
