/**
 * Publicis.ThirdParty.Spry.TabbedPanel is observable and more
 * needs Spry.Widget.TabbedPanels (SpryTabbedPanels.js) for working, which must be included before the publicis framework
 * @namespace Publicis.ThirdParty.Spry
 * @class  Publicis.ThirdParty.Spry.TabbedPanel
 * @extends Spry.Widget.TabbedPanels
 * @constructor
 * Creates a new Publicis.ThirdParty.Spry.TabbedPanel Instance
 * @param {Object} cfg Configuration options
 * @author Alexis Dorn
 */

 Publicis.ThirdParty.Spry.TabbedPanel = function (cfg){
	/**
	 * @cfg {DOMNode|String} element <b>Obligatory:</b> The first parameter of parent constructor, element or element id of widget container 
	 */
	/**
	 * @cfg {Object} options <b>Obligatory:</b> The second parameter of parent constructor, configuration options 
	 */
	Publicis.ThirdParty.Spry.TabbedPanel.superclass.constructor.call(this, cfg.element, cfg.options);
	this.pubObserver = new Publicis.Observable({});
	this.pubObserver.registerEvent([
		/**
		 * @event tabclick
		 * Fires when tab has been clicked
		 * @param {Publicis.ThirdParty.Spry.TabbedPanel} this
		 * @param (DOMNode) node the li DOM Element
		 * @param (String) value the inner html of the element
		 */
		"tabclick"
	]);
	Publicis.ThirdParty.Spry.SpryMgr.register(this);
};
 
Publicis.extend(Publicis.ThirdParty.Spry.TabbedPanel, Spry.Widget.TabbedPanels, {
	  /**
	   * Extending parent method by fireing event click
	   * @param {Event} e
	   * @param {DOMNode} tab the LI DOMElement
	   * @private
	   */
	onTabClick: function(e, tab){
		var fb = Publicis.ThirdParty.Spry.TabbedPanel.superclass.onTabClick.call(this, e, tab),
			content = tab.innerHTML,
			R_INNERTEXT = />.*(?=<.*)/;
		 
		if( ( /[^a-zA-Z0-9]/).test(content) && R_INNERTEXT.test( content ) ) {
			try {
				content = R_INNERTEXT.exec( content )[ 0 ].substr( 1 );
			} catch( e ) {
				content = tab.innerHTML;
			}
		}
        if( typeof Magnifier !== "undefined" ) {
            var magnifier = Magnifier.getInstance();
            if( magnifier != null ) {
                magnifier.closeCurrent();
            }
        }
		this.pubObserver.fireEvent("tabclick", this, tab, content );
		return fb;
	 },
	/**
	 * Activates a panel by given content string
	 * @param {String} content
	 */
	avtivatePanelByContent: function (content){
		var tabs = this.getTabs();
		content = content.strip(); 
		for (var i = 0; i < tabs.length; i++){
			if( Publicis.innerText( tabs[i] ) == content ){
				this.showPanel(tabs[i]);
				break;
			}
		}
	},
	/**
	 * Adds a callback to a event
	 * @param {String} eventname
	 * @param {Function} callback The function that will be called as callback
	 * @param {Object} scope The this scope in callback function
	 */
	addListener: function (eventname, callback, scope){
		this.pubObserver.addListener(eventname, callback, scope);
	},
	/**
	 * Disables firing of all Events, until you will call enableEvents again 
	 */
	disableEvents: function (){
		this.pubObserver.disableEvents();
	},
	/**
	 * Enables firing of all Events, after you disableEvents has been called 
	 */
	enableEvents: function (){
		this.pubObserver.enableEvents();
	}
 });
 
 /**
 * Publicis.ThirdParty.Spry.Accordion is observable and more
 * needs Spry.Widget.Accordion (SpryTabbedPanels.js) for working, which must be included before the publicis framework
 * @namespace Publicis.ThirdParty.Spry
 * @class  Publicis.ThirdParty.Spry.Accordion
 * @extends Spry.Widget.Accordion
 * @constructor
 * Creates a new Publicis.ThirdParty.Spry.Accordion Instance
 * @param {Object} cfg Configuration options
 * @author Alexis Dorn
 */

Publicis.ThirdParty.Spry.Accordion = function (cfg){
	/**
	 * @cfg {DOMNode|String} element <b>Obligatory:</b> The first parameter of parent constructor, element or element id of widget container 
	 */
	/**
	 * @cfg {Object} options <b>Obligatory:</b> The second parameter of parent constructor, configuration options 
	 */
	Publicis.ThirdParty.Spry.Accordion.superclass.constructor.call(this, cfg.element, cfg.options);
	this.pubObserver = new Publicis.Observable({});
	this.pubObserver.registerEvent([
		/**
		 * @event tabclick
		 * Fires when tab has been clicked
		 * @param {Publicis.ThirdParty.Spry.Accordion} this
		 * @param (DOMNode) node the div DOM Element
		 * @param (String) value the inner html of the element
		 */
		"tabclick"
	]);
	Publicis.ThirdParty.Spry.SpryMgr.register(this);
};
 
Publicis.extend(Publicis.ThirdParty.Spry.Accordion, Spry.Widget.Accordion, {
   
	/**
	 * adds event tabclick
	 * @param {Object} e
	 * @param {Object} panel
	 * @private
	 */
	onPanelTabClick: function(e, panel){
		var fb = Publicis.ThirdParty.Spry.Accordion.superclass.onPanelTabClick.call(this, e, panel);
		var tab = this.getPanelTab( panel ),
			content = Publicis.innerText( tab );
		 
		this.pubObserver.fireEvent( "tabclick", this, tab, content );
		return fb;
	},
	/**
	 * Activates a panel by given content string
	 * @param {String} content
	 */
	avtivatePanelByContent: function(content){
		var panels = this.getPanels(), tab;
		content = content.strip();
		
		for (var i = 0; i < panels.length; i++){
			tab = this.getPanelTab(panels[i])
			
			if( Publicis.innerText( tab ) == content ){
				this.openPanel( panels[ i ] );
				break;
			}
		}
	 },
	/**
	 * Adds a callback to a event
	 * @param {String} eventname
	 * @param {Function} callback The function that will be called as callback
	 * @param {Object} scope The this scope in callback function
	 */
	addListener: function (eventname, callback, scope){
		this.pubObserver.addListener(eventname, callback, scope);
	},
	/**
	 * Disables firing of all Events, until you will call enableEvents again 
	 */
	disableEvents: function (){
		this.pubObserver.disableEvents();
	},
	/**
	 * Enables firing of all Events, after you disableEvents has been called 
	 */
	enableEvents: function (){
		this.pubObserver.enableEvents();
	}
}); 

/**
 * Registers all existing Publicis.ThirdParty.Spry.Accordion and Publicis.ThirdParty.Spry.TabbedPanel instances 
 * @class Publicis.ThirdParty.Spry.SpryMgr
 * @singleton
 * @namespace Publicis.ThirdParty.Spry
 * @constructor
 * @author Alexis Dorn
 */
Publicis.ThirdParty.Spry.SpryMgr = function (){
	var spryComps = [];
	return {
		/**
		 * Gets all registered instances as an array
		 * @return Array
		 */
		getAll: function (){
			return spryComps;
		},
		/**
		 * Register an instance, should be calles in constructor of any component
		 * @param {Publicis.ThirdParty.Spry.Accordion|Publicis.ThirdParty.Spry.TabbedPanel} cmp
		 */
		register: function (cmp){
			spryComps.push(cmp);
		}
	}
}()

