home *** CD-ROM | disk | FTP | other *** search
/ Clickx 23 / Clickx 23.iso / DATA / moveabletype / images / sifr.js < prev    next >
Encoding:
JavaScript  |  2005-04-13  |  12.5 KB  |  377 lines

  1. /*    sIFR (Scalable Inman Flash Replacement) Version 2.0 RC1
  2.     Copyright 2004 Mike Davidson, Shaun Inman, Tomas Jogin and Mark Wubben
  3.  
  4.     This software is licensed under the CC-GNU LGPL <http://creativecommons.org/licenses/LGPL/2.1/>
  5. */
  6.  
  7. var hasFlash = function(){
  8.     var nRequiredVersion = 6;    
  9.     
  10.     if(navigator.appVersion.indexOf("MSIE") != -1 && navigator.appVersion.indexOf("Windows") > -1){
  11.         document.write('<script language="VBScript"\> \n');
  12.         document.write('on error resume next \n');
  13.         document.write('hasFlash = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash." & ' + nRequiredVersion + '))) \n');  
  14.         document.write('<'+'/script\> \n');
  15.         /*    If executed, the VBScript above checks for Flash and sets the hasFlash variable. 
  16.             If VBScript is not supported it's value will still be undefined, so we'll run it though another test
  17.             This will make sure even Opera identified as IE will be tested */
  18.         if(window.hasFlash != null){
  19.             return window.hasFlash;
  20.         };
  21.     };
  22.     
  23.     if(navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"] && navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin){
  24.         var flashDescription = (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]).description;
  25.         var flashVersion = parseInt(flashDescription.charAt(flashDescription.indexOf(".") - 1));
  26.         return flashVersion >= nRequiredVersion;
  27.     };
  28.     
  29.     return false;
  30. }();
  31.  
  32. String.prototype.normalize = function(){
  33.     return this.replace(/\s+/g, " ");
  34. };
  35.  
  36. /* IE 5.0 does not support the push method, so here goes */
  37. if(Array.prototype.push == null){
  38.     Array.prototype.push = function(item){
  39.         this[this.length] = item;
  40.         return this.length;
  41.     };
  42. };
  43.  
  44. /*    Implement function.apply for browsers which don't support it natively
  45.     Courtesy of Aaron Boodman - http://youngpup.net */
  46. if (!Function.prototype.apply){
  47.     Function.prototype.apply = function(oScope, args) {
  48.         var sarg = [];
  49.         var rtrn, call;
  50.  
  51.         if (!oScope) oScope = window;
  52.         if (!args) args = [];
  53.  
  54.         for (var i = 0; i < args.length; i++) {
  55.             sarg[i] = "args["+i+"]";
  56.         };
  57.  
  58.         call = "oScope.__applyTemp__(" + sarg.join(",") + ");";
  59.  
  60.         oScope.__applyTemp__ = this;
  61.         rtrn = eval(call);
  62.         oScope.__applyTemp__ = null;
  63.         return rtrn;
  64.     };
  65. };
  66.  
  67. /*    The following code parses CSS selectors.
  68.     This script however is not the right place to explain it,
  69.     please visit the documentation for more information. */
  70. var parseSelector = function(){
  71.     var reParseSelector = /^([^#\.>\`]*)(#|\.|\>|\`)(.+)$/;
  72.     function parseSelector(sSelector, oParentNode, sMode){
  73.         sSelector = sSelector.replace(" ", "`");
  74.         var selector = sSelector.match(reParseSelector);
  75.         var node, listNodes, listSubNodes, subselector;
  76.         var listReturn = [];
  77.  
  78.         if(selector == null){ selector = [sSelector, sSelector]    };
  79.         if(selector[1] == ""){ selector[1] = "*" };
  80.         if(sMode == null){ sMode = "`" };
  81.         
  82.         switch(selector[2]){
  83.             case "#":
  84.                 subselector = selector[3].match(reParseSelector);
  85.                 if(subselector == null){ subselector = [null, selector[3]] };
  86.                 node =     document.getElementById(subselector[1]);
  87.                 if(node == null || (selector[1] != "*" && node.nodeName.toLowerCase() != selector[1].toLowerCase())){
  88.                     return listReturn;
  89.                 };
  90.                 if(subselector.length == 2){
  91.                     listReturn.push(node);
  92.                     return listReturn;    
  93.                 };
  94.                 return parseSelector(subselector[3], node, "#");
  95.             case ".":
  96.                 if(sMode == "`"){
  97.                     listNodes = getElementsByTagName(oParentNode, selector[1]);
  98.                 } else {
  99.                     listNodes = oParentNode.childNodes;
  100.                 };
  101.                 
  102.                 for(var i = 0; i < listNodes.length; i++){
  103.                     node = listNodes[i];
  104.                     if(node.nodeType != 1){
  105.                         continue;    
  106.                     };
  107.  
  108.                     subselector = selector[3].match(reParseSelector);
  109.                     if(subselector != null){
  110.                         if(node.className.match("\\b" + subselector[1] + "\\b") == null){
  111.                             continue;
  112.                         };
  113.                         listSubNodes = parseSelector(subselector[3], node, subselector[2]);
  114.                         listReturn = listReturn.concat(listSubNodes);    
  115.                     } else if(node.className.match("\\b" + selector[3] + "\\b") != null){
  116.                         listReturn.push(node);
  117.                     };
  118.                 };
  119.                 return listReturn;
  120.             case ">":
  121.                 if(sMode == "`"){
  122.                     listNodes = getElementsByTagName(oParentNode, selector[1]);
  123.                 } else {
  124.                     listNodes = oParentNode.childNodes;
  125.                 };
  126.                 for(var i = 0; i < listNodes.length; i++){
  127.                     node = listNodes[i];
  128.                     if(node.nodeType != 1){
  129.                         continue;    
  130.                     };
  131.                     if(node.nodeName.toLowerCase() != selector[1].toLowerCase()){
  132.                         continue;
  133.                     };
  134.                     listSubNodes = parseSelector(selector[3], node, ">");
  135.                     listReturn = listReturn.concat(listSubNodes);    
  136.                 };
  137.                 return listReturn;
  138.             case "`":
  139.                 listNodes = getElementsByTagName(oParentNode, selector[1]);
  140.                 for(var i = 0; i < listNodes.length; i++){
  141.                     node = listNodes[i];
  142.                     listSubNodes = parseSelector(selector[3], node, "`");
  143.                     listReturn = listReturn.concat(listSubNodes);    
  144.                 };
  145.                 return listReturn;
  146.             default:
  147.                 listNodes = getElementsByTagName(oParentNode, selector[0]);
  148.                 for(var i = 0; i < listNodes.length; i++){
  149.                     listReturn.push(listNodes[i]);
  150.                 };
  151.                 return listReturn;
  152.         };
  153.     };
  154.     
  155.     function getElementsByTagName(oParentNode, sTagName){
  156.         /*    IE5.x does not support document.getElementsByTagName("*")
  157.             therefore we're resorting to element.all */
  158.         if(sTagName == "*" && oParentNode.all != null){
  159.             return oParentNode.all;
  160.         };
  161.         return oParentNode.getElementsByTagName(sTagName);
  162.     };
  163.     
  164.     return parseSelector;
  165. }();
  166.  
  167. /*    Executes an anonymous function which returns the function sIFR (defined inside the function).
  168.     You can replace elements using sIFR.replaceElement()
  169.     All other variables and methods you see are private. If you want to understand how this works you should
  170.     learn more about the variable-scope in JavaScript. */
  171. var sIFR = function(){
  172.     if(window.hasFlash == false || !document.createElement || !document.getElementById){ return function(){return false} };
  173.  
  174.     /* Providing a hook for you to hide certain elements if Flash has been detected */
  175.     if(document.documentElement){
  176.         document.documentElement.className = document.documentElement.className.normalize() + (document.documentElement.className == "" ? "" : " ") + "sIFR-hasFlash";
  177.     };
  178.  
  179.     /* Opera and Mozilla require a namespace when creating elements in an XML page */
  180.     var sNameSpaceURI = "http://www.w3.org/1999/xhtml";
  181.     var UA = function(){ 
  182.         var sUA = navigator.userAgent.toLowerCase();
  183.         var oReturn =  {
  184.             bIsKHTML: sUA.indexOf('safari') > -1 || sUA.indexOf('konqueror') > -1 || sUA.indexOf('omniweb') > -1,
  185.             bIsOpera : sUA.indexOf('opera') > -1,
  186.             bIsGecko : navigator.product != null && navigator.product.toLowerCase() == 'gecko',
  187.             bIsXML : document.contentType != null && document.contentType.indexOf('xml') > -1
  188.         };
  189.         oReturn.bIsIE = sUA.indexOf('msie') > -1 && ! oReturn.bIsOpera && !oReturn.bIsKHTML && !oReturn.bIsGecko;
  190.         return oReturn;
  191.     }();
  192.  
  193.     var bIsInitialized = false;
  194.     var stackReplaceElementArguments = [];
  195.     
  196.     function fetchContent(oNode, oNewNode, sCase){
  197.         var sContent = "";
  198.         var oSearch = oNode.firstChild;
  199.         var oRemove, oRemovedNode, oTarget;
  200.  
  201.         while(oSearch){
  202.             if(oSearch.nodeType == 3){
  203.                 switch(sCase){
  204.                     case "lower":
  205.                         sContent += oSearch.nodeValue.toLowerCase();
  206.                         break;
  207.                     case "upper":
  208.                         sContent += oSearch.nodeValue.toUpperCase();
  209.                         break;
  210.                     default:
  211.                         sContent += oSearch.nodeValue;
  212.                 };
  213.             } else if(oSearch.nodeType == 1){
  214.                 if(oSearch.nodeName.toLowerCase() == "a"){
  215.                     if(oSearch.getAttribute("target")){
  216.                         oTarget = oSearch.getAttribute("target");
  217.                     } else {
  218.                         oTarget = "";
  219.                     };
  220.                     sContent += '<a href="' + oSearch.getAttribute("href") + '" target="' + oTarget + '">';
  221.                 };                
  222.                 if(oSearch.hasChildNodes){
  223.                     sContent += fetchContent(oSearch, null, sCase);
  224.                 };
  225.                 if(oSearch.nodeName.toLowerCase() == "a"){
  226.                     sContent += "</a>";
  227.                 };
  228.             };
  229.             oRemove = oSearch;
  230.             oSearch = oSearch.nextSibling;
  231.             if(oNewNode != null){
  232.                 oRemovedNode = oRemove.parentNode.removeChild(oRemove);
  233.                 oNewNode.appendChild(oRemovedNode);    
  234.             };
  235.         };
  236.         return sContent;
  237.     };
  238.     
  239.     function createElement(sTagName){
  240.         if(document.createElementNS){
  241.             return document.createElementNS(sNameSpaceURI, sTagName);    
  242.         } else {
  243.             return document.createElement(sTagName);
  244.         };
  245.     };
  246.  
  247.     function createObjectParameter(nodeObject, sName, sValue){
  248.         var node = createElement("param");
  249.         node.setAttribute("name", sName);    
  250.         node.setAttribute("value", sValue);
  251.         nodeObject.appendChild(node);
  252.     };
  253.     
  254.     function replaceElement(sSelector, sFlashSrc, sColor, sLinkColor, sHoverColor, sBgColor, nPaddingTop, nPaddingRight, nPaddingBottom, nPaddingLeft, sFlashVars, sCase){
  255.         if(!mayReplace()){
  256.             return stackReplaceElementArguments.push(arguments);    
  257.         };
  258.         
  259.         if(sFlashVars != null){
  260.             sFlashVars = "&" + sFlashVars.normalize();
  261.         } else {
  262.             sFlashVars = "";    
  263.         };
  264.         
  265.         var sWmode = (sBgColor == "transparent") ? "transparent" : "opaque";
  266.         var node, sWidth, sHeight, sMargin, sPadding, sText, sVars, nodeAlternate, nodeFlash;
  267.         var listNodes = parseSelector(sSelector, document);
  268.         if(listNodes.length == 0){ return false };
  269.  
  270.         for(var i = 0; i < listNodes.length; i++){
  271.             node = listNodes[i];
  272.             
  273.             /* Prevents elements from being replaced multiple times. */
  274.             if(node.className.match(/\bsIFR\-replaced\b/) != null){ continue; };
  275.  
  276.             sWidth = node.offsetWidth - nPaddingLeft - nPaddingRight;
  277.             sHeight = node.offsetHeight - nPaddingTop - nPaddingBottom;
  278.  
  279.             nodeAlternate = createElement("span");
  280.             nodeAlternate.className = "sIFR-alternate";
  281.  
  282.             sText = fetchContent(node, nodeAlternate, sCase);
  283.             sText = sText.replace(/%\d{0}/g, "%25");
  284.             sText = sText.replace(/\+/g, "%2B");
  285.             sText = sText.replace(/&/g, "%26");
  286.             sText = sText.replace(/\"/g, "%22");
  287.             sText = sText.normalize();
  288.  
  289.             sVars = "txt=" + sText + sFlashVars + "&w=" + sWidth + "&h=" + sHeight;
  290.             if (sColor != null){sVars += "&textcolor=" + sColor};
  291.             if (sLinkColor != null){sVars += "&linkcolor=" + sLinkColor}; 
  292.             if (sHoverColor != null){sVars += "&hovercolor=" + sHoverColor};
  293.             
  294.             node.className = node.className.normalize() + (node.className == ""  ? "" : " ") + "sIFR-replaced";
  295.  
  296.             /*    Opera only supports the object element, other browsers are given the embed element,
  297.                 for backwards compatibility reasons between different browser versions. */
  298.             if(UA.bIsOpera){
  299.                 nodeFlash = createElement("object");
  300.                 nodeFlash.setAttribute("type", "application/x-shockwave-flash");
  301.                 nodeFlash.setAttribute("data", sFlashSrc);
  302.                 createObjectParameter(nodeFlash, "quality", "high");
  303.                 createObjectParameter(nodeFlash, "wmode", sWmode);
  304.                 createObjectParameter(nodeFlash, "bgcolor", sBgColor);
  305.                 createObjectParameter(nodeFlash, "flashvars", sVars);
  306.             } else {
  307.                 nodeFlash = createElement("embed");
  308.                 nodeFlash.setAttribute("src", sFlashSrc);
  309.                 nodeFlash.setAttribute("flashvars", sVars);
  310.                 nodeFlash.setAttribute("type", "application/x-shockwave-flash");
  311.                 nodeFlash.setAttribute("pluginspage", "http://www.macromedia.com/go/getflashplayer");
  312.                 nodeFlash.setAttribute("wmode", sWmode);
  313.                 nodeFlash.setAttribute("bgcolor", sBgColor);
  314.             };
  315.             nodeFlash.className = "sIFR-flash";
  316.             nodeFlash.setAttribute("width", sWidth);
  317.             nodeFlash.setAttribute("height", sHeight);
  318.             nodeFlash.style.width = sWidth + "px";
  319.             nodeFlash.style.height = sHeight + "px";
  320.             node.appendChild(nodeFlash);
  321.                             
  322.             node.appendChild(nodeAlternate);
  323.             
  324.             /*    Workaround to force KHTML-browsers to repaint the document. 
  325.                 Additionally, IE for both Mac and PC need this.
  326.                 See: http://neo.dzygn.com/archive/2004/09/forcing-safari-to-repaint */
  327.  
  328.             if(UA.bIsKHTML || UA.bIsIE){
  329.                 node.innerHTML += "";
  330.             };
  331.         };
  332.     };
  333.     
  334.     function mayReplace(e){
  335.         if(((UA.bIsXML && UA.bIsGecko || UA.bIsKHTML) && e == null && bIsInitialized == false) || document.getElementsByTagName("body").length == 0){
  336.             return false;
  337.         };
  338.         return true;
  339.     };
  340.     
  341.     function sIFR(e){
  342.         if((!sIFR.bAutoInit && (window.event || e) != null) || !mayReplace(e)){
  343.             return;    
  344.         };
  345.         bIsInitialized = true;
  346.  
  347.         for(var i = 0; i < stackReplaceElementArguments.length; i++){
  348.             replaceElement.apply(null, stackReplaceElementArguments[i]);
  349.         };
  350.         stackReplaceElementArguments = [];
  351.     };
  352.  
  353.     sIFR.replaceElement = replaceElement;
  354.     sIFR.UA = UA;
  355.     sIFR.bAutoInit = true;
  356.     
  357.     if(window.attachEvent){
  358.         window.attachEvent("onload", sIFR);
  359.     } else if(document.addEventListener || window.addEventListener){
  360.         if(document.addEventListener){
  361.             document.addEventListener("load", sIFR, false);    
  362.         };
  363.         if(window.addEventListener){
  364.             window.addEventListener("load", sIFR, false);    
  365.         };
  366.     } else {
  367.         if(typeof window.onload == "function"){
  368.             var fOld = window.onload;
  369.             window.onload = function(){ fOld(); sIFR(); };
  370.         } else {
  371.             window.onload = sIFR;
  372.         };
  373.     };
  374.  
  375.     return sIFR;
  376. }();
  377.