var timer;
var pause = 3000;
var fade = 1000;

var images = new Array();
var links = new Array();
var targets = new Array();

var eid;
var xpos;
var ypos;
var w;
var h;
var hasOverlay;
var hasLinks;
var hasButtons;

if (document.images)

function initEvent(_eid, _numEvents, _x, _y, _w, _h, _hasOverlay, _hasLinks, _hasButtons) {
	eid = _eid;
	numEvents = _numEvents;
	xpos = _x;
	ypos = _y;
	w = _w;
	h = _h;
	hasOverlay = typeof(_hasOverlay) != 'undefined' ? _hasOverlay : false;
	hasLinks = typeof(_hasLinks) != 'undefined' ? _hasLinks : false;
	hasButtons = typeof(_hasButtons) != 'undefined' ? _hasButtons : false;
	
	$.ajax({
		type: 'GET',
		url: '/images/Image/user/events.xml', 
		dataType: 'xml',
		success: xmlLoaded
	});	
}

function xmlLoaded(xml) {
	$(xml).find('event').each(function() {
		images.push($(this).attr('image'));
		links.push($(this).attr('link'));
		targets.push($(this).attr('target'));
	});
	
	setupEvent();
}

function setupEvent() {
	for (i=0; i<numEvents; i++) {
		$(eid).append('<img class="inactive event" src="/images/Image/user/' + images[i] + '" border="0" alt="" />');
	}
	
	$(eid).css({width:w, height:h});
	$('img.event', eid).css({left:xpos, top:ypos});
	$('.event', eid).first().attr('class', 'active event').css('display', 'inline').fadeTo(fade, 1);
	if (hasOverlay) {
		$(eid).append('<img class="overlay" src="/images/Image/eventsOverlay.png" border="0" alt="" />');
	}
	
	if (hasLinks) {
		$('img', eid).css('cursor', 'pointer');
		
		$('img', eid).click(function(e) {
			var index = $('img.active',eid).index();
			//alert (links[index]);
			if (targets[index] == '_self')
				window.location.href = links[index];
		});
	}
	
	if (hasButtons) {
		$(eid).append('<div class="buttons"><ul></ul></div>');
		var styles = {};
		styles['right'] = '10px';
		styles['bottom'] = '10px';
		
		$('div.buttons', eid).css(styles);
		
		for (i=0; i<numEvents; i++) {
			$('.buttons ul').append('<li class="inactive btn"><div class="button"></div></li>');
		};
		
		$('.btn', '.buttons').mouseout(function(e){
			timer=setTimeout('rotate()', pause);
		});
		
		$('.btn', '.buttons').mouseover(function(e){
			clearTimeout(timer);
			$('.active', '.buttons').attr('class', 'inactive btn');
			$(this).attr('class', 'active btn');
			
			$('img.active', eid).attr('class', 'inactive event').stop().fadeTo(fade,0,function(){$(this).css('display', 'none');});
			var index = $(this).index();
			$('img.event').eq(index).attr('class', 'active event').css('display', 'inline').stop().fadeTo('fade',1);
		});
	}
	
	$('img.event').first().attr('class', 'active event').css('display', 'inline');
	$('.btn', '.buttons').first().attr('class', 'active btn');
	timer=setTimeout('rotate()', pause);
}

function rotate() {
	$('.active', eid).addClass('disable');
	
	if ($('img.active', eid).next('.event').length != 0) {
		$('img.active', eid).next('.event').attr('class', 'active event').css('display', 'inline').fadeTo(fade, 1);
		$('li.active', eid).next('.btn').attr('class', 'active btn');
	}
	else {
		$('.event', eid).first().attr('class', 'active event').css('display', 'inline').fadeTo(fade, 1); 
		$('.btn', '.buttons').first().attr('class', 'active btn'); 
	}
	
	$('img.disable', eid).attr('class', 'inactive event').fadeTo(fade,0,function(){$(this).css('display', 'none');});
	$('.disable', '.buttons').attr('class', 'inactive btn');
	
	timer=setTimeout('rotate()', pause);
}
