function generateColumns(liArr, container, itemLen, type) {
	var ct = 0;
	jQuery.each(liArr, function() {
		if (ct % itemLen == 0) {jQuery(".bhg-link-arrow", container).before("<ul class=\"" + type + "\"><\/ul>");}
		jQuery("ul:last", container).append(this);
		ct++;
	});
}

function getListColObj(node) {
	var myList = {};
	var typeMatch = /col\d+/;
	myList.type = typeMatch.exec(node.className);
	if (myList.type[0] == null) {
		console.log("no type!");
		myList.type[0] = "col1";
	}
	myList.type = myList.type[0];
	var numMatch=/\d+/;
	myList.numcol = parseInt(numMatch.exec(myList.type));
	
	myList.defaultLenCol = (myList.numcol > 1) ? 3 : 4;
	myList.defaultLenTotal = myList.defaultLenCol * myList.numcol;
	myList.lis = jQuery(node).children().clone();
	myList.parent = node.parentNode
	
	return myList;
}

jQuery(document).ready(function(e) {
    //init 'paragraph' link/content
    if (jQuery(".office-intro:first, .agent-intro:first").height() > 252) {
        jQuery(".office-intro:first, .agent-intro:first").height(252);
        jQuery(".linkSplit:first .bhg-link-arrow").css("display", "inline-block");
    }

    //bind 'paragraph' link
    jQuery(".linkSplit:first .bhg-link-arrow").bind("click", function(e) {
        var str = jQuery.trim(jQuery(this).text());
        if (str == "More Info") {
            jQuery(".office-intro:first, .agent-intro:first").height("auto");
            jQuery(this).text("Less Info");
        } else {
            jQuery(".office-intro:first, .agent-intro:first").height(252);
            jQuery(this).text("More Info");
        }
        e.preventDefault();
    });

    //init 'column' link/content
    jQuery(".listhold").each(function() {
        var currentList = getListColObj(this);
        if (currentList.lis.length > currentList.defaultLenTotal) {
            jQuery(".bhg-link-arrow", this.parentNode).css("display", "block");
            currentList.lis = currentList.lis.slice(0, currentList.defaultLenTotal);
        }
        generateColumns(currentList.lis, this.parentNode, currentList.defaultLenCol, currentList.type);
    });

    //bind 'column' links
    jQuery(".office-details .bhg-link-arrow, .agent-details .bhg-link-arrow").bind("click", function(e) {
        var str = jQuery.trim(jQuery(this).text());
        var clistnode = jQuery(".listhold", this.parentNode);
        var currentList = getListColObj(clistnode.get(0));
        jQuery("ul:not(.listhold)", this.parentNode).remove();
        if (str == "Show All") {
            var listlen = jQuery(clistnode.get(0)).children().length;
            var columnset = listlen / currentList.numcol;
            var finlen = Math.floor(columnset) + ((listlen % currentList.numcol > 0) ? 1 : 0);
            generateColumns(currentList.lis, this.parentNode, finlen, currentList.type);
            jQuery(this).text("Show Less");
        } else {
            currentList.lis = currentList.lis.slice(0, currentList.defaultLenTotal);
            generateColumns(currentList.lis, this.parentNode, currentList.defaultLenCol, currentList.type);
            jQuery(this).text("Show All");
        }
        e.preventDefault();
    });

    //Webtrends on link out to agent website
    jQuery('.find_webLink_agent').click(function() {
        dcsMultiTrack('DCS.dcsuri', '/Views/General/FindAgentResults/AgentWeblink.html', 'WT.ti', 'Agent Website Link', 'WT.cg_n', 'indirect conversion', 'WT.si_n', '', 'WT.si_x', '', 'WT.si_cs', '', 'WT.z_engage_type', 'Indirect', 'WT.z_engage_event', 'Agent Weblink', 'WT.z_offsite_url', this.href, 'WT.z_agent_id', agent_Id, 'WT.z_agent_state', state_Id, 'WT.z_office_id', '');
        pageTracker._trackPageview('agent_redirect.html');
        return true;
    });

    //Webtrends on link out to agent website
    jQuery('.find_webLink_office').click(function() {
        dcsMultiTrack('DCS.dcsuri', '/Views/General/FindOfficeResults/OfficeWeblink.html', 'WT.ti', 'Office Website Link', 'WT.cg_n', 'indirect conversion', 'WT.si_n', '', 'WT.si_x', '', 'WT.si_cs', '', 'WT.z_engage_type', 'Indirect', 'WT.z_engage_event', 'Office Weblink', 'WT.z_offsite_url', this.href, 'WT.z_agent_id', '', 'WT.z_agent_state', '', 'WT.z_office_id', office_Id);
        return true;
    });
});
