var maxSplash=0;
var currentSplash=0;
var splashTimer;
var splashDelayTime=5000;
var splashTransitionTime=1750;

var totalSlides=0;
var currentSlide=0;

$(document).ready(function() {
  maxSplash=$('.splash li').length;
  $('.splash li').css({top:0,left:0}).not(':eq('+currentSplash+')').hide();
  splashTimer=setTimeout('nextSplash()',splashDelayTime);
  
  $.ajax({
        type: "GET",
	url: "messageboard.xml",
	dataType: "xml",
	success: function(xml) { parse_xml(xml); }
  });
  
}) // .ready


function parse_xml(xml)
{
	cnt=0;
	if (xml && $("#messageData").length>0 )
	{
		//$('body').append('<div id="messageData"></div>');
		$("#messageData").css("display", "none");		
		
		$(xml).find('slide').each(function() {
			cnt++;
			
			$("#messageData").append('<div id="slide'+cnt+'"></div>');
			$("#slide"+cnt).attr('duration', $(this).attr('duration'));
			$("#slide"+cnt).addClass('slide');
			$("#slide"+cnt).css('background-color', $(this).attr('bgcolor'));
			
			itemcnt=0;
			$(this).find('item').each(function(){
					
				itemcnt++;
				t = $(this).attr('type');			
				w = $(this).attr('width');
				h = $(this).attr('height');
				bg= $(this).attr('bgcolor');
				c = $(this).attr('center-text');
				cx = $(this).attr('center-x');
				cy = $(this).attr('center-y');
				cb = $(this).attr('center-both');
				pd = $(this).attr('padding');
				mr = $(this).attr('margin');
				itop = $(this).attr('top');
				left = $(this).attr('left'); 
				//alert('w: '+w+', h: '+h);
				//alert('t: '+t+' w: '+
				$("#slide"+cnt).append('<div id="slide'+cnt+'_item'+itemcnt+'"></div>');
				i = $('#slide'+cnt+'_item'+itemcnt);
				
				if (pd!=undefined) { i.css('padding', pd+'px'); }
				if (mr!=undefined) { i.css('margin', mr+'px'); }
				if (bg!=undefined) { i.css('background-color', bg); }
				
				if (c!=undefined)
				{ 
					i.css('text-align', 'center');					
				}
				
				if (cx!=undefined || cb!=undefined)
				{
					i.css('margin-left', 'auto');
					i.css('margin-right', 'auto');
					i.css('text-align', 'center');
				}

				if (cy!=undefined || cb!=undefined)
				{
					i.css('margin-top', 'auto');
					i.css('margin-bottom', 'auto');
					//i.css('vertical-align', 'middle');
					
				}
				
				if (itop!=undefined) 
				{
					i.css('position', 'relative');
					i.css('top', itop+'px'); 
				}

				if (left!=undefined) 
				{
					i.css('position', 'relative');
					i.css('left', left+'px'); 
				}
				
				switch(t)
				{
					case 'image':
						i.append('<img />');
						img = $('#slide'+cnt+'_item'+itemcnt+' > img ');
						img.attr('src', $(this).text());
						if (w!=undefined) { img.width(w+'px'); }
						if (h!=undefined) { img.height(h+'px'); }
						break;
					
					case 'text':
						fs= $(this).attr('font-size');
						fc= $(this).attr('font-color');
						
						i.html($(this).text());
						i.css('color', fc);
						i.css('font-size', fs+'px');	
						if (w!=undefined) { i.width(w+'px'); }
						if (h!=undefined) { i.height(h+'px'); }
						break;
				}
				
				if ($(this).attr('link'))
				{
					i.attr('onClick', "location='"+$(this).attr('link')+"'");
					i.css('cursor', 'pointer');
				}				
			});
				
		});		
	}
	totalSlides = cnt;
	currentSlide =1;
	view_current_slide();
}


function view_current_slide()
{	
	if (totalSlides>0)
	{
		$('#main_annoucement_area').show();
		
		if ($("#message_holder > div"))
		{
			$("#message_holder > div").appendTo('#messageData');
		}
		
		cur = $("#slide"+currentSlide);	
		cur.appendTo("#message_holder");

		dur = cur.attr('duration')!=null?cur.attr('duration'):4000;
		
		currentSlide++; 
		if (currentSlide > totalSlides) { currentSlide=1;}
		
		messageTimer = setTimeout('view_current_slide()', dur);
	}
}

function nextSplash() {
  var n=currentSplash+1;
  if(n>=maxSplash)
    n=0;
  var target=$('.splash li').eq(currentSplash);
  var nTarget=$('.splash li').eq(n);
  
  target.css('z-index','91');
  nTarget.css('z-index','92');
  
  nTarget.stop().fadeIn({duration:splashTransitionTime,
                         easing:'easeOutQuart',
                         complete:function() {
                           target.stop().hide();
                           currentSplash=n;
                         }
                        });
  
  splashTimer=setTimeout('nextSplash()',splashDelayTime);
}
