$(function() {
	
	//Variables
	var $bg	= $('#background'),
		$bg_image = $bg.find('img'),
		count = 0,
		backgroundsNo = 0,
		cache = [];
	
	
	var Menu = (function(){
		var init = function() {
			
			//Find out how many background there are;
			$.when(number_backgrounds()).done(function(number) {
				backgroundsNo = number;
			});
			
			//Load first background
			$.when(showBackground('/wp-content/themes/allure/images/HomeHero/home' +count+ '.JPG')).done(function() {
				
				//Now that the first background has been loaded we can start rotating them
				changeBackground();
				
				//Manually update the count to account for loading the first image
				++count;
				
			});				
				
			
			//Check if we need a custom scroll and call if needed
			if ($(".customScroll").length > 0) {
				$('.customScroll').jScrollPane({showArrows: true});
			}
			
			//Bind the nav trigger to show or hide the menu
			$('#navTrigger').live('click', function() {
				if($('#nav').find('ul').width() == 0) {
					showMenu();
				} else {
					hideMenu();
				}
			});
				
			//Bind the scroll menu so we can adjust the current background
			$(window).bind('resize.Menu' , function(e) {
				adjustImageSize($bg_image);
			});
			
		},
		loadPage = function() {
			loadPost();
			$.when(loadPost()).done(function(){
				$('.box').animate({
					width: $('.box').find('.content').width(),
					marginRight: -($('.box').find('.content').width() / 2)
				}, 1000);
				$('.box').animate({
					height: $('.box').find('.wrapper').height(),
					marginTop: -($('.box').find('.wrapper').height() /2) + 40
				}, 1000, function() {
					$('.box').find('.loader').hide();
					$('.box').find('.content').show();
				});
			});
		},
		loadPost = function() {
			return $.Deferred(
			function(dfd) {
				$.ajax({
					type: 'POST',
					url: "/wp-admin/admin-ajax.php",
					data: {
						action:'load_post',
						postId: $('#postId').val()
					},
					success: function(data) {
						if (data['success']) {
							$('.box').find('.content').html(data['data']);
							dfd.resolve();
						}
					},
					dataType: 'json'
				});
			}).promise();
		},
		changeBackground = function() {
			var timer = setInterval( function() {
				
				//Restart counter if it hits the limit
				if (count == backgroundsNo) {
					count = 0;
				}
				
				//Load image and show it 
				$.when(loadImage('/wp-content/themes/allure/images/HomeHero/home' + count + '.JPG')).done(function() {
					$.when(showBackground('/wp-content/themes/allure/images/HomeHero/home' + count + '.JPG')).done(function() {
						count++;
					});
				});
			
				
			}, 10000);
		},
		showBackground = function(source) {
			return $.Deferred(
			function(dfd) {
	
				console.log(source);
				if($bg_image.attr('src') === source)
					return false;


				$("<img/>") // Make in memory copy of image to avoid css issues
					.attr({ src : source })
					.css('opacity', '0')
					.load(function() {
						
						$('#background').prepend(this); 
						
						var w_w	= $(window).width(),
						w_h	= $(window).height(),
						r_w	= w_h / w_w,
						i_w	= this.width,
						i_h	= this.height,
						r_i	= i_h / i_w,
						new_w,new_h,
						new_left,new_top;
					
						if(r_w > r_i){
							new_h	= w_h;
							new_w	= w_h / r_i;
						}
						else{
							new_h	= w_w * r_i;
							new_w	= w_w;
						}
					
						$(this).css({
							width	: new_w + 'px',
							height	: new_h + 'px',
							left	: (w_w - new_w) / 2 + 'px',
							top		: (w_h - new_h) / 2 + 'px'
						});		
						


						
						$bg_image.animate({
							opacity: 0
						}, 1500, function() {
							$(this).remove();
						});
						
						$(this).animate({
							opacity : 1
						},1500);
						
						$bg_image = $(this);
						
					});






				

				
				dfd.resolve();
			}
			).promise();
		},
		loadImage = function(source) {
			return $.Deferred(
			function(dfd) {

				//Load new image only if its not in the array
				
				
				
					$('<img />').attr({ src: source }).load(function() {
						
						dfd.resolve();
					});
				
			}
			).promise();
		},
		number_backgrounds = function() {
			return $.Deferred(
			function(dfd) {
				$.ajax({
					type: 'POST',
					url: "/wp-admin/admin-ajax.php",
					data: {
						action:'no_backgrounds'
					},
					success: function(data) {
						dfd.resolve(backgroundNo = parseInt(data['number'], 10));
					},
					dataType: 'json'
				});
			}
			).promise();
		},
		showMenu = function() {


			
			

			
			$('#nav ul').stop().animate({
				'width': '980px'
			}, 500, function() {
				$('#nav li a').each( function(i) {
					$(this).stop().animate({
						marginTop: '0px',
						opacity: 1
					}, 200 + i * 200, 'easeOutBack', function() {

					})
				});
			});
			
		},
		hideMenu = function() {
			var totalItems = $('#nav ul').children('li').length;



			

			
			$('#nav li a').each( function(i) {
				$(this).stop().animate({
					marginTop: '-32px',
					opacity: 0
				}, 200 + i * 200, 'easeInBack', function() {
					if (i === (totalItems -1)) {
						$('#nav ul').animate({
							'width': '0px'
						}, 500, function() {

						});
					}
				});
			});
					
		},
		adjustImageSize = function($img) {
			var w_w	= $(window).width(),
			w_h	= $(window).height(),
			r_w	= w_h / w_w,
			i_w	= $img.width(),
			i_h	= $img.height(),
			r_i	= i_h / i_w,
			new_w,new_h,
			new_left,new_top;

			if(r_w > r_i){
				new_h	= w_h;
				new_w	= w_h / r_i;
			}
			else{
				new_h	= w_w * r_i;
				new_w	= w_w;
			}

			$img.css({
				width	: new_w + 'px',
				height	: new_h + 'px',
				left	: (w_w - new_w) / 2 + 'px',
				top		: (w_h - new_h) / 2 + 'px'
			});
		};
			
		return {
			init : init
		};
	})();

	Menu.init();
	
});




