window.vilcon = (function(w,d){
	function getId(id) {
		return d.getElementById(id);
	};
	function getElementsByClassName(className, tag) {
		if (this.getElementsByClassName) {
			return this.getElementsByClassName(className);
		}
		if (!tag)
			tag = '*';
		var results = [];
		var elements = this.getElementsByTagName(tag);
		className = new RegExp('(?:^|\\s)'+className+'(?:\\s|$)');
		for (var i = 0; i < elements.length; i++) {
			if (className.test(elements[i].className)) {
				results.push(elements[i]);
			}
		}
		return results;
	};

	var
		spotTimeout = 0,
		browser = lightCore.getBrowser();

	return {
		clickSpot: function(event, spot) {
		
			if (event.ctrlKey || event.shiftKey || event.altKey && w.core) {
				if (spot.className.indexOf('click') == -1)
					spot.className += ' click';
				else
					spot.className = spot.className.replace(/\sclick/, '');
			}
		},
		slideshow: (function() {
			function Slideshow(id) {
				var
					parent,
					index = 0,
					lastIndex = 0,
					elements = [],
					fx,
					timeout,
					callback = function() {
						lightCore.setOpacity(elements[index], 0);
						for (var i = 0; i < elements.length; i++)
							elements[i].style.zIndex = 1;
						elements[lastIndex].style.zIndex = 2;
						elements[index].style.zIndex = 3;
						fx.start();
					},
					fx = new secoya.fx({
						from: 0,
						to: 100,
						duration: 1,
						callback: function(x) {
							lightCore.setOpacity(elements[index], x);
						},
						finish: function() {
							var x = -1;
							do {
								x = Math.floor(Math.random()*elements.length);
							} while (x == index && elements.length > 1);
							if (elements.length > 1) {
								lastIndex = index;
								index = x;
								elements[lastIndex].style.zIndex = 3;
								elements[index].style.zIndex = 1;
								lightCore.setOpacity(elements[index], 0);
								timeout = setTimeout(callback, 5000);
							}
						}
					});
				this.start = function() {
					fx.kill();
					clearTimeout(timeout);
					if (parent = getId(id)) {
							elements = getElementsByClassName.call(parent, 'background', 'div');
							index = Math.floor(Math.random()*elements.length);
							if (elements.length > 1) {
								for (var i = 0; i < elements.length; i++)
									lightCore.setOpacity(elements[i], 0);
								lightCore.setOpacity(elements[index], 100);
								fx.finish();
							} else if (elements.length > 0) {
								lightCore.clearOpacity(elements[0]);
							}
					}
				};
			};
			var slideshows = {};
			return function(id) {
				if (!slideshows[id]) {
					slideshows[id] = new Slideshow(id);
				}
				slideshows[id].start();
			};
		})(),
		showpopup: function() {
			lightCore.registerInit({
				initialize: function() {
					var popup = secoya.cookie.get('popup');
					if (!popup) {
						popup = getId('popup');
						if (popup && popup.className != 'secoyaEmptySection') {
							popup.className = 'visible';
							secoya.cookie.set('popup', '1', 60*60*24*365);
						}
					}
				}
			});
		},
		setuppopup: function(){
			var p = getId('popup');
			if (p) {
				p.onclick = function(event) {
					event = event || w.event;
					if (event.ctrlKey || event.shiftKey || event.altKey) {
						p.className = p.className == 'visible' ? '' : 'visible';
					}
				};
			}
		},
		showspot: function(spot, event) {
			clearTimeout(spotTimeout);
			if (spot.className.indexOf('over') != -1)
				return;
			spot.className += ' over';
			var
				big = getElementsByClassName.call(spot, 'big', 'div')[0];
			big.style.display = 'block';
			(new secoya.fx({
				from: 0.28,
				to: 1,
				duration: 0.5,
				type: secoya.fx.power,
				callback: function(x) {
					big.style.zoom = x;
					if (browser.getName() == 'IE' && browser.getVersion() == 8) {
						big.style.width = '';
						big.style.width = big.offsetWidth*x + 'px';
					}
					big.style.transform = big.style.OTransform = big.style.WebkitTransform = big.style.MozTransform = 'scale('+x+')';
				},
				finish: function() {
					spot.className = spot.className.replace('over', 'finishover');
				}
			})).start();
			(new secoya.fx({
				from: 0,
				to: 100,
				duration: 0.5,
				callback: function(x) {
					lightCore.setOpacity(big, x);
				},
				finish: function() {
					lightCore.clearOpacity(big)
				}
			})).start();
		},
		hidespot: function(spot, event) {
			if (spot.className.indexOf('finishover') == -1)
				return;
			spotTimeout = setTimeout(function(){
				var
					big = getElementsByClassName.call(spot, 'big', 'div')[0];

				(new secoya.fx({
					from: 1,
					to: 0.28,
					duration: 0.5,
					type: secoya.fx.power,
					callback: function(x) {
						big.style.zoom = x;
						if (browser.getName() == 'IE' && browser.getVersion() == 8) {
							big.style.width = '';
							big.style.width = big.offsetWidth*x + 'px';
						}
						big.style.transform = big.style.OTransform = big.style.WebkitTransform = big.style.MozTransform = 'scale('+x+')';
					},
					finish: function() {
						spot.className = spot.className.replace(' finishover', '');
						big.style.display = 'none';
					}
				})).start();

				(new secoya.fx({
					from: 100,
					to: 0,
					duration: 0.5,
					callback: function(x) {
						lightCore.setOpacity(big, x);
					}
				})).start();
			}, 1);
		}
	};
})(window,document);
