var minWidth = 1030;
var minHeight = 686;

$(document).ready(doLoad);

function doLoad() {
	$(window).bind('resize', doResize);
	doResize();
	$("#jquery_jplayer").jPlayer({
		ready: function () {
			$(this).jPlayer("setMedia", {
				mp3: "/audio/summersplash2011.mp3",
				oga: "/audio/summersplash2011.ogg"
			});
			$(this).bind($.jPlayer.event.ended + ".repeat", function () {
				$(this).jPlayer("play");
			});
			var audio = getCookie("audio");
			if (audio == null || audio == "on")
				toggleMusic();
		},
		swfPath: "/audio",
		supplied: "mp3, oga"
	});

	$(".Button").hover(
  function () { $(this).css('background', 'url(/images/summersplash/2011/button.png) 0px -45px') },
  function () { $(this).css('background', 'url(/images/summersplash/2011/button.png) 0px 0px') }
);

	$(".Bounce").hover(function () {
		if (!$(this).attr('transition')) {
			$(this).attr('transition', '0');
		}

		if ($(this).attr('transition') == '0') {
			var width = $(this).width();
			var height = $(this).height();

			$(this).attr('transition', '1');

			$(this).animate(
      {
      	height: height - 14,
      	width: width - 14,
      	marginTop: 8,
      	marginLeft: 8
      },
      75,
      'linear',
      function () {
      	$(this).animate(
          {
          	height: height,
          	width: width,
          	marginTop: 0,
          	marginLeft: 0
          },
          75,
          'linear',
          function () {
          	$(this).attr('transition', '0');
          }
        );
      }
    );
		}
	});
}

function doResize() {
	$('.page_body').css({ height: Math.max(686, $(window).height()) + 'px', width: Math.max(1030, $(window).width()) + 'px' });
	resizeBackground();
}

function resizeBackground() {
	var background = $('.page_background');
	var backgroundWidth = background.width();
	var backgroundHeight = background.height();

	var backgroundImage = background.children('img').last();
	if (backgroundImage.length == 0) { return; }
	var imageWidth = backgroundImage.get(0).naturalWidth;
	var imageHeight = backgroundImage.get(0).naturalHeight;
	var ratio = imageHeight / imageWidth;

	if (backgroundWidth / imageWidth > backgroundHeight / imageHeight) {
		imageWidth = backgroundWidth;
		imageHeight = imageWidth * ratio;
	}
	else {
		imageHeight = backgroundHeight;
		imageWidth = imageHeight / ratio;
	}
	backgroundImage.css({ width: imageWidth + 'px', height: imageHeight + 'px', left: (backgroundWidth - imageWidth) / 2 + 'px' });
}

function toggleMusic() {
	var _musicController = $('#musicController');
	if (_musicController.html() == 'OFF') {
		_musicController.html('ON');
		$("#jquery_jplayer").jPlayer("play");
		setCookie("audio", "on", 1);
	}
	else {
		_musicController.html('OFF');
		$("#jquery_jplayer").jPlayer("stop");
		setCookie("audio", "off", 1);
	}
}

function setCookie(c_name, value, exdays) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
	document.cookie = c_name + "=" + c_value;
}

function getCookie(c_name) {
	var i, x, y, cookies = document.cookie.split(";");
	for (i = 0; i < cookies.length; i++) {
		x = cookies[i].substr(0, cookies[i].indexOf("="));
		y = cookies[i].substr(cookies[i].indexOf("=") + 1);
		x = x.replace(/^\s+|\s+$/g, "");
		if (x == c_name) {
			return unescape(y);
		}
	}
	return null;
}

function Popup(URL) {
	$("#PopupContent").load(URL, function () {
		$("#ModalBackground").fadeIn('fast');
		$("#ModalBox").fadeIn('fast');
	});
}

function PopupClose() {
	$("#ModalBackground").fadeOut('fast');
	$("#ModalBox").fadeOut('fast');
}



