
	window.onload = function ()
	{
		/*cbHandler.setMode(cbHandler._auto);

		if (cbHandler._auto == cbHandler.getMode())
		{
			var reg = utils.elmsByClass(cbHandler.clsReg);
			var sel = utils.elmsByClass(cbHandler.clsSel);

			var _ev = new Array
				(
					{"event" : "mousedown", "action" : "cbHandler.mousedown(this);"},
					{"event" : "contextmenu", "action" : ""}
				);

			utils.traverseAndAttach(reg, _ev);
			utils.traverseAndAttach(sel, _ev);
		}*/
	}

	document.onmouseup = function ()
	{
		//hScroll.stopScrolls();
	}

	var utils =
		{
			cClass : function (elm, cls)
			{
				elm.className = cls;
			},
			findPos : function (obj)
			{
				var curleft = curtop = 0;

				if (obj.offsetParent)
				{
					do
					{
						curleft += obj.offsetLeft;
						curtop 	+= obj.offsetTop;
					}
					while (obj = obj.offsetParent);

					return {'x' : curleft, 'y' : curtop};
				}
			},
			elmsByClass : function (cls, tag, elm)
			{
				if (!tag) tag = '*';
				if (!elm) elm = document;

				var retnode = [];
				var myclass = new RegExp('\\b' + cls + '\\b');
				var elem 		= elm.getElementsByTagName(tag);

				for (var i = 0 ; i < elem.length ; i++)
				{
					var classes = elem[i].className;
					if (myclass.test(classes)) retnode.push(elem[i]);
				}

				return retnode;
			},
			handleDefaultVal : function (elm, cond)
			{
				if (!cond) cond = 0;

				switch (cond)
				{
					case 0: if (elm.defaultValue == elm.value) elm.value = ''; break;
					case 1: if ('' == elm.value) elm.value = elm.defaultValue; break;
				}
			},
			showHideElms : function ()
			{
				for (var x = 0 ; x < arguments.length ; x++)
				{
					this.showHideElm(arguments[x], 'block');
				}
			},
			showHideElm : function (elm, visible)
			{
				switch (elm.style.display)
				{
					case visible: elm.style.display = 'none'; break;
					case 'none'	: elm.style.display = visible;
				}
			},
			traverseAndAttach : function (arrElm, arrEv)
			{
				for (var x = 0 ; x < arrElm.length ; x++)
				{
					for (var y = 0 ; y < arrEv.length ; y++)
					{
						this.attach(arrElm[x], arrEv[y]['event'], arrEv[y]['action']);
					}
				}
			},
			attach : function (elm, ev, act)
			{
				switch (ev)
				{
					case 'mousedown':
						elm.onmousedown = function ()
						{
							eval(act); return false;
						}
						break;
					case 'contextmenu':
						elm.oncontextmenu = function ()
						{
							eval(act); return false;
						}
				}
			}
		};

	var storage =
		{
			dataList : new Object(),
			addItem : function (key, val)
			{
				if ('undefined' == typeof(this.dataList[key]))
				{
					this.dataList[key] = val;

					return true;
				}

				return false;
			},
			getItem : function (key)
			{
				if ('undefined' != typeof(this.dataList[key]))
				{
					return this.dataList[key];
				}

				return false;
			},
			remItem : function (key)
			{
				if ('undefined' != typeof(this.dataList[key]))
				{
					delete this.dataList[key];

					return true;
				}

				return false;
			},
			empty : function ()
			{
				delete this.dataList;

				this.dataList = new Object();
			}
		};

	var queue =
		{
			dataList : new Object(),
			transfer : function (obj)
			{
				this.dataList &= obj;
			},
			resolveAll : function (key)
			{
				if ('undefined' != typeof(this.dataList[key]))
				{
					while (0 < this.dataList[key].length)
					{
						this.resolveNext(key);
					}
				}
			},
			resolveNext : function (key)
			{
				if ('undefined' != typeof(this.dataList[key]))
				{
					if ('undefined' != typeof(eval(this.dataList[key])))
					{
						eval(this.dataList[key][0]);

						delete this.dataList[key][0];

						this.rearrange(key);
					}
				}
			},
			rearrange : function (key)
			{
				if ('undefined' != typeof(this.dataList[key]))
				{
					var newList = new Array();
					var cnt 		= 0;

					for (var x in this.dataList[key])
					{
						newList[cnt] = this.dataList[key][x];
						cnt++;
					}

					this.dataList[key] = newList;
				}
			}
		};

	var ajax =
		{

		};

	var sBasket =
		{

		};

	var cbHandler =
		{
			clsSel : 'checkBoxChecked',
			clsReg : 'checkBox',
			valDump : '.dump',
			groupActions : new Object(),
			selected : new Object(),
			groupTypes : new Object(),
			selElements : new Array(),

			_radio : 0,
			_check : 1,

			_auto 	: 1,
			_manual : 2,

			setMode : function (mode)
			{
				this.mode = mode;
			},
			getMode : function ()
			{
				return this.mode;
			},
			mousedown : function (i)
			{
				var group = (-1 != i.id.indexOf('_')) ? i.id.split('_')[0] : i.id;

				if (i.id != this.selected[group] || this._check == this.groupTypes[group])
				{
					this.handleGroup(i.id, group);
					this.changeClass(i);

					if ('undefined' != typeof(this.groupActions[group])) eval(this.groupActions[group]);
				}
			},
			dumpValue : function (group, value)
			{
				if (document.getElementById(group + '_' + this.valDump))
				{
					document.getElementById(group + '_' + this.valDump).value = value;
				}
			},
			changeClass : function (i)
			{
				switch (i.className)
				{
					case this.clsSel: i.className = this.clsReg; break;
					case this.clsReg: i.className = this.clsSel;
				}
			},
			traverseGroup : function (group)
			{
				var _group;
				var elm;

				for (var x in this.selElements)
				{
					elm			= this.selElements[x];
					_group 	= (-1 != elm.id.indexOf('_')) ? elm.id.split('_')[0] : elm.id;

					if (_group == group)
					{
						this.selected[group] = elm.id;
						this.dumpValue(group, elm.id.split('_')[1]);

						delete this.selElements[x];
					}
				}
			},
			handleGroup : function (id, group)
			{
				var prev = this.selected[group];
				var val;

				if (document.getElementById(prev) && this._radio == this.groupTypes[group])
				{
					this.changeClass(document.getElementById(prev));
				}

				if (prev == id && this._check == this.groupTypes[group])
				{
					this.selected[group] = 0;
					this.dumpValue(group, '');
				}
				else
				{
					val = id.split('_')[1];

					this.selected[group] = id;
					this.dumpValue(group, val);
				}
			},
			setGroupAction : function (group, action)
			{
				if (group) this.groupActions[group] = action;
			},
			define : function ()
			{
				this.selElements = utils.elmsByClass(this.clsSel, 'div');

				var groups = arguments;

				for (var x = 0 ; x < groups.length ; x++)
				{
					this.groupTypes[groups[x][0]] = groups[x][1];
					this.traverseGroup(groups[x][0]);
				}
			}
		};

	var ddHandler =
		{

		};

	var hScroll =
		{
			_left : 0,
			_right : 1,
			intervals : new Object(),
			scroll : function (id, d, spd)
			{
				if (id && 'undefined' == typeof(this.intervals[id]))
				{
					this.intervals[id] = setInterval("hScroll.performScroll('" + id + "', " + d + ", " + spd + ");", 50);
				}
			},
			performScroll : function (id, d, spd)
			{
				try
				{
					var elm = document.getElementById(id);
				} catch (e) { }

				if (elm)
				{
					switch (d)
					{
						case this._left	: elm.scrollLeft -= spd; break;
						case this._right: elm.scrollLeft += spd;
					}
				}
			},
			stopScrolls : function ()
			{
				for (var x in this.intervals)
				{
					clearInterval(this.intervals[x]);

					delete this.intervals[x];
				}
			},
			stopScroll : function (id)
			{
				clearInterval(this.intervals[id]);

				delete this.intervals[id];
			}
		};

	var gallery =
		{
			containers : new Object(),
			selected : new Object(),
			pictures : new Object(),
			scrolls : new Object(),
			overlayCls : 'gallery_unique_overlay',
			thumbs : new Object(),
			actions : new Object(),
			archAct : new Object
				({
					'photocount'	: null,
					'showcomment'	: null
				}),
			galleryDissonance : new Object(),
			_next : 1,
			_prev : -1,
			loadPicture : function (id, pid)
			{
				try
				{
					var elm = document.getElementById(id);
				} catch (e) { }

				if (elm && 'undefined' != typeof(this.containers[id]))
				{
					// siia custom actionite käivitamised

					if ('undefined' != typeof(this.actions[id].photocount))
					{
						var func = this.actions[id].photocount + "(" + this.findPicture(id, pid) + ", " + this.pictures[id].length + ");";
						eval(func);
					}
					if ('undefined' != typeof(this.actions[id].showcomment))
					{
						var func = this.actions[id].showcomment + "(" + this.findPicture(id, pid) + ");";
						eval(func);
					}

					this.handleThumbs(id, pid);
					this.scrollTo(id, pid);
					this.setOverlay(id);

					elm.src = this.containers[id].replace(/IMGID/, pid);
					this.selected[id] = pid;
				}
			},
			handleThumbs : function (id, pid)
			{
				if ('undefined' != typeof(this.thumbs[id]) && this.selected[id])
				{
					document.getElementById(this.thumbs[id]._id + this.selected[id]).parentNode.className = this.thumbs[id].reg;
					document.getElementById(this.thumbs[id]._id + pid).parentNode.className = this.thumbs[id].sel;
				}
			},
			scrollTo : function (id, pid)
			{
				if ('undefined' != typeof(this.scrolls[id]))
				{
					try
					{
						var stp = document.getElementById(this.scrolls[id].strip);
						var img = document.getElementById(this.thumbs[id]._id + pid);
					} catch (e) { }

					if (stp && img)
					{
						var p_img = utils.findPos(img);
						var p_stp = utils.findPos(stp);

						stp.scrollLeft = p_img.x - p_stp.x - (stp.clientWidth / 2) + this.galleryDissonance[id];
					}
				}
			},
			setOverlay : function (id)
			{
				try
				{
					var elm = document.getElementById(id).parentNode;
				} catch (e) { }

				if (elm)
				{
					if (!document.getElementById(id + '_overlay'))
					{
						var w = elm.clientWidth;
						var h = elm.clientHeight;
						var p = utils.findPos(elm);

						var ovr = document.createElement('div');

						ovr.id						= id + '_overlay';
						ovr.className			= this.overlayCls;
						ovr.style.width 	= w + 'px';
						ovr.style.height 	= h + 'px';
						ovr.style.top		 	= p.y + 'px';
						ovr.style.left		= p.x + 'px';
						ovr.style.display	= 'block';

						document.body.appendChild(ovr);
					}
					else
					{
						document.getElementById(id + '_overlay').style.display = 'block';
					}
				}
			},
			clearOverlay : function (id)
			{
				try
				{
					document.getElementById(id + '_overlay').style.display = 'none';
				} catch (e) { }
			},
			defineCont : function (id, u)
			{
				this.containers[id] = u;

				document.getElementById(id).onload = function ()
				{
					gallery.clearOverlay(id);
				}
			},
			addPictures : function (id, arr)
			{
				if (arr instanceof Array)
				{
					this.pictures[id] = arr;
					this.selected[id] = arr[0];
				}
			},
			loadNextPrev : function (id, w)
			{
				if ('undefined' == typeof(this.selected[id])) this.selected[id] = this.pictures[id][0];

				var sel = this.pictures[id][(this.findPicture(id, this.selected[id]) + w)];

				if ('undefined' == typeof(sel))
				{
					switch (w)
					{
						case this._next : sel = this.pictures[id][0]; break;
						case this._prev : sel = this.pictures[id][this.pictures[id].length - 1];
					}
				}

				this.loadPicture(id, sel);
			},
			findPicture : function (id, pid)
			{
				for (var x = 0 ; x < this.pictures[id].length ; x++)
				{
					if (this.pictures[id][x] == pid) return x;
				}

				return false;
			},
			defineAutoscroll : function (id, strip)
			{
				this.scrolls[id] = new Object
					({
						'strip' 	: strip
					});
			},
			defineThumbs : function (id, reg, sel, pref)
			{
				this.thumbs[id] = new Object
					({
						'reg' : reg,
						'sel' : sel,
						'_id'	: pref
					});
			},
			setGalleryDissonance : function (id, num)
			{
				this.galleryDissonance[id] = num;
			},
			defineAction : function (id, when, act)
			{
				var tmp 				= this.archAct;
				tmp[when] 			= act;
				this.actions[id]= tmp;

				delete tmp;
			}
		};

	var gExtension =
		{
			changeCount : function (cur, total)
			{
				document.getElementById('gallery_big_photocount').innerHTML = '(' + (cur + 1) + ' / ' + total + ')';
			},
			showComment : function (num)
			{
				if (arrComments instanceof Array) document.getElementById('gallery_big_commentspn').innerHTML = arrComments[num];
			}
		};

	var svgHandler =
		{
			data : new Object(),
			msgID: 'franchise_data_container',
			sel	 : null,
			over : function (i)
			{
				if (null === this.sel)
				{
					if (this.data[i.id])
						document.getElementById(this.msgID).innerHTML = '<span class="franchise_country">' + this.data[i.id].title + '</span> : ' + this.data[i.id].short;
					else document.getElementById(this.msgID).innerHTML = 'Andmed riigi kohta puuduvad.';

					this.setClass(i, 'over');
				}
			},
			setClass : function (elms, cls)
			{
				var child = elms.firstChild;

				while (child != null)
				{
					if ('path' == child.nodeName) child.setAttribute('class', cls);

					child = child.nextSibling;
				}
			},
			out : function (i)
			{
				if (null === this.sel)
				{
					document.getElementById(this.msgID).innerHTML = '';

					this.setClass(i, ((0 == this.data[i.id].status) ? 'empty' : 'filled'));
				}
			},
			click : function (i)
			{
				if (this.data[i.id] && null === this.sel)
				{
					this.setClass(i, 'selected');
					this.sel = i;
					document.getElementById(this.msgID).innerHTML = '<span class="franchise_country">' + this.data[i.id].title + '</span> : ' + this.data[i.id].full;
				}
				else if (this.sel)
				{
					var tmp 	= this.sel;
					this.sel 	= null;

					this.out(tmp);
				}
			},
			addData : function (obj)
			{
				if (obj instanceof Object) this.data = obj;
			},
			register : function (evt)
			{
				var svg 	= evt.target.ownerDocument;
				var elms	= svg.documentElement;
				var child = elms.firstChild;
				var status;

				while (null !== child)
				{
					if ('g' == child.nodeName && child.id)
					{
						if (this.data[child.id])
						{
							status = this.data[child.id].status;

							switch (status)
							{
								case 0 : this.setClass(child, 'empty'); break;
								case 1 : this.setClass(child, 'filled');
							}
						}
					}

					child = child.nextSibling;
				}
			}
		};