$(document).ready(proc_init);

function proc_init() {
	selectbox();
	
	$("#submenu dl.select span.select-active").click( function() {
		toggle_submenu(this);
	});
	$("dl.toggle1 dt span.toggle").click( function() {
		toggle1(this);
	});

	var u = document.location.toString();
	if (u.indexOf('#:') != -1){
		var d = u.split("#:");

		if (d[1] && $_("tab-" + d[1])) {
			toggle_tab(d[1]);
		}
	}
}

function $_(n) {
	if(!n) return false;
	if(!document.getElementById(n)) return false;
	return document.getElementById(n)
}

function isNumber(e) {
	if ((e.keyCode < 48) || (e.keyCode > 57))
		e.returnValue = false;
}

function isLatin(e, _this) {
	if ($_('note-latin')) $('#note-latin').remove();
	if (e.keyCode > 122) {
		var msg = isLatinMsg ? isLatinMsg : '';
		$(_this.parentNode).after('<div id="note-latin" class="note">' + msg + '</div>');
		e.returnValue = false;
	}
}

function selection_disable() {
	var sel;
	if(document.selection && document.selection.empty) {
		document.selection.empty();
	} else if(window.getSelection) {
		sel=window.getSelection();
		if(sel && sel.removeAllRanges) sel.removeAllRanges() ;
	}
}

function bubbling_no(e) {
	e = bubbling_ie(e); /* for ie */
	if (!e.cancelBubble) e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
	return false;
}
function bubbling_ie( e ) {
	if (!e) var e = window.event;
	return e;
}

function selectbox(){
	var a = $("dl.select").get();
	var aLength = a.length;
	if(aLength > 0) {
		var z = 100;

		for(i=0; i<aLength; i++) {
			a[i].style.zIndex = z - i;
			a[i].parentNode.style.width = a[i].offsetWidth + "px";;
		}

		function selectbox_close() {
			$("dl.select-active").removeClass("select-active");
		}

		$(document).click(selectbox_close);

		$("dl.select").click( function(e) {
			bubbling_no(e);
			if(this.className == "select") {
				selectbox_close();
				this.className = "select select-active"
			} else {
				this.className = "select";
			}

		});

		$("dl.select").dblclick(selection_disable);
	}
};

function loading_set(id) {
		var w = $_(id).offsetWidth + "px";
		var h = $_(id).offsetHeight + "px";

		$("#"+id+" div.loading").css({ width:w, height:h });
}

function loading_show(id) {
	$("#"+id).prepend("<div class=\"preloading\"><div></div></div>");
	loading_set(id);
	
	$(window).resize(function() {
		loading_set(id);
	});
}

function scroll_top(id) {
	var s = bounds($_(id)).top - 20;	
	scroll(0, s);
}

function toggle_tooltab(_this, id) {
	var tab = _this.parentNode.parentNode.parentNode;
	$("#toolbar-tags div.toolcontent").css({ display:"none" });
	
	if (tab.className.indexOf("tooltab-active") == -1) {
		$("#toolbar").addClass("toolbar-active");
		$("#toolbar div.tooltab").removeClass("tooltab-active");
		tab.className = "tooltab tooltab-active";
		$("#toolcontent").slideDown("fast");
		$("#toolcontent div.toolcontent").css({ display:"none" });
		$("#"+id).css({ display:"block" });
	} else {
		tab.className = "tooltab";
		$("#toolbar").removeClass("toolbar-active");
		$("#toolcontent").slideUp("fast");
	}
}

function toggle_submenu(obj) {
	$("div.submenu-group").css({ display:"none" });
	$("div.submenu-head span.select-deactive").removeClass("select-deactive");
	$("#"+obj.id+"-content").fadeIn("fast");
	$_("menu_active").innerHTML = obj.innerHTML;
	obj.className = "select-active select-deactive";
}

function toggle1(obj) {
	selection_disable();

	if(obj.className == "toggle") {
		$("#"+obj.id+"-content").fadeIn("fast");
		obj.className = "toggle toggle-active";
	} else {
		$("#"+obj.id+"-content").fadeOut("fast");
		obj.className = "toggle";
	}
}

function toggle(this_, id) {
	selection_disable();

	if($_(id)) {
		if(this_.className == "toggle") {
			$("#"+id).slideDown("fast");
			this_.className = "toggle toggle-active";
		} else {
			$("#"+id).slideUp("fast");
			this_.className = "toggle";
		}
	}
}

function toggle_dlist(this_, id, g) {
	selection_disable();

	if($_(g) && $_(id)) {
		if(this_.className == "toggle-active") {
			$("#"+id).css({ display:"none" });
			this_.className = "toggle";
		} else {
			$("#"+g+" dt").removeClass("toggle-active");
			$("#"+g+" dd").css({ display:"none" });
			this_.className = "toggle-active";
			$("#"+id).slideDown("fast");
		}
	}
}

function toggle_bubble(id, bubble) {
	selection_disable();

	var obj = $_(id);

	if(obj) {
		if(obj.style.display == "none") {
			$("#"+id).fadeIn("fast");
			if (bubble) $("#"+bubble).fadeIn("fast");
		} else {
			$("#"+id).fadeOut("fast");
			if (bubble) $("#"+bubble).fadeOut("fast");
		}
	}
}

function toggle_tab(d, group) {
	selection_disable();

	if ($_("tab-" + d) && $_("content-" + d)) {
		if ($_("tab-" + d).className == "active") return;
		
		group = group ? "#" + group + " " : "";

		var u = document.location.toString();
		u = u.split("#");
		document.location = u[0] + "#:" + d;

		$(group + "div.bookmarks-tabs li").removeClass("active");
		$(group + "div.bookmarks-content").css({ display:"none" });

		$_("tab-" + d).className = "active";
		$_("content-" + d).style.display = "block";
	}
}

