/**
 * Javascript-Functions - PROJECT
 * (c) CLICKSPORTS DIGITAL SOLUTIONS
 * http://www.clicksports.de
 * 
 * $Rev: 2065 $
 * $Author: cs $
 * $Date: 2010-06-17 11:35:58 +0200 (Do, 17 Jun 2010) $
 */
document.observe('dom:loaded', function() {
	
	var sclr = new UI.Carousel('colors_carousel');

	// Only run on candidates page
	if($('page_candidates') !== undefined) {

		showCandidateLetters();
		showVotings();
	}
});

// Use custom event to save our ratings
document.observe('starbox:rated', saveStar);

// Remove error handling
window.onerror = function() { return true; }

/**
 * Show letters for candidates
 * 
 * @example showCandidateLetters();
 */
function showCandidateLetters() {

	$$('#content .row_wrapper .row .letter').each(function(row) {
		
		var cnt = row.down('span').innerHTML;
		var img = row.down('img');
		
		new Tip(img, cnt);
	});
}

/**
 * Handle votings for candidates
 * 
 * @example showVotings();
 */
function showVotings() {
	
	$$('#content .row_wrapper .row .votecount span.sb').each(function(vote) {

		new Starbox(vote, vote.title, { locked: true });
		vote.title = '';
	});
}

/**
 * Save results of a rating
 * 
 * @param {Object} evt
 * @return {Boolean}
 */
function saveStar(evt) {
	
	// Get the candidates id
	var candidate_id = evt.memo.identity.replace('candidate_', '');
	var url = '/templates/modules/rating.inc.php';
	
	// Add id to memo
	evt.memo.id = parseInt(candidate_id);

	new Ajax.Request(url, {
		parameters: evt.memo,
		onSuccess: function(transport) {
			
			// Check if the query was successfull
			if(transport.responseText != 0) {
				
				alert("Beim Speichern deines Votes ist leider ein Fehler aufgetreten");
			}
		}
	});

	return true;
}

/**
 * Show candidate video
 * 
 * @param {String} url URL to show
 * @param {String} title Title
 */
function showCandidateVideo(url, title) {

	myLightWindow.activateWindow({
		href: 'templates/modules/embed_youtube.php?v=' + url,
		title: title,
		type: 'page'
	});
}

