window.onload = pageLoad;

var images = new Array(theme_url + 'images/slide1.jpg', theme_url + 'images/slide2.jpg', theme_url + 'images/slide3.jpg');

var nextImage = 1;
var imageWidth = 144;
var imageHeight = 119;
var imageTimeout = 2000;

function setOpacity(el, opacity) {

	opacity /= 100;

	el.style.opacity = opacity;
	el.style.MozOpacity = opacity;
	el.style.filter = "alpha(opacity=" + (opacity*100) + ")";

}

function fadeImage(el, currentOpacity) {

	currentOpacity += 5;

	if (currentOpacity > 100) {
		setOpacity(el, 100);
		var prevEl = el.previousSibling ? el.previousSibling : el.parentNode.lastChild;
		prevEl.style.visibility = 'hidden';
		el.style.zIndex = 1;
		window.setTimeout(startFading, imageTimeout);
	}
	else {
		setOpacity(el, currentOpacity);
		window.setTimeout(function() { fadeImage(el, currentOpacity); }, 50);
	}

}


function startFading() {

	var el = document.getElementById('slide').childNodes[nextImage];

	el.style.visibility = 'visible';
	el.style.zIndex = 2;
	setOpacity(el, 0);
	fadeImage(el,0);

	nextImage = (nextImage < images.length-1) ? nextImage + 1 : 0;

}

function pageLoad() {

	var el = document.getElementById('slide');

	while (el.firstChild) { el.removeChild(el.firstChild); }

	el.style.width = imageWidth + 'px';
	el.style.height = imageHeight + 'px';

	for(var i=0; i<images.length; i++) {

		var t = document.createElement('IMG');
		t.setAttribute('src',images[i]);
		t.setAttribute('width',imageWidth);
		t.setAttribute('height',imageHeight);
		t.style.position = 'absolute';
		t.style.visibility = 'hidden';
		el.appendChild(t);

	}

	el.firstChild.style.visibility = 'visible';

	window.setTimeout(startFading, imageTimeout);

	$('#nav li a.main').each(function(){
		var link = $(this);
		var parentLi = link.parent();
		var dropDown = link.next('ul');

		if (dropDown.length > 0)
		{
			parentLi.mouseover(function(){
				timer = setTimeout(function(){
					dropDown.slideDown();
				}, 300);
			});
			parentLi.mouseleave(function(){
				if (typeof(timer) != 'undefined')
					clearTimeout(timer);
				hide = setTimeout(function(){
					dropDown.slideUp();
				}, 500);
			});
			dropDown.mouseover(function(){
				if (typeof(hide) != 'undefined')
					clearTimeout(hide);
			});
		}
	});

	$('#news-ticker ul').newsTicker();

	$('#sidebar-affiliates marquee').marquee().mouseover(function(){
		$(this).trigger('stop');
	}).mouseout(function(){
		$(this).trigger('start');
	});
}
