"use strict";

/*globals Image, document, $, jQuery, window */

(function () {
	var wrap,
		prefix,
		endpoint,
		imgSrc, 
		isSplash,
		cacheImg, 
		header;
	
	function dynamicMargins(callback) {
		if ($('body').attr('id') === 'splash') {
			var w, h, hW, hH, switchRef = callback !== undefined;

    		h = $(window).height();
    		w = $(window).width();					
			
			hH = parseInt(header.css('height'), 10);
			hW = parseInt(header.css('width'), 10);

			header.css('margin', [(h - hH)/ 2, 'px 0 0 ', (w - hW) / 2, 'px'].join(''));
		
			if (switchRef && jQuery.isFunction(callback)) {
				callback();
			}
		}	
	}
	
	function setHomeImageHover() {
		$('.home_images').find('li')
			.live('click', function () {
				window.location.href = endpoint + '/residences?home=' + (parseInt(getHomeImages().index(this), 10) +  1); 
			})
			.live('mouseover', function () {
				$(this).addClass('on');
			})
			.live('mouseout', function () {
				$(this).removeClass('on');		
			});		
	}
	
	function animateToHomeScreen() {
		var elem;		
		window.setTimeout(function () {
			$.ajax({
				type 	: 'get',
				url 	: endpoint + '/home/welcome?ajax',
				cache	: false,
				success : function (data) {
					$(window).unbind('resize');					
					$('h1').fadeOut('slow', function () {
						window.setTimeout(function () {							
							$('#transback').fadeOut('slow', function () {
								window.setTimeout(function () {
									$('body').removeAttr('id');	
									$('#splash_content').replaceWith(data);
									$('#wrapper').get(0).className = 'ajax';
							
									// NEW DATA
									$('h1').removeAttr('style');
									$('#wrapper').fadeIn('slow', function () {
										window.setTimeout(function () {					
											$('#content').fadeIn('slow', function () {
												$('#message').fadeIn();
												window.setTimeout(function () {
													$('.home_images').show().find('li').each(function () {
														$(this).fadeIn('slow');
													});
												}, 1000);	
											});	
										}, 1000);						
									});	
								}, 1000);		
							});	
						}, 1000);									
					});	
				},
				error	: function (error) {
					window.alert(error);
				}, 
				complete: function () {
					//complete fires too soon after success
				}
			});
		}, 5000);
	}
				
	function getHomeImages() {
		return $('.home_images').find('li');
	}

	$(document).ready(function () {
		wrap = $('#wrapper');
		isSplash = $('body').attr('id') === 'splash';
		
		if (getHomeImages().length || isSplash) {
			prefix = $('h1 a').attr('href');

			if (prefix.indexOf('/home') === 0) {
				endpoint = '';
			} else {
				endpoint = prefix.split('/home')[0];
			}			
			
			setHomeImageHover();
			
			cacheImg = new Image();
			cacheImg.src = 'img/placeholderOver.gif';
		}
		
		if (isSplash) {
			header = $('#splash_content');
			
			window.onload = function () {
    			dynamicMargins(function () {
    				header.fadeIn();
    				$('h1').show();
    			});
    			
				$(window).bind('resize', dynamicMargins);
    			animateToHomeScreen();
    		};
		}		
	});
				
}());				
