



if (typeof window.console == "undefined") window.console = { log: function() {} };

var QUIZ = {
	
	title: '',
	
	tabs: null, 
	
	tabsAPI: null,
	
	selectedScore: 0,
	
	currentQuestionNumber: '',
	
	currentPane: null,
	
	scores: {},
	
	shouldChangeTab: false,
	
	setScore: function(score) {
		this.selectedScore = parseInt(score, 10);
	},
	
	tabSelected: function(event, index) {
				
		var shouldAllowChange = true;
		
		if (index != 0) {
			
			if (this.shouldChangeTab) {
				
				this.shouldChangeTab = false;
				
			} else {
								
				shouldAllowChange = false;
			}
		} 

		return shouldAllowChange;
	},
	
	goToTab: function(event) {	
		
		this.shouldChangeTab = true;
		
		if (typeof event === 'object') {
			this.tabsAPI.next();
		} else if (typeof event === 'number') {
			this.tabsAPI.click(event);
		} else {
			this.tabsAPI.next();
		}
		
		this.currentPane = this.tabsAPI.getCurrentPane();
	},
	
	enableNextButton: function() {
		this.currentPane.find('.next a.button').css('opacity', 1);
	},
	
	disableNextButton: function() {	
		if (!this.didAnswerQuestion()) {
			this.shouldChangeTab = false;
			this.currentPane.find('.next a.button').css('opacity', 0.5);
		}
	},
	
	didAnswerQuestion: function() {
		var isChecked = false;
				
		this.currentPane.find(':radio').each(function(index, el) {
			
			if (isChecked != true) {
				if ($(el).is(":checked")) {
					isChecked = true;
				}
			}
		});
		
		return isChecked;
	},
	
	nextQuestion: function() {
		
		if (this.didAnswerQuestion()) {
			
			this.currentQuestionNumber = this.currentPane.attr('data-question-number');
			this.scores[this.currentQuestionNumber] = this.selectedScore;
			
			//change tabs
			this.goToTab();
			this.disableNextButton();
			
			this.currentQuestionNumber = this.currentPane.attr('data-question-number');
			
			if (this.currentQuestionNumber === undefined) {
				this.trackProgress(true);
				this.showResults();
			} else {
				this.trackProgress();	
			}
			
			return true;
		} else {
			return false;
		}
		
	},
	
	start: function() {
		
		//bug fix for Firefox... it was defaulting to input[type=radio] checked=tru
		this.tabs.parent().find('input[type=radio]').each(function(index, el) {
			el.checked = false;
		});
		
		jQuery('.fakeradio').removeClass('active');
		
		this.scores = {};
		this.goToTab();
		this.disableNextButton();
	},
	
	reset: function() {
		this.goToTab(0);
		this.shouldChangeTab = false;		
	},
	
	showResults: function() {
		this.currentPane.find('#quiz-total-score').html(this.getTotal());
	},
	
	getTotal: function() {
		
		var length = this.scores.length,
			total = 0;

		for (score in this.scores) {
			total += this.scores[score] || 0;
		};
		
		return total;
	},

	answerSelected: function() {
		this.enableNextButton();
	},

	trackProgress: function(end) {
		// quiz title, question completed, question #
		if (end) {
			var d = new Date();
			_gaq.push(['_trackEvent', this.title, 'quiz completed', d.toUTCString()]);
		} else {
			_gaq.push(['_trackEvent', this.title, 'question completed', '#'+this.currentQuestionNumber]);
		}
	},
	
	init: function() {
		this.tabs = jQuery("#inline4 ul.tabs");

		this.tabsAPI = this.tabs.tabs("div.panes > div", {
			onBeforeClick: jQuery.proxy(this, 'tabSelected'),
			rotate: true,
			api: true
		});
		
		jQuery('#quiz-start').click(jQuery.proxy(this, 'start'));
		jQuery('.next a.button').click(jQuery.proxy(this, 'nextQuestion'));
		
		this.title = jQuery('#quiz-title').text().toString();
	}
};

var PRISMA = PRISMA || {};

