var cover={

total:0,
current:0,
timeout:6000,
timefade:500,
timeouthandle:0,

init:function(){

  if (!cover.data || !cover.data.length) {
	var today = this.getToday();
	$.ajax({url:"getjson.php", data: "data", dataType: "json", async: false, success:function(data){
		cover.data = [];
		$.each(data.items, function(key, val) {
			if ((val.start == '0000-00-00 00:00:00' || today > val.start) && (val.end == '0000-00-00 00:00:00' || today < val.end)) {
				cover.data.push({"title":val.title, "desc":val.blurb, "img":val.image_1, "link":val.content_url}); 
				}
			});
		}});
	}

  if (!cover.data || !cover.data.length) { return false; }
  
	this.total = cover.data.length;

	for (var i=0; i<this.total; i++) {
		$('#coverfeatimage').append('<img src="' + cover.data[i].img + '" />');
		$('#coverpages').append('<a href="javascript:void(0);" onclick="cover.gotoPage(' + i + ')">' + (i+1) + '</a>');
		}
	$('#coverfeatimage img').css({opacity: 0.0});
	$('#coverfeatimage img:first').css({opacity: 1.0});
	$('#covertitle div').html('<a target="_blank" href="' + cover.data[0].link + '"><h2>' + cover.data[0].title + '</h2></a>' + cover.data[0].desc + '<a target="_blank" href="' + cover.data[0].link + '" class="readmore"></a>');
	$('#coverpages a:first').addClass('active');
	
	this.timeouthandle = window.setTimeout('cover.gotoNextPage()', this.timeout);
	},
	
gotoPage:function(page) {
	if (page>=this.total || page<0) { return false; }
	if (page==this.current) { return false; }

	$('#coverfeatimage img:eq('+page+')').css({opacity: 0.0}).animate({opacity: 1.0}, this.timefade);
	$('#coverfeatimage img:eq('+this.current+')').animate({opacity: 0.0}, this.timefade);
	$('#coverpages a').removeClass('active');
	$('#coverpages a:eq('+page+')').addClass('active');
	$('#covertitle div').html('<a target="_blank" href="' + cover.data[page].link + '"><h2>' + cover.data[page].title + '</h2></a>' + cover.data[page].desc + '<a target="_blank" href="' + cover.data[page].link + '" class="readmore"></a>');
	
	this.current = page;
	
	window.clearTimeout(this.timeouthandle);
	this.timeouthandle = window.setTimeout('cover.gotoNextPage()', this.timeout);
	},

gotoNextPage:function() {
	var nextpage = this.current+1;
	if (nextpage == this.total) { nextpage = 0; }
	this.gotoPage(nextpage);
	},
	
getToday:function() {
	var d = new Date();
	var curr_date = d.getDate();
	var curr_month = d.getMonth() + 1;
	var curr_year = d.getFullYear();
	var curr_hour = d.getHours();
	var curr_min = d.getMinutes();
	return curr_year+'-'+this.pad(curr_month,2)+'-'+this.pad(curr_date,2)+' '+this.pad(curr_hour,2)+':'+this.pad(curr_min,2)+':00';
	},
	
pad:function(number, length) {
	var str = '' + number;
	while (str.length < length) { str = '0' + str; }
    return str;
	}

};

