/* debug */
function trace() {
	var debug = document.getElementById('debug');
	if (!debug) {
		var debug = document.createElement('div');
		debug.setAttribute('id', 'debug');
		document.body.appendChild(debug);
		/*
		debug.onclick = function() {
			if (this.closed) {
				this.style.height = '200px';
				this.closed = false;
			} else {
				this.style.height = '2px';
				this.closed = true;
			}
		}
		*/
	}
	if (debug) {
		var o = '';
		o = 'arguments.length = ' + arguments.length + '<br />';
//		o += arguments;
		for (var i=0; i<arguments.length; i++) {
			o += '<div style="margin-left: 16px;">';
			o += arguments[i];
			for (j in arguments[i]) {
				o += '<div style="margin-left: 16px;">';
				o += '<b>' + j +'</b>&nbsp;=&nbsp;' + String(arguments[i][j]).split('<').join('&lt;').split('>').join('&gt;').split('\"').join('&raquo;');
				o += '</div>';
			}				
			o += '</div>';
		}
		debug.innerHTML = o;
	}
}
/* /debug */



/* preset */
var oID = document.getElementById;
var IE = /*@cc_on!@*/false;
var IE6 = (IE && (navigator['appVersion'].indexOf('MSIE 6') > 0)) ? true : false;
/* /preset */



/* event handlers */
function addEvent(o, sEvent, fFunc) {
	if (window.attachEvent) {
		o.attachEvent('on' + sEvent, fFunc);
	} else if (window.addEventListener) {
		o.addEventListener(sEvent, fFunc, false);
	}
}

function addLoadEvent(fFunc) {
	addEvent(window, 'load', fFunc);
}

function addDOMLoadEvent(func) {
	if (!window.__load_events) {

		var init = function () {
			if (arguments.callee.done) return; // quit if this function has already been called
			arguments.callee.done = true; // flag this function so we don't do the same thing twice
			if (window.__load_timer) { // kill the timer
				clearInterval(window.__load_timer);
				window.__load_timer = null;
			}
			for (var i=0; i<window.__load_events.length; i++) { // execute each function in the stack in the order they were added
				window.__load_events[i]();
			}
			window.__load_events = null;
		};

		// for Mozilla/Opera9
		if (document.addEventListener) {
			document.addEventListener('DOMContentLoaded', init, false);
		}

		// for Internet Explorer
		/*@cc_on @*/
		/*@if (@_win32)
			document.write('<scr'+'ipt id=__ie_onload defer src=//0><\/scr'+'ipt>');
			var script = document.getElementById('__ie_onload');
			script.onreadystatechange = function() {
				if (this.readyState == 'complete') {
					init(); // call the onload handler
				}
			};
		/*@end @*/

		// for Safari
		if (/WebKit/i.test(navigator.userAgent)) { // sniff
			window.__load_timer = setInterval(function() {
				if (/loaded|complete/.test(document.readyState)) {
					init(); // call the onload handler
				}
			}, 10);
		}

		// for other browsers
		window.onload = init;

		// create event function stack
		window.__load_events = [];
	}

	// add function to event stack
	window.__load_events.push(func);
}
/* /event handlers */



/* trim */
String.prototype.trim = function () {
	return this.replace(/^\s+|\s+$/g, "");
}
/* /trim */



