var ThemeSwitcher = Class.extend({ ePRELOAD_COMPLETE : 'ePRELOAD_COMPLETE', // *** _domElement : null, _targetClassName : null, _currentTheme : null, _nextThemeStack : null, _themes : null, _imagePath : null, _imageType : null, _switchMs : null, _switchRunning : null, _autoSwitchDelayMs : null, _autoSwitchDelayTimer : null, init : function(config, autoStart){ if( jQuery.isPlainObject(config) && (typeof config.imagePath == 'string') && (jQuery.isArray(config.themes) && (config.themes.length > 0)) /*(typeof config == 'object') && (typeof config.imagePath == 'string') && (config.themes.length > 0)*/ ){ this._domElement = (typeof config.domElement == 'string') ? jQuery(config.domElement) : jQuery('body:first'); this._targetClassName = (typeof config.targetClassName == 'string') ? config.targetClassName : 'theme'; this._themes = config.themes; this._imagePath = config.imagePath; this._imageType = (typeof config.imageType == 'string') ? config.imageType : 'png'; this._switchMs = (typeof config.switchMs == 'number') ? config.switchMs : 250; this._switchRunning = false; this._autoSwitchDelayMs = (typeof config.autoSwitchDelayMs == 'number') ? config.autoSwitchDelayMs : 0; autoStart = ((autoStart === undefined) || (autoStart === true)) ? true : false; if( autoStart ){ this.start(); } } else { throw 'cannot initialize ThemeSwitcher, missing params or wrong param type(s)'; } }, // *** // xxx destroy : function(){ this._switchRunning = true; this.stopAutoSwitching(); }, // xxx //--|FUNCTIONALITY---------- start : function(){ this.switchTo(this._themes[0], 0); this.startAutoSwitching(); }, switchToNext : function(){ this.switchTo(); }, switchTo : function(themeName, ms){ if( (themeName === undefined) || ((jQuery.inArray('' + themeName, this._themes) != -1) && (themeName != this._currentTheme)) ){ if( !this._switchRunning ){ this._switchRunning = true; var that = this; if( themeName === undefined ){ if( this._currentTheme == null ){ themeName = this._themes[0]; } else { themeName = this._themes[(jQuery.inArray(this._currentTheme, this._themes) + 1) % this._themes.length]; } } if( ms === undefined ){ ms = this._switchMs; } this._currentTheme = '' + themeName; var oldElements = this._domElement.find('.' + this._targetClassName); var newElements = null; var elementsToLoad = oldElements.length; var loadedElements = 0; oldElements.each(function(){ var themeFileInfo = that.parseThemeFileInfo(jQuery(this).attr('class'), that._targetClassName); if( themeFileInfo.themeName !== null ){ var themeFileExtension = (themeFileInfo.themeFileExtension !== null) ? themeFileInfo.themeFileExtension : that._imageType; var newImageUrl = that._imagePath + that._currentTheme + '/' + themeFileInfo.themeName + '.' + themeFileExtension; var hasSrc = (jQuery(this).attr('src') !== undefined); var hasBackgroundImage = (jQuery(this).css('background-image') != 'none'); var newElement = jQuery(this).clone(); newElement.hide(); jQuery(this).after(newElement); if( newElements == null ){ newElements = newElement; } else { newElements = newElements.add(newElement); } if( hasSrc || hasBackgroundImage ){ var loader = new Image(); loader.onload = function(){ if( hasSrc ){ newElement.attr('src', newImageUrl); if ( jQuery.browser.msie ) { /*newElement.get(0).style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',sizingMethod='image',src='"+ newElement.attr('src') +"')";*/ /*newElement.css("Filter", "progid: DXImageTransform.Microsoft.AlphaImageLoader(enabled='true', sizingMethod='image', src='"+ newElement.attr('src') +"')");*/ } } if( hasBackgroundImage ){ newElement.css('background-image', 'url(' + newImageUrl + ')'); } loadedElements++; if( loadedElements == elementsToLoad){ oldElements.fadeOut(ms); var completedNewElements = 0; newElements.fadeIn(ms, function(){ completedNewElements++; if( completedNewElements == newElements.length ){ oldElements.remove(); that._switchRunning = false; if( that._nextThemeStack != null ){ var nextThemeName = that._nextThemeStack; that._nextThemeStack = null; that.switchTo(nextThemeName, ms); } } }); } }; loader.onerror = function(){ throw 'cannot execute switch, an img-url is incorrect: ' + this.src; }; loader.src = newImageUrl; } else { elementsToLoad--; } } else { elementsToLoad--; } }); } else { this._nextThemeStack = themeName; } } }, startAutoSwitching : function(){ if( this._autoSwitchDelayMs > 0 ){ this._autoSwitchDelayTimer = window.setInterval(jQuery.proxy(this.switchToNext, this), this._autoSwitchDelayMs); } }, stopAutoSwitching : function(){ if( this._autoSwitchDelayTimer != null ){ window.clearInterval(this._autoSwitchDelayTimer); this._autoSwitchDelayTimer = null; } }, preload : function(){ var that = this; var elements = this._domElement.find('.' + this._targetClassName); var loadedElements = 0; for( var i = 0; i < this._themes.length; i++ ){ elements.each(function(){ var themeFileInfo = that.parseThemeFileInfo(jQuery(this).attr('class'), that._targetClassName); if( themeFileInfo.themeName !== null ){ var loadHandler = function(){ loadedElements++; if( loadedElements == elements.length ){ jQuery(that).trigger(that.ePRELOAD_COMPLETE); } }; var themeFileExtension = (themeFileInfo.themeFileExtension !== null) ? themeFileInfo.themeFileExtension : that._imageType; var newImageUrl = that._imagePath + that._themes[i] + '/' + themeFileInfo.themeName + '.' + themeFileExtension; var loader = new Image(); loader.onload = function(){ loadHandler(); }; loader.onerror = function(){ loadHandler(); throw 'cannot properly preload ThemeSwitcher, an img-url is incorrect: ' + this.src; }; loader.src = newImageUrl; } }); } }, parseThemeFileInfo : function(classString, targetClassName){ var nameClassRex = new RegExp('\\b' + targetClassName + 'name-([a-z\-_]+)\\b', 'gi'); var nameClassInfo = nameClassRex.exec(classString); var extClassRex = new RegExp('\\b' + targetClassName + 'extension-([a-z\-_]+)\\b', 'gi'); var extClassInfo = extClassRex.exec(classString); return { themeName : ( (nameClassInfo !== null) && (nameClassInfo.length >= 2) ) ? nameClassInfo[1] : null , themeFileExtension : ( (extClassInfo !== null) && (extClassInfo.length >= 2) ) ? extClassInfo[1] : null }; } });