// answers.js

var answers = ({
   init: function(){
      $("#disclaimer-modal").css({
         opacity: 0,
         visibility: "hidden",
         display: "none"
      });
      
      $("#form-disclaimer").click(function(ev){
         ev.preventDefault();
         
         $("#disclaimer-modal").css({
            visibility: "visible",
            display: "block"
         }).animate({
            opacity: 1
         });
         
      });

      $("#disclaimer-modal .closer").click(function(ev){
         ev.preventDefault();
         
         $("#disclaimer-modal").animate({
            opacity: 0
         });
         
         setTimeout(function(){
            
            $("#disclaimer-modal").css({
               visibility: "hidden",
               display: "none"
            });
         
         },500);
         
      });

		$('#ask-us-form').bind('submit', function(ev) {
         ev.preventDefault();
         answers.askUs($(this));
		});

      // custom shadowbox - we need to pause the playing movie when the updates form
      // is opened, and start it playing again when its closed
      Shadowbox.setup("#get-updates-button", {
         width: 754,
         height: 725,
         onOpen: function(){
            global.getFlashMovie("vp").pauseMovie();
         },
         onClose: function(){
            global.getFlashMovie("vp").playMovie();
         }
      });

    	var urls = ["explore-vehicles_on"];
    	$.preload( urls, {
    		base:'/images/buttons/',
    		ext:'.png'
    	});
		

      answers.flashSetup();
      answers.linkSetup();
      
   },
   askUs: function(form){
      
		$(form).ajaxSubmit({
		   success: function(responseText){
            var container = $("#ask-us-form-container");

            $(container).animate({
               opacity: 0
            },function(){

               $("#form-disclaimer").remove();
               
   		      if(responseText === "success"){

                  var responseHtml = "<div class='thanks'><span>Thank you for submitting your question.</span> We read all submitted questions and post as many answers as we can. In the meantime, take a look at the answered questions below - it's possible someone has made a similar query.</div>";

   		      }else{

                  var responseHtml = "<div class='thanks'><span>We're sorry.</span> There appears to be a problem recording your question.  Pleaes try again later.</div>";

   		      }

               $(container).html(responseHtml).animate({
                  opacity: 1
               });
               
            });
		      
		   }
		});
      
      
   },
   flashSetup: function(){
      
      // set up flash if we're on v9
      var playerVersion = swfobject.getFlashPlayerVersion();
      
		if(playerVersion.major >= 9){

   		var params = {
   			menu: "false",
   			scale: "noScale",
   			allowFullscreen: "true",
   			allowScriptAccess: "always"
   		};

   		var flashvars = {};
   		var attributes = {
   			id:"vp",
   			name:"vp"
   		};

      	swfobject.embedSWF("/includes/swf/video_player.swf", "player", "600", "374", "9.0.0","expressInstall.swf", flashvars, params, attributes);

      }else{

         $("#get-flash").css({visibility: "visible"});         

      }


	},
	flashReady: function(){
      // check if we have a hash; if so, load that video, otherwise, load the first

      var hash = window.location.hash;
      var loadMe = 0;
      if(hash.length > 0){
         loadMe = hash.replace("#","");
      }
	   
      setTimeout(function(){
   	   global.getFlashMovie("vp").sendToFlash(loadMe);
   	   answers.setActiveVideo(loadMe);
      },2000);

	},
	linkSetup: function(){
	   
	   var links = $("#people a");
	   
	   var hereLink = $("#people a.here")
	   
	   $(links).bind("click",function(ev){
	      ev.preventDefault();
	      
	      if($(this).hasClass("here")){return false;}
	      
	      var id = $(this).attr("rel");
	      
	      global.getFlashMovie("vp").sendToFlash(id);
	      
	      answers.setActiveVideo(id);
	      
	      
	   });
	   
	},
	setActiveVideo: function(curr){
	   var links = $("#people a");

      $(links).each(function(){
         if($(this).attr('rel') == curr){
            $(this).addClass("here");
            window.location.hash = "#" + curr;
         }else{
            $(this).removeClass("here");
         }
      });
	},
	flashMsg: function(msg){
		if($.browser.msie){
			alert("as: " + msg);
		}else{
			console.log("as: " + msg);
		}
	}
});