function getXMLNodeSerialisation(xmlNode) {
  var text = false;
  try {
    // Gecko-based browsers, Safari, Opera.
    var serializer = new XMLSerializer();
    text = serializer.serializeToString(xmlNode);
  }
  catch (e) {
    try {
      // Internet Explorer.
      text = xmlNode.xml;
    }
    catch (e) {}
  }
  return text;
}

var Cookie = {
  set: function(name, value, daysToExpire) {
    var expire = '';
    if (daysToExpire != undefined) {
      var d = new Date();
      d.setTime(d.getTime() + (86400000 * parseFloat(daysToExpire)));
      expire = '; expires=' + d.toGMTString();
    }
    return (document.cookie = escape(name) + '=' + escape(value || '') + expire);
  },
  get: function(name) {
    var cookie = document.cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]*)'));
    return (cookie ? unescape(cookie[2]) : null);
  },
  erase: function(name) {
    var cookie = Cookie.get(name) || true;
    Cookie.set(name, '', -1);
    return cookie;
  },
  accept: function() {
    if (typeof navigator.cookieEnabled == 'boolean') {
      return navigator.cookieEnabled;
    }
  }
};


function clickElement(e){
    
    if (typeof e == 'object') {
        /*if(typeof e.click != 'undefined') {
            e.click();
            alert('click');
            return false;
        }
        else */
        if(document.createEvent) {
            var evObj = document.createEvent('MouseEvents');
            evObj.initEvent('click',true,true);
            e.dispatchEvent(evObj);
           //alert('createEvent');
            return false;
        }
        else if(document.createEventObject) {
            e.fireEvent('onclick');
           // alert('createEventObject');
            return false;
        }
        else {
            e.click();
           // alert('click');
            return false;
        }
    }
}



if (typeof DOMParser == "undefined") {
   DOMParser = function () {}

   DOMParser.prototype.parseFromString = function (str, contentType) {
      if (typeof ActiveXObject != "undefined") {
         var d = new ActiveXObject("MSXML.DomDocument");
         d.loadXML(str);
         return d;
      } else if (typeof XMLHttpRequest != "undefined") {
         var req = new XMLHttpRequest;
         req.open("GET", "data:" + (contentType || "application/xml") +
                         ";charset=utf-8," + encodeURIComponent(str), false);
         if (req.overrideMimeType) {
            req.overrideMimeType(contentType);
         }
         req.send(null);
         return req.responseXML;
      }
   }
}


var Navigation = Class.create();
Navigation.prototype = {
  	// Constructor
  	initialize: function(divId) {
		this.div = $(divId);
		this.current_ul = new Array();
		this.expand = this._expand.bindAsEventListener(this);
		this.hide = this._hide.bindAsEventListener(this);
		this.level = this._level.bindAsEventListener(this);
		this.highlight_img = this._highlight_img.bindAsEventListener(this);
		this.unhighlight_img = this._unhighlight_img.bindAsEventListener(this);
		this.unhighlight_all_imgs = this._unhighlight_all_imgs.bindAsEventListener(this);
		this.set_current_image = this._set_current_image.bindAsEventListener(this);
		
		var oClass = this;
		this.div.getElementsBySelector("a").each(function(a){
			a.observe("click", function(){
				li = a.up("li");
				ul = li.down("ul");
				img = a.down("img");

				if (img)	{ 
					oClass.unhighlight_all_imgs();
					oClass.set_current_image(img)
				}
				//if (ul) oClass.expand(ul)
			});
		});
		
		this.div.getElementsBySelector("img").each(function(img){
			img.observe("mouseover", function(){
				oClass.highlight_img(img)
			});
			img.observe("mouseout", function(){
				oClass.unhighlight_img(img)
			});
		});		
		
		this.div.getElementsBySelector("ul.current").each(function(ul){
			oClass.current_ul[oClass.level(ul)] = ul;
		});
		
		this.div.getElementsBySelector("li.active img").each(function(img){
			oClass.set_current_image(img)
		});
		
	},
	
	_highlight_img: function(img) {
		if (img.src.indexOf("_ro.gif") < 0)
			img.src = img.src.gsub(".gif", "_ro.gif")
	},	
	
	_unhighlight_img: function(img) {
		if ((this.current_img == null || this.current_img != img) && img.src.indexOf("about") < 0 )
			img.src = img.src.gsub("_ro.gif", ".gif")
	},
	
	_unhighlight_all_imgs: function() {
		this.current_img = null;
		var oClass = this;
		this.div.getElementsBySelector("img").each(function(el){
			oClass.unhighlight_img(el)
		});
	},
	
	_expand: function(ul) {
		var level = this.level(ul)
		
		if (this.current_ul[level]) this.hide(this.current_ul[level]);	
		
		this.current_ul[level] = ul;
		Effect.BlindRight(ul, {duration:0.25, queue: {position: 'end', scope: 'navigation', limit:2}});
	},
	
	_hide: function(ul) {
		Effect.BlindLeft(ul, {duration:0.25, queue: {position: 'end', scope: 'navigation', limit:2}, afterFinish:function(){
			ul.getElementsBySelector("ul").each(function(child_ul){
				Element.hide(child_ul);
			});
		}});
	},
	
	_level: function(ul) {
		var level = 1;
		ul.ancestors().each(function(el){
			if (el.tagName == "UL") level += 1;		
		})
		
		return level;
	},
	
	_set_current_image: function(img) {
	    this.current_img = img;
	    this.highlight_img(img);
	}
}

