var _upAnimPlay = false;
var _activeId = 0;
var _lastId = 0;
var _newsPlay = false;
var _newsSet = true;
var _showMenu = null;

function scrollUp(targetEl) {
	if (_upAnimPlay) {
		return;
	}
	_upAnimPlay = true;
	var scrollOffset = dojo.marginBox(targetEl.children[0]).h + 30;
	var b = dojo.marginBox(targetEl.children[targetEl.children.length - 1]);
	var lastTop = Math.round(b.t + b.h - scrollOffset + 30);
	dojo.forEach(targetEl.children, function(node, index) {
		var t = dojo.marginBox(node).t - scrollOffset;
		dojo.fx.slideTo({
			node: node,
			left: "0",
			top: t.toString(),
			unit: "px",
			onEnd: function() {
				if (0 == index) {
					dojo.place(node, targetEl);
					dojo.style(node, "top", lastTop.toString() + "px");
				}
				if ((targetEl.children.length - 1) == index) {
					_upAnimPlay = false;
				}
			}
		}).play();
	});
};

function newsSlider(targetEl) {
	if (_newsPlay) {
		return;
	}
	_newsPlay = true;
	var space = 3;
	if (_newsSet) {
		var l = 0;
		dojo.forEach(targetEl.children, function(node, index) {
			dojo.style(node, {
				"position": "absolute",
				"top": "0",
				"left": l.toString() + "px"
			});
			l += dojo.marginBox(node).w + space;
		});
		_newsSet = false;
	};
	var b = dojo.marginBox(targetEl.children[targetEl.children.length - 1]);
	var scrollOffset = dojo.marginBox(targetEl.children[0]).w + space;
	var lastLeft = Math.round(b.l + b.w - scrollOffset + space);
	dojo.forEach(targetEl.children, function(node, index) {
		var l = dojo.marginBox(node).l - scrollOffset;
		dojo.fx.slideTo({
			node: node,
			duration: 5000,
			left: l.toString(),
			top: "0",
			unit: "px",
			onEnd: function() {
				if (0 == index) {
					dojo.place(node, targetEl);
					dojo.style(node, "left", lastLeft.toString() + "px");
				}
				if ((targetEl.children.length - 1) == index) {
					_newsPlay = false;
				}
			}
		}).play();
	});
};

function showReklama(id) {
	if (0 == _activeId) {
		_activeId = dojo.byId("lok-det").children[0].id.substring(4);
	}
	if (0 == _lastId) {
		_lastId = dojo.byId("lok-det").children[0].id.substring(4);
//	} else {
//		_lastId = dojo.query("#lok-det div:not(.lok-hid)")[0].id.substring(4);
	}
	if (0 == id) {
		id = _lastId;
	}
//	alert("id: "+id+"_lastId: "+_lastId+"_activeId: "+_activeId);
	if (id != _activeId) {
		_lastId = _activeId;
		dojo.removeClass(dojo.byId("lok-" + id), "lok-hid");
		dojo.addClass(dojo.byId("lok-" + _activeId), "lok-hid");
		_activeId = id;
	}
};

function showMenu() {
	clearTime = null;
	dojo.style("header", "zIndex", "1000");
};

function hideMenu() {
	dojo.style("header", "zIndex", "1");
	_showMenu = setTimeout(dojo.style("header", "zIndex", "1"),1000);
};

function editReklama(id, x, y) {
	alert(id + " " + x + " " + y);
};

function removeReklama(id) {
	alert(id);
};
dojo.require("dojof.dojof");
dojo.addOnLoad(function() {
	if (undefined != dojo.byId('contributors')) {
		// hide contributor
		dojo.forEach(dojo.byId('contributors').children, function(node, index) {
			if (index > 0) {
				dojo.style(node, {
					opacity: "0"
				})
			}
		});
		// contributor switchter
		dojo.connect(dojo.byId("contributorPager"), "onclick", function(evt) {
			dojof.switchContributor(evt.target);
		});
		// automatic switchting of contributors
		window.setInterval(function() {
			var button = dojo.byId("button-contributor" + (dojof.curContrib + 1));
			if (!button) {
				button = dojo.byId("button-contributor1");
			}
			dojof.switchContributor(button);
		}, 5000);
	}
	if (undefined != dojo.byId('cus-loga')) {
		window.setInterval(function() {
			scrollUp(dojo.byId("cus-loga"));
		}, 3000);
	}
});
dojo.require("dojox.widget.AutoRotator");
dojo.require("dojox.widget.rotator.Pan");
dojo.require("dojox.widget.rotator.Fade");