/* QWEswfobj for IE*/
// disable the blinking when set outerHTML, may need to load external css file, where only flash objects have display: none property
if (IE) document.writeln('<style type="text/css">object {display: none;}</style>');
// rewrite satay swf objects
function QWEswfobj() {
	var swf = '';
	var obj = document.getElementsByTagName('object');
	for (var i=0; i<obj.length; i++) {
		if ((obj[i].getAttribute('type') == 'application/x-shockwave-flash') || (obj[i].getAttribute('classid') == 'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000')) {
			swf = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'
			swf += (obj[i].getAttribute('id')) ? ' id="' + obj[i].getAttribute('id') + '"' : '';
			swf += (obj[i].getAttribute('width')) ? ' width="' + obj[i].getAttribute('width') + '"' : '';
			swf += (obj[i].getAttribute('height')) ? ' height="' + obj[i].getAttribute('height') + '"' : '';
			swf += '>' + "\n";
			for (var j=0; j<obj[i].childNodes.length; j++) {
				if ((obj[i].childNodes[j].nodeName) && (obj[i].childNodes[j].nodeName.toLowerCase() == 'param')) {
					swf += '<param name="'+obj[i].childNodes[j].getAttribute('name')+'" value="'+obj[i].childNodes[j].getAttribute('value')+'" />'+"\n";
				} else if ((obj[i].childNodes[j].nodeName) && (obj[i].childNodes[j].nodeName.toLowerCase() == 'div')) {
					swf += '<div>' + obj[i].childNodes[j].innerHTML + '</div>' + "\n";				
				}
			}
			swf += '</object>'+"\n";
			obj[i].outerHTML = swf;
		}
		obj[i].style.display = 'block';
	}
}
/* /QWEswfobj */



/* :hover for IE6 */
function hover(obj) {
	obj.onmouseover = function() {
		this.className += ' hover';
	}
	obj.onmouseout = function() {
		this.className = this.className.split('hover').join('');
	}
}
/* /:hover for IE6 */



/* get elements by className */
function getElementsByClassName(parentNode, className) {
	var result = [];
	if (!className) {
		if (typeof(parentNode) == 'string') {
			var className = parentNode;
			parentNode = document;
		} else {
			return result;
		}
	}
	var obj = parentNode.getElementsByTagName('*');
	for (var i=0; i<obj.length; i++) {
		if (obj[i].className.match(new RegExp('\\b' + className + '\\b'))) {
			result.push(obj[i]);
		}
	}
	return result;
}
/* /get elements by className */



/* get style */
function getStyle(obj, styleProp) {
	var result = (window.getComputedStyle) ? document.defaultView.getComputedStyle(obj, null).getPropertyValue(styleProp) : ((obj.currentStyle) ? obj.currentStyle[styleProp] : '');
	if (String(result).indexOf('rgb') >= 0) {
		var h = '0123456789abcdef';
		var d = result.split('rgb(')[1].split(')')[0].split(' ').join('').split(',');
		result = '#' + h[d[0] >> 4] + h[d[0] % 16] + h[d[1] >> 4] + h[d[1] % 16] + h[d[2] >> 4] + h[d[2] % 16];
	}
	return result;
}
/* /get style */



/* get dimension */
function getDimension(obj) {
	var pobj = obj;
	obj.x = 0;
	obj.y = 0;
	obj._x = obj.offsetLeft;
	obj._y = obj.offsetTop;
	obj._width = obj.offsetWidth;
	obj._height = obj.offsetHeight;
	while (pobj.nodeName != 'BODY') {
		obj.x += pobj.offsetLeft;
		obj.y += pobj.offsetTop;
		pobj = pobj.parentNode;
	}
	return {x: obj.x, y: obj.y, _x: obj._x, _y: obj._y, _width: obj._width, _height: obj._height};
}
/* /get dimension */



/* get mouse */
function getMouse(e) {
	e = e || window.event;
	return {x: (e.pageX || e.clientX + document.documentElement.scrollLeft), y: (e.pageY || e.clientY + document.documentElement.scrollTop)};
}
/* /get mouse */



/* scroll object */
function scrollobjDirect(obj, x, y) {
	obj.style.left = String(Math.round(x)) + 'px';
	obj.style.top = String(Math.round(y)) + 'px';
}

