/* author: Vadim Korolev 2009 */

var TOGGLE_TAGS = 'div p pre blockquote ul ol table form a span section article aside nav';

addLoadEvent(initPage);

function initPage(){
	bind('.del',confirmLink); // a.del, input.del
	bind('a.openwin',openWin);
	bind('a.toggle',prepareToggle,1); //set visibility from cookie
	bind('a.toggle',toggle);
	//bind('a.item',addObjLink);
}

// actions

function addObjLink(e){
	if(e.ctrlKey) this.href += '&add=1';
	else if(e.altKey) this.href += '&id=_&edit=1';
	return true;
}

function prepareToggle(node){
	var x = getTarget(node);
	if(!x || !x.tagName) return;
	var ready = (node.className.indexOf('ready')==-1) ? 0 : 1;
	var hid = (x.className.indexOf('hid')==-1) ? 0 : 1;
	if(!ready && (' '+TOGGLE_TAGS+' ').indexOf(x.tagName.toLowerCase())>-1) x.className += ' togglable';
	var v = 0;
	if(x) v = getCookie('visible_'+x.id);
	if(!v) if(!ready || hid) v = -1; //hide by default
	if(v) x.style.display = (v>0) ? (isInline(x) ? 'inline' : 'block') : 'none';
}

function toggle(){
	var x = getTarget(this);
	if(x){
		var vis = (x.offsetHeight==0);
		x.style.display = vis ? (isInline(x) ? 'inline' : 'block') : 'none';
		if(x.id) setCookie('visible_'+x.id,(vis ? 1 : -1));
		//this.blur();
	}
	return false;
}


function isInline(x){
	return x.tagName.toLowerCase().match(/^a|span$/);
}

function confirmLink(){
	return confirm(this.title ? this.title : 'Confirm?');
}

function openWin(){
	var x = 30, y = 30, w = screen.width-100, h = screen.height-250, url = this.href;
	var wh = this.title.match(/\d+x\d+/);
	if(wh && wh.length>0){
		wh = wh[0].split('x');
		w = 20 + Math.max(Number(wh[0]),100);
		h = 20 + Math.max(Number(wh[1]),100);
		x = (screen.width-w)/2;
		y = (screen.height-180-h)/2;
		if(x<30){
			x = 30;
			w = screen.width-100;
		}
		if(y<30){
			y = 30;
			h = screen.height-250;
		}
	}
	var menubar = (this.className.indexOf('menubar')!=-1) ? '1' : '0';
	var d = new Date();
	var tt = this.href.replace(/\W+/g,'');
	var prop = 'status='+menubar+',menubar='+menubar+',location='+menubar+',';
	var w = window.open(this.href,tt,'toolbar=0,'+prop+'left='+x+',top='+y+',width='+w+',height='+h+',scrollbars=1,resizable=1');
	w.focus();
	return false;
}

//cookies

function setCookie(name, value) {
    if((name+'='+escape(value)).length<=4000) document.cookie = name+'='+escape(value)+'; path=/';
}

function getCookie(name) {
    name += '=';
    var c1 = document.cookie.indexOf(name);
    if(c1==-1) return null;
    var c2 = document.cookie.indexOf(";", c1+name.length);
    if(c2==-1) c2 = document.cookie.length;
    return unescape(document.cookie.substring(c1+name.length, c2));
}

function deleteCookie(name) {
    if(getCookie(name)) document.cookie = name + '=; path=/; expires=Thu, 01-Jan-70 00:00:01 GMT';
}

// util

function nextTag(node,tags){
	while(node = node.nextSibling) if(node.tagName) if((' '+tags+' ').indexOf(' '+node.tagName.toLowerCase()+' ')!=-1) return node;
	return false;
}

function getTarget(node){
	var t = node.title.split('|')[0];
	if(t.length>1) return nod('#'+t);
	else{
		var x;
		if(x=nextTag(node,TOGGLE_TAGS)) return x;
		if(x=nextTag(node.parentNode,TOGGLE_TAGS)) return x;
	}
	return false;
}

function bind(sel,fn,event){
	//window.event.cancelBubble = true;
	if(typeof(sel)=='string') sel = nod(sel);
	if(!isArray(sel)) sel = [sel];
	for(var i=0;i<sel.length;i++) if(sel[i]){
		if(1==event) fn(sel[i]);
		else{
			var tag = sel[i].tagName.toLowerCase();
			if(tag=='form') sel[i].onsubmit = fn;
			else if(tag=='select') sel[i].onchange = fn;
			else sel[i].onclick = fn;
		}
	}
}

// nod(#id|tag|.class|tag.class) - returns node for #id, else array of nodes
// nod(selector, node) - same but search inside node
function nod(sel,context) {
	if(sel.substr(0,1)=='#') return sel.length>1 ? document.getElementById(sel.substr(1)) : null;
	else{
		var cls = sel.split('.');
		var tag = cls.shift();
		cls = cls.shift();

		r = new Array();
		if(!context) context = document;
		if(!tag) tag = '*';
		var els = context.getElementsByTagName(tag);//Object
		//convert Object to Array
		for(var i=0;i<els.length;i++) if(!cls || (' '+els[i].className+' ').indexOf(' '+cls+' ')!=-1) r.push(els[i]);
		return r;
	}
}

function isArray(obj) {
	return Object.prototype.toString.call(obj) === '[object Array]'; 
	//return obj.constructor.toString().indexOf("Array") != -1
}

//add event on window load
function addLoadEvent(func) {
	var oldonload = window.onload;
	if(typeof window.onload != 'function') window.onload = func;
	else{
		window.onload = function() {
			if(oldonload) oldonload();
			func();
		}
	}
}
