/****************************************************************************************

Copyright (c) 2010 Fusion Alliance, Inc.(Fusion).

This software is the confidential and proprietary information of Fusion. ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in accordance with the terms of the license agreement you entered into with Fusion.

DOC			Site Scripts
VERSION 	1.1
AUTHOR		Doug Scamahorn
EMAIL		dscamahorn@fusionalliance.com

DATE       	NAME           	DESCRIPTON
04/21/2010 	Doug Scamahorn 	Initial creation.

****************************************************************************************/

/* =EXPAND/COLLAPSE ACCORDION WIDGET
---------------------------------------------------------------------------------------*/

	//Publish the accordion control widget
	function publishAccordion(){
		
		//Find each accordian list
		$(".accordionList").each(function (i) {
			
			//Modify Markup
			
				//Wrap accordion in a div
				$(this).wrap("<div class='accordionGroup'></div>");
				
				//Add the expand/collapse all link
				$(this).before("<a href='#' class='accordionControl'>Expand All</a>");
				
				//Strip accordion tables
				$(this).find("table tr:even").addClass("altrow");
							
			//Events
			
				//Attach toggle to expand/collapse control
				$(this).prev().click(function() {
					expandCollapseAccordionList($(this),$(this).text());
					return false;
				});
				
				//Set the initial state of the expand/collapse link - importatn if all the rows in the list are set to open
				changeAccordionControlLabel($(this).prev());
			
			
			//Find each dt in the list
			$(this).find("dt").each(function(){
															
				//Modify Markup
				
					//Add markup to the dt and the dd
					$(this).wrapInner("<span></span>").next().wrapInner("<div class='accordionRowContent'></div>");
				
				//Events
				
					//Test to see if the accordion is set to be open on start
					if ($(this).hasClass("open")) {
					} else {
						//if it isn't open, hide it
						$(this).next().hide();
					}
					
					//Attach the click event to the dt
					$(this).click(function() {
						//If it's open
						if ($(this).hasClass("open")) {
							//remove the class
							$(this).removeClass("open");
						} else {
							//If not, add the class
							$(this).addClass("open");
						}
						//Animate the opacity and height of the dd
						$(this).next().animate({opacity: 'toggle', height: 'toggle'}, function(){
							//Change the control label
							changeAccordionControlLabel($(this).parent(".accordionList").prev());
							//When the animation completes, make the analytics call
							//trackJavaScriptEvent($(this).text(), "accordion");THIS IS THE NOT THE CORRECT OBJECT TO GET THE TEXT FROM
						});
					});
			
			});
			
		});

	}
	
	//
	function expandCollapseAccordionList(eventTarget,eventAction){
		if (eventAction == "Expand All") {
			
			//Open the accordion rows
			$(eventTarget).next().find("dt").each(function(){
				//If it's open
				if ($(this).hasClass("open")) {
					//Do nothing
				} else {
					//If not, add the class
					$(this).addClass("open");//CONSIDER MOVING INSIDE THE ANIMATE FUNCTION		
					//Animate the opacity and height of the dd
					$(this).next().animate({opacity: 'toggle', height: 'toggle'}, function(){
						
						//When the animation completes, make the analytics call
						//trackJavaScriptEvent($(this).text(), "accordion");
					});
					
				}
			});
			
			
			//Change the control label
			changeAccordionControlLabel(eventTarget);
			
		} else if (eventAction == "Collapse All") {
			
			//Close the accordion rows
			$(eventTarget).next().find("dt").each(function(){
				//If it's open
				if ($(this).hasClass("open")) {
					//remove the class
					$(this).removeClass("open");//CONSIDER MOVING INSIDE THE ANIMATE FUNCTION
					//Animate the opacity and height of the dd
					$(this).next().animate({opacity: 'toggle', height: 'toggle'}, function(){
						//When the animation completes, make the analytics call
						//trackJavaScriptEvent($(this).text(), "accordion");
					});
				} else {
					//Do nothing
				}
			});
			
			//Change the control label
			changeAccordionControlLabel(eventTarget);
			
		}
	}
	
	//
	function changeAccordionControlLabel(listTarget){
		var rowCounter = 0;
		var totalRows = $(listTarget).next().find('dt').length;
		$(listTarget).next().find('dt').each(function(){
			
			if ($(this).attr("class") == "open") {
				rowCounter++;
			}
			
		});
		
		//if each dt has the class open then change the label to read collapse, if not then label should read expand
		if (rowCounter == totalRows) {
			if ($(listTarget).text() == "Expand All") {
				$(listTarget).fadeOut(6,function(){$(this).text("Collapse All").fadeIn();});
			}
			
		} else {
			if ($(listTarget).text() == "Collapse All") {
				$(listTarget).fadeOut(6,function(){$(this).text("Expand All").fadeIn();});
			}
		}
	}

/* =DEBUGGER
---------------------------------------------------------------------------------------*/

	//Debug Flash/JS
	function debugScript(alertMessage){
		try {
			console.log(alertMessage);
		} catch(err) {
			alert(alertMessage);
		}
	}

/* =CALL EVENTS
---------------------------------------------------------------------------------------*/
	$(document).ready(function(){
   		//Functions to Execute
			//INTERACTIVE ELEMENTS
				publishAccordion();
	});
