/****************************************************
* Log
****************************************************/
function log(msg){
	if (typeof window.console != 'undefined' && typeof window.console.log == 'function') {
  	console.log(msg);
	}
}

/****************************************************
* app
****************************************************/
var app = (function(){
	function init(){
		this.bgController.init();	
		this.registerController.init();
	}
	return {
		init:init
	};
})();


/****************************************************
* bgController
****************************************************/
app.bgController = (function(){

	var _this = this;
	var _skipIntro = false;
	var _showContainer = true;

	var _bgWidth = 1416;
	var _bgHeight = 800;
	var _bgRatio = _bgWidth/_bgHeight;
	var _starLeft = 680;
	var _starLeftRatio = _starLeft/_bgWidth;
	var _starWidth= 130;
	var _starWidthRatio = _starWidth/_bgWidth;
	var _starHeight = 800;
	var _starRatio = _starWidth/_starHeight;

	function init(){

		resizePage();
		
		if(_skipIntro){
			$("#bg_star, #bgflare").show();
			showContainer();
		}else{
			showStar();
		}
		//onStarDone();

		$(window).resize(function() {
	  		resizePage();
		});
	}

	function loadBg(){
		// var featureImg = new Image();
		// $(featureImg).load(function () {
		// 	$('#inner_background_wrapper').append(featureImg);
		// 	$('#inner_background_wrapper img').css({'display':'block'});
		// 	resizePage();
		// })
		// .attr('src', bgUrl);
	}
	
	function showStar(){
		$("#bg_star").delay(300).fadeTo(1500,1,function(){ onStarDone(); });
	}
	
	function onStarDone(){
		//log("onStarDone");
		//$("#bg_flare_up").fadeTo(1500,1);
//		$("#bg_flare_up").css('display','block').css('opacity',0).animate({opacity:1},2000);
		$("#bg_flare").fadeTo(1500,1, function(){ onFlareDone(); });
	}
	
	function onFlareDone(){
		showContainer();
	}

	function showContainer(){
		if(_showContainer) $("#container_outer").delay(2000).fadeIn(1000);
	}

	function resizePage(){
	
		var windowWidth,
			windowHeight,
			windowRatio,
			imgHeight,
			imgWidth,
			newWidth,
			newHeight,
			newTop,
			newLeft,
			newStarWidth,
			newStarHeight,
			newStarLeft;
			
			
		//log("resizePage");
		/* background resizing */
		if ($('#bg_image').length == 1) {
			windowWidth = parseInt($('#bg').width());
			windowHeight = parseInt($('#bg').height());
			windowRatio = windowWidth/windowHeight;			
			
			if (windowRatio > _bgRatio) {
				imgHeight = windowWidth / _bgRatio;
				newWidth = windowWidth;
				newHeight = imgHeight;
				newTop = (windowHeight-imgHeight)/2;
				newLeft = 0;
				newStarLeft = windowWidth * _starLeftRatio;
				newStarHeight = imgHeight;
				newStarWidth = windowWidth *_starWidthRatio;
			} else {
				imgWidth = windowHeight * _bgRatio;
				newWidth = imgWidth;
				newHeight = windowHeight;
				newTop = 0;
				newLeft = (windowWidth-imgWidth)/2;
				newStarWidth = imgWidth * _starWidthRatio;
				newStarHeight = newHeight;
				newStarLeft = imgWidth * _starLeftRatio;
			};

			
			$('#bg_inner').css({'width':newWidth+'px','height':newHeight+'px','top':newTop+'px','left':newLeft+'px'});
			$('#bg_image').css({'width':newWidth+'px','height':newHeight+'px', 'left':'0px','top':'0px'});
			// $('#bg_star').css({'width':newWidth+'px','height':newHeight+'px', 'left':'0px','top':'0px'});
			// $('#bg_flare_up').css({'width':newWidth+'px','height':newHeight+'px', 'left':'0px','top':'0px'});
			//$('#bg_flare_down').css({'width':newWidth+'px','height':newHeight+'px', 'left':'0px','top':'0px'});

			// $('#bg_star').css({'width':newStarWidth+'px','height':newStarHeight+'px', 'left':newStarLeft+'px','top':'0px'});
			// $('#bg_flare').css({'width':newStarWidth+'px','height':newStarHeight+'px', 'left':newStarLeft+'px','top':'0px'});

			$('#bg_star').css({'width':newWidth+'px','height':newHeight+'px', 'left':'0px','top':'0px'});
			$('#bg_flare').css({'width':newWidth+'px','height':newHeight+'px', 'left':'0px','top':'0px'});
			
		};
	};

	return{
		init:init
	}
})();

/****************************************************
* Register
****************************************************/
app.registerController = (function(){

	function init(){
		bindEvents();
		styleCheckBoxes();
		addValidation();
	}

	function bindEvents(){
		$("#register_button").click(function(){
			$("#intro").slideUp(1000,function(){
				$("#registration").slideDown(1000);
			});
			// $("#intro").fadeTo(1000,0,function(){
			// 	$("#intro").hide();
			// 	$("#registration").fadeTo(1000,1);
			// });
			return false;
		});
	}

	function styleCheckBoxes(){
		$('input[type=checkbox]').each(function() {
			$(this).wrap(function() {
				return ($(this).is(':checked')) ? '<span class="custom_checkbox selected" />' : '<span class="custom_checkbox" />';
			});
		});

		$('.custom_checkbox input[type=checkbox]').click(function () {
			$(this).parent().toggleClass('selected');
		});
	}

	function addValidation(){
		$('form').validate({
			errorElement:"span",
			
			errorPlacement: function(error, element) {
				if(element.attr('type')==='checkbox'){
					error.appendTo( element.closest('fieldset') );
				}else if(element.type==='select'){
					error.appendTo(element.parent());
				}else{
					error.appendTo(element.parent());
				}
			},
			
			submitHandler:function(form){
				send();
			}
		});
	}
	
	function send(){
		$("#registration").slideUp(500,function(){
			$("#sending_message").slideDown(500);
		});
		
		var $form = $("form");
		
		$.ajax({
			type:'GET',
			dataType: 'json',
			url:$form.attr('action')+'.json?callback=?',
			data:$form.serialize(),
			success:function() {
				sendDone();
			}
		});
		
	}
	
	function sendDone(){
		$("#sending_message").slideUp(500,function(){
			$("#thankyou_message").slideDown(500);
		});
	}

	return{
		init:init
	}
})();

/****************************************************
* Init app
****************************************************/
$(function(){
	app.init();
});