function scrollobj(obj, hspace, vspace) {
	var outer = obj.parentNode;
	outer.inner = obj;
	outer.hspace = hspace;
	outer.vspace = vspace;
	outer.onmousemove = function(e) {
		getDimension(this.inner);
		getDimension(this);
		scrollobjDirect(this.inner, Math.min(0, Math.max(this._width-this.inner._width, (this._width-this.inner._width)*(getMouse(e).x-this.x-this.hspace)/(this._width-2*this.hspace))), Math.min(0, Math.max(this._height-this.inner._height, (this._height-this.inner._height)*(getMouse(e).y-this.y-this.vspace)/(this._height-2*this.vspace))));
	}
}
/* /scroll object */



/* label in input field */
function labelin() {
	var field = getElementsByClassName('labelin');
	for (var i=0; i<field.length; i++) {
		if ((String(' input textarea ').indexOf(field[i].nodeName.toLowerCase()) >= 0) && (String(' text password textarea ').indexOf(field[i].type || 'textarea') >= 0)) {
			getDimension(field[i]);
			field[i].labelin = document.createElement('label');
			field[i].labelin.className = 'labelin';
			field[i].labelin.style.left = String(field[i]._x) + 'px';
			field[i].labelin.style.top = String(field[i]._y) + 'px';
			field[i].labelin.innerHTML = field[i].title || 'untitled';
			field[i].labelin.field = field[i];
			field[i].labelin.onclick = field[i].labelin.onfocus = function() {
				this.style.display = 'none';
				this.field.focus();
			}
			field[i].onfocus = function() {
				this.labelin.style.display = 'none';
			}
			field[i].onblur = function() {
				this.labelin.style.display = (this.value.length == 0) ? 'block' : 'none';
			}
			field[i].labelin.style.display = (field[i].value.length == 0) ? 'block' : 'none';
			field[i].parentNode.appendChild(field[i].labelin);
		}
	}
}
/* /label in input field */



/* gallery */
var classGallery = function() {

	this.g = Array();
	this.tmaxlen = 27;

	this.init = function(obj) {
		this.g.push(obj);
		obj.items = Array();
		var div = obj.getElementsByTagName('div');
		var tmp;
		for (var i=0; i<div.length; i++) {
			if (div[i].className.indexOf('item') >= 0) {
				obj.items.push(div[i]);
				div[i].index = obj.items.length-1;
				// product item title trim
				if (obj.className.indexOf('products') >= 0) {
					tmp = div[i].getElementsByTagName('h3')[0];
					tmp.textnode = tmp.childNodes[0].childNodes[0];
					tmp.title = tmp.textnode.nodeValue.trim();
					if (tmp.title.length > this.tmaxlen) {
						if (tmp.title.indexOf(' ', this.tmaxlen) >= 0) {
							if (this.tmaxlen - tmp.title.substr(0, this.tmaxlen).lastIndexOf(' ') < tmp.title.indexOf(' ', this.tmaxlen) - this.tmaxlen) {
								tmp.textnode.nodeValue = tmp.title.substr(0, tmp.title.substr(0, this.tmaxlen).lastIndexOf(' ')) + '...';
							} else {
								tmp.textnode.nodeValue = tmp.title.substr(0, tmp.title.indexOf(' ', this.tmaxlen)) + '...';
							}
						} else {
							tmp.textnode.nodeValue = tmp.title.substr(0, this.tmaxlen) + '...';
						}
					}
					div[i].attribs = div[i].getElementsByTagName('div')[1].innerHTML;
				}
				div[i].a = div[i].getElementsByTagName('a');
				for (var j=0; j<div[i].a.length; j++) {
					div[i].href = div[i].a[j].href;
					div[i].a[j].galleryindex = this.g.length-1;
					div[i].a[j].itemindex = obj.items.length-1;
					div[i].a[j].onclick = function() {
						gallery.show(this.galleryindex, this.itemindex);
						return false;
					}
				}
			}
		}
	}

	this.show = function(i, j) {
		gallery.hide();
		var obj = gallery.g[i].items[j];
		var tmp;
		var o = '';
		var img = obj.getElementsByTagName('img')[0].src.split('/_t').join('');
		o += '<div class="inner">';
		o += (obj.parentNode.className.indexOf('products') >= 0) ? '<h6>' + obj.getElementsByTagName('h3')[0].title + '<div class="close" onclick="gallery.hide();">x</div></h6>' : '';
		o += '<div class="img"><img src="' + img + '" alt="" onclick="gallery.hide();" /></div>';
		o += (obj.attribs) ? '<div class="attributes">' + obj.attribs + '</div><div class="clearboth"></div>' : '';
		if (gallery.g[i].items.length > 1) {
			o += '<div class="paging">';
			tmp = (j - 1 + gallery.g[i].items.length) % gallery.g[i].items.length;
			o += '<a href="' + gallery.g[i].items[tmp].href + '" onclick="gallery.show(' + i + ', ' + tmp + '); return false;" class="prev bulli bullr inv">előző</a>';
			tmp = (j + 1) % gallery.g[i].items.length;
			o += '<a href="' + gallery.g[i].items[tmp].href + '" onclick="gallery.show(' + i + ', ' + tmp + '); return false;" class="next bulli bullr inv">következő</a>';
			o += '</div>';
		}
		o += '</div>';
		o += '<div class="bg_lt"></div><div class="bg_t"></div><div class="bg_rt"></div><div class="bg_l"></div><div class="bg_r"></div><div class="bg_lb"></div><div class="bg_b"></div><div class="bg_rb"></div>';
		var cover = document.getElementById('fixed');
		if (cover) {
			cover.popup = document.createElement('div');
			cover.popup.className = 'popup';
			cover.popup.innerHTML = o;
			cover.appendChild(cover.popup);
			cover.style.display = 'block';
			if (cover.popup.scrollHeight > 600) {
				cover.popup.style.top = '40%';
			}
		}
		if (obj.className.indexOf('visited') < 0) {obj.className += ' visited';}
		return false;
	}

	this.hide = function() {
		var cover = document.getElementById('fixed');
		if (cover) {
			if (cover.popup) {cover.removeChild(cover.popup);}
			cover.popup = null;
			cover.style.display = 'none';
		}
	}

}

