jQuery.fn.center = function (xOffset, yOffset) {
  if(!xOffset) xOffset = 0;
  if(!yOffset) yOffset = 0;
  this.css("position","absolute");
  this.css("top", ( $(window).height() - this.height() ) / 2 + $(window).scrollTop() + yOffset + "px");
  this.css("left", ( $(window).width() - this.width() ) / 2 + $(window).scrollLeft() + xOffset + "px");
  return this;
}

var soundManagerLoaded = false;
var moveClouds = true;

function enter() {
  closeFancybox();
  setTimeout(showArContent, 500);
}

function closeFancybox() {
  $.fancybox.close();
}

function closeFancyboxLaunchSeeHowItWorks() {
  closeFancybox();
  $("#goat").trigger('click');
}

function showArContent() {
  moveClouds = false;
  
  $('#arContent').show();
  $('#flashContent').show();
  
  $('#arClick').fancybox({
    'width'               : 640, 
    'height'              : 570, 
    'overlayColor'        : '#000', 
    'hideOnOverlayClick'  : false, 
    'transitionIn'        : 'none', 
    'transitionOut'       : 'none', 
    'scrolling'           : 'no', 
    'padding'             : 0,
    'onClosed'            : arCloseHandler, 
    'autoScale'           : false
  }).trigger('click');
  
  $("#codeInputContainer").fadeIn(500);
  
  $("#codeInput").focus(function(){
    if($(this).val() == "Enter your code to proceed") $(this).val('')
  })
  
  $("#codeInput").blur(function(){
    if($(this).val() == "") $(this).val('Enter your code to proceed')
  })
  
  $("#codeForm").submit(function(){
  	$(".error").hide();
		$("#ajaxLoader").show();
		var hasError = false;

		var codeVal = $("#codeInput").val();
		
		if(codeVal == '') {
			$(".error").html('Hmmm, try again!?').fadeIn('fast');
			hasError = true;
		}
		
		if(!hasError) {
		  $.post("checkCode.php",
             { code: codeVal },
         	   function(data){
         	     $("#ajaxLoader").hide();
      		     if(data) {
      		       window.location = "site";
    		       } else {
    		         $(".error").html('Invalid code').fadeIn('fast');
    		       }
      	     }
      );
		} else {
		  $("#ajaxLoader").hide();
		}
    
		return false;
  });
  
}

function arCloseHandler() {
  moveClouds = true;
  
  moveCloud1Down();
  moveCloud2Down();
  $('#arContent').hide();
  $('#flashContent').hide();
  setTimeout(closeArForGood, 500)
}

function closeArForGood() {
  
}

function initArFlash() {
  var attributes = {};
	var flashvars = {};
	var params = {};
	params.scale =  "noscale";
	params.bgcolor = "#000000";
	params.allowScriptAccess = "sameDomain";
	// IE doesn't like transparency
	//params.wmode = "transparent";
	params.scriptaccess = true;
	swfobject.embedSWF("KogenateAr.swf", "flashContent", "640", "480", "10.0.0", null, flashvars, params, attributes);
}

function initSound() {
  soundManager.url = 'flash/';
  soundManager.flashVersion = 9;
  soundManager.useFlashBlock = false;

  soundManager.onload = function() {
    soundManagerLoaded = true;
    soundManager.createSound('sheep1', 'sounds/sheepshort1.mp3');
    soundManager.createSound('sheep2', 'sounds/sheepshort2.mp3');
    soundManager.createSound('sheep3', 'sounds/sheepshort3.mp3');
  };
}

function initClouds() {
  $("#cloud1").css({top: -$(this).height() + "px"});
  $("#cloud2").css({top: -$(this).height() + "px"});
  moveCloud1Down();
  moveCloud2Down();
}

function initLinks() {
  $("#campQuestival, #clickToEnter").click(function(){
    $.fancybox({
      'href'                : 'pages/whatToDo.html', 
      'padding'             : 0, 
  		'width'				        : 729,
  		'height'			        : 565,
  		'titleShow'           : false, 
  		'overlayColor'        : '#000', 
      'transitionIn'	    	: 'fade',
  		'transitionOut'		    : 'none',
  		'type'				        : 'iframe', 
  		'autoScale'           : false
    });

    return false;
  });

  $("#help").fancybox({
    'padding'         : 0, 
		'width'				    : 729,
		'height'			    : 565,
		'titleShow'       : false, 
  	'overlayColor'    : '#000', 
    'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				    : 'iframe', 
		'autoScale'       : false
	});

  $("#about").fancybox({
    'padding'         : 0, 
		'width'				    : 729,
		'height'			    : 565,
		'titleShow'       : false, 
		'overlayColor'    : '#000', 
    'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				    : 'iframe', 
		'autoScale'       : false
	});
	
	// Click to see how it works
	$("#goat").click(function() {
  	$.fancybox({
  	  'padding'         : 0, 
			'width'		        : 680,
			'height'		      : 400,
			'overlayColor'    : '#000', 
			'href'			      : this.href, 
			'type'			      : 'swf',
			'swf'			        : {'allowfullscreen' : 'true'}
		});

  	return false;
  }).mouseover(function() {
    if (soundManagerLoaded) {
      var ran = Math.ceil(Math.random() * 3)
      soundManager.play('sheep' + ran);
    }
  }); 
  
}

function positionClouds() {
  $("#cloud1").animate({"left": ($(window).width() / 3) * 2 + "px"}, 1000)
  $("#cloud2").animate({"left": (($(window).width() / 3) * 2) + 100 + "px"}, 1000)
}

function resizePositionClouds() {
  $("#cloud1").css({"left": ($(window).width() / 3) * 2 + "px"})
  $("#cloud2").css({"left": (($(window).width() / 3) * 2) + 100 + "px"})
}

function clearClouds() {
  $("#cloud1").animate({"left": $(window).width() + "px"}, 3000)
  $("#cloud2").animate({"left": $(window).width() + "px"}, 3000)
}

function moveCloud1Up() {
  $("#cloud1").animate({"top": "50px"}, 3000, null, moveCloud1Down)
}

function moveCloud1Down() {
  var onComplete = moveClouds ? moveCloud1Up : null;
  $("#cloud1").animate({"top": "75px"}, 3000, null, onComplete)
}

function moveCloud2Up() {
  $("#cloud2").animate({"top": "50px"}, 5000, null, moveCloud2Down)
}

function moveCloud2Down() {
  var onComplete = moveClouds ? moveCloud2Up : null;
  $("#cloud2").animate({"top": "75px"}, 5000, null, onComplete)
}

function resizeHandler() {
  $("#campQuestival").center(0, -10);
  var clickToEnterYOffset = ($(window).height() < 650) ? -20 : -70;
  $("#clickToEnter").center(320, clickToEnterYOffset);
  var bayerBalloonYOffset = ($(window).height() < 650) ? -200 : -250;
  $("#bayerBalloon").center(-250, bayerBalloonYOffset);
  $("#planeMan").center(200, -280);
  $("#Content").center(0, -30);

  resizePositionClouds();
};

// On document ready

$(document).ready(function(){

  $(document).pngFix(); 
  
  if(swfobject.hasFlashPlayerVersion('10.0.0')) {
    $('#withJavascript').show();
    $('#jsOff').hide();

    initSound();
    initArFlash();
  }
  
  $(window).bind('resize', resizeHandler);
  resizeHandler();
  
  initClouds();
  initLinks();
  
});
