var $ = jQuery;

/* MISC */

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}

/* LOAD */


addLoadEvent(function() {
	var top = document.getElementById("top");
	var left = document.getElementById("left");
	var main = document.getElementById("main");
	var right = document.getElementById("right");
	var footer = document.getElementById("footer");
	
	var maxHeight = left.offsetHeight;
	if (main.offsetHeight > maxHeight) maxHeight = main.offsetHeight;
	if (right.offsetHeight > maxHeight) maxHeight = right.offsetHeight;
	if (left.offsetHeight < maxHeight) {
		var menuHeight = left.getElementsByTagName("div")[0].offsetHeight;
		var address = left.getElementsByTagName("p");
		var addressHeight = 0;
		for (var i = 0; i < address.length; i++) {
			addressHeight += address[i].offsetHeight;
		}
		
		address[0].style.marginTop = (maxHeight - menuHeight - addressHeight - 23) + "px";
	}
	if (main.offsetHeight < maxHeight) {
		main.style.height = maxHeight + "px"
	}
	
	var h2s = right.getElementsByTagName("h2");
	if (h2s.length != 0) {
		h2s[0].className = "first";
	}
	
	var fader = new ImageFader(document.getElementById("banner"));
	var images = document.getElementById("banners").getElementsByTagName("img");
	if (images.length == 0) {
		images = document.getElementById("banners-default").getElementsByTagName("img");
	}
	for (var i = 0; i < images.length; i++) {
		fader.add(images[i].src);
	}
	fader.start();
});

/* RIGHT BLOCKS */

function showBlock(id) {
	var block = document.getElementById('right_block_' + id);
	var content = block.getElementsByTagName('div')[0];
	var img = document.getElementById('right_block_link_' + id).getElementsByTagName('img')[0];
	if (block.offsetHeight == 0) {
		$(block).animate({ height: content.offsetHeight }, 250);
		img.src = img.src.replace('plus', 'minus');
	} else {
		$(block).animate({ height: 0 }, 250);
		img.src = img.src.replace('minus', 'plus');
	}
}

function hoverBlock(id, hover) {
	var top = document.getElementById('right_block_link_top_' + id);
	var bottom = document.getElementById('right_block_link_bottom_' + id);
	var link = document.getElementById('right_block_link_' + id);
	if (hover) {
		top.style.display = 'block';
		bottom.style.display = 'block';
		link.className += ' hover';
	} else {
		top.style.display = 'none';
		bottom.style.display = 'none';
		link.className = link.className.replace(' hover', '');
	}
}

/* IMAGE FADER */

function ImageFader(elm) {
	var self = this;
	this.element = elm;
	this.curr = 0;
	this.src = [], this.alt = [];
	var dir = "";

	this.start = function() {
		this.curr = Math.floor(Math.random() * this.src.length);
		this.setNext();
		window.setTimeout(function() {
			self.change();
		}, 4000);
	};
	
	this.add = function(src, alt) {
		this.src.push(src);
		this.alt.push(alt);
	};

	this.change = function() {
		if (this.src.length > 1) {
			fade(this.element, 100, -5, function() {
				self.setNext();
				fade(self.element, 0, 5, function() {
					window.setTimeout(function() {
						self.change();
					}, 4000);
				});
			});
		}
	};
	
	this.setNext = function() {
		if (this.src.length > 0 && this.alt.length > 0) {
			this.curr = (this.curr + 1) % this.src.length;
			this.element.innerHTML = "<img src=\"" + dir + this.src[this.curr] + "\" \\>";
		}
	};
}

function fade(element, currOpacity, step, callback) {
	if (typeof callback == "function")
		callback();
	/*
	var newOpacity = currOpacity + step;
	if ((step < 0 && newOpacity >= 0) || (step > 0 && newOpacity <= 100)) {
		element.style.opacity = newOpacity / 100;
		element.style.filter = "alpha(opacity=" + newOpacity + ")";
		setTimeout(function() {
			fade(element, newOpacity, step, callback);
		}, 40);
	} else if ((step < 0 && newOpacity < 0) || (step > 0 && newOpacity > 100)) {
		newOpacity = (step > 0) ? 100 : 0;
		element.style.opacity = newOpacity;
		element.style.filter = "alpha(opacity=" + newOpacity + ")";
		if (typeof callback == "function") {
			callback();
		}
	} else {
		return;
	}
	*/
}

/* GALLERY */

var g, imgs, overlay, galleriaContainer, galleriaLoaded;

function prepareGallery() {
	Galleria.loadTheme('/sites/all/themes/lyseng/js/galleria/theme/galleria.classic.min.js');
	
	overlay = document.getElementById('overlay');
	galleriaContainer = document.getElementById('galleria');
	galleriaLoaded = false;
}

function loadGallery(imgs, path) {
	prepareGallery();
	
	if (!imgs) imgs = document.getElementById('gallery-images').getElementsByTagName('img');
	if (!path)
		path = '/sites/default/files/gallery';
	else
		path = '' + path;
	
	addGalleryEvents();
	
	for (var i = 0; i < imgs.length; i++) {
		var visible = $(imgs[i]).is(':visible');
		if (!visible) continue;
		
		var link = document.createElement("a");
		link.href = path + imgs[i].src.substr(imgs[i].src.lastIndexOf("/"));
		link.appendChild(imgs[i].cloneNode(true));
		
		galleriaContainer.appendChild(link);
		
		imgs[i].style.cursor = 'pointer';
		
		imgs[i].onclick = function() {
			var theImg = this;
			if (!g) {
				$(galleriaContainer).galleria();
				g = Galleria.get(0);
			}
			showGallery(true);
			
			setTimeout(function() {
				gotoGalleryImage(imgs, theImg);
			}, 500);
		};
	}
}

function addGalleryEvents() {
	document.onkeydown = function(e) {
		e = e || window.event;
		var key = e.keyCode || e.charCode || e.which;
		if (key == 27) {
			showGallery();
		}
	};
	overlay.onclick = showGallery;
}

function showGallery(yes) {
	if (yes != true) yes = false;
	overlay.style.display = yes ? 'block' : 'none';
	galleriaContainer.style.display = yes ? 'block' : 'none';
}

var currentGalleryIndex = 0;
function gotoGalleryImage(imgs, img) {
	var i = 0;
	while (imgs[i] != img) i++;
	if (i != currentGalleryIndex) {
		g.show(i);
		currentGalleryIndex = i;
	}
}
