// Plugin for DOMAssistant

DOMAssistant.hrbc = function () {
	return {
		publicMethods : [
			"setCurrent",
			"popup",
			"toggle",
			"print",
			"close",
			"formPopup",
			"formProtect"
		],
		
		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");
		},
		
		// close window
		close : function (elm) {
 			var elms = $(elm);
  			var i = 0;
 			while (i < elms.length){
				elms[i].onclick = function()
				{
 					window.close();
					return false;
				};
				i++;
			};
		},
		
		// print window
		print : function (elm) {
			var elms = $(elm);
			var i = 0;
			while (i < elms.length){
				elms[i].onclick = function(){
 					window.print();
					return false;
				};
				i++;
			};
		},
		
		// 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=500,height=500,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 toggleGroupPattern = new RegExp('^togglegroup_','g');
					var elmHidden = new RegExp("(^| )"+h+"( |$)");
					// loop through classes
					while (c < classes.length){
						// if they start with toggle_[element_id] the 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);
								$(this).addClass('toggleOn');
							}else{
								$("#"+e).addClass(h);
								$(this).removeClass('toggleOn');
							}
						}
						
						if(classes[c].match(toggleGroupPattern)){
							var eG = $("."+classes[c]);
							var gI = 0;
 							while (gI < eG.length)
 							{
 							
   								if(eG[gI] != this)
 								{
									var gClasses = eG[gI].className.split(' ');
									var gC = 0;
									while (gC < gClasses.length){
										if(gClasses[gC].match(togglePattern)){
 											var gE = gClasses[gC].replace("toggle_","");
  											$("#"+gE).addClass(h);
											$(eG[gI]).removeClass('toggleOn');
										}
										gC++;
									}
  								}
 								gI++;
 							}
 						}
						c++;
					}
					return false;
				};
				i++;
			};
		},

		// toggles other elements in the toggle group
 		togglegroup : 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('^togglegroup_','g');
					var elmHidden = new RegExp("(^| )"+h+"( |$)");
					// loop through classes
					while (c < classes.length){
						// if they start with toggle_[element_id] the hide or show the
						// element with the id "element_id"
						if(classes[c].match(togglePattern)){
							var e = $(classes[c]);
							alert(e.length());
// 							var eClass = document.getElementById(e).className;
// 							if (elmHidden.test(eClass)){
// 								$("#"+e).removeClass(h);
// 								$(this).addClass('toggleOn');
// 							}else{
// 								$("#"+e).addClass(h);
// 								$(this).removeClass('toggleOn');
// 							}
						}
						c++;
					}
					return false;
				};
				i++;
			};
		},

 		// select a row from a result set
		formPopup : function (form, windowName, params) {
  			var newWindow = window.open('', windowName,  params || 'width=400,height=400,toolbar=no,location=no,status=no,menubar=no,resizable=no,scrollbars=yes,titlebar=no');
			form.target = windowName;
			newWindow.focus();
			return true;
  		},

 		// protect forms
		formProtect : function () {
			var elms = document.forms;
  			var i = 0;
			// loop through elements
			while (i < elms.length)
			{
 				if(elms[i].website){
  					elms[i].website.value = "http://www.google.com/";
 				}
				i++;	
			}
  		}

	};	
}();


DOMAssistant.attach(DOMAssistant.hrbc);