$(function(){
	
	$(window).bind('hashchange', function(event){
		if(event.fragment == '' && $('#fancybox-wrap').is(':visible')){
			jQuery.fancybox.close();
		}
		if(!$('#fancybox-wrap').is(':visible')){
			if($('a.home-popup[rel="' + event.fragment + '"]').length > 0){
				$('a.home-popup[rel="' + event.fragment + '"]').trigger('click');
			}
		}
	});
	
	$('a.home-popup').fancybox({
		padding:0,
		centerOnScroll:true,
		overlayColor: '#000',
		overlayOpacity: 0.85,
		showCloseButton:false,
		showNavArrows:false,
		onComplete: function(e){
			location.hash = e.attr('rel');
			$('div.popup a.btn-close').click(function(){
				$.fancybox.close();
				return false;
			});
			initTabs();
			clearInputs();
			initCufon();
			initCustomForms();
			initFormValidation();
		},
		onClosed: function(e){
			location.hash = '';
		}
	});
	
	if(window.location.hash.length > 0){
		$('#overlay, #text-holder').hide();
		$(window).trigger('hashchange');
	}
	
	if($.cookie('visited') == 1){
		//$('#overlay, #text-holder').hide();
		//return true;
	}
	
	$('#overlay').height($('body').outerHeight());
	$('#text-holder').hide();
	
	var text_images = [
		'text1',
		'text2',
		'text3',
		'text4'
	];
	
	// Click to skip for now
	$('#intro-skip').click(function(e){ 
		$('#text-holder, #overlay').fadeOut('slow');
		$('#text-holder').dequeue();
		return false;
	});
	
	$.each(text_images, function(index, value){
		var image = value;
		$('#text-holder').fadeOut('slow');
		$('#text-holder').queue(function() { 
			$('img', this).hide();
			$('#' + image, this).show();
			$(this).dequeue();
		});
		$('#text-holder').fadeIn('slow').delay(2000);
		if(index == text_images.length - 1){
			$('#text-holder').queue(function() { 
				$('#text-holder, #overlay').fadeOut('slow');
				$(this).dequeue();
			});
		}
	});
	
	//$.cookie('visited', 1, { expires: 30 });
	$.cookie('visited', 1);
	
});
