// (c) 1999-2008 Bright Interactive Limited. All rights reserved.
// http://www.bright-interactive.com | info@bright-interactive.com
// Tel: 0870 240 6520

// Plugin for DOMAssistant

DOMAssistant.Bright = function () {
	return {
		publicMethods : [
			"setCurrent",
			"popup",
			"toggle"
		],
		
		init : function () {
			
			// this loads an extra style sheet which is for 
			// javascript dependant css 
			var head = document.getElementsByTagName("head")[0];
			if (head) {
				var scriptStyles = document.createElement("link");
				scriptStyles.rel = "stylesheet";
				scriptStyles.type = "text/css";
				scriptStyles.href = "/css/script-styles.css";
				head.appendChild(scriptStyles);
			}
		},
		
		
		// set a class name on an element
		// for id's code should be "#id" or for classes ".class"
		// optional: pass the class to be assigned otherwise "current" is used
		setCurrent : function (elm,classname) {
			$(elm).addClass(classname || "current");
		},
		
		// opens the url in a new window
		// specify the class of the link
		// optional: options for opening e.g. "width=x, height=y"
		// optional: href specify the url (for links uses href value)
		// optional: windowRef specify the reference to the window
		popup : function (elm,options,href,windowRef) {
			var elms = $(elm);
			var i = 0;
			while (i < elms.length){
				elms[i].onclick = function(){
					var newWindow = window.open(href || this.href, windowRef || "newWindow",options || "width=400,height=400,toolbar=no,location=no,status=no,menubar=no,resizable=no,scrollbars=yes,titlebar=no");
					if(newWindow.blur){newWindow.focus();};
					return false;
				};
				i++;
			};
		},
		
		// toggles an elements visibility
		// optional: set the class that has display:none; assigned
		toggle : function (elm, hiddenClass) {
			// get a list of elements
			var elms = $(elm);
			var h = hiddenClass || "js-hide";
			var i = 0;
			// loop through elements
			while (i < elms.length){
				elms[i].onclick = function(){
					// get array of classes
					var classes = this.className.split(' ');
					var c = 0;
					var togglePattern = new RegExp('^toggle_','g');
					var elmHidden = new RegExp("(^| )"+h+"( |$)");
					// loop through classes
					while (c < classes.length){
						// if they start with toggle_[element_id] then hide or show the
						// element with the id "element_id"
						if(classes[c].match(togglePattern)){
							var e = classes[c].replace("toggle_","");
							var eClass = document.getElementById(e).className;
							if (elmHidden.test(eClass)){
								$("#"+e).removeClass(h);
							}else{
								$("#"+e).addClass(h);
							}
						}
						c++;
					}
					return false;
				};
				i++;
			};
		}

	};	
}();
DOMAssistant.attach(DOMAssistant.Bright);