
var mycarousel_itemList = Array();
var attentionListReady = false;
var activityListReady = false;
var carrouselReady = false;

google.load('visualization', '1', {});
google.setOnLoadCallback(fillPromo);

var itemId = 0;

function fillPromo() {

	var query = new google.visualization.Query(
			'http://spreadsheets.google.com/tq?key=0AhsB0I41k0kgdEtvM0h6cU8xZWRId2ZBLVJ1WWhGTVE&gid=2');
	var qu = 'SELECT A,B,C WHERE A!="Tekst" ';
	query.setQuery(qu);
	query.send(handleAttentionQueryResponse);

	// coming events
	var now = new Date();
	var query2 = new google.visualization.Query(
			'http://spreadsheets.google.com/tq?key=0AhsB0I41k0kgdEtvM0h6cU8xZWRId2ZBLVJ1WWhGTVE&gid=1');
	var qu2 = 'SELECT A,B,C,D,E,F,G,H,I WHERE A!="Provincie" AND E >= date "'
			+ now.getFullYear() + "-" + (now.getMonth() + 1) + "-"
			+ now.getDate() + '" ORDER BY E,D LIMIT 5 ';
	query2.setQuery(qu2);
	query2.send(handleActivityQueryResponse);

}

function handleAttentionQueryResponse(response) {
	if (response.isError()) {
		alert('Error in query: ' + response.getMessage() + ' '
				+ response.getDetailedMessage());
		return;
	}

	var data = response.getDataTable();

	for (row = 0; row < data.getNumberOfRows(); row++) {
		var current = new Object();
		current.id = itemId++;
		current.text = data.getValue(row, 0);
		current.img = data.getValue(row, 1);
		current.url = data.getValue(row, 2);
		mycarousel_itemList[mycarousel_itemList.length] = current;
	}

	attentionListReady = true;
	startUp();
}

function handleActivityQueryResponse(response) {
	if (response.isError()) {
		alert('Error in query: ' + response.getMessage() + ' '
				+ response.getDetailedMessage());
		return;
	}

	var data = response.getDataTable();
	var start;
	var startTS;
	var endTS;
	
	var startTSstr;
	var endTSstr;
	
	for (row = 0; row < data.getNumberOfRows(); row++) {
		var current = new Object();
		current.id = itemId++;
		current.text = data.getValue(row, 3);
		current.img = data.getValue(row, 8);

		if(data.getValue(row, 4))
			start = formatDate(data.getValue(row, 4));

		startTS = data.getFormattedValue(row, 5);
		if (!startTS || startTS == ''){
			startTS = data.getValue(row, 5);
			startTSstr = formatGTime(startTS);
		} else {
			startTSstr = formatTime(startTS);
		}
		
		endTS = data.getFormattedValue(row, 6);
		if (!endTS || endTS == ''){
			endTS = data.getValue(row, 6);
			endTSstr = formatGTime(endTS);
		} else {
			endTSstr = formatTime(endTS);
		}

		current.url = "activiteiten/index.htm?province="
				+ escape(data.getValue(row, 0)) + "&city="
				+ escape(data.getValue(row, 1)) + "&kb="
				+ escape(data.getValue(row, 2)) + "&activity="
				+ escape(data.getValue(row, 3)) 
				+ "&date=" + escape(start)
				+ "&starttime=" + escape(startTSstr)
				+ "&endtime=" + escape(endTSstr)
				+ "&moreinfo=" + escape(data.getValue(row, 7));
		mycarousel_itemList[mycarousel_itemList.length] = current;
	}

	activityListReady = true;
	startUp();
}

function mycarousel_itemLoadCallback(carousel, state) {
	for ( var i = carousel.first; i <= carousel.last; i++) {
		if (carousel.has(i)) {
			continue;
		}

		if (i > mycarousel_itemList.length) {
			break;
		}

		carousel.add(i, mycarousel_getItemHTML(mycarousel_itemList[i - 1]));
	}
};

/**
 * Item html creation helper.
 */
function mycarousel_getItemHTML(item) {
	var content;
	if (item.img && item.img != "")
		content = item.text + '<img src="img/' + item.img + '" alt="'
				+ item.url + '" />';
	else
		content = item.text+'<br/>';
	
	if (item.url && item.url != ""){
		return '<p class="attention"><a href="#" onclick="openItem('+item.id+');return false;">'	+ content + '</a></p>'; 
	} else
		return '<p class="attention">' + content + '</p>';
};

function mycarousel_initCallback(carousel) {
	// Disable autoscrolling if the user clicks the prev or next button.
	carousel.buttonNext.bind('click', function() {
		carousel.startAuto(0);
	});

	carousel.buttonPrev.bind('click', function() {
		carousel.startAuto(0);
	});

	// Pause autoscrolling if the user moves with the cursor over the clip.
	carousel.clip.hover(function() {
		carousel.stopAuto();
	}, function() {
		carousel.startAuto();
	});
};

function startCarrousel() {
	jQuery('#mycarousel').jcarousel( {
		size : mycarousel_itemList.length,
		scroll : 1,
		auto : 8,
		wrap : 'last',
		initCallback : mycarousel_initCallback,
		itemLoadCallback : {
			onBeforeAnimation : mycarousel_itemLoadCallback
		}
	});
}

jQuery(document).ready(function() {
	carrouselReady = true;
	startUp();
});

function startUp() {
	if (carrouselReady && attentionListReady && activityListReady)
		startCarrousel();

}

function openItem(carouselitemnb){
	var item;
	if(mycarousel_itemList){
		if(carouselitemnb < mycarousel_itemList.length) {
			item = mycarousel_itemList[carouselitemnb];
		}
	}
	
	if(item && item.url){
		if(false)
			window.alert(item.url);
		
		window.open(item.url,'_blank','location=yes,menubar=yes,status=yes,toolbar=yes,scrollbars=yes,resizable=yes');
	}
	
	
}