function toggle_order (_this, url, el) {
	selection_disable();

	if($_(el).style.display == "none") {
		_this.className = "toggle toggle-active";
		_this.parentNode.parentNode.className = "active";
		$_(el).style.display = "";
		$.ajax({
			url: url,
			cache: false,
			success: function(html){
				$("#"+el).html(html);
			}
		});
	} else {
		_this.className = "toggle";
		_this.parentNode.parentNode.className = "";
		$_(el).style.display = "none";
		$("#"+el).html("");
	}
}

function toggle_info(id, c) {
	selection_disable();

	var obj = $_(id);

	if (obj) {
		if (obj.style.display == "none") {
			$("div."+c).css({ display:"none" });
			$(obj).fadeIn("fast");
		}
	} else {
		$("div."+c).css({ display:"none" });
	}
}

function toggle_popular(obj, url) {
	$("#popular span.select-deactive").removeClass("select-deactive");
	$_("popular_active").innerHTML = obj.innerHTML;
	obj.className = "select-active select-deactive";
	$.ajax({
		url: url,
		cache: false,
		success: function(html){
			$("#popular_content").html(html);
		}
	});
}

function toggle_registr(_this, id) {
	var tab = _this.parentNode.parentNode.parentNode;
	if (tab.className.indexOf("b6-active") == -1) {
		var b = $_(id);
		$("div.b6-tabs div.b6").removeClass("b6-active");
		$(tab).addClass("b6-active");
		$("div.ext-fields").fadeOut("fast");
		if (b) $(b).fadeIn("fast");
	}
	_this.blur();
}

function load_page(url, id) {
	$.ajax({
		type: "get",
		url: url,
		success: function(html) {
			$("#"+id).html(html);
		}
	});
}

function load_form(url, id, submit, _this) {
	var data = _this && submit ? submit+"=1&"+$(_this).serialize() : "";
	var type = _this && submit ? "post" : "get";
	$.ajax({
		type: type,
		url: url,
		data: data,
		success: function(html) {
			$("#"+id).html(html);
		}
	});
}

function email_print(name, dom, a, display) {
	var m = "mailto:";
	document.write('<a href="'+m+name+'@'+dom+'.'+a+'">'+(display?display:name+'@'+dom+'.'+a)+'</a>');
}


/* http://www.kryogenix.org/code/browser/searchhi/ */
/* Modified 20021006 to fix query string parsing and add case insensitivity */
/* Modified 20070316 to stop highlighting inside nosearchhi nodes */
function highlightWord( node, word ) {

	if (!node || !word) return;
	// Iterate into this nodes childNodes
	if (node.hasChildNodes) {
		var hi_cn;
		for (hi_cn=0;hi_cn<node.childNodes.length;hi_cn++) {
			highlightWord(node.childNodes[hi_cn],word);
		}
	}

	// And do this node itself
	if (node.nodeType == 3) { // text node
		tempNodeVal = node.nodeValue.toLowerCase();
		tempWordVal = word.toLowerCase();
		if (tempNodeVal.indexOf(tempWordVal) != -1) {
			pn = node.parentNode;
			// check if we're inside a "nosearchhi" zone
			checkn = pn;
			while (checkn.nodeType != 9 &&
			checkn.nodeName.toLowerCase() != 'body') {
			// 9 = top of doc
				if (checkn.className.match(/bnosearchhib/)) { return; }
				checkn = checkn.parentNode;
			}
			if (pn.className != "searchword") {
				// word has not already been highlighted!
				nv = node.nodeValue;
				ni = tempNodeVal.indexOf(tempWordVal);
				// Create a load of replacement nodes
				before = document.createTextNode(nv.substr(0,ni));
				docWordVal = nv.substr(ni,word.length);
				after = document.createTextNode(nv.substr(ni+word.length));
				hiwordtext = document.createTextNode(docWordVal);
				hiword = document.createElement("span");
				hiword.className = "searchword";
				hiword.appendChild(hiwordtext);
				pn.insertBefore(before,node);
				pn.insertBefore(hiword,node);
				pn.insertBefore(after,node);
				pn.removeChild(node);
			}
		}
	}
}

function highlightSentence( node, str ) {

	var words = str.split(/\s+/);
	for (var i = 0; i < words.length; i++) {
		highlightWord( node, words[i] );
	}

}

function setCookie( name, value, expires, path, domain, secure ) {
	if (!name) return;
	//else name = '636-'+name;
	var path = "/";
	var today = new Date();
	today.setTime(today.getTime());

	if (expires) {
		expires = expires * 1000 * 60 * 60 * 24;
	}

	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name+'='+escape(value) +
		((expires) ? ';expires='+expires_date.toGMTString() : '') +
		((path) ? ';path=' + path : '') +
		((domain) ? ';domain=' + domain : '') +
		((secure) ? ';secure' : '');
}

function setGoodListStyle( type ) {
	setCookie('shop-view', type, 365);
	location.href = location.href;
}

function xwin(url, w, h) {
	w = w ? w : 800;
	h = h ? h : 600;
	pos_left = (screen.width) ? (screen.width-w)/2 : 0;
	pos_top  = (screen.height) ? (screen.height-h)/3 : 0;

	settings = "width=" + w + ",height=" + h + ",top=" + pos_top + ",left=" + pos_left + ",scrollbars=yes";
	win = window.open(url,"xwin",settings)
	win.focus();
}
