//  LauncherOSX
//
//  Created by Boris Schneiderman.
//  Copyright (c) 2014 Readium Foundation and/or its licensees. All rights reserved.
//  
//  Redistribution and use in source and binary forms, with or without modification, 
//  are permitted provided that the following conditions are met:
//  1. Redistributions of source code must retain the above copyright notice, this 
//  list of conditions and the following disclaimer.
//  2. Redistributions in binary form must reproduce the above copyright notice, 
//  this list of conditions and the following disclaimer in the documentation and/or 
//  other materials provided with the distribution.
//  3. Neither the name of the organization nor the names of its contributors may be 
//  used to endorse or promote products derived from this software without specific 
//  prior written permission.
//  
//  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
//  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
//  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
//  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
//  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
//  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
//  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
//  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
//  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
//  OF THE POSSIBILITY OF SUCH DAMAGE.

/**
 *
 * @constructor
 */
ReadiumSDK.Models.Switches = function() {

};

// Description: Parse the epub "switch" tags and hide
// cases that are not supported
ReadiumSDK.Models.Switches.apply = function(dom) {


    // helper method, returns true if a given case node
    // is supported, false otherwise
    function isSupported(caseNode) {

        var ns = caseNode.attributes["required-namespace"];
        if(!ns) {
            // the namespace was not specified, that should
            // never happen, we don't support it then
            console.log("Encountered a case statement with no required-namespace");
            return false;
        }
        // all the xmlns that readium is known to support
        // TODO this is going to require maintenance
        var supportedNamespaces = ["http://www.w3.org/1998/Math/MathML"];
        return _.include(supportedNamespaces, ns);
    }

    $('switch', dom).each( function() {

        // keep track of whether or now we found one
        var found = false;

        $('case', this).each(function() {

            if( !found && isSupported(this) ) {
                found = true; // we found the node, don't remove it
            }
            else {
                $(this).remove(); // remove the node from the dom
//                    $(this).prop("hidden", true);
            }
        });

        if(found) {
            // if we found a supported case, remove the default
            $('default', this).remove();
//                $('default', this).prop("hidden", true);
        }
    })
};

@@ -1874,102 +1874,8 @@
  %7B%0A%0A
-%0A    // helper method, returns true if a given case node%0A    // is supported, false otherwise%0A
     
@@ -2453,16 +2453,22 @@
 aces, ns
+.value
 );%0A    %7D
@@ -2477,40 +2477,273 @@
     
-$('switch', dom).each( function(
+var getQuery = window.navigator.userAgent.indexOf(%22Trident%22) %3E 0%0A        ? function (elementName) %7B return 'epub%5C%5C:' + elementName; %7D%0A        : function (elementName) %7B return elementName; %7D;%0A%0A    _.each(dom.querySelectorAll(getQuery('switch')), function(switchNode
 ) %7B%0A
@@ -2836,29 +2836,62 @@
     
-$('case', this).each(
+_.each(switchNode.querySelectorAll(getQuery('case')), 
 func
@@ -2891,24 +2891,32 @@
 ), function(
+caseNode
 ) %7B%0A%0A       
@@ -2946,20 +2946,24 @@
 pported(
-this
+caseNode
 ) ) %7B%0A  
@@ -3079,20 +3079,24 @@
       $(
-this
+caseNode
 ).remove
@@ -3135,60 +3135,8 @@
 dom%0A
-//                    $(this).prop(%22hidden%22, true);%0A
     
@@ -3137,32 +3137,33 @@
 m%0A            %7D%0A
+%0A
         %7D);%0A%0A   
@@ -3169,16 +3169,17 @@
       if
+ 
 (found) 
@@ -3176,24 +3176,25 @@
 f (found) %7B%0A
+%0A
             
@@ -3261,39 +3261,89 @@
     
-$('default', this).remove();%0A//
+_.each(switchNode.querySelectorAll(getQuery('default')), function(defaultNode) %7B%0A
     
@@ -3360,47 +3360,48 @@
   $(
-'
 default
-', this).prop(%22hidden%22, true
+Node).remove();%0A            %7D
 );%0A
+%0A
     
@@ -3406,18 +3406,20 @@
       %7D%0A
+%0A
     %7D)
+;
 %0A%7D;%0A

Improve switch element query and namespace check