;(function($, window) {
	/** Settings **/
			
	// List of background images to use, the default image will be the first one in the list
    var backgrounds = [
       'images/backgrounds/sunrise.jpg',
       'images/backgrounds/sunn.jpg',
       'images/backgrounds/sunnnn.jpg',
       'images/backgrounds/sunnnn.jpg',,
       'images/backgrounds/sunnnn.jpg',
       'images/backgrounds/sunnnn.jpg',
       'images/backgrounds/sunnnn.jpg',
       'images/backgrounds/sunnnn.jpg',
       'images/backgrounds/sunnnn.jpg',
       'images/backgrounds/sunrise.jpg',
       'images/backgrounds/city.jpg'
    ],
	
	// The speed of the fade transition between backgrounds in milliseconds (1000 = 1 second)
	speed = 1250,
	
	// The speed of the fade transition to hide the website
	fadeOutSpeed = 1000,
	
	// The speed of the fade transition to show the website
	fadeInSpeed = 1000,
	
	// Should the current background be saved across pages in a cookie?
	save = true,
	
	// Should the cache be used for faster image loading?
	preload = true,
	
	// Twitter username
	twitterUsername = 'sabrewebdesign',
		
	// Number tweets to show, set to 0 to disable
	tweetCount = 2;
	
	/** End settings **/
	
	Cufon.replace('h1, h2, h3, h4, h5');
	
	$('html').addClass('js-enabled');
		
	$(document).ready(function() {
		// Initialise the full screen backgrounds
		$.fullscreen({
			backgrounds: backgrounds,
			speed: speed,
			fadeOutSpeed: fadeOutSpeed,
			fadeInSpeed: fadeInSpeed,
			save: save,
			preload: preload
		});
		
		// Initialise the menu
		$('ul.sf-menu').superfish({ speed: 0 });		
		
		// Bind the search button to show/hide the search field
		$('#search-button').click(function() {
			$('.search-container').fadeToggle();
			return false;
		});
				
		// Bind the social button to show/hide the social icons box
		$('#social-pop-out-trigger').click(function() {
			var $allBoxes = $('.footer-pop-out-box');
			if ($allBoxes.is(':animated')) {
				return false;
			}
					
			var $thisBox = $('.social-pop-out-box');
			if ($thisBox.is(':visible')) {
				$thisBox.slideUp();
			} else {
				if ($allBoxes.is(':visible')) {
					$allBoxes.filter(':visible').slideUp(function() {
						$thisBox.slideDown();
					});
				} else {
					$thisBox.slideDown();
				}
			}
			
			return false;
		});
		
		// Bind the Twitter button to show/hide the Twitter feed box
		$('#twitter-pop-out-trigger').click(function() {
			var $allBoxes = $('.footer-pop-out-box');
			if ($allBoxes.is(':animated')) {
				return false;
			}
					
			var $thisBox = $('.twitter-pop-out-box');		
			if ($thisBox.is(':visible')) {
				$thisBox.slideUp();
			} else {
				if ($allBoxes.is(':visible')) {
					$allBoxes.filter(':visible').slideUp(function() {
						$thisBox.slideDown();
					});
				} else {
					$thisBox.slideDown();
				}
			}
			
			return false;
		});
		
		// Bind the view map button to slide down / up the map
		var $viewMapButton = $('.view-map'),
		$mapImg = $('.hidden-map'),
		$contactInfoWrap = $('.contact-info-wrap'),
		viewMapButtonText = $('.view-map').text();
		
		$viewMapButton.click(function() {
			if (!$mapImg.add($contactInfoWrap).is(':animated')) {
				if (!$mapImg.hasClass('map-visible')) {
					$contactInfoWrap.slideUp(600, function() {
						$mapImg.slideDown(600, function() {
							$mapImg.addClass('map-visible');
							$viewMapButton.text('Cerrar Mapa');
						});
					});
				} else {
					$mapImg.removeClass('map-visible').slideUp(600, function() {
						$contactInfoWrap.slideDown(600, function() {
							$viewMapButton.text(viewMapButtonText);
						});
					});
				}
			}
			return false;
		});
		
		// Bind any links with the class 'scroll-top' to animate the scroll to the top
		$('a.scroll-top'). click(function() {
			if ($('html,body').scrollTop() > 0) {
				$('html,body').animate({ scrollTop: 0 }, 1000);
			}
			return false;
		});
		
		// Make the form inputs and search fields clear value when focused
		$('#search-input').toggleVal({ populateFrom: 'custom', text: 'Search' });
		$('#name, #email, #phone, #message').toggleVal({ populateFrom: 'label', removeLabels: true });
		
		// Create the gallery rollover effect
		$('li.one-portfolio-item a').append(
			$('<div class="portfolio-hover"></div>').css({ opacity: 0, display: 'block' })
		).hover(function() {
			$(this).find('.portfolio-hover').stop().fadeTo(400, 0.6);
		}, function() {
			$(this).find('.portfolio-hover').stop().fadeTo(400, 0.0);
		});
	}); // End (document).ready
	
	$(window).load(function() {
		// Load the Twitter feed
		if (twitterUsername && tweetCount > 0) {
			(function() {
				var t = document.createElement('script'); t.type = 'text/javascript'; t.src = 'http://twitter.com/statuses/user_timeline/' + twitterUsername + '.json?callback=twitterCallback2&count=' + tweetCount;
				var h = document.getElementsByTagName('head')[0]; h.appendChild(t);
			})();
		}
	}); // End (window).load
	
	// Global image preloader
	window.preloadedImages = [];
	window.preload = function(images) {
		for (var i in images) {
			var elem = document.createElement('img');
			elem.src = images[i];
			window.preloadedImages.push(elem);
		}
	};

	// Any images to preload
	window.preload([
        'images/nav-a-bg1.png',
 		'images/search1.png',
 		'images/minimise1.png',
 		'images/2-col-hover.png',
 		'images/3-col-hover.png',
 		'images/4-col-hover.png',
 		'images/5-col-hover.png',
 		'images/6-col-hover.png',
 		'images/grid-hover.png'
	]);
})(jQuery, window);