PRISMA.externalLinks = function() {
	
	$('a').each( function(index, element) {
			
		var link = $(this);
	
		var testURL = ((link.attr('href') !== undefined && link.attr('href').match(/:\/\/(.[^/]+)/) !== null) ? link.attr('href').match(/:\/\/(.[^/]+)/) : ['', link.attr('href')] || ['', link.attr('href')])[1],
			siteURL = PRISMA.SITE_URL.match(/:\/\/(.[^/]+)/)[1];
		
		var isInternal = function(testURL) {
								
			try {
				if (new RegExp('^'+siteURL+'.*').test(testURL) || new RegExp('^\\/').test(testURL) || testURL === '#') {
					return true;
				} else if (jQuery.inArray(testURL, []) !== -1) {
					return true;
				} else {
					return false;
				}
			} catch(e) { 
				return false; 
			}
			
		};
			
		if (!isInternal(testURL)) {
			link.attr('rel', 'nofollow');
		}
		
	});
};

PRISMA.corners = function() {
	if($.browser.msie)
	{
		$("a.button").corner();	
		$("ul.tabs li a").corner("20px");
	}
};





$().ready(function() {

	/*******************************************************************************
	jQuery Radio Button Style v1.0
	http://www.weblee.co.uk
	Licensed under:
	Creative Commons Attribution 3.0 http://creativecommons.org/licenses/by/3.0/
	*****************************************************************************/
	// RADIO BUTTONS
	$('#inline4 .choices ol a').live('click', function (event) {
		event.preventDefault();
		$(this).parent().children().removeClass('active');
		var id = this.id;
		var newselect = id.replace('link', 'option');
		$('#'+newselect).attr('checked', 'checked');
		var divselect = id.replace('link', 'details');
		$('#inline4 .details div').css('display', 'none');
		var divSelected = $('.'+divselect);
		divSelected.css('display', 'block');
		QUIZ.setScore(divSelected.find('h4').attr('data-score'));
		QUIZ.answerSelected();
		$('#inline4 .choices ol a').removeClass('active');
		$('#inline4 .choices ol a').addClass('off');
		$(this).toggleClass('active');
		return false;
	});
	$('input[type="checkbox"]').addClass('checkradio');
	$('input[type="radio"]').addClass('checkradio');

	$('#coda-slider-1').codaSlider({
		dynamicArrows: false,
		dynamicTabs: false
		/*firstPanelToLoad: 3*/
	});
  
	jQuery("ul.tabs").not('.quiz').tabs("div.panes > div", {
		rotate: true
	}).slideshow();

	//OVERLAYS
	// select one or more elements to be overlay triggers
	$("#slideshow a[rel]").overlay({
		mask: {
			color: "#0077c0",
			closeSpeed: 0
		},
		onBeforeLoad: function() {
			QUIZ.reset();
		}
	});
	$("#right a[rel]").overlay({
		mask: {
			color: "#0077c0",
			closeSpeed: 0
		},
		onBeforeLoad: function() {
			QUIZ.reset();
		}
	});
	
	$("#banner_major a[rel]").overlay();
	$("#inline3 .panes a[rel]").overlay({
		mask: "#0077c0",
		closeSpeed:0
	});
	$("#inline4 .panes a[rel]").overlay();
	$("#inline5 .panes a[rel]").overlay({
		mask: "#0077c0",
		closeSpeed:0
	});
	$("#inline7 .panes a[rel]").overlay({
		mask: "#0077c0",
		closeSpeed:0
	});
	
 	$("#inline6 .panes a[rel]").overlay({
		mask: "#0077c0",
		closeSpeed:0
	});
	$("#homeLeft a[rel]").overlay({
		mask: "#0077c0",
		closeSpeed:0
	});
	
	$('a.overlay-open').overlay({
		mask: "#0077c0",
		closeSpeed:0
	});
	
   
	// TOOLTIPS
	$("#footer .sitemap .box_footer a[title]").tooltip({effect: 'slide'});
	
	$("#definitions").tooltip({
		position: "top left",
		offset: [-150, 200], 
		effect: "slide"
	});
	
	$("#content_consume").tooltip({
		position: "top left",
		offset: [-50, 100], 
		effect: "slide"
	});
	
	$("#train").tooltip({
		position: "top left",
		offset: [-50, 100], 
		effect: "slide"
	});
	
	PRISMA.externalLinks();
	PRISMA.corners();
	QUIZ.init();
});