var gallery = new classGallery();
/* /gallery */



/* initialization */
function DOMLoadInit() {

	var tmp = null;

	/* execute QWEswfobj in IE */
	if (IE) QWEswfobj();
	/* /execute QWEswfobj in IE */

	/* IE6 bugfix */
	if (IE6) {
		var cite = document.getElementsByTagName('cite');
		for (var i=0; i<cite.length; i++) {
			hover(cite[i]);
		}
	}
	/* /IE6 bugfix */

	/* input init */
	var input = document.getElementsByTagName('input');
	for (var i=0; i<input.length; i++) {
		if (IE6 && (input[i].parentNode.className.indexOf('submit') >= 0)) {hover(input[i]);}
		if (input[i].type.indexOf('hidden') >= 0) {input[i].className += ' hidden';}
	}
	/* /input init */

	/* modify links */
	var a = document.getElementsByTagName('a');
	for (var i=0; i<a.length; i++) {
		if (a[i].className.indexOf('_blank') >= 0) {
			a[i].onclick = function() {
				this.blur();
				window.open(this.href);
				return false;
			}
		}
	}
	/* /modify links */

	/* label in input */
	labelin();
	/* /label in input */
	
	/* product item */
	var div = document.getElementsByTagName('div');
	for (var i=0; i<div.length; i++) {
		if (div[i].className.indexOf('gallery') >= 0) {
			gallery.init(div[i]);
		}
	}
	/* /product item */
	
	/* popup */
	if (document.getElementById('popup')) {
		document.getElementById('fixed').style.display = 'block';
	}
	/* /popup */
}

function onLoadInit() {
}

addDOMLoadEvent(DOMLoadInit);

addLoadEvent(onLoadInit);
/* /initialization */


