/* Merged Plone Javascript file
 * This file is dynamically assembled from separate parts.
 * Some of these parts have 3rd party licenses or copyright information attached
 * Such information is valid for that section,
 * not for the entire composite file
 * originating files are separated by ----- filename.js -----
 */

/* ----- formsubmithelpers.js ----- */
function inputSubmitOnClick(event) {
    if (!event) var event = window.event; // IE compatibility

    if (hasClassName(this, 'submitting')) {
        return confirm(window.form_resubmit_message);
    } else {
        addClassName(this, 'submitting');
    }
    return true;
}

function registerSubmitHandler() {
    var nodes = cssQuery('input[type=submit]');
    for (var i=0; i<nodes.length; i++) {
        var node = nodes[i];
        if (!node.onclick) {
            node.onclick = inputSubmitOnClick;
        }
    }
}
registerPloneFunction(registerSubmitHandler);


/* ----- javascripts/ddb_main.js ----- */
// ***********************************************************
// SHOW SUB-MENUS
// ***********************************************************
// When a side nav button is clicked
// 1) hide all sub-menus (set 'display: none')
// 2) displays only the requested sub-menu (set 'display: block')

function showSubNav(subNavName)
{
	// Retrieve div#subNav-container
	var subMenuDiv = document.getElementById("subNav-container");
	// Retrieve all unordered lists that reside within div#subNav-container	
	var x = subMenuDiv.getElementsByTagName("ul");
	// Set all lists to 'display:none'
	for (var i=0; i<x.length; i++)
	{
		x[i].style.display = 'none';
	}

	// Display the requested sub-menu list
	document.getElementById(subNavName).style.display = 'block';
}




// ***********************************************************
// SHOW SIMPLE SEARCH BOX (TOP NAV - Homepage only)
// ***********************************************************
// In the homepage top nav, when the "Search" button is clicked...
// 1) Check if simple search box is visible or hidden
// 2) If hidden, make visible and leave "Search" button blue
// 3) If visible, make hidden and remove blue from "Search" button
// Use 'inline' instead of 'block' to avoid IE double-margin float bug

function showSimpleSearch()
{
	// IF SIMPLE SEARCH IS HIDDEN...
	if (document.getElementById('ddb-searchbox').style.display != 'inline') {
		// Make simple search box visible
			document.getElementById('ddb-searchbox').style.display = 'inline';
		// Get the "Search" button
			var searchListItem = document.getElementById('navItem-search');
		// Find the "a" tag within
			var x = searchListItem.getElementsByTagName('a');
		// Make the background color blue
			x[0].style.backgroundColor = '#5491b3';
		// Make the text white
			x[0].style.color = '#fff';
			
	// IF SIMPLE SEARCH IS VISIBLE...
	} else {
		// Make simple search box hidden
			document.getElementById('ddb-searchbox').style.display = 'none';
		// Get the "Search" button
			var searchListItem = document.getElementById('navItem-search');
		// Find the "a" tag within
			var x = searchListItem.getElementsByTagName('a');
		// Make the background color 'none'
			x[0].style.backgroundColor = 'transparent';
		// Make the text grey
			x[0].style.color = '#2a3136';
	}
}



// ***********************************************************
// SHOW SEARCH OPTIONS (Advanced Search Page)
// ***********************************************************
// When a search option is clicked (people, offices or content):
// 1) hide all forms (set 'display: none')
// 2) displays only the requested form (set 'display: block')
// 3) changes the state of the link that was clicked
// 4) hides all instruction texts in barker column (set 'display: none')
// 5) displays only the appropriate instructions (set 'display: block')

