$(document).ready(function(){
	var flash = false
	
	var version = null;
	if (window.ActiveXObject) {
	    var control = null;
	    try {
	        control = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
	    } catch (e) {
			
	        //return;
	    }
	    if (control) {
			flash = true;
	        version = control.GetVariable('$version').substring(4);
	        version = version.split(',');
	        version = parseFloat(version[0] + '.' + version[1]);
	    }
	} else {
	    var flash = navigator.plugins["Shockwave Flash"];
	}

	if (!flash) {
		$('#no-flash-link').css('display','block');
	}
	
	$('.absolute').click(function(){
		$('#' + this.id.substring(6)).focus();
	});
	$('.field').focus(function(){
		$('#label_' + this.id).hide();
	});
	$('.field').blur(function(){
		if ($('#' + this.id).val() == '')
		{
			$('#label_' + this.id).show();
		}
	});
	
	$('#in-theaters-now').click(function(){ $('#in-theaters-now .demand-in-theaters').toggle(); });
	$('#in-theaters-now form input').click(function(event){ event.stopPropagation(); });
	$('#in-theaters-now form label').click(function(event){ event.stopPropagation(); });
	$('#home-contestrules form input').click(function(event){ event.stopPropagation(); });
	$('#home-contestrules form label').click(function(event){ event.stopPropagation(); });
	$('#in-theaters-now form input[type=text]').each(function(){ 
		if ($('#' + this.id).val() != '') {
			$('#label_' + this.id).hide();
		}
	});
	
	$('#home-contestrules form input[type=text]').each(function(){ 
		if ($('#' + this.id).val() != '') {
			$('#label_' + this.id).hide();
		}
	});
	
	checkAnswer();
	$('.star').hover(function(){
		var currentId = parseInt(this.id.substring(5));
		$('.star').removeClass('white');
		for (var i=1;i<=currentId;i++)
		{
			$('#star-' + i).addClass('white');
		}
	});
	
	$('.star').click(function(){
		var currentId = parseInt(this.id.substring(5));
		$('#raters').remove();
		
		$.ajax({
		   type: "POST",
		   url: rateMovieUrl,
		   data: "val=" + currentId,
		   success: function(data) {
		   		$('#currentRateNew').css('background-position', '0px -' + (15 * (parseInt(data) - 1)).toString() + 'px');
				$('#currentRateNew').show();
				
				facebook_publish_feed_story({"verb":"just rated the trailer for","link":baseUrl,"body":""});
		   }
		 });
	});
	
	$('#currentRate').hover(function(){
		$('#currentRate').hide();
		$('#raters').show();
	});
	
	$('#raters').mouseleave(function(){ 
		$('#currentRate').show();
		$('#raters').hide();
	});
	
	$('.player-holder').hover(function(){ $('#player-overlay').slideDown(); });
	$('.player-holder').mouseleave(function(){ $('#player-overlay').slideUp(); });
	
	$('#splash').click(function(){
		$('#splash').hide();
		$f().play();
	});
	
	$('#movie-finished .addthis_toolbox').click(function(event){
		event.stopPropagation();
	});
	
	$('#movie-finished').click(function(){
		$('#movie-finished').hide();
		$f().play();
	});
	
	$('#home-contestrules img').click(function(){
		$('#home-contestrules form').toggle();
	});
	$('#home-contestrules form').click(function(event){
		$('#home-contestrules form').hide();
	});
});

function checkAnswer() {
	$('#choose-answer').click(function(){
		var quizId = $('#quiz-id').val();
		var selVal = $('.quiz-form input[type=radio]:checked').val();
		var isCorrect = '<div class="correct">CORRECT</div>';
		var isIncorrect = '<div class="correct">INCORRECT</div>';
		
		$.getJSON(answerUrl, {quizId: quizId, choice: selVal},function(data){
			var correctAnswer = '<div class="correct_answer">The answer is<br/>' + data.LETTER + '.) ' + data.ANSWER + '</div><a id="get-new">Get Another Question.</a>';
			if(data.CORRECT) 
			{
				$('#quiz').html(isCorrect + correctAnswer);
			}
			else 
			{
				$('#quiz').html(isIncorrect + correctAnswer);
			}
			getNewEvent();
		});
	});
}

function getNewEvent() {
	$('#get-new').click(function(){
		 $.ajax({
		   type: "POST",
		   url: getNewUrl,
		   data: "",
		   success: function(msg){
		   	  $('#quiz').html(msg);
			  checkAnswer();
		   }
		 });
	});
}
