/**
 * Default Fitzrovia Javascript
 * @version 1.0
 * @author Tsuyoshi Saito <on-IDLE>
 */
(function($){
	$(function(){
		/**
		 * Debug
		 * @param obj
		 */
		function debug(obj) {
			if(typeof(console) != 'undefined') {
				console.log(obj);
			}
		}
		
		var imageLoaded = false;
		var imageSize = {}
		
		function resizeBackground() {
			var iw = imageSize.width;
			var ih = imageSize.height;
			var w = $('#screen').width();
			var h = $('#screen').height();
			var xr = w / iw;
			var yr = h / ih;
			var rate = Math.max(xr, yr);
			var nw = iw * rate;
			var nh = ih * rate;
			$('#screen img').width(nw).height(nh);
			$('#screen').fadeIn();
		}
		
		$('form .input, form .submit').addClass('clearfix');
		
		// Window resize
		$(window).unbind('resize').bind('resize', function() {
			// Resize content area
			if ($('#header').length > 0 && $('#footer').length > 0) {
				var headerHeight = $('#header').height();
				var footerPos = $('#footer').offset();
				var contentHeight = footerPos.top - headerHeight;
				$('#screen, #content-canvas').css({
					top: headerHeight + 'px'
				}).height(contentHeight);
			
				// Scale screen image
				$('#screen').hide();
				if (!imageLoaded) {
					var src = $('#screen img').attr('src');
					$('<img />').load(function(e){
						imageSize.width = this.width;
						imageSize.height = this.height;
						resizeBackground();
					}).attr('src', src);
				} else {
					resizeBackground();
				}
			}
		}).trigger('resize');
		
		// Logo click handler
		$('#logo').bind('click', function(e) {
			return window.location = $(this).find('a').attr('href');
		});
	});
})(jQuery);
