/*
WebKeg Css Switcher 1.0
*/

function replaceCssFile(targetFile, newfilename, cssType)
    {           
         var filetype = 'css';
            //http://www.javascriptkit.com/javatutors/loadjavascriptcss2.shtml
         var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none" //determine element type to create nodelist using
         var targetattr=(filetype=="js")? "src" : (filetype=="css")? "href" : "none" //determine corresponding attribute to test for
         var allsuspects=document.getElementsByTagName(targetelement)
         
         targetFile = targetFile + '.css';
         
         for (var i=allsuspects.length; i>=0; i--)
         { 
             if(allsuspects[i] && allsuspects[i].getAttribute(targetattr)!=null)
             {
                var href = allsuspects[i].getAttribute(targetattr) ;
             
                var indexOfName = allsuspects[i].getAttribute(targetattr).indexOf(targetFile);        
                if(indexOfName>-1)
                {
                    var newelement=createCssfile(newfilename, cssType)
                    allsuspects[i].parentNode.replaceChild(newelement, allsuspects[i])                    
                }
                
                var indexOfCssType = allsuspects[i].getAttribute(targetattr).indexOf(cssType);//this is used if we have alreday changed the css
                if(indexOfCssType>-1)
                {
                    var newelement = createCssfile(newfilename, cssType);
                    allsuspects[i].parentNode.replaceChild(newelement, allsuspects[i])
                }                
             }           
         }
    }
    
    function createCssfile(filename, csstype)
    {     
      var fileref=document.createElement("link")
      fileref.setAttribute("rel", "stylesheet")
      fileref.setAttribute("type", "text/css")
      fileref.setAttribute("href", 'http://localhost/webkegcms/wkskins/' + csstype +'/'+ filename )
           
     return fileref
    }
