/**
 * @author Aaron.Lisman
 */

(function($){

	// extend jquery with the plugin
 	$.fn.merckAccordian = function(options) {		
		return new merckAccordian(this,options);				
	}
	
	// constructor
	function merckAccordian(el,options) {
		this.Init(el);		
	}
	
	// plugin methods and props
	$.extend(merckAccordian.prototype, {
		// props, collections		
		Panes: null,
		Tabs: null,
		TargetEl: null,
		Mode: "autoClose",
		//
		Init: function(el) {
			this.TargetEl = el;
			this.Items = this.TargetEl.children("li");
			this.Panes = this.TargetEl.children("li").children("div");
			this.Tabs = this.TargetEl.children("li").children("h4");
			this.SetStyles();
			this.SetHandlers();
			this.OpenDefault();
		},
		SetHandlers: function() {
			var self = this;
			this.Tabs.click( function() {
				// if its open, close it
				if ($(this).hasClass("module-merckAccordian-accordian-tab-open")) {
					self.ClosePane(this);
				} else { // open it!
					self.OpenPane(this);
				}
			});
			//set hover states
			this.Tabs.hover( 				
				function() {
					self.DetermineMode();
					if (this.Mode != "openMode") {						
						$(this).toggleClass("merckAccordian-tab-over");
					}
				},
				function() {
					self.DetermineMode();
					if (!this.Mode != "openMode") {
						$(this).toggleClass("merckAccordian-tab-over");
					}
				}
			);						
		},
		DetermineMode: function() {		
			// determine mode
			if ($(this.TargetEl).hasClass("leaveOpenMode")) {
				this.Mode = "leaveOpenMode";
			} else if
			($(this.TargetEl).hasClass("openMode")) {
				this.Mode = "openMode";
			} else {
				this.Mode = "autoCloseMode";
			}
		},
		OpenPane: function(el) {	
			this.DetermineMode();	
			if (this.Mode in this.Openers) {
				this.Openers[this.Mode].call(this,el);				
			}
		},
		ClosePane: function(el) {
			var h4 = $(el);			
			h4.removeClass("module-merckAccordian-accordian-tab-open")	
				 .next("div").hide();
			h4.find("a").attr("title",h4.find("a").attr("title").replace("- open","- closed"));		
		},
		SetStyles: function() {
			var self = this;
			
			// add style classes dynamically to avoid repetition in markup
			this.Items.addClass("module-merckAccordian-accordian-item");
			this.Tabs.addClass("module-merckAccordian-accordian");
			this.Tabs.find("a").addClass("accButton");
			
			// add padding where panes DO NOT contain nested modules
			this.Panes.not(":has(.module-merckTabs)").addClass("pane-padded");
			
			// these are the problem lines Jose
			this.Panes.prepend("<img src='includes/audio-video/swf/assets/images/accordian/icon-arrow-up.gif' class='accordianUpDownArrow' />");
			this.Panes.find('.accordianUpDownArrow').click(function() {
				self.ClosePane($(this.parentNode.parentNode).children("h4").get(0));
			});



		},
		Openers: {
			leaveOpenMode:function(el) {
				this.GeneralOpener(el);
			},
			openMode: function(el) {
				// do nothing, they're already open
			},
			autoCloseMode: function(el) {
					this.CloseAll();
					this.GeneralOpener(el);
			}
		},
		GeneralOpener: function(el) {
			var h4 = $(el);
			
			var openFunc = function() {
				h4.toggleClass("module-merckAccordian-accordian-tab-open")
					.next("div").toggle();
				h4.find("a").attr("title",h4.find("a").attr("title").replace("- closed","- open"));		
			}			
			// to fix ie6 background behavior bug, set a small timeout
			setTimeout(openFunc,100);
								
		},
		CloseAll: function() {
			var self = this;
			this.Tabs.each( function() { self.ClosePane(this); })
		},
		OpenDefault: function() {
			var el = this.Items.filter("[open=true]").get(0);
		
			var self = this;
			this.Items.filter("[open=true]").children("h4").each( function() {
				self.GeneralOpener($(this).get(0));	
			});
			
		}
	});
	
})(jQuery);