Event.onDOMReady(function(){
    if ($(document.body).hasClassName("home") == false)
	    nav = new Navigation('navigation');
	else
	{
	    $$("#buttons img, #what_is_first_division").each(function(img){
	        img.onmouseover = function(el) {
	            if (img.src.indexOf("_ro.gif") < 0)
			        img.src = img.src.gsub(".gif", "_ro.gif")
	        }
	        
	        img.onmouseout = function(el) {
	            img.src = img.src.gsub("_ro.gif", ".gif")
	        }
	    });
	}
	    
    if ($(document.body).hasClassName("t5b")) {
	    right_content_images = $$("#exhibit_right_content img");
	    if (right_content_images.length > 1) {
	        main_image = right_content_images[0];
	        main_image.addClassName("main");
	        
	        right_content_images.splice(0,1);
	        side_images = right_content_images
	        
	        side_images.each(function(img) {
	            img.addClassName("thumb");
	            magnifier = addMagnifier(img);
	            [img, magnifier].invoke("observe", "click", function() {
	                tmp = main_image.src;
	                main_image.src = img.src;
	                img.src = tmp;
	                Scroller.updateAll();
	            });
	        });
	    }
	}
	
	if ($(document.body).hasClassName("t5c")) {
	    $$("#exhibit_right_content img").each(function(img) {
            a = document.createElement('a'); 
            a.href = img.src
            a.target = "_blank";
            img.remove();
            a.appendChild(img);
            $('exhibit_right_content').appendChild(a);
            img.addClassName('thumb');
            magnifier = addMagnifier(img);
            
        });
	  
	}
});


