$(function(){
	/*================================================================
	  Fancybox Popovers
	  Also enabled for content loaded via ajax
	================================================================*/
	$('.popup').live('click', function() {
		$(this).filter(':not(.fb)').fancybox({
			'titleShow'		: false,
			'autoDimensions': false, 
			'width'			: 610,
			'height'		: 'auto',
			'padding'		: 50,
			'type'			: 'ajax',
			'ajax'			: {	dataFilter: function(data) { return $(data).find('#content').children().unwrap(); }}
		}).addClass('fb');
		$(this).triggerHandler('click');
		return false;
	});
	/*================================================================
	  Tooltips
	================================================================*/
	//$('.icon').tooltip({effect: 'slide'});
	/*$('.icon').live('mouseover',function(){
		$(this).tooltip({effect: 'slide'});
	});*/
	function initialize_tooltips() {
		$('.icon').tooltip({effect: 'slide'});
	}
	/*================================================================
	  Contact Form
	  Show/hide file upload based on dropdown selection/page hash
	================================================================*/
	// Check hash
	if (window.location.hash.slice(1) == 'advertise') { $('#input_2_9').val("I'd like to advertise").attr('selected', 'selected'); };
	if (window.location.hash.slice(1) == 'adspecs') { $('#input_2_9').val("I have an ad specs enquiry").attr('selected', 'selected'); };
	if (window.location.hash.slice(1) == 'techissue') { $('#input_2_9').val("I've noticed a technical problem").attr('selected', 'selected'); };
	if (window.location.hash.slice(1) == 'editor') { $('#input_2_9').val("I'd like to contact an editor").attr('selected', 'selected'); };
	if (window.location.hash.slice(1) == 'media') { $('#input_2_9').val("I'm a member of the media").attr('selected', 'selected'); };
	if (window.location.hash.slice(1) == 'jobapplication') { console.log('good'); $('#input_2_9').val("I want to work at Allure").attr('selected', 'selected'); };
	if (window.location.hash.slice(1) == 'other') { $('#input_2_9').val("I have a different reason").attr('selected', 'selected'); };
	$('<span class="required"> (Required)</span>').replaceAll('.gfield_required');
	$(function() {
		$('.gform_image_button').hover(function() {
			$(this).attr('src', $(this).attr('src').split('.png').join('_hover.png'));
		}, function() {
			$(this).attr('src', $(this).attr('src').split('_hover.png').join('.png'));
		});
	}); 

	/*================================================================
	  Navigation
	  Animation and active state check
	================================================================*/
	// Animation
	$('#navigation li:not(.current-menu-item) a').hover(function(){
		$(this).stop().animate({'padding-right':'22px' }, 'fast').css('background-image', $(this).css('background-image').replace('_normal','_hover'))
	},function(){
    	$(this).stop().animate({'padding-right':'18px' }, 'fast').css('background-image', $(this).css('background-image').replace('_hover','_normal'))
	});
	// *****************************		ACTIVE STATE CHECK		*****************************
	/*================================================================
	  Content Slider
	  Load specific content with ajax, then animate into position
	================================================================*/
	// Variables
	var slider_width = $('#slider_container').width();
	var slider_duration = 500;
	var home_height = $('#slider_home').outerHeight(true);
	var content_height;
	var specs_height = $('#specs_header').outerHeight(true);
	var slider_orientation;
	if ($('#slider_container').hasClass('vertical')) { slider_orientation='vertical'; };
	if ($('#slider_container').hasClass('horizontal')) { slider_orientation='horizontal'; };
	var loadingTimer;
	var loadingFrame = 1;
	$('#slider_container').append('<div id="loading"><div></div></div>');
	// Track hash and direct accordingly
	$.address.change(function(event){
		if (window.location.hash == '' || window.location.hash =='#home'){ animate_slideout(slider_orientation);  }
		else { show_content(window.location.hash.substring(1)); }
	});
	// Define, then load the content
	function show_content(current_hash) {
		// Manually assign page hash it's unique class identifier - is there a better way to do this? <<<<<<<********************************************* CHECK
		if (current_hash == '#gizmodo') { current_hash = '.page-item-12' };
		if (current_hash == '#kotaku') { current_hash = '.page-item-22' };
		if (current_hash == '#lifehacker') { current_hash = '.page-item-29' };
		if (current_hash == '#defamer') { current_hash = '.page-item-40' };
		if (current_hash == '#popsugar') { current_hash = '.page-item-72' };
		if (current_hash == '#fabsugar') { current_hash = '.page-item-80' };
		if (current_hash == '#bellasugar') { current_hash = '.page-item-82' };
		if (current_hash == '#shopstyle') { current_hash = '.page-item-84' };
		// And more for the specs page...
		if (current_hash == '#standard') { current_hash = '.page-item-500' };
		if (current_hash == '#panorama') { current_hash = '.page-item-502' };
		if (current_hash == '#backdrop') { current_hash = '.page-item-504' };
		if (current_hash == '#fireplace') { current_hash = '.page-item-506' };
		if (current_hash == '#marketplace') { current_hash = '.page-item-508' };
		if (current_hash == '#spotlight') { current_hash = '.page-item-510' };
		// Show Loader
		clearInterval(loadingTimer);
		$('#loading').show();
		loadingTimer = setInterval(animate_loading, 66);
		// Get link from identifier then load the content
		//var load_page = $(current_hash+' a').attr('href') + ' #content';
		console.log('.'+current_hash);
		var load_page = $('.'+current_hash).attr('href') + ' #content';
		$('#slider_content').load(load_page, function() {
			// Using this will weirdly return incorrect height values on occasion
			//content_height = $(this).height();
			//content_height = $('#slider_content').height();
			// Hide loader
			$('#loading').hide();
			// Remove content wrapper div from loaded content
			$(this).find('#content').children().unwrap();
			// Show back button on ajax loaded pages
			$('a.back').css({'display' : 'inline-block'});
			// Start animation based on orientation
			//animate_slidein(slider_orientation, content_height);
			animate_slidein(slider_orientation);
			initialize_tooltips();
		})
	};
	// Loading animation
	function animate_loading() {
		if (!$('#loading').is(':visible')){
			clearInterval(loadingTimer);
			return;
		}
		$('#loading div').css('top', (loadingFrame * -40) + 'px');
		loadingFrame = (loadingFrame + 1) % 12;
	};
	// Slide in
	function animate_slidein(this_orientation) {
		this_height = $('#slider_content').height();
		if (this_orientation == 'vertical'){			
			$('#specs_header').stop().animate({ 'height' : 0, 'opacity' : 0 }, slider_duration, function() { $(this).hide(); } );
			$('#slider_home').stop().animate({ 'height' : 0, 'opacity' : 0 }, slider_duration, function() { $(this).hide(); } );
			$('#slider_content').stop().show().animate({ 'opacity' : 1 }, slider_duration );
			$('#slider_wrapper').stop().animate({ 'height' : this_height }, slider_duration);
		}
		if (this_orientation == 'horizontal'){
			if (this_height<273) { this_height = 273; };
			$('#slider_content').stop().show().animate({ 'opacity' : 1 }, slider_duration );
			$('#slider_home').stop().animate({ 'margin-left' : -slider_width, 'opacity' : 0 }, slider_duration, function() { $(this).hide(); } );
			$('#slider_wrapper').stop().animate({ 'height' : this_height }, slider_duration);
		}
	};
	// Slide out
	function animate_slideout(this_orientation) {
		if (this_orientation == 'vertical'){
			$('#specs_header').stop().show().animate({ 'height' : specs_height, 'opacity' : 1 }, slider_duration );
			$('#slider_home').stop().show().animate({ 'height' : home_height, 'opacity' : 1 }, slider_duration );
			$('#slider_content').stop().animate({ 'opacity' : 0 }, slider_duration, function() { $(this).hide(); } );
			$('#slider_wrapper').stop().animate({ 'height' : home_height }, slider_duration);
		}
		if (this_orientation == 'horizontal'){
			$('#slider_content').stop().animate({ 'opacity' : 0 }, slider_duration, function() { $(this).hide(); } );
			$('#slider_home').stop().show().animate({ 'margin-left' : 0, 'opacity' : 1 }, slider_duration );
			$('#slider_wrapper').stop().animate({ 'height' : home_height }, slider_duration);
		}
	};
	// Slider buttons
	$('#slider_home a:not(.download)').click(function(event) {
	//if !($(this).hasClass('download')){ };
		window.location.hash = $(this).attr('class');
		event.preventDefault();
	});	

});
