/*

Scripts written by Alex Dickson for Acura Multimedia (http://www.acura.com.au)
jQuery (http://www.jquery.com) by John Resig (http://www.ejohn.org)

*/

// add this class to specify .javascript * in css for quicker css application
$('html').addClass('javascript');

// on DOM ready
$(document).ready(function(){
	$('html').addClass('dom-loaded');

	try { //try get 24bit png working on ie6
		$('img[src$=png]').pngfix();
	} catch(err) {}
	

	questions.init();
    externalLinks.setup();	
});



var questions = {
    currentQuestion: 0,
    currentModule: 0, 
    init: function() {
        // set up handlers
        
       moduleIndex = $('input[name=module]').val();
       questions.currentModule = moduleIndex;
       
       $('#questions-form ul').each(function() {
       
            var questionIndex = $('fieldset').index($(this).parents('fieldset'));
            
            $(this).before('<div class="answer-results"></div>');
            
            
            $('li', this).each(function() {
            
                var answerIndex = $('li', $(this).parent()).index(this);
                
                //prettyRadio = $('<span class="pretty-radio">' + (answerIndex + 1) + '</span>');
                prettyRadio = $('<span class="pretty-radio">&bull;</span>');
                
                radio = $('input[type=radio]', this);
                
                radio.after(prettyRadio);
               
                $(radio).bind('click', function() {
                    $('.pretty-radio', $(this).parent().parent()).removeClass('selected');

                    $(this).next('.pretty-radio').addClass('selected');
                    
                    updateObject = $(this).parents('fieldset').find('.answer-results');
                    
                    questions.checkAnswer(moduleIndex, questionIndex, answerIndex, updateObject);
                    $(this).unbind('click');

                });
               
                prettyRadio.bind('click', function() {
                
                    $('.pretty-radio', $(this).parent().parent()).removeClass('selected');

                    $(this).addClass('selected').prev('input').click();
            
                });
                
                $('label', this).bind('click', function() { 
                
                   inputId = $(this).attr('for');
                  // alert(inputId);
                   
                   $('#' + inputId).trigger('click');

                    //$(radio).trigger('change');

                
                });
 
                
            
            });
       
       
       });
       
      
       
       //initiate default states
       
       $('input:checked').next('.pretty-radio').addClass('selected');
    
    },
    checkAnswer: function(module, question, answer, objectToUpdate) {
        $(objectToUpdate).css({backgroundImage: 'url(../images/layout/5-0.gif)', backgroundPosition: '3px 3px'});
        
        $.getJSON('/accreditation/check-answer/' + module + '/' + question + '/' + answer + '/',
            function(data){
             
                answerResults = eval(data);
                correct = answerResults.correct;
                $(objectToUpdate).css({backgroundImage: 'url(../images/layout/correct-wrong.gif)'});
                if (correct) {
                   $(objectToUpdate).text('Correct').css({backgroundPosition: '-7px -16px'});
                   questions.moveToNext();
                   
                    
                } else {
                   $(objectToUpdate).text('Wrong').css({backgroundPosition: '-80px -16px'}); 
                }

             
            });
            

    
    
    },
    moveToNext: function() {
       $('fieldset')
       .eq(questions.currentQuestion)
       .find('li')
       .fadeOut(1000);
       
       
       if (questions.currentQuestion == ($('fieldset').length - 1)) {
       
            $('#submit').before('<div id="loading-message"></div>').fadeIn(1000);    
       
         
             
              $.getJSON('/accreditation/get-results/' + questions.currentModule + '/',
            function(data){   
                
                message = eval(data);
                
                successMsg = message.successMsg;
              
                if (questions.currentModule == 3) {
                    buttonVal = 'Finish';
                } else {
                    buttonVal = 'Proceed to next module';
                }
              
                $('#submit')
                    .attr('value', buttonVal)
                    .show()
                    .before('<div class="client-message information"><span>' + successMsg + '</span></div>');
                $('#loading-message').fadeOut(500);
              
                  
            
            });

       
            
    
       
       } else {
           
           questions.currentQuestion++;
           $('fieldset')
           .eq(questions.currentQuestion)
           .slideDown(1000);
           
           $('#questions-footer').scrollTo(1000);
           
        }
        
    
    }
    
    



}

jQuery.fn.extend({
  scrollTo : function(speed, easing) {
    return this.each(function() {
      var targetOffset = $(this).offset().top;
      $('html,body').animate({scrollTop: targetOffset}, speed, easing);
    });
  }
});

$('#my-el').scrollTo(1000);




var externalLinks = {
	newWindowTitleText: 'Link opens in a new window',
	setup: function() {
		
		$('a[rel=external]').each(function() { 
										   
										   if ($(this).attr('title')) {
			   									newTitle = $(this).attr('title') + ' (' + externalLinks.newWindowTitleText + ')';
										   } else {
												newTitle = externalLinks.newWindowTitleText;   
										   }
										   $(this).attr('title', newTitle).bind('click', function() {  
																		  externalLinks.openNewWindow($(this).attr('href'));
																		  return false;
																		  }) 
										   });
	},
	
	openNewWindow: function(URI) {
		
		var newBlankWindow = window.open(URI, '_blank');
		newBlankWindow.focus();	
	
	}
	
}


