/* highbeam research styleswitcher v1.0 (c) 2003 / justin blecher / digital pulp, inc. inspired by and based on code from http://www.alistapart.com/articles/alternate/ */ // == prefs ==================================================================== var ss_cookie_name = "HBRSTYLE"; var ss_cookie_expiration = 365; // days var ss_naming_convention = ": "; // == constructors ============================================================= // alternate ss object constructor function AltStyleSheets() { // methods this.init = initAltStyleSheets; this.checkAvailableProps = checkAvailableProps; this.savePreferredStyles = savePreferredStyles; this.usePreferredStyles = usePreferredStyles; this.convertActiveStylesToString = convertActiveStylesToString; this.getValueForProperty = getValueForProperty; // properties this.available_stylesheets = Array(); this.available_properties = Array(); } // END: AltStyleSheets() // stylesheet wrapper constructor function StyleSheet(ss_obj) { // properties this.css = ss_obj; // methods this.activate = function() { var ss_name; // turn off other alt css first for (ss_name in alt_css.available_stylesheets) { if (this.getPropertyName() == alt_css.available_stylesheets[ss_name].getPropertyName()) alt_css.available_stylesheets[ss_name].css.disabled = true; } // re-enable this ss this.css.disabled = false; }; this.getPropertyName = function () { return this.css.getAttribute("title").substring(0, this.css.getAttribute("title").indexOf(':')); }; } // END: StyleSheet() // == methods ================================================================== function initAltStyleSheets() { var all_link_tags, css_title; all_link_tags = document.getElementsByTagName("link"); // loop through link tags and pick out the ones we want for (i=0; i < all_link_tags.length; i++) { // if link is a alternate stylesheet if ( (all_link_tags[i].getAttribute("rel").indexOf("style") != -1) && (all_link_tags[i].getAttribute("rel").indexOf("alt") != -1) ) { css_title = all_link_tags[i].getAttribute("title"); if (css_title) { this.available_stylesheets[css_title] = new StyleSheet(all_link_tags[i]); // add the stylesheet to the list this.available_stylesheets[css_title].css.disabled = true; // for ie/win because it thinks that all alt stylesheets are enabled until disabled. this.available_properties[this.available_stylesheets[css_title].getPropertyName()] = true; // to avoid duplicate entires for properties } // end if css title } // end if is css } // end for all links } // end initAltStyleSheets() function getValueForProperty(prop_to_find) { var ss_title; for (ss_title in this.available_stylesheets) { if (ss_title.indexOf(prop_to_find) != -1) { if (this.available_stylesheets[ss_title].css.disabled == false) return ss_title.substring(ss_title.indexOf(' ')+1, ss_title.length); } ss_title = ""; } // end for in loop return false; } // END: getValueForProperty() function checkAvailableProps(prop_to_find) { var prop; for (prop in this.available_properties) { if (prop_to_find == prop) return true; } return false; } // END: checkAvailableProps() function convertActiveStylesToString() { var active_ss = new Array(); for (ss in this.available_stylesheets) { if (!this.available_stylesheets[ss].css.disabled) { active_ss[active_ss.length] = this.available_stylesheets[ss].css.getAttribute("title"); } } return active_ss.join(','); } // END: convertActiveStylesToPrefs() // == functions ================================================================ function changeStyleProperty(prop, value){ var ss_name = prop + ss_naming_convention + value; if (!alt_css.checkAvailableProps(prop)) return false; alt_css.available_stylesheets[ss_name].activate(); return true; } // END: changeStyleProperty() function usePreferredStyles() { var cookie_data, active_ss, parts; cookie_data = unescape(readCookie(ss_cookie_name)); if (cookie_data) { active_ss = cookie_data.split(","); for (var i=0; i< active_ss.length; i++) { parts = active_ss[i].split(ss_naming_convention); changeStyleProperty(parts[0] , parts[1]); parts = null; } } } // END: usePreferredStyles() function savePreferredStyles() { var active_styles, cookie_data; active_styles = alt_css.convertActiveStylesToString(); cookie_data = escape(active_styles); createCookie(ss_cookie_name, cookie_data, ss_cookie_expiration); } // END: savePreferredStyles() // == cookie utility functions ================================================= /* courtesy of ppk and ala... thanks! */ function createCookie(name,value,days) { if (days) { var date = new Date(); date.setTime(date.getTime() + (days*24*60*60*1000)); var expires = "; expires=" + date.toGMTString(); } else expires = ""; document.cookie = name + "=" + value+expires + "; domain=highbeam.com; path=/"; // document.cookie = name + "=" + value+expires + "; path=/"; } // END: createCookie() function readCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for(var i=0;i < ca.length;i++) { var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1, c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); } return null; } // END: readCookie() // == global vars ============================================================== var alt_css = new AltStyleSheets(); alt_css.init(); alt_css.usePreferredStyles(); window.onunload = function(e) { alt_css.savePreferredStyles(); } // END: onunload