function showSearchOption(formName,linkName,instrName)
{
	// Retrieve div#portal-column-content
		var subMenuDiv = document.getElementById("portal-column-content");
	// Retrieve all forms that reside within div#portal-column-content
		var x = subMenuDiv.getElementsByTagName("form");
	// Set all forms to 'display:none'
		for (var i=0; i<x.length; i++)
		{
			x[i].style.display = 'none';
		}
	// Display the requested form
		document.getElementById(formName).style.display = 'block';


	// Change all of the Search Option links to neutral state
		var optionLinks = document.getElementById("portal-column-two");
		var x = optionLinks.getElementsByTagName("a");
		for (var i=0; i<x.length; i++)
			x[i].style.backgroundColor = 'transparent'; // Use white. Firefox doesn't like 'none'
	// Change the selected Search Option link to active state
		var x = 	document.getElementById(linkName);
		x.style.backgroundColor = '#e8f0f5';


	// Retrieve div#instructionalText
		var subMenuDiv = document.getElementById("instructionalText");
	// Retrieve all divs that reside within div#instructionalText
		var x = subMenuDiv.getElementsByTagName("div");
	// Set all divs to 'display:none'
		for (var i=0; i<x.length; i++)
		{
			x[i].style.display = 'none';
		}
	// Display the requested instructions
		document.getElementById(instrName).style.display = 'block';
}



// ***********************************************************
// SEARCH OPTION OUT (Advanced Search Page)
// ***********************************************************
// Handles font colors onMouseOut. If that form is showing, 
// keep the link in active state. Otherwise, change it back to
// neutral state.

function searchOptionOut(formName,linkName)
{
	var x = document.getElementById(formName);
	var y = document.getElementById(linkName);
	
	if (x.style.display == 'none')
		y.style.color = '#5491b3';
}



// ***********************************************************
// CHECKBOX SHOW (Advanced Search Page)
// ***********************************************************
// When the user checks the checkbox, the designated element is revealed.
// When the user unchecks the checkbox, the element is hidden and 
// and checkboxes within that element are unchecked as well (to
// prevent their values from being accidently passed).

function checkboxShow(elementID)
{
	var x = document.getElementById(elementID);
	
	if (x.style.display != 'block') {
		x.style.display = 'block';
	} else {
		x.style.display = 'none';
		// uncheck all checkboxes within the element
		var checkboxes = x.getElementsByTagName('input');

		for (i = 0; i < checkboxes.length; i++)
			checkboxes[i].checked = false ;
	}

}


// ***********************************************************
// SHOW FEATURED BOX (Publish Asset Page)
// ***********************************************************
// When the user checks a checkbox in the Asset Category section,
// an optional checkbox pops-up. This optional checkbox allows
// the user the designate the asset as the "Featured" asset on
// that category's main page.

function showFeaturedBox(checkboxID)
{
	// IF FEATURED BOX IS HIDDEN...
	if (document.getElementById(checkboxID).style.display != 'block') {
		// Make the featured box visible
		document.getElementById(checkboxID).style.display = 'block';
	} else {
		// Make the featured box hidden
		document.getElementById(checkboxID).style.display = 'none';
		// Uncheck the "featured" checkbox so the value won't accidently get passed
			// Retrieve the div
				var featDiv = document.getElementById(checkboxID);
			// Retrieve the checkbox within the div
				var x = featDiv.getElementsByTagName("input");
			// Uncheck the box
				x[0].checked = false;
	}
}



/**
 * SWFObject v1.4.2: Flash Player detection and embed - http://blog.deconcept.com/swfobject/
 *
 * SWFObject is (c) 2006 Geoff Stearns and is released under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * **SWFObject is the SWF embed script formerly known as FlashObject. The name was changed for
 *   legal reasons.
 */
if(typeof deconcept == "undefined") var deconcept = new Object();
if(typeof deconcept.util == "undefined") deconcept.util = new Object();
if(typeof deconcept.SWFObjectUtil == "undefined") deconcept.SWFObjectUtil = new Object();
deconcept.SWFObject = function(swf, id, w, h, ver, c, useExpressInstall, quality, xiRedirectUrl, redirectUrl, detectKey){
	if (!document.getElementById) { return; }
	this.DETECT_KEY = detectKey ? detectKey : 'detectflash';
	this.skipDetect = deconcept.util.getRequestParameter(this.DETECT_KEY);
	this.params = new Object();
	this.variables = new Object();
	this.attributes = new Array();
	if(swf) { this.setAttribute('swf', swf); }
	if(id) { this.setAttribute('id', id); }
	if(w) { this.setAttribute('width', w); }
	if(h) { this.setAttribute('height', h); }
	if(ver) { this.setAttribute('version', new deconcept.PlayerVersion(ver.toString().split("."))); }
	this.installedVer = deconcept.SWFObjectUtil.getPlayerVersion();
	if(c) { this.addParam('bgcolor', c); }
	var q = quality ? quality : 'high';
	this.addParam('quality', q);
	this.setAttribute('useExpressInstall', useExpressInstall);
	this.setAttribute('doExpressInstall', false);
	var xir = (xiRedirectUrl) ? xiRedirectUrl : window.location;
	this.setAttribute('xiRedirectUrl', xir);
	this.setAttribute('redirectUrl', '');
	if(redirectUrl) { this.setAttribute('redirectUrl', redirectUrl); }
}
deconcept.SWFObject.prototype = {
	setAttribute: function(name, value){
		this.attributes[name] = value;
	},
	getAttribute: function(name){
		return this.attributes[name];
	},
	addParam: function(name, value){
		this.params[name] = value;
	},
	getParams: function(){
		return this.params;
	},
	addVariable: function(name, value){
		this.variables[name] = value;
	},
	getVariable: function(name){
		return this.variables[name];
	},
	getVariables: function(){
		return this.variables;
	},
	getVariablePairs: function(){
		var variablePairs = new Array();
		var key;
		var variables = this.getVariables();
		for(key in variables){
			variablePairs.push(key +"="+ variables[key]);
		}
		return variablePairs;
	},
	getSWFHTML: function() {
		var swfNode = "";
		if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) { // netscape plugin architecture
			if (this.getAttribute("doExpressInstall")) { this.addVariable("MMplayerType", "PlugIn"); }
			swfNode = '<embed type="application/x-shockwave-flash" src="'+ this.getAttribute('swf') +'" width="'+ this.getAttribute('width') +'" height="'+ this.getAttribute('height') +'"';
			swfNode += ' id="'+ this.getAttribute('id') +'" name="'+ this.getAttribute('id') +'" ';
			var params = this.getParams();
			 for(var key in params){ swfNode += [key] +'="'+ params[key] +'" '; }
			var pairs = this.getVariablePairs().join("&");
			 if (pairs.length > 0){ swfNode += 'flashvars="'+ pairs +'"'; }
			swfNode += '/>';
		} else { // PC IE
			if (this.getAttribute("doExpressInstall")) { this.addVariable("MMplayerType", "ActiveX"); }
			swfNode = '<object id="'+ this.getAttribute('id') +'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="'+ this.getAttribute('width') +'" height="'+ this.getAttribute('height') +'">';
			swfNode += '<param name="movie" value="'+ this.getAttribute('swf') +'" />';
			var params = this.getParams();
			for(var key in params) {
			 swfNode += '<param name="'+ key +'" value="'+ params[key] +'" />';
			}
			var pairs = this.getVariablePairs().join("&");
			if(pairs.length > 0) {swfNode += '<param name="flashvars" value="'+ pairs +'" />';}
			swfNode += "</object>";
		}
		return swfNode;
	},
	write: function(elementId){
		if(this.getAttribute('useExpressInstall')) {
			// check to see if we need to do an express install
			var expressInstallReqVer = new deconcept.PlayerVersion([6,0,65]);
			if (this.installedVer.versionIsValid(expressInstallReqVer) && !this.installedVer.versionIsValid(this.getAttribute('version'))) {
				this.setAttribute('doExpressInstall', true);
				this.addVariable("MMredirectURL", escape(this.getAttribute('xiRedirectUrl')));
				document.title = document.title.slice(0, 47) + " - Flash Player Installation";
				this.addVariable("MMdoctitle", document.title);
			}
		}
		if(this.skipDetect || this.getAttribute('doExpressInstall') || this.installedVer.versionIsValid(this.getAttribute('version'))){
			var n = (typeof elementId == 'string') ? document.getElementById(elementId) : elementId;
			n.innerHTML = this.getSWFHTML();
			return true;
		}else{
			if(this.getAttribute('redirectUrl') != "") {
				document.location.replace(this.getAttribute('redirectUrl'));
			}
		}
		return false;
	}
}

/* ---- detection functions ---- */
deconcept.SWFObjectUtil.getPlayerVersion = function(){
	var PlayerVersion = new deconcept.PlayerVersion([0,0,0]);
	if(navigator.plugins && navigator.mimeTypes.length){
		var x = navigator.plugins["Shockwave Flash"];
		if(x && x.description) {
			PlayerVersion = new deconcept.PlayerVersion(x.description.replace(/([a-zA-Z]|\s)+/, "").replace(/(\s+r|\s+b[0-9]+)/, ".").split("."));
		}
	}else{
		// do minor version lookup in IE, but avoid fp6 crashing issues
		// see http://blog.deconcept.com/2006/01/11/getvariable-setvariable-crash-internet-explorer-flash-6/
		try{
			var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
		}catch(e){
			try {
				var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
				PlayerVersion = new deconcept.PlayerVersion([6,0,21]);
				axo.AllowScriptAccess = "always"; // throws if player version < 6.0.47 (thanks to Michael Williams @ Adobe for this code)
			} catch(e) {
				if (PlayerVersion.major == 6) {
					return PlayerVersion;
				}
			}
			try {
				axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
			} catch(e) {}
		}
		if (axo != null) {
			PlayerVersion = new deconcept.PlayerVersion(axo.GetVariable("$version").split(" ")[1].split(","));
		}
	}
	return PlayerVersion;
}
deconcept.PlayerVersion = function(arrVersion){
	this.major = arrVersion[0] != null ? parseInt(arrVersion[0]) : 0;
	this.minor = arrVersion[1] != null ? parseInt(arrVersion[1]) : 0;
	this.rev = arrVersion[2] != null ? parseInt(arrVersion[2]) : 0;
}
deconcept.PlayerVersion.prototype.versionIsValid = function(fv){
	if(this.major < fv.major) return false;
	if(this.major > fv.major) return true;
	if(this.minor < fv.minor) return false;
	if(this.minor > fv.minor) return true;
	if(this.rev < fv.rev) return false;
	return true;
}
/* ---- get value of query string param ---- */
deconcept.util = {
	getRequestParameter: function(param) {
		var q = document.location.search || document.location.hash;
		if(q) {
			var pairs = q.substring(1).split("&");
			for (var i=0; i < pairs.length; i++) {
				if (pairs[i].substring(0, pairs[i].indexOf("=")) == param) {
					return pairs[i].substring((pairs[i].indexOf("=")+1));
				}
			}
		}
		return "";
	}
}
/* fix for video streaming bug */
deconcept.SWFObjectUtil.cleanupSWFs = function() {
	var objects = document.getElementsByTagName("OBJECT");
	for (var i=0; i < objects.length; i++) {
		objects[i].style.display = 'none';
		for (var x in objects[i]) {
			if (typeof objects[i][x] == 'function') {
				objects[i][x] = null;
			}
		}
	}
}
if (typeof window.onunload == 'function') {
	var oldunload = window.onunload;
		window.onunload = function() {
		deconcept.SWFObjectUtil.cleanupSWFs();
		oldunload();
	}
} else {
	window.onunload = deconcept.SWFObjectUtil.cleanupSWFs;
}
/* add Array.push if needed (ie5) */
if (Array.prototype.push == null) { Array.prototype.push = function(item) { this[this.length] = item; return this.length; }}

/* add some aliases for ease of use/backwards compatibility */
var getQueryParamValue = deconcept.util.getRequestParameter;
var FlashObject = deconcept.SWFObject; // for legacy support
var SWFObject = deconcept.SWFObject;


// Edit warning popup
function dontLeaveMe() {
    return "If you are not ready to create your asset hit cancel in this prompt and cancel again on the asset page. Do not hit OK.";
}

function goOnAndLeave() {
    window.onbeforeunload = null;
}

var urlRegExp = new RegExp("http.*base_edit.*");
if (urlRegExp.exec(window.location.href)) {
    window.onbeforeunload = dontLeaveMe;
    
    // XXX: why dont these work?
    var saves = document.getElementsByName("form_submit");
    for (var i = 0; i < saves.length; i++){
        saves[i].onclick = goOnAndLeave;
    }
    
    var cancels = document.getElementsByName("form.button.cancel");
    for (var i = 0; i < cancels.length; i++){
        cancels[i].onclick = goOnAndLeave;
    }
}

