function navCurrentClass ( parentId, tagName, index )
{
		var x = document.getElementById(parentId).getElementsByTagName(tagName);
		x[index].className = 'current';
}

// Initialize quiz score
var quizscore = 0;

$(function() {

	// Create quiz
  if ($('table#quiz').length > 0) {
		// Get XML data
		$.ajax({
			type: "GET",
			url: "includes/quiz.xml",
			dataType: "xml",
			success: function(xml) {
				// Create empty quiz object
				var quizhtml = $('<table id="quiz"></table>');
				// Fill quiz object with question/answer pairs
				$(xml).find('quiz > question').each(function(){
					var id = $(this).attr('id');
					var q = $(this).text();
					var a = $(this).attr('val');
					var expl = $(this).next('#'+id).text();

					$('<tr><td id="q'+id+'"class="'+a+'"><h3>Pregunta '+id+':</h3><p>'+q+'</p><p><a class="T">Verdadero</a>&nbsp;<a class="F">Falso</a></td><td id="expl'+id+'"><p>'+expl+'</p></td></tr>').appendTo(quizhtml);
				});

				// Insert into DOM
				$('#quiz').replaceWith(quizhtml);
				$('#quiz').after('<div id="quizscore"></div>');

				// Create helper function
				var showexpl = function(el,idx) {
					var nextlink = el.append('<p><a class="next">Sigue</a></p>');
					if (idx < $("#quiz tr").length-1) {
						nextlink.click(function(){
							el.fadeOut('fast',function(){ el.parent().hide().next('tr').fadeIn('fast'); })
						});
					} else {
						nextlink.click(function(){
							$('#quiz, #quizscore').fadeOut('fast',function(){
								$('#quiz').replaceWith('<table id="quiz"><tr><td><h3>Puntos finales: '+quizscore+'/'+$("tr",quizhtml).length+'</h3><p><a href="entrehispanos.html">Para aprender m&aacute;s, clique aqu&iacute;</a></p></td></tr></table>').fadeIn('fast');
							})
						});
					}
					el.fadeIn('fast');
					$('#quizscore').html('Puntos: '+quizscore+"/"+$('#quiz tr').length);
				}

				// Set up interactions
				$("#quiz tr, #quiz td").hide().filter("td[id^='q']").show().each(function(){
					var q = $(this), a = $(this).attr('class'), expl = $(this).next("td[id^='expl']");
					// Correct answer
					$('a.'+a,this).click(function(){
						quizscore++;
						q.fadeOut('fast',function(){
							expl.prepend('<h3>Correcto!</h3>');
							showexpl(expl,$("#quiz tr").index($(this).parent('tr')));
						});
					});
					// Incorrect answer
					$('a[class!='+a+']',this).click(function(){
						q.fadeOut('fast',function(){
							expl.prepend('<h3>Incorrecto.</h3>');
							showexpl(expl,$("#quiz tr").index($(this).parent('tr')));
						});
					});
				}).end().filter("#quiz tr:first").fadeIn('fast');
			}
		});
	}
  if ($('.cycle img').length > 0) {
	  $('.cycle').cycle({fx: 'fade', timeout: 4500, pause: 0});
  }
});

// load the swf file animations with swfobject
$(function() {
		
  if ($('#swf_instructional_stroke').length > 0) {
		var flashvars = false;
		var params = {};
		var attributes = {
		  id: "myDynamicContent",
		  name: "myDynamicContent"
		};

	  swfobject.embedSWF("flash/derrame_cerebral.swf", "swf_instructional_stroke", "550", "400", "9.0.0", 0, flashvars, params, attributes);
  }
});