var version = parseFloat(navigator.appVersion.split('MSIE')[1]);
var ie_fix_opacity =  ((version >= 5.5) && (version < 7));

   
//Event.observe(window, "load", function(){
Event.onDOMReady(function() {

	if (ie_fix_opacity) {
        document.getElementsByClassName('ie-fix-opacity').each(function(poElement){
		// if IE5.5+ on win32, then display PNGs with AlphaImageLoader
		
			var cSrc = poElement.src;
			poElement.src = '/images/blank.gif';		
			poElement.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + cSrc + "', sizingMethod='scale')";
	    });
	}
	
	$$(".iframe_popup").each(function(a){
	
	    a.onclick = function(el){
	        Lightview.show({
              href: a.href,
              rel: 'iframe',
              options: {width:645, height:443, radius:0}
            });
            return false;
	    };
	})

    if ($('left_content')) {
	    $$("#window #left_content li").each(function(li){
		    li.observe("click", function(el){
			    li.addClassName("current");
			    $$("#left_content li").each(function(other_li){
				    if (other_li != li)
					    other_li.removeClassName("current")
			    });
			    return false;
		    });
	    });
	    
        
	    
        if ($(document.body).hasClassName("t3f")) {
	        $$("#left_content img").each(function(img) {
	            magnifier = addMagnifier(img);
	            [img, magnifier].invoke("observe", "click", function() {
	                $("content").down("img").src = img.src;
	            });
	        });
	    }  
        if ($(document.body).hasClassName("t3g")) {
        
	        $$("#window #left_content img").each(function(img){
	          if (img.up("ul.noclip") == undefined)
	            img.replace('<div class="clip"><img src="'+img.src+'" /></div>');
	        });
	        
	       $$("#window #left_content a").each(function(a){
		        a.observe("click", function(el){
			        a.addClassName("current");
			        $$("#left_content a").each(function(other_a){
			            if (other_a != a)
			    	        other_a.removeClassName("current")
		            });
			        return false;
		        });
	        });
	    }    
	
	}
	
	$$("body #right #bottom ul.buttons li, body.timeline #bottom ul.buttons li").each(function(li){
	    li.observe("mouseover", function(el){
	        li.addClassName("hover");
	    });
	    li.observe("mouseout", function(el){
	        li.removeClassName("hover");
	    });
	});
	
    if ($(document.body).hasClassName("t3a")) {
        if ($('lightview')) {
            Element.hide("lightview")
        }
    }  
    
    
	
});


var AudioButton = Class.create();
AudioButton.prototype = {
  	// Constructor
  	initialize: function(audio) {
		this.audio = audio;
		this.sound_obj = new Sound();
  
        if (Prototype.Browser.IE)
            this.onSoundBridgeLoad();
        else
            this.sound_obj.onSoundBridgeLoad = this.onSoundBridgeLoad.bindAsEventListener(this);
       
        // On Sound Complete
        this.sound_obj.onSoundComplete = this.markStopped.bindAsEventListener(this);
		
		// On Click
		this.audio.onclick = this.click.bindAsEventListener(this);
	},
	
	onSoundBridgeLoad: function() {
	    if (this.isAutoplay() && this.userAllowAutoplay())
	        this.play();
	},
	
	play: function() {
	    this.sound_obj.loadSound(this.audio.href, true);
	    this.sound_obj.setVolume(100);
        this.markPlaying();
        
        if (this.isAutoplay())
            Cookie.erase("audio_autoplay");
    },
	
	stop: function() {
	    this.sound_obj.stop();
        this.markStopped();
        
        if (this.isAutoplay()) 
            Cookie.set("audio_autoplay", "false", 365);  
    },
    
    markStopped: function() {
        this.audio.innerHTML = "Listen to audio"
        this.audio.removeClassName("playing");
    },
    
    markPlaying: function() {
        this.audio.addClassName("playing");
        this.audio.innerHTML = "Stop audio";
    },
    
    click: function() {
        if (this.isPlaying()) this.stop(); else this.play();
        return false;
    },
    
    isPlaying: function() {
        if (this.audio.hasClassName("playing")) return true; else return false;
    },
    
    isAutoplay: function() {
        return this.audio.hasClassName("autoplay");
    },
    
    userAllowAutoplay: function() {
        var autoplay = Cookie.get("audio_autoplay")
        if (autoplay == "false") return false; else return true;
    }
}

Event.onDOMReady(function() {
    // Background Audio
    $$('a.audio').each(function(audio){
       a = new AudioButton(audio);
    });
});

function addMagnifier(img) {
    magnifier = document.createElement('img'); 
    magnifier = $(magnifier);
    magnifier.src = "/images/magnifier.png";
    magnifier.width = 13;
    magnifier.height = 16;
    magnifier.className = "magnifier";
    
    if (ie_fix_opacity) {
		var cSrc = magnifier.src;
		magnifier.src = '/images/blank.gif';		
		magnifier.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + cSrc + "', sizingMethod='scale')";
	}
    
    
    img.insert({ after: magnifier});
    
    return magnifier;
}

function getBookmarkFromUrl() {
    return bookmark = String(document.location).gsub(/^[^#]*#/, "")
}

function getUrlWithoutBookmark() {
    return url = String(document.location).gsub(/#.+$/, "")
}
