/*

BASE Functions

==================================================================================================================*/	

function makeDateReady(machineDate) {
	humanDate = "";
	if (machineDate) {
		machineDate = machineDate.split('T');
		tempDate = machineDate[0].split('-');
		humanDate = tempDate[1] + "/" + tempDate[2] + "/" + tempDate[0];
	}
	return humanDate;
}

//jQuery Load
$(document).ready(function() {

	$('#index-graphics ul')
	.after('<div class="graphics-nav"><div id="graphics-nav"></div></div>')
	.cycle({ 
		fx:     'fade',
		speed:  'fast',
		timeout: 6000,
		pager:  '#graphics-nav',
		pagerAnchorBuilder: function(idx, slide) { 
			cnt = idx + 1;
			return '<a href="#">' + cnt + '</a>'; 
		}
	});
	var	paused = false;
	$('.graphics-nav')
	.prepend('<a href="#" id="graphics-pause"><img src="/images/pray/i_pause.gif" alt="Pause" /></a>')
	.find('#graphics-pause')
	.bind('click', function() {
		if (paused) {
			$('#index-graphics ul').cycle('resume');
			$(this).removeClass('paused').find('img').attr('src', '/images/pray/i_pause.gif').attr('alt', 'Pause');
			paused = false;
		} else {
			$('#index-graphics ul').cycle('pause');
			$(this).addClass('paused').find('img').attr('src', '/images/pray/i_play.gif').attr('alt', 'Play');
			paused = true;
		}
	});
	
	$('#n-search input[type=text]').placeholder();
	
	loadStyles();
	setActiveStyle(getStyleFromCookie());
	
	$('.date-filter').show();
	$('.datepicker').datepicker();
	$('.filter-button a.filter-now').click(function() {
		begin = $('#date-start').val();
		end = $('#date-end').val();
		
		if (begin) {
			begin = new Date(begin);
			if (end) end = new Date(end);
			else end = ""
			
			$('li.event').each(function() {
				$this = $(this);
				eventBegin = $this.find('.dtstart').attr('title');
				thisBegin = new Date(makeDateReady(eventBegin));
				// if you care to limit end dates as well:
				//thisEnd = new Date(makeDateReady(eventBegin));
				
				if ( (thisBegin >= begin) && ( (end && (thisBegin <= end)) || !end) ) {
					$this.show();
				} else {
					$this.hide();
				}
				
			});
		}
		return false;
	});
	$('.filter-button a.reset-filter').click(function() {
		$('li.event').show();
		return false;
	});
	
	$('.top-link a').click(function() {
		$(document).scrollTo($(this).attr('href'), 1000);	
		return false;
	});

},function(){
	GUnload();
});


// Print Page
function printThis() {
	(window.print) ? window.print() : alert('To print his page press Ctrl-P on your keyboard \nor choose print from your browser or device after clicking OK');
}
