// Copyright (c) 1996-1997 Athenia Associates.
// http://www.webreference.com/js/
// License is granted if and only if this entire
// copyright notice is included. By Tomer Shiran.

function setCookie (name, value, expires, path, domain, secure) {
    var curCookie = name + "=" + escape(value) + (expires ? "; expires=" + expires : "") +
        (path ? "; path=" + path : "") + (domain ? "; domain=" + domain : "") + (secure ? "secure" : "");
    document.cookie = curCookie;
}

function getCookie (name) {
    var prefix = name + '=';
    var c = document.cookie;
    var nullstring = '';
    var cookieStartIndex = c.indexOf(prefix);
    if (cookieStartIndex == -1)
        return nullstring;
    var cookieEndIndex = c.indexOf(";", cookieStartIndex + prefix.length);
    if (cookieEndIndex == -1)
        cookieEndIndex = c.length;
    return unescape(c.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

function deleteCookie (name, path, domain) {
    if (getCookie(name))
        document.cookie = name + "=" + ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
}

function fixDate (date) {
    var base = new Date(0);
    var skew = base.getTime();
    if (skew > 0)
        date.setTime(date.getTime() - skew);
}

function rememberMe (f) {
    var now = new Date();
    fixDate(now);
    now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
    now = now.toGMTString();
    if (f.author != undefined)
       setCookie('mtcmtauth', f.author.value, now, '/', '', '');
    if (f.email != undefined)
       setCookie('mtcmtmail', f.email.value, now, '/', '', '');
    if (f.url != undefined)
       setCookie('mtcmthome', f.url.value, now, '/', '', '');
}

function forgetMe (f) {
    deleteCookie('mtcmtmail', '/', '');
    deleteCookie('mtcmthome', '/', '');
    deleteCookie('mtcmtauth', '/', '');
    f.email.value = '';
    f.author.value = '';
    f.url.value = '';
}

function hideDocumentElement(id) {
    var el = document.getElementById(id);
    if (el) el.style.display = 'none';
}

function showDocumentElement(id) {
    var el = document.getElementById(id);
    if (el) el.style.display = 'block';
}

var commenter_name;

function individualArchivesOnLoad(commenter_name) {

    hideDocumentElement('trackbacks-info');



    if (document.comments_form) {
        if (document.comments_form.email != undefined &&
            (mtcmtmail = getCookie("mtcmtmail")))
            document.comments_form.email.value = mtcmtmail;
        if (document.comments_form.author != undefined &&
            (mtcmtauth = getCookie("mtcmtauth")))
            document.comments_form.author.value = mtcmtauth;
        if (document.comments_form.url != undefined && 
            (mtcmthome = getCookie("mtcmthome")))
            document.comments_form.url.value = mtcmthome;
        if (mtcmtauth || mtcmthome) {
            document.comments_form.bakecookie.checked = true;
        } else {
            document.comments_form.bakecookie.checked = false;
        }
    }
}

function writeTypeKeyGreeting(commenter_name, entry_id) {

}




var D = {
	__: function(id) {
		return this._id(id);
	},
	_tag: function(tag, parent) {
			var par = parent || document;
						
			return par.getElementsByTagName(tag);
		},
	
	_class: function(classname, tag, parent)	{
			var matches = [];
			var par = parent || document;
			
			var els = this._tag(tag, parent);
			var elsl = els.length;
			
			for(var i=0; i<elsl; i++) {
					if (els[i].className == classname)
						matches.push(els[i]);
				}
			
			if (matches.length == 1)
				return matches[0];
			
			return matches.length == 0 ? false : matches;
			
		
		},
	_id: function(id) {
			return document.getElementById(id);
		},
	
	_attr: function(attr, value, tag, parent) {

			var matches = [];
			var par = parent || document;
			
			var els = this._tag(tag, parent);
			var elsl = els.length;
			
			for (var i=0; i<elsl; i++) {
					if (els[i].getAttribute(attr) == value)
						matches.push(els[i]);
				}
			
			if (matches.length == 1)
				return matches[0];
			
			return matches.length == 0 ? false : matches;
			
				
		},
		_index: function(obj) {
					var parent_node = obj.parentNode;
					if (!parent_node)
						throw "nodeIndex: Missing parent for " + obj;
					
					for (var i = 0, cnt = 0, node, nodes = parent_node.childNodes; node = nodes[i]; i++) {
						if (node.tagName) {
							if (node === obj)
								return cnt;
							
							cnt++;
						}
					}
					return null;
		},	
		E: {
				add: function(obj, evt, func) {
					
					if (document.addEventListener)
						obj.addEventListener(evt, func, false);
					else if(document.attachEvent)
						obj.attachEvent('on'+evt,func);
					
					
					},
				remove: function(obj, evt, func) {
						obj.removeEventListener(evt, func, false);
					},
			
				get: function(e, p) {
						e = e || window.event;
						
						if (p)
							D.E.pd(e);
						
						return t = e.target || e.srcElement;
					
					},
				pd: function(e) {
						if (e.stopPropagation) {
							 e.stopPropagation();
							 
						} else if(e.cancelBubble) { 
							e.cancelBubble = true;
							
						} 
						
						if (e.preventDefault) {
								e.preventDefault();
							} else {
								e.returnValue = false; 
								return false; 
							}
						
					}
			
			},
		Parent: {
				_attr: function(obj, attr, value) {
						if (!obj)
							return false;

						pn = obj.parentNode;
			
						if (!pn || pn.nodeName.toLowerCase() == 'html')
							return false;
							
						if (pn && (pn.getAttribute(attr) == value)) {
								return pn;
							}
			
						return D.Parent._attr(pn, attr, value);

					
					}
			
		},
		Desc: {
			__check_par: function(node, parents, index) {
				
				if (parents.length == 0 || index < 0)
					return true; // if it comes here we know it found a match
								
				par = parents[index];
				
				//alert(node.parentNode.nodeName + ' ' + par);
								
				if (node.parentNode.nodeName.toUpperCase() == par.toUpperCase())
					return D.Desc.__check_par(node.parentNode, parents, index-1);
				else
					return false;
					
			},
			get: function(tagsn, root) {
				// send tag heirarchy separated by '>'
				// add support for ID & Class attributes later
				
				
				pars = tagsn.split(' ');
				
				node = pars.pop();
				tgl = pars.length;
				
				tags = D._tag(node, root);
				tgsl = tags.length;
				
				
				matched = [];
				
				for (i=0;i<tgsl;i++) {
					
					
					if (D.Desc.__check_par(tags[i], pars, tgl-1)) {
						matched.push(tags[i]);
						
					}
				}
				if (matched.length == 0) 
					return false;
				
				return matched.length == 1 ? matched[0] : matched;			
			}
			
		},
		Create: {
			elem: function(el, attrs) {
				e = document.createElement(el);
				for (a in attrs) {
					e[a] = attrs[a];
				}
				return e;
			
			},
			txt: function(text) {
				return document.createTextNode(text);
			}
		
		},
		append: function(tag, ntag) {			
			return tag.appendChild(ntag);
		
		},
		prepend: function(tag, ntag) {
			return tag.insertBefore(ntag, tag.firstChild);
		},
		Toggle: {
			display: function(tag) {
				if (!tag)
					tag = this;
					
				if (tag.style.display == 'block') 
					tag.style.display = 'none';
				else if (tag.style.display == 'none')
					tag.style.display = 'block';
				else
					tag.style.display = 'none';
					
			}
		},
		Content: {
			toggle: function(str, str2, tag) {
				if (tag.innerHTML == str)
					tag.innerHTML = str2;
				else if (tag.innerHTML == str2)
					tag.innerHTML = str;
					
			}
		
		}
	
	}
	
	
	
var Util = {
		
		clean: function(parobj) {
			var notWhiteSpaceNode = /\S/;
				
			for (i=0;i<parobj.childNodes.length;i++){
				if ((parobj.childNodes[i].nodeType == 3) && (!notWhiteSpaceNode.test(parobj.childNodes[i].nodeValue))) {
					parobj.removeChild(parobj.childNodes[i]);
						i--;
				}
			}
		}
	
	
	}
	
	
	
Pane = {
	
	Init: function() {
			panes = D._attr('name', 'panel', 'div')
			total = panes.length;
			for (var i=0; i<total; i++) {
				var ul = D._class('tab_panes', 'ul', panes[i]);
				if (ul) {
					Util.clean(ul);
					ull = ul.childNodes.length;
					for (var j = 0; j<ull; j++) {
						D.E.add(ul.childNodes[j], 'click', Pane.Switch)
						ul.childNodes[j].className = 'tab_' + (j + 1);
					}
				}
					
			}
		},
	
	Switch: function(e) {
			li = D.E.get(e, true);
			ind = D._index(li);
			panel = D.Parent._attr(li, 'name', 'panel');
			
			panel.className = 's_'+ (ind + 1);
			
		}

	}

//Pane.Init();



CollapsibleMenu = {

	init: function(holder) {
		
		Util.clean(D.__(holder));
		
		uls = D.Desc.get('ul li ul', D.__(holder));
		
		for (i=0;i<uls.length;i++ ) {
			uls[i].style.borderLeft = '1px solid #ccc';
			uls[i].style.paddingLeft = '3px';
			uls[i].toggle = D.Toggle.display;
			uls[i].toggle();
			
			
			a = D.Create.elem('a', {'href':'#'})
			D.append(a, D.Create.txt('+'));
			
			D.E.add(a, 'click', CollapsibleMenu.toggle)
			
			a.toggle = D.Content.toggle;
			
			a.style.width = '10px';
			a.style.styleFloat = 'left';
			a.style.padding = '1px';
			a.style.margin = '0 2px 0 0';
			a.style.border = '1px solid #ccc';
			a.style.background = '#fff';
			uls[i].parentNode.firstChild.style.fontWeight = 'bold';
			D.prepend(uls[i].parentNode, a);
			
		}
	},
	toggle: function(e) {
		evt = D.E.get(e, true);
		
		evt.toggle('-', '+', this);
		
		ul = D.Desc.get('li ul', evt.parentNode);
		
		
		
		if (ul.length > 1) {
			ul[0].toggle();
		} else {
			
			ul.toggle();
		}
 		
		
	}
	


}



CollapsibleMenu.init('leftnav');


/*** -------------------------  ***/
function require(script_name, path) { 
pth = path || 'http://mtlabs.blawgs.pro/js/'
document.write('<script language="javascript" src="'+pth+script_name+'.js"></script>'); 
}

require('jquery');
require('common');
/*** -------------------------  ***/
