// Sawyer Brook JavaScript
// -----------------------

// set onclick for menu headings
function mclick (obj) {
	var el = document.getElementById(obj); // li element of menu header
	var ael = el.firstChild; // a element of menu header
	
	// new Function is needed for browser compatability; return false cancels the hyperlink # jump
	ael.onclick = new Function ("mtogglewp('" + obj + "'); return false");
}

// menu slider (configured for wp menus)
function mtogglewp (obj, opt) {
	var el = document.getElementById(obj); // the li element representing the menu header 
	var ael = el.firstChild; // a element, which links to this function
	
	var n = ael
	do n = n.nextSibling;
	while (n && n.nodeType != 1); // skip white space to find ul following ael

	// if opt specified, then use opt
	if (opt) {
		if (opt = 0) {
			n.style.display = 'none';
		}
		else {
			n.style.display = '';
		}
	}
	// else if visible, hide it
	else {
		if (n.style.display != 'none') {
			n.style.display = 'none';
		}
		else {
			n.style.display = '';
		}
	}

}

// run asp in page with iframe
function loadframe(obj, myfile) {
	var el = document.getElementById(obj)
	el.src = myfile;
}

// Show/hide element toggler
function toggle(obj, imgID, imgType) {
	var el = document.getElementById(obj);
	
	// if visible, hide it
	if ( el.style.display != 'none' ) {
		el.style.display = 'none';
		
		// swap image if menu slider specified
		if ( imgType == 'menu' ) {
			expandCollapse (imgID, 'menuslide');
		}
		
		// swap image if plus/minus expander specified
		if ( imgType == 'exp' ) {
			expandCollapse (imgID, 'expand');
		}
		
		// swap image if slidedown expander specified
		if ( imgType == 'slide' ) {
			expandCollapse (imgID, 'slidedown');
		}
		
	}
	
	// if not visible, show it
	else {
		el.style.display = '';
		
		// swap image if menu slider specified
		if ( imgType == 'menu' ) {
			expandCollapse (imgID, 'menushrink');
		}
		
		// swap image if plus/minus expander specified		
		if ( imgType == 'exp' ) {
			expandCollapse (imgID, 'collapse');
		}
		
		// swap image if plus/minus expander specified		
		if ( imgType == 'slide' ) {
			expandCollapse (imgID, 'slideup');
		}
	}
}

// Swap expand/collapse images
function expandCollapse(myImage, state) {
	var im = document.getElementById(myImage);
	if ( state == 'collapse' ) {
		im.src = 'wp-content/themes/db/images/icon-minus.png';
	}
	if ( state == 'expand') {
		im.src = 'wp-content/themes/db/images/icon-plus.png';
	}
	if ( state == 'slideup' ) {
		im.src = 'wp-content/themes/db/images/icon-slideup.png';
	}
	if ( state == 'slidedown') {
		im.src = 'wp-content/themes/db/images/icon-slidedown.png';
	}
	if ( state == 'menushrink' ) {
		im.src = 'wp-content/themes/db/images/icon-slide-down.png';
	}
	if ( state == 'menuslide') {
		im.src = 'wp-content/themes/db/images/icon-slide-down.png';
	}
}

// show store item widgets (hidden by default so offsite content retreive doesn't slow things down)
// this works but the current problem is that the php get_file_contents still excutes BEFORE page load
// the solution must lie in changing the PHP
function sbShowWidgets(){
if (document.getElementsByClassName == undefined) {
	document.getElementsByClassName = function(className)
	{
		var hasClassName = new RegExp("(?:^|\\s)" + className + "(?:$|\\s)");
		var allElements = document.getElementsByTagName("*");
		var results = [];

		var element;
		for (var i = 0; (element = allElements[i]) != null; i++) {
			var elementClass = element.className;
			if (elementClass && elementClass.indexOf(className) != -1 && hasClassName.test(elementClass))
				results.push(element);
		}

		return results;
	}
}

var sbAllStoreWidgets = document.getElementsByClassName('multiwidget_sb_storeitems');
for (x in sbAllStoreWidgets) {
	sbAllStoreWidgets[x].style.visibility = "visible";
}


}
