var events				= new Array();

var current				= '';
var offState			= new Array();
offState['what']		= 'global/images/nav/subnav/home_whatOff.jpg';
offState['who']			= 'global/images/nav/subnav/home_whoOff.jpg';
offState['where']		= 'global/images/nav/subnav/home_whereOff.jpg';

function intro() {
	$('#what').fadeIn('fast');
	document.images['sn_what'].src = "global/images/nav/subnav/home_whatOn.jpg";
	current = 'what';
}
function overFade(target) {
	$('#'+current).fadeOut(300);
	$('#'+target).delay(300).fadeIn(300);
	current = target;
}
function swapImage() {
	document.images['sn_'+current].src = offState[current];
}

// base event index, first shown on the list
var be = 0;

function getXML( url ) {
	$.ajax({
		url: url,
		dataType: "xml",
		encoding:"UTF-8",
		success: xmlSuccess
	});
}

function xmlSuccess( data, status ) {
	//Write your Parse XML Function
	parseXML( data );
}

function parseXML( xml ){
	// XML Tag identifying each item
	var itemTag = "event";
	// XML Tag identifying the title
	var titleTag = "title";
	// XML Tag identifying the date
	var dateTag = "date";
	// XML Tag identifying the link
	var linkTag = "link";
	
	//Parse the XML and build new <li>'s for each NODE
	$( xml ).find( itemTag ).each(function() {
		events.push( [$( this ).find( dateTag ).text(),
					  $( this ).find( titleTag ).text(),
					  $( this ).find( linkTag ).text()] );
	});

	if (events.length > 0) {
		event_init();
	}
}

function event_init() {
	for(var i=0;i<5;i++) {
		event_create(i);
	}
	setInterval("event_rotate()", 7000);
}

function event_rotate() {
	var ni	= be+5;	// next index
	if (ni >= events.length) {
		ni = ni - events.length;
	}
	$('#events .event_shell').first().stop().animate({marginTop: -24, opacity: 0}, 300, function() {
		$(this).remove();
		event_create(ni);
	});
	be++;
	if (be > events.length) {
		be = 1;
	}
}

function event_create(id) {
	var s	= $('<div></div>');
	var d	= $('<div></div>');
	var e	= $('<div></div>');
	d.html(events[id][0]);
	e.html('<a href="'+events[id][2]+'">'+events[id][1]+'</a>');

	s.addClass('event_shell');
	d.addClass('date');
	e.addClass('event');
	s.append(d);
	s.append(e);
	$('#events').append(s);
}

//

function toggle_state( target ) {
	var copy = target.parent('div').prev();
	var more = target.parent().parent().next();
	if (copy.height() > target.attr('cont')) {
		var newH = ( parseInt(target.attr('cont')) + 18 + target.height() ) - 60;
		target.find('img').attr('src', 'global/images/home/exp_arrow.png');
		more.animate( { height: newH }, 300 );
		copy.animate( { height: parseInt(target.attr('cont')) }, 300);
	} else {
		var newH = ( parseInt(target.attr('exp')) + 18 + target.height() ) - 60;
		target.find('img').attr('src', 'global/images/home/cont_arrow.png');
		more.animate( { height: newH }, 300 );
		copy.animate( { height: parseInt(target.attr('exp')) }, 300);
	}
}

function login_validate() {
	
}

function checkFocus(item, val) {
	if (item.value == val) item.value = '';
	if (item.value == '' && val == 'password') item.type = 'password';
}

function checkBlur(item, val) {
	if (item.value == '') item.value = val;
	if (item.value == 'password') item.type = 'text';
}

jQuery(function( $ ){
    getXML('global/php/home_event_scroller.php');
	$('.exp_toggle').each(function() {
		var newH = ( parseInt($(this).attr('cont')) + 18 + $(this).children('img').height() ) - 60;
		$(this).parent().parent().next().css('height', newH);

		$(this).click(function() {
			toggle_state( $(this) );
		});
		$(this).mouseup(function() {
			var myImg = $(this).find('img');
			if (myImg.attr('src').indexOf('cont') != -1) {
				myImg.attr('src', myImg.attr('up'));
			} else {
				myImg.attr('src', myImg.attr('down'));
			}
		}).mousedown(function() {
			var myImg = $(this).find('img');
			var parts = myImg.attr('src').split('.');
			myImg.attr('src', parts[0]+"Press."+parts[1]);
		});
	});
});
