// (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",
			"removePopup",
			"createCookie",
			"readCookie",
			"eraseCookie"
		],
		
		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,func) {
			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();};
					if (func){
					    func();
					};
					return false;
				};
				i++;
			};
		},
		
		// quick way to remove popup
		removePopup : function (elm) {
			var elms = $(elm);
			var i = 0;
			while (i < elms.length){
				elms[i].onclick = function(){
                    return true;
				};
				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] 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);
							}else{
								$("#"+e).addClass(h);
							}
						}
						c++;
					}
					return false;
				};
				i++;
			};
		},
		
		// create a cookie
        createCookie : function(name,value,days) {
            if (days) {
                var date = new Date();
                date.setTime(date.getTime()+(days*24*60*60*1000));
                var expires = "; expires="+date.toGMTString();
            }
            else var expires = "";
            document.cookie = name+"="+value+expires+"; path=/";
        },
        
		// read the cookie
        readCookie : function(name) {
            var nameEQ = name + "=";
            var ca = document.cookie.split(';');
            for(var i=0;i < ca.length;i++) {
                var c = ca[i];
                while (c.charAt(0)==' ') c = c.substring(1,c.length);
                if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
            }
            return null;
        },
        
		// remove the cookie
        eraseCookie : function(name) {
            createCookie(name,"",-1);
        }